๐Ÿ•๏ธ ICPC Sinchon/Sorting

[BOJ S4][C++] ๋ฐฑ์ค€ 10825๋ฒˆ : ๊ตญ์˜์ˆ˜

์„ ๋‹ฌ 2022. 9. 4. 00:45
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

๋„ํ˜„์ด๋„ค ๋ฐ˜ ํ•™์ƒ N๋ช…์˜ ์ด๋ฆ„๊ณผ ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™ ์ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์œผ๋กœ ํ•™์ƒ์˜ ์„ฑ์ ์„ ์ •๋ ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

  1. ๊ตญ์–ด ์ ์ˆ˜๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ์ˆœ์„œ๋กœ
  2. ๊ตญ์–ด ์ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด ์˜์–ด ์ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ
  3. ๊ตญ์–ด ์ ์ˆ˜์™€ ์˜์–ด ์ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด ์ˆ˜ํ•™ ์ ์ˆ˜๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ์ˆœ์„œ๋กœ
  4. ๋ชจ๋“  ์ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด ์ด๋ฆ„์ด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ (๋‹จ, ์•„์Šคํ‚ค ์ฝ”๋“œ์—์„œ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์— ์˜จ๋‹ค.)

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋„ํ˜„์ด๋„ค ๋ฐ˜์˜ ํ•™์ƒ์˜ ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๊ฐ ํ•™์ƒ์˜ ์ด๋ฆ„, ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™ ์ ์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ฃผ์–ด์ง„๋‹ค. ์ ์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์ด๋ฆ„์€ ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด๊ณ , ๊ธธ์ด๋Š” 10์ž๋ฆฌ๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

์ถœ๋ ฅ

๋ฌธ์ œ์— ๋‚˜์™€์žˆ๋Š” ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ ์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

sort๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌ์„ ํ•ด์ฃผ๋Š”๋ฐ ์ด๋•Œ Compare comp ๋ฅผ ์ž˜ ์„ค์ •ํ•ด์ฃผ๋ฉด ์‰ฝ๊ฒŒ ํ’€์ดํ•  ์ˆ˜ ์žˆ๋‹ค.

bool cmp(student a, student b) {
    if(a.kor == b.kor) {
        if(a.eng == b.eng) {
            if(a.math == b.math) {
                return a.name < b.name;
            }
            return a.math > b.math;
        }
        return a.eng < b.eng;
    }
    return a.kor > b.kor;
}

๋ง ๊ทธ๋Œ€๋กœ ํ‘ผ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๊ฒ ์ง€๋งŒ...

๋ณด๊ธฐ ๋„ˆ๋ฌด ์•ˆ์ข‹๋‹ค

 

bool cmp(student a, student b) {
    if(a.kor != b.kor) return a.kor > b.kor;
    if(a.eng != b.eng) return a.eng < b.eng;
    if(a.math != b.math) return a.math > b.math;
    else return a.name < b.name;
}

๊ฐ ์ ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์— ์กฐ๊ฑด์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋„๋ก ๋ฐ”๊พธ๋ฉด ์ฝ”๋“œ๊ฐ€ ๋”์šฑ ๊น”๋”ํ•ด์ง„๋‹ค

 

// Authored by : seondal
// Co-authored by : -

// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct student {
    string name;
    int kor, eng, math;
};

bool cmp(student a, student b) {
    if(a.kor != b.kor) return a.kor > b.kor;
    if(a.eng != b.eng) return a.eng < b.eng;
    if(a.math != b.math) return a.math > b.math;
    else return a.name < b.name;
}


int main() {
    int n;
    cin >> n;
    
    vector<student> v(n);
    for(int i=0; i<n; i++) cin >> v[i].name >> v[i].kor >> v[i].eng >> v[i].math;
    
    sort(v.begin(), v.end(), cmp);
    
    for(int i=0; i<n; i++) cout << v[i].name << "\n";
    
    return 0;
}

/*
 */
๋ฐ˜์‘ํ˜•