https://www.acmicpc.net/problem/1931

문제 설명
n개의 회의 시작 시간과 회의 끝나는 시간이 주어질 때, 최대로 사용할 수 있는 회의의 최대 개수를 출력하는 문제이다.
주요 로직
1. 정렬이다.
-> 회의가 끝나는 시간 기준으로 정렬을 해야한다.
정답 코드
#include <bits/stdc++.h>
using namespace std;
int n, s, e;
vector<pair<int,int>> v;
bool cmp(pair<int,int> a, pair<int,int> b){
if(a.second == b.second)
return a.first < b.first;
return a.second < b.second;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n;
for(int i=0; i<n; i++){
cin >> s >> e;
v.push_back({s,e});
}
sort(v.begin(), v.end(), cmp);
int cnt = 0;
int time = 0;
for(auto it : v){
if(time <= it.first) time = it.second, cnt++;
}
cout << cnt << "\n";
return 0;
}

'PS' 카테고리의 다른 글
[백준] 3273번 두 수의 합 (C++) (0) | 2024.10.18 |
---|---|
[백준] 1644번 소수의 연속 (C++) (2) | 2024.10.16 |
[백준] 1781번 컵라면 (C++) (0) | 2024.10.15 |
[백준] 9935번 문자열 폭발 (C++) (0) | 2024.10.15 |
[백준] 14729번 칠무해 (C++) (5) | 2024.10.14 |
https://www.acmicpc.net/problem/1931

문제 설명
n개의 회의 시작 시간과 회의 끝나는 시간이 주어질 때, 최대로 사용할 수 있는 회의의 최대 개수를 출력하는 문제이다.
주요 로직
1. 정렬이다.
-> 회의가 끝나는 시간 기준으로 정렬을 해야한다.
정답 코드
#include <bits/stdc++.h>
using namespace std;
int n, s, e;
vector<pair<int,int>> v;
bool cmp(pair<int,int> a, pair<int,int> b){
if(a.second == b.second)
return a.first < b.first;
return a.second < b.second;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n;
for(int i=0; i<n; i++){
cin >> s >> e;
v.push_back({s,e});
}
sort(v.begin(), v.end(), cmp);
int cnt = 0;
int time = 0;
for(auto it : v){
if(time <= it.first) time = it.second, cnt++;
}
cout << cnt << "\n";
return 0;
}

'PS' 카테고리의 다른 글
[백준] 3273번 두 수의 합 (C++) (0) | 2024.10.18 |
---|---|
[백준] 1644번 소수의 연속 (C++) (2) | 2024.10.16 |
[백준] 1781번 컵라면 (C++) (0) | 2024.10.15 |
[백준] 9935번 문자열 폭발 (C++) (0) | 2024.10.15 |
[백준] 14729번 칠무해 (C++) (5) | 2024.10.14 |