728x90

 0. [c++] 백준  - 


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


 1. 풀이


vector<pair<int, int> >에 x,y좌표를 입력받고, 오름차순으로 정렬하면 완성.


이때, algorithm헤더의 sort함수를 활용하였고, default로 pair<int, int>의 경우 앞을 우선 정렬하고, 앞이 같은 경우 뒤를 오름차순으로 정렬해주기 때문에, 따로 비교함수를 구현하지는 않았다.



 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
#include<iostream>
#include<vector>
#include<algorithm>
 
using namespace std;
 
vector<pair<intint> > v;
 
bool cmp(pair<intint>pair<intint>) {
 
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int N;
    int a, b;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> a >> b;
        v.push_back({ a,b });
    }
    sort(v.begin(), v.end());
 
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].first << " " << v[i].second << "\n";
    }
 
    return 0;
}
cs


 3. 참고


구종만, 「프로그래밍 대회에서 배우는 알고리즘 문제 해결 전략」, 인사이트, 2012, p.216~236.



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

도움 되셨으면 하트 꾹!


+ Recent posts