문제 : https://www.acmicpc.net/problem/11650


[알고리즘풀이]

1) Algorithm Library Sort함수 이용

좌표를 다 입력받고, 기준에 맞춰서 sort하면 됩니다.

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

vector<pair<int, int>> v;
bool compare(pair<int, int>, pair<int, int>);

int main(void) {
	int n;
    cin >> n;
	for (int i = 0; i < n; i++){
        int x, y;
        cin >> x >> y;
        v.push_back(make_pair(x,y));
    }
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++)
        cout << v[i].first << ' ' << v[i].second << '\n';
}

bool compare(pair<int, int> input1, pair<int, int> input2) {
	if (input1.first < input2.first)
		return true;
	else if (input1.first > input2.first)
		return false;
	else {
		if (input1.second < input2.second)
			return true;
		else
			return false;
	}
}

2) Queue Library Priority Queue 이용

우선순위큐를 문제에 맞춰 우선순위를 할당하고, 우선순위큐에 좌표를 다 넣는다.

#include<iostream>
#include<queue>

using namespace std;

struct cmp {
	bool operator()(pair<int, int>n1, pair<int,int> n2) {
		if (n1.first > n2.first)
			return true;
		else if (n1.first == n2.first)
			if (n1.second > n2.second)
				return true;
			else
				return false;
		return false;
	}
};

int main(void) {
	priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x, y;
		cin >> x >> y;
		pq.push(make_pair(x, y));
	}
	while (!pq.empty()) {
		cout << pq.top().first << ' ' << pq.top().second << '\n';
		pq.pop();
	}
}

 

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 12042 - Lazy Spelling Bee (Small)  (0) 2019.10.15
[BOJ] 4949 - The Balance of the World 코드 개선  (0) 2019.10.04
[BOJ] 2161 - 카드1  (0) 2019.10.04
[BOJ] 9421 - Happy Prime Number  (0) 2019.10.03
[BOJ] 1456 - 거의 소수  (0) 2019.10.03

+ Recent posts