Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1931: 회의실 배정 #23

Open
minsoo0715 opened this issue Sep 25, 2023 · 0 comments
Open

1931: 회의실 배정 #23

minsoo0715 opened this issue Sep 25, 2023 · 0 comments
Assignees
Labels
#그리디 그리디 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들

Comments

@minsoo0715
Copy link
Owner

1931: 회의실 배정

소스 코드

아이디어

회의실 배정을 할 때 많은 회의실을 배정할 수 있는 경우는 바로 끝나는 시간이 빠른 경우를 고를 때라고 생각할 수 있다. 또한 끝나는 시간이 동일하면, 더 일찍 시작할수록 유리하다.

따라서 끝나는 시간 기준, 내림차순으로 정렬하고, 끝나는 시간이 동일한 경우 시작 시간 기준으로 내림차순으로 정렬하고, 고르면 된다.

구현

sort()에 comparator 람다 표현식을 넘겨서 정렬 후, 시간이 겹치지 않는다면 그 회의를 카운팅한다.

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
#define endl '\n';
vector<pair<int, int>> c = vector<pair<int, int>>(100001);

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, cnt = 1, end;
    cin >> N;
    for(int i = 0; i<N; ++i) {

        cin >> c[i].first >> c[i].second;
    }

    sort(c.begin(), c.begin()+N, [](auto p1, auto p2) {
        if(p1.second == p2.second) {
            return p1.first < p2.first;
        }
        return p1.second < p2.second;
    });

    end = c[0].second;

    for(int i = 1; i<N; ++i) {
        if(end <= c[i].first) {
            ++cnt;
            end = c[i].second;
        }
    }
    cout << cnt;
    return 0;
}
@minsoo0715 minsoo0715 added @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들 #그리디 그리디 문제 labels Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#그리디 그리디 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들
Projects
None yet
Development

No branches or pull requests

1 participant