QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#97662 | #6303. Inversion | ayersz | TL | 4ms | 19464kb | C++20 | 1.6kb | 2023-04-17 20:43:35 | 2023-04-17 20:43:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int rec[N][N];
int query(int l, int r) {
if (rec[l][r] != -1) {
return rec[l][r];
}
if (l == r) {
return 0;
}
printf("? %d %d\n", l, r);
fflush(stdout);
int ret;
scanf("%d", &ret);
return rec[l][r] = ret;
}
int norm(int x) { return (x % 2 + 2) % 2; }
// 0:a[l]<a[r], 1:a[l]>a[r]
int p[N], ans[N];
int cnt;
int calc(int l, int r) {
int ret = 0;
for (int i = l; i < r; i++) {
for (int j = i + 1; j <= r; j++) {
if (p[i] > p[j]) {
ret++;
}
}
}
return ret;
}
int pd(int l, int r) {
if (l + 1 == r) {
return query(l, r);
}
int C = 0;
int A = norm(calc(l, r - 1) - calc(l + 1, r - 1));
int B = norm(query(l + 1, r) - calc(l + 1, r - 1));
return norm(query(l, r) - A - B - calc(l + 1, r - 1));
}
int main() {
memset(rec, -1, sizeof rec);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int l = 1, r = i - 1, ret = i;
while (l <= r) {
int mid = (l + r) >> 1;
if (pd(p[mid], i)) {
ret = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
for (int j = i; j > ret; j--) {
p[j] = p[j - 1];
}
p[ret] = i;
}
for (int i = 1; i <= n; i++) {
ans[p[i]] = i;
}
printf("!");
for (int i = 1; i <= n; i++) {
printf(" %d", ans[i]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 19464kb
input:
3 0 1 0
output:
? 1 2 ? 2 3 ? 1 3 ! 2 3 1
result:
ok OK, guesses=3
Test #2:
score: -100
Time Limit Exceeded
input:
1993 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 1 0 0 1 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 0 1 1 0 1 0 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0...
output:
? 1 2 ? 2 3 ? 1 3 ? 3 4 ? 2 4 ? 3 5 ? 2 5 ? 1 5 ? 3 6 ? 2 6 ? 4 6 ? 5 6 ? 3 7 ? 2 7 ? 6 7 ? 5 7 ? 3 8 ? 2 8 ? 6 8 ? 5 8 ? 7 8 ? 2 9 ? 1 9 ? 8 9 ? 7 9 ? 2 10 ? 1 10 ? 4 10 ? 3 10 ? 5 10 ? 7 10 ? 6 10 ? 2 11 ? 1 11 ? 5 11 ? 4 11 ? 3 11 ? 11 12 ? 5 12 ? 4 12 ? 7 12 ? 6 12 ? 12 13 ? 11 13 ? 5 13 ? 4 13 ...