This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 404;
int a[N];
set<int> pos[N / 2];
int mx[N];
int n, r, rnk;
int cal() {
for (int i = 0;i < n;i++) {
pos[i].clear();
pos[i].insert(a[2 * i]);
pos[i].insert(a[2 * i + 1]);
}
for (int _ = 1;_ <= 2 * n;_++) {
for (int i = 0;i < n;i++) {
auto x = pos[i].begin();
if (i == 0) {
x = pos[i].end();
x--;
}
mx[i] = *x;
pos[i].erase(x);
}
for (int i = 0;i < n;i++) {
pos[(i + n - 1) % n].insert(mx[i]);
}
}
if (rnk == 1) return 0;
if (rnk > n + 1) {
for (int i = 0;i < n;i++) {
if (pos[i].count(rnk)) return i;
}
}
int idx = 0;
for (int i = 0;i < n;i++) if (pos[i].count(rnk)) idx = i;
return (idx + r) % n;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> r;
r -= 2 * n;
for (int i = 0;i < 2 * n;i++) cin >> a[i];
rnk = a[0];
int mn = n, idx = -1;
for (int i = 0;i < 2 * n;i += 2) {
if (i) swap(a[i], a[i - 2]);
int val = cal();
if (val <= mn) {
mn = val;
idx = i / 2;
}
}
cout << idx + 1 << '\n';
return 0;
}
/*
7 4
2 6
5 8
1 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |