[백준] 1946_신입 사원 C++
in Study on Coding Test
그리디 알고리즘 문제
정답제출코드
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int T, N;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
for (int i = 0; i < T; i++)
{
cin >> N;
vector<pair<int,int>> score;
for (int j = 0; j < N; j++)
{
int A, B;
cin >> A >> B;
score.push_back({A, B});
}
sort(score.begin(), score.end());
int max_score = score[0].second;
int cnt = 0;
for (int i = 0; i < N; i++)
{
if (score[i].second <= max_score)
{
cnt++;
max_score = score[i].second;
}
}
cout << cnt << '\n';
}
return 0;
}
정렬을 해서 푸는 그리디 알고리즘 문제였다.
서류 점수로 정렬을 한 다음에 면접 점수를 비교하면서 카운트를 세주면 되는 문제였다.
그래서 pair로 점수를 받고 first로 정렬을 한 뒤 second에서 max를 저장해가며
비교하도록 구현하였다.