Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 정석
- 너비우선탐색
- OS
- 자료구조
- 코테
- 북리뷰
- Computer science
- Operating System
- 문제풀이
- 정석학술정보관
- 오퍼레이팅시스템
- 그래프
- 구현
- 스택
- 오에스
- vector
- 브루트포스
- bfs
- DP
- Stack
- cs
- 백준
- 컴공
- 컴퓨터공학과
- 코딩
- 컴공과
- 개발
- coding
- 알고리즘
- c++
Archives
- Today
- Total
Little Jay
[C++] 백준 11651번 좌표 정렬하기2 본문
sort를 적절히 활용하는 문제
sort에서 compare함수를 조금 잘 정의해야할 것 같다
여기 부분때문에 3번정도 틀린 것 같다.
문제가 y 좌표를 기준으로 비교하고,
y좌표가 같을때는 다시 x좌표를 기준으로 정렬해야되니까
이 부분 신경써서 코드를 작성해야한다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(const pair<int, int> &a, const pair<int, int> &b)
{
if (a.second < b.second) {
return true;
}
else if (a.second == b.second) {
if (a.first < b.first)
return true;
}
return false;
}
int main() {
int testCase;
cin >> testCase;
vector<pair<int, int>> vec(testCase);
for (int i = 0; i < testCase; i++) {
cin >> vec[i].first >> vec[i].second;
}
sort(vec.begin(), vec.end(), compare);
for (int i = 0; i < testCase; i++) {
cout << vec[i].first << " " << vec[i].second << "\n";
}
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 11866번 요세푸스 문제 0 (0) | 2021.04.04 |
---|---|
[C++] 백준 2869번 달팽이는 올라가고 싶다 (0) | 2021.04.04 |
[C++] 백준 10866번 덱 (0) | 2021.03.29 |
[C++] 백준 10989번 수 정렬하기3 (0) | 2021.03.26 |
[C++] 백준 10845번 큐 (0) | 2021.03.26 |
Comments