Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

차근차근

[백준 11651] 좌표 정렬하기 2 본문

대학교/Algorithm

[백준 11651] 좌표 정렬하기 2

SWKo 2020. 3. 9. 02:05

0. 제목

  • 백준 11651 좌표 정렬하기 2
  • BOJ 11651 좌표 정렬하기 2
  • C++ 11651 좌표 정렬하기 2

1. 문제

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


2. 풀이

  • vector의 sort기준인 comp함수식만 잘 세우면 되는 문제이다.
  • 좌표쌍을 vector에 넣어줘야 하므로 make_pair를 사용하였다.

3. 코드

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
33
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
typedef pair<intint> P;
 
int N;
vector<P> v;
 
bool comp(P a, P b){
    if(a.second == b.second){
        return a.first < b.first;
    }
    return a.second < b.second;
    
}
 
int main(int argc, const char * argv[]) {
    cin >> N;
    int x, y;
    for(int i = 0; i < N; i++){
        cin >> x >> y;
        v.push_back(make_pair(x, y));
    }
    sort(v.begin(), v.end(), comp);
    
    for(int i = 0; i < N; i++){
        cout << v[i].first << ' ' << v[i].second << '\n';
    }
    return 0;
}
 
 

'대학교 > Algorithm' 카테고리의 다른 글

[백준 11725] 트리의 부모 찾기  (0) 2020.03.11
[백준 2869] 달팽이는 올라가고 싶다  (0) 2020.03.09
[백준 1003] 피보나치 함수  (0) 2020.03.08
[백준 14501] 퇴사  (0) 2020.03.07
[백준 2309] 일곱 난쟁이  (0) 2020.03.06
Comments