[c/c++] ๋ฐฑ์ค€ 10825 (Silver 4)

์€๋™ยท2023๋…„ 1์›” 22์ผ
0

Baekjoon

๋ชฉ๋ก ๋ณด๊ธฐ
9/49

๐Ÿ”จ ๋ฌธ์ œ

https://www.acmicpc.net/problem/10825

<์š”์•ฝ>
n๋ช…์˜ ํ•™์ƒ์˜ ์ด๋ฆ„๊ณผ ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™ ์„ฑ์ ์„ ์ž…๋ ฅ๋ฐ›๊ณ  ์•„๋ž˜์˜ ์กฐ๊ฑด์„ ๊ฐ–์ถฐ์„œ ์ •๋ ฌํ•˜๋Š” ๋ฌธ์ œ

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

๐Ÿ”จ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

  1. ์กฐ๊ฑด์ด ๋งŽ๋‹ค! ์ด๋Ÿด ๋• ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๊ฒŒ ์ •๋ ฌ์ด ํ•„์š”ํ•จ (์กฐ๊ฑด 3๊ฐœ ์ด์ƒ)(์ด์ฐจ์›๋ฐฐ์—ด์€ sort๋ฅผ ๋ชปํ•จ)
typedef struct {
	string name;
	int kor;
	int eng;
	int math;
}student(๊ตฌ์กฐ์ฒด ์ด๋ฆ„);
  1. ๊ตฌ์กฐ์ฒด ๋ฒกํ„ฐ ์„ ์–ธ
vector <student> score(n);

-> ๊ตฌ์กฐ์ฒด ๋ฒกํ„ฐ๋Š” ์ž๋ฃŒํ˜•์„ ๊ตฌ์กฐ์ฒด ์ด๋ฆ„์œผ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋จ

  1. ๋น„๊ต ํ•จ์ˆ˜ ์„ ์–ธ
sort(score.begin(), score.end(), compare);

bool compare(student x, student y) {
	if (x.kor == y.kor) { 
		if (x.eng == y.eng) {
			if (x.math == y.math) {
				return x.name < y.name;	// ์ด๋ฆ„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ฆฌ
			}
			else return x.math > y.math; // ์ˆ˜ํ•™ ์„ฑ์  ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ฆฌ
		}
		else return x.eng < y.eng;	// ์˜์–ด ์„ฑ์  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ฆฌ
	}
	else return x.kor > y.kor; // ๊ตญ์–ด ์„ฑ์  ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ฆฌ
}

-> a>b? ์—์„œ true๊ฐ€ ๋ฐ˜ํ™˜๋˜๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
a<b?์—์„œ true๊ฐ€ ๋ฐ˜ํ™˜๋˜๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ


๐Ÿ”จ ์ฝ”๋“œ

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

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

bool compare(student x, student y) {
	if (x.kor == y.kor) {
		if (x.eng == y.eng) {
			if (x.math == y.math) {
				return x.name < y.name;
			}
			else return x.math > y.math;
		}
		else return x.eng < y.eng;
	}
	else return x.kor > y.kor;
}

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n;
	cin >> n;
	vector <student> score(n);
	
	for (int i = 0; i < n; i++) {
		cin >> score[i].name >> score[i].kor >> score[i].eng >> score[i].math;
	}
	sort(score.begin(), score.end(), compare);

	for (int i = 0; i < n; i++) {
		cout << score[i].name << '\n';
	}

	return 0;
}
profile
์ž์ž ์„ ์ˆ˜์ž…์žฅ~

0๊ฐœ์˜ ๋Œ“๊ธ€