반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. 정렬


comp함수만 짤 수 있다면 문제를 해결 할 수 있다.


y좌표가 서로 같을 때 return a.first < b.first (x좌표 오름차순)

그 이외에는 a.second < b.second (y좌표 오름차순)으로 설정한다.






소스 코드 : 


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
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
 
using namespace std;
 
typedef pair<intint> pii;
 
vector<pii> vc;
 
bool comp(const pii &a, const pii &b)
{
    if (a.second == b.second)
        return a.first < b.first;
 
    return a.second < b.second;
}
int main()
{
    int n;
    scanf("%d"&n);
 
    for (int i = 0; i < n; i++)
    {
        int a, b;
        scanf("%d %d"&a, &b);
 
        vc.push_back({ a, b });
    }
 
    sort(vc.begin(), vc.end(), comp);
 
    for (auto i : vc)
        printf("%d %d\n", i.first, i.second);
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus



반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[14927번] 전구 끄기  (0) 2018.02.19
[14925번] 목장 건설하기  (0) 2018.02.19
[10840번] 구간 성분  (0) 2018.02.07
[2257번] 화학식량  (2) 2018.02.06
[9536번] 여우는 어떻게 울지?  (0) 2018.02.05