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 = 5050;
int a[2 * N];
int val[N][2];
int mx[N];
int n, r, rnk;
int cal() {
for (int i = 0;i < n;i++) {
val[i][0] = min(a[2 * i], a[2 * i + 1]);
val[i][1] = max(a[2 * i], a[2 * i + 1]);
}
for (int _ = 1;_ <= 2 * n;_++) {
for (int i = 0;i < n;i++) {
if (i == 0) swap(val[i][0], val[i][1]);
mx[i] = val[i][0];
}
for (int i = 0;i < n;i++) {
val[i][0] = mx[(i + 1) % n];
if (val[i][0] > val[i][1])
swap(val[i][0], val[i][1]);
}
}
if (rnk == 1 || n + 1 < rnk) {
for (int i = 0;i < n;i++) {
if (val[i][0] == rnk || val[i][1] == rnk) return i;
}
}
for (int i = 0;i < n;i++)
if (val[i][0] == rnk || val[i][1] == rnk) return (i + r) % n;
return -1;
}
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... |