๐Ÿ’  BOJ/Class 2

[BOJ][C++] ๋ฐฑ์ค€ 11650๋ฒˆ: ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ

์„ ๋‹ฌ 2023. 3. 13. 23:27
๋ฐ˜์‘ํ˜•

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

 

๋ฌธ์ œ

2์ฐจ์› ํ‰๋ฉด ์œ„์˜ ์  N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ขŒํ‘œ๋ฅผ x์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์œผ๋กœ, x์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด y์ขŒํ‘œ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ๋‹ค์Œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” i๋ฒˆ์ ์˜ ์œ„์น˜ xi์™€ yi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (-100,000 ≤ xi, yi ≤ 100,000) ์ขŒํ‘œ๋Š” ํ•ญ์ƒ ์ •์ˆ˜์ด๊ณ , ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๋‘ ์ ์€ ์—†๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ ์„ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

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

using namespace std;

typedef pair<int, int> ci;

bool cmp(ci a, ci b) {
    if(a.first == b.first)
        return a.second < b.second;
    return a.first < b.first;
}

int main() {
    
    // ์ž…๋ ฅ
    int n;
    cin >> n;
    vector<ci> v(n);
    for(int i=0; i<n; i++) {
        int a, b;
        cin >> a >> b;
        v[i] = {a,b};
    }
    
    // ์—ฐ์‚ฐ
    sort(v.begin(), v.end(), cmp);
    
    //  ์ถœ๋ ฅ
    for(ci i : v) {
        cout << i.first << " " << i.second << "\n";
    }
    
    return 0;
}
๋ฐ˜์‘ํ˜•