728x90

 0. [c++] 백준


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


 1. 풀이


algorithm에 있는 sort함수를 활용하였고, 비교함수를 따로 만들어 문제를 해결하였다.


처음 문제를 풀 때 시간초과가 발생했었는데, endl을 "\n"으로 변경하여 문제를 해결하였다.



 2. 소스코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
 
using namespace std;
 
bool cmp(pair<intint> a, pair<intint> b) {
    if(a.second == b.second)
        return a.first < b.first;
    return a.second < b.second;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int N;
    cin >> N;
 
    vector<pair<intint>> arr(N);
    for (int i = 0; i < N; i++
        cin >> arr[i].first >> arr[i].second;
    
    sort(arr.begin(), arr.end(), cmp);
 
    for (int i = 0; i < N; i++) {
        cout << arr[i].first << " " << arr[i].second << "\n";
    }
 
    return 0;
}
cs


 3. 참고




질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


+ Recent posts