코테/정렬
[C/C++] 백준 10825, 정렬
상똥
2023. 3. 8. 14:40
코드
#include <bits/stdc++.h>
using namespace std;
struct student {
string name;
int k;
int e;
int m;
};
bool cmp(struct student A, struct student B) {
if (A.k == B.k) {
if (A.e == B.e) {
if (A.m == B.m)
return A.name < B.name;
return A.m > B.m;
}
return A.e < B.e;
}
return A.k > B.k;
}
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<student> input(N);
for (int i = 0; i < N; i++)
cin >> input[i].name >> input[i].k >> input[i].e >> input[i].m;
sort(input.begin(), input.end(), cmp);
for (int i = 0; i < N; i++)
cout << input[i].name << "\n";
return 0;
}
풀이
1. struct 를 활용해 하나의 벡터에 서로 다른 타입의 데이터를 넣는다. 이때 벡터의 크기를 미리 N으로 설정해 out of bounds 방지
2. cmp함수를 따로 만들어 조건에 맞게 벡터를 정렬한다.
참고로 나는 시간초과가 자꾸 떠서,, 서치를 통해 해결했다 참고하시길,,,,
[C++] 시간초과 해결방법 (tistory.com)