본문 바로가기
Problem Solving

[C++] [백준 11650] 좌표 정렬하기

by tls1107 2021. 7. 27.
728x90
반응형


문제 풀기 전 생각 : 

/*
pair 와 sort 함수를 사용하면 쉽게 
풀 수 있는 문제이다!
*/

#include <iostream>
#include <algorithm>
using namespace std;
bool comp(pair<int,int> a,pair<int,int> b){
  if(a.first == b.first){
    return a.second < b.second;
  }
  return a.first < b.first;
}
int main(){
  int n;
  cin >> n;
  pair<int,int> arr[n];

  for(int i=0 ; i<n ; i++){
    cin >> arr[i].first >> arr[i].second;
  }

  sort(arr,arr+n,comp);

  for(int i=0 ; i<n ; i++){
    cout << arr[i].first << " " << arr[i].second << "\n";
  }
  return 0;
}

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


풀 때 어려웠던 점 또는 느낀점 :

 

이정도는 이제 껌이죠~~


개선방안 :

#include <iostream>
#include <algorithm>
using namespace std;


int main()
{
	cin.tie(0); ios_base::sync_with_stdio(0);
	pair<int, int> v[100000];
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> v[i].first >> v[i].second;
	}
	sort(v, v+n);
	for(int i = 0; i < n; i++)
		cout << v[i].first << " " << v[i].second << '\n';
	return 0;
}

같은 접근법이다!

나이스~!

728x90
반응형

댓글