Educational Codeforces Round 129 (Rated for Div. 2) 1545 → 1537
https://codeforces.com/contest/1681
Dashboard - Educational Codeforces Round 129 (Rated for Div. 2) - Codeforces
codeforces.com
!!!! 모든 답은 제 풀이일 뿐 공부를 위한 올바른 정해가 아닐 수 있습니다 !!!!
A. Game with Cards
https://codeforces.com/contest/1681/problem/A
Problem - A - Codeforces
codeforces.com
문제
앨리스와 밥은 각각 n개의 카드와 m개의 카드를 들고 있다. 각 턴마다 번갈아가면서 카드를 하나 뽑는데 직전 카드보다 높은 수를 가진 카드를 뽑아야 한다. 엘리스가 먼저 시작했을때와 밥이 먼저 시작했을때 누가 이기는지 출력하는 문제
풀이
가장 높은 숫자를 가진 사람이 이긴다. 만약 같다면 먼저하는사람이 이긴다.
코드
#include <bits/stdc++.h>
using namespace std;
int a[51], b[51];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 0; i < n; i++)cin >> a[i];
sort(a, a + n);
int m;
cin >> m;
for (int i = 0; i < m; i++)cin >> b[i];
sort(b, b + m);
if (a[n - 1] > b[m-1]) {
cout << "Alice\n";
cout << "Alice\n";
}
else if (a[n - 1] < b[m - 1]) {
cout << "Bob\n";
cout << "Bob\n";
}
else {
cout << "Alice\n";
cout << "Bob\n";
}
}
}
B. Card Trick
https://codeforces.com/contest/1681/problem/B
Problem - B - Codeforces
codeforces.com
문제
n과 m이 주어진다. n개의 카드와 m번의 카드 섞기이며 b[i]번째 섞기에는 난맨 위에서부터 b[i]개의 카드를 그대로 맨 밑에 넣는 과정을 m번 반복한다. 이때 다 섞은 후 가장 위 카드를 출력하는 문제
풀이
모든 b[i]값을 더한후 n으로 나눈 나머지의 인덱스를 출력해준다.
코드
#include <bits/stdc++.h>
using namespace std;
int a[200001], b[200001];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 0; i < n; i++)cin >> a[i];
int m;
cin >> m;
int sum = 0;
for (int i = 0; i < m; i++) {
cin >> b[i];
sum += b[i];
sum %= n;
}
cout << a[sum] << '\n';
}
}
C. Double Sort
https://codeforces.com/contest/1681/problem/C
Problem - C - Codeforces
codeforces.com
문제
n과 n길이의 배열 a,b가 주어진다. 인덱스 i,j를 골라 swap(a[i],a[j]),swap(b[i],b[j]) 하는 연산을 최대 10000번 할 수 있을때 둘다 비 내림차순으로 만들 수 있다면 연산의 수와 i,j연산을 순서대로 출력하고 아니라면 -1을 출력하는 문제
풀이
pair로 a와 b를 받아 그대로 sort한 후 비 내림차순이 되었다면 가능하다. 순서 출력은 인덱스를 따로 저장하여 결과로 나온 인덱스를 초기 상태로 만드는 과정을 저장하여 거꾸로 출력해주면 된다.
코드
#include <bits/stdc++.h>
using namespace std;
struct A {
pair<int, int> pa;
int idx;
bool operator < (const A& p)const {
return pa < p.pa;
}
};
A a[101];
vector<pair<int, int>> v;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int T;
cin >> T;
while (T--) {
v.clear();
int n;
cin >> n;
for (int i = 0; i < n; i++)cin >> a[i].pa.first;
for (int i = 0; i < n; i++) {
cin >> a[i].pa.second;
a[i].idx = i;
}
sort(a, a + n);
bool flag = true;
for (int i = 1; i < n; i++) {
if (a[i - 1].pa.first > a[i].pa.first) {
flag = false;
break;
}
if (a[i - 1].pa.second > a[i].pa.second) {
flag = false;
break;
}
}
if (flag) {
for (int i = 0; i < n; i++) {
if (a[i].idx != i) {
int i2 = i + 1;
for (int j = i + 1; j < n; j++) {
if (a[j].idx == i) {
i2 = j;
break;
}
}
swap(a[i].idx, a[i2].idx);
v.push_back({ i + 1,i2 + 1 });
}
}
cout << v.size() << '\n';
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i].first << ' ' << v[i].second << '\n';
}
}
else {
cout << "-1\n";
}
}
}
자세한 설명에 중점을 두기보다는 대회 기록에 중점을 둔 글입니다 !
틀린 부분은 감사히 지적받겠습니다.