QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#97353 | #6303. Inversion | ksunhokim | RE | 1ms | 3324kb | C++20 | 2.1kb | 2023-04-16 17:52:01 | 2023-04-16 17:52:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int cnt = 0;
map<pair<int,int>, bool> cache;
map<pair<int,int>, bool> cache2;
// #define HIDDEN
vector<int> hidden = {3, 1, 2};
int query(int l, int r) {
if (l == r)
return 0;
if (cache.count({l,r}))
return cache[{l,r}];
cnt ++;
assert(cnt <= 4e4);
cout << "? " << l + 1 << " " << r + 1 << endl;
int res;
cin >> res;
#ifdef HIDDEN
int rr = 0;
for (int i=l;i<=r;i++){
for (int j=l;j<=i;j++){
if (hidden[j] > hidden[i]) {
rr++;
}
}
}
res = rr%2;
#endif
return cache[{l,r}] = res;
}
bool compare(int i, int j) {
if (i > j)
return !compare(j,i);
if (cache2.count({i,j}))
return cache2[{i,j}];
if (i == j) {
return false;
}
if (i == j-1) {
return cache2[{i,j}] = query(i,j);
}
return cache2[{i,j}] = (query(i, j) - query(i, j-1) - query(i+1, j) + query(i+1, j-1) + 8)%2;
}
int main() {
int n;
cin >> n;
// unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
// hidden.assign(n,0);
// iota(begin(hidden), end(hidden),0);
// shuffle(begin(hidden), end(hidden), std::default_random_engine(seed));
vector<int> A(n);
iota(begin(A), end(A), 0);
vector<vector<ll>> inv(n, vector<ll>(n));
for (int i=1;i<n;i++) {
int ng = -1, ok = i;
while (ok-ng > 1) {
int m = (ok+ng)/2;
if (compare(A[i], A[m])) {
ok = m;
} else {
ng = m;
}
}
int tmp = A[i];
for (int j=i;j>=ok+1;j--){
A[j] = A[j-1];
}
A[ok] = tmp;
int cnt = 0;
for (int j=i-1;j>=0;j--) {
if (A[j] > tmp)
cnt ++;
inv[A[j]][i] = inv[A[j]][i-1] + cnt;
if (A[j] > tmp) {
cache[{A[j], tmp}] = inv[j][i] % 2;
} else {
cache[{tmp, A[j]}] = (inv[j][i] % 2)^1;
}
}
}
reverse(begin(A), end(A));
vector<int> ans(n);
for (int i=0;i<n;i++) ans[A[i]] = i;
cout << "! ";
for (int i=0;i<n;i++){
cout << ans[i] + 1 << " ";
}
cout << endl;
}
// 2 1
// 3 1 2
//
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3324kb
input:
3 0 1 0
output:
? 1 2 ? 2 3 ? 1 3 ! 2 3 1
result:
ok OK, guesses=3
Test #2:
score: -100
Dangerous Syscalls
input:
1993 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 0 0 0 0 1 0 1 1 0 1 1 1 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 1 0 0 1 0 1 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 1 0...
output:
? 1 2 ? 2 3 ? 2 4 ? 3 4 ? 3 5 ? 4 5 ? 2 5 ? 1 5 ? 1 4 ? 2 6 ? 3 6 ? 1 6 ? 5 6 ? 2 7 ? 3 7 ? 6 7 ? 5 7 ? 1 8 ? 1 7 ? 2 8 ? 3 8 ? 4 8 ? 4 7 ? 2 9 ? 3 9 ? 6 9 ? 6 8 ? 7 9 ? 7 8 ? 1 9 ? 9 10 ? 6 10 ? 7 10 ? 5 10 ? 5 9 ? 8 10 ? 8 9 ? 9 11 ? 10 11 ? 5 11 ? 6 11 ? 1 11 ? 1 10 ? 2 11 ? 2 10 ? 11 12 ? 8 12 ?...