![[백준 / BOJ] 11651번 좌표 정렬하기 2(C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcmODb6%2FbtrJrQMDQyu%2FbgwCh5jZonmrfgJLJvNnnk%2Fimg.png)
[백준 / BOJ] 11651번 좌표 정렬하기 2(C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 6. 1. 13:35
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/11651
11651번: 좌표 정렬하기 2
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
문제

문제 풀이
지난 문제에서 정렬의 기준이 x에서 y로 바뀐 점 말고는 모두 동일하다. 참고하면 좋을 것 같다.
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
class pair { | |
public: | |
int x; | |
int y; | |
}; | |
bool cmp(pair a, pair b) { | |
if (a.y == b.y) | |
return a.x < b.x; | |
else | |
return a.y < b.y; | |
} | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int N; | |
std::cin >> N; | |
std::vector<pair> v(N); | |
for (int i = 0; i < N; ++i) | |
std::cin >> v[i].x >> v[i].y; | |
std::sort(v.begin(), v.end(), cmp); | |
for (int i = 0; i < N; ++i) | |
std::cout << v[i].x << " " << v[i].y << "\n"; | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin.readline | |
N = int(input()) | |
arr = [] | |
for i in range(N): | |
x, y = map(int, input().split()) | |
arr.append([y, x]) | |
arr.sort() | |
for y, x in arr: | |
print(x, y) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 10814번 나이순 정렬 (C++, Python) (0) | 2022.06.01 |
---|---|
[백준 / BOJ] 1181번 단어 정렬(C++, Python) (0) | 2022.06.01 |
[백준 / BOJ] 11650번 좌표 정렬하기 (C++, Python) (0) | 2022.05.31 |
[백준 / BOJ] 1427번 소트인사이드 (C++, Python) (0) | 2022.05.31 |
[백준 / BOJ] 2108번 통계학 (C++, Python) (0) | 2022.05.31 |
@Reo :: 코드 아카이브
자기계발 블로그