백준 11651번 (좌표 정렬하기 2, C++) [BAEKJOON]

좌표 정렬하기 2

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

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초256 MB53955350182984067.141%

문제

2차원 평면 위의 점 N개가 주어진다.

좌표를 y좌표가 증가하는 순으로,

y좌표가 같으면 x좌표가 증가하는 순서로 정렬한 다음 출력하는 프로그램을 작성하시오.

입력

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 

둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000)

좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

출력

첫째 줄부터 N개의 줄에 점을 정렬한 결과를 출력한다.

예제 입력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
5
0 4
1 2
1 -1
2 2
3 3
5 0 4 1 2 1 -1 2 2 3 3
5
0 4
1 2
1 -1
2 2
3 3

예제 출력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 -1
1 2
2 2
3 3
0 4
1 -1 1 2 2 2 3 3 0 4
1 -1
1 2
2 2
3 3
0 4

출처

알고리즘 분류


백준 11650번과 차이가 없는 문제 입니다.

통과된 코드

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <map>
using namespace std;
multimap<int, int> myMap;
multimap<int, int> myMap2;
int arr[3];
// multimap의 특성을 이용한 정렬
int main()
{
ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
cin.tie(NULL);
cout.tie(NULL);
cin >> arr[0];
for (int i = 0; i < arr[0]; i++) {
cin >> arr[1] >> arr[2];
myMap.insert(make_pair(arr[1],arr[2]));
}
for (auto it = myMap.begin(); it != myMap.end(); it++) {
myMap2.insert(make_pair(it->second, it->first));
}
for (auto it = myMap2.begin(); it != myMap2.end(); it++) {
cout << it->second << " " << it->first << "\n";
}
return 0;
}
#include <iostream> #include <map> using namespace std; multimap<int, int> myMap; multimap<int, int> myMap2; int arr[3]; // multimap의 특성을 이용한 정렬 int main() { ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화 // cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다. cin.tie(NULL); cout.tie(NULL); cin >> arr[0]; for (int i = 0; i < arr[0]; i++) { cin >> arr[1] >> arr[2]; myMap.insert(make_pair(arr[1],arr[2])); } for (auto it = myMap.begin(); it != myMap.end(); it++) { myMap2.insert(make_pair(it->second, it->first)); } for (auto it = myMap2.begin(); it != myMap2.end(); it++) { cout << it->second << " " << it->first << "\n"; } return 0; }
#include <iostream>
#include <map>

using namespace std;

multimap<int, int> myMap;
multimap<int, int> myMap2;

int arr[3];

// multimap의 특성을 이용한 정렬

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> arr[0];
	for (int i = 0; i < arr[0]; i++) {
		cin >> arr[1] >> arr[2];
		myMap.insert(make_pair(arr[1],arr[2]));
	}

	for (auto it = myMap.begin(); it != myMap.end(); it++) {
		myMap2.insert(make_pair(it->second, it->first));
	}

	for (auto it = myMap2.begin(); it != myMap2.end(); it++) {
		cout << it->second << " " << it->first << "\n";
	}

	return 0;
}

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다