#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200200;
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Execution timed out |
2096 ms |
5724 KB |
Time limit exceeded |
3 |
Correct |
87 ms |
5724 KB |
Output is correct |
4 |
Execution timed out |
2074 ms |
5976 KB |
Time limit exceeded |
5 |
Correct |
3 ms |
5720 KB |
Output is correct |
6 |
Correct |
412 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
5776 KB |
Output isn't correct |
2 |
Incorrect |
408 ms |
5724 KB |
Output isn't correct |
3 |
Execution timed out |
2029 ms |
5720 KB |
Time limit exceeded |
4 |
Execution timed out |
2050 ms |
7260 KB |
Time limit exceeded |
5 |
Runtime error |
19 ms |
14424 KB |
Execution killed with signal 11 |
6 |
Correct |
413 ms |
5720 KB |
Output is correct |
7 |
Execution timed out |
2033 ms |
5720 KB |
Time limit exceeded |
8 |
Execution timed out |
2040 ms |
7256 KB |
Time limit exceeded |
9 |
Execution timed out |
2049 ms |
7772 KB |
Time limit exceeded |
10 |
Execution timed out |
2075 ms |
5724 KB |
Time limit exceeded |
11 |
Execution timed out |
2041 ms |
8024 KB |
Time limit exceeded |
12 |
Execution timed out |
2051 ms |
5976 KB |
Time limit exceeded |
13 |
Runtime error |
18 ms |
14172 KB |
Execution killed with signal 11 |
14 |
Execution timed out |
2092 ms |
6236 KB |
Time limit exceeded |
15 |
Execution timed out |
2049 ms |
9048 KB |
Time limit exceeded |
16 |
Correct |
402 ms |
5724 KB |
Output is correct |
17 |
Execution timed out |
2069 ms |
5724 KB |
Time limit exceeded |
18 |
Execution timed out |
2049 ms |
5980 KB |
Time limit exceeded |
19 |
Execution timed out |
2065 ms |
5980 KB |
Time limit exceeded |
20 |
Execution timed out |
2061 ms |
6236 KB |
Time limit exceeded |
21 |
Execution timed out |
2076 ms |
7772 KB |
Time limit exceeded |
22 |
Execution timed out |
2043 ms |
9048 KB |
Time limit exceeded |
23 |
Runtime error |
17 ms |
14172 KB |
Execution killed with signal 11 |
24 |
Correct |
408 ms |
5724 KB |
Output is correct |
25 |
Execution timed out |
2050 ms |
5720 KB |
Time limit exceeded |
26 |
Execution timed out |
2024 ms |
6236 KB |
Time limit exceeded |
27 |
Execution timed out |
2032 ms |
7768 KB |
Time limit exceeded |
28 |
Runtime error |
16 ms |
14172 KB |
Execution killed with signal 11 |
29 |
Execution timed out |
2068 ms |
5724 KB |
Time limit exceeded |
30 |
Execution timed out |
2039 ms |
6232 KB |
Time limit exceeded |
31 |
Execution timed out |
2028 ms |
7768 KB |
Time limit exceeded |
32 |
Runtime error |
16 ms |
14172 KB |
Execution killed with signal 11 |
33 |
Correct |
410 ms |
5720 KB |
Output is correct |
34 |
Correct |
410 ms |
5720 KB |
Output is correct |
35 |
Execution timed out |
2044 ms |
5976 KB |
Time limit exceeded |
36 |
Execution timed out |
2033 ms |
6232 KB |
Time limit exceeded |
37 |
Execution timed out |
2071 ms |
7516 KB |
Time limit exceeded |
38 |
Execution timed out |
2091 ms |
8284 KB |
Time limit exceeded |
39 |
Correct |
372 ms |
5720 KB |
Output is correct |
40 |
Execution timed out |
2063 ms |
5724 KB |
Time limit exceeded |
41 |
Execution timed out |
2040 ms |
5976 KB |
Time limit exceeded |
42 |
Execution timed out |
2029 ms |
6232 KB |
Time limit exceeded |
43 |
Execution timed out |
2069 ms |
6236 KB |
Time limit exceeded |
44 |
Execution timed out |
2063 ms |
6748 KB |
Time limit exceeded |
45 |
Execution timed out |
2044 ms |
7768 KB |
Time limit exceeded |
46 |
Execution timed out |
2073 ms |
8028 KB |
Time limit exceeded |
47 |
Runtime error |
17 ms |
14172 KB |
Execution killed with signal 11 |