https://www.acmicpc.net/problem/11650
11650번: 좌표 정렬하기
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
vector를 안지 며칠이 안지났기에.. 잘 알지 못한다. 필요할때 찾아서 공부하기 때문
그래서 아무생각없이 쳐내려갔더니 결과는 틀렸습니다.
그래서 2차원 vector에 대해 찾아보고 적용시켜본 결과 정답이 나왔다.
아래 첫번째 소스코드는 1차원벡터를 사용한 오답이다.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, x, y, num;
vector<int> a;
vector<int> b;
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> x >> y;
a.push_back(x);
b.push_back(y);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for (int i = 0; i < N; i++)
{
cout << a[i] << " " << b[i] << "\n";
}
}
아래는 2차원 vector를 사용한 정답.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, x, y, num;
vector<pair<int, int>> vec;
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> x >> y;
vec.push_back(make_pair(x, y));
}
sort(vec.begin(), vec.end(), less<>());
for (int i = 0; i < N; i++)
{
cout << vec[i].first << " " << vec[i].second << "\n";
}
}
'STUDY > Baekjoon' 카테고리의 다른 글
[백준] 2108 c++ 통계학 (0) | 2022.08.22 |
---|---|
[백준] 10814 c++ 나이순 정렬 (0) | 2022.08.21 |
[백준] 10816 c++ 숫자 카드 2 (0) | 2022.08.19 |
[백준] 1920 c++ 수 찾기 (0) | 2022.08.19 |
[백준] 10773 c++ 제로 (0) | 2022.08.19 |