문제
https://www.acmicpc.net/problem/25955
시간 제한 | 메모리 제한 | solved.ac 티어 |
1초 | 512MB | 실버 4 |
풀이
오늘은 개인적으로 일이 바빠서 실버문제 한문제만 풀고 정리하려고 한다. 정렬문제이다. 랜덤으로 뽑았는데 이게 나왔다.
정렬되어있지 않은 두개의 값을 찾는 문제이다. 원소의 개수도 많지 않고, 찾아야 하는 원소도 두개뿐이니, 정렬한 다음에 같은 인덱스의 다른 원소 둘을 찾아 출력하면 된다.
코드
#include <bits/stdc++.h>
#define ALL(X) X.begin(), X.end()
#define endl '\n'
using namespace std;
int n;
vector<string> diff;
string symbol = "BSGPD";
void input() {
cin >> n;
diff.resize(n);
for(auto& str : diff) cin >> str;
}
bool cmp(string& s1, string& s2) { // s1 < s2 : true
if (s1[0] != s2[0]) {
return symbol.find(s1[0]) < symbol.find(s2[0]);
}
else {
int n1 = stoi(s1.substr(1, s1.length() - 1));
int n2 = stoi(s2.substr(1, s2.length() - 1));
return n1 > n2;
}
}
void sol() {
if (is_sorted(ALL(diff), cmp)) {
cout << "OK";
}
else {
cout << "KO\n";
vector<string> repl = diff;
sort(ALL(repl), cmp);
for(int i= n - 1; i >= 0; i--) {
if (repl[i] != diff[i]) {
cout << diff[i] << ' ';
}
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
input();
sol();
}
'coding_test > BAEKJOON' 카테고리의 다른 글
백준 17252번, 17253번 C++ 풀이 (0) | 2025.01.21 |
---|---|
백준 17451번 C++ 풀이 (0) | 2025.01.21 |
백준 17609번 C++ 풀이 (0) | 2025.01.19 |
백준 1253번 Python 풀이 (0) | 2025.01.19 |
백준 12904번 C++ 풀이 (0) | 2025.01.19 |