QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#219732 | #6303. Inversion | ucup-team198 | TL | 0ms | 0kb | C++23 | 2.6kb | 2023-10-19 17:50:22 | 2023-10-19 17:50:22 |
answer
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cassert>
using namespace std;
// #define debug(x) cout<<"[debug]"#x<<"="<<x<<endl
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ld eps=1e-8;
const int INF=0x3f3f3f3f,mod=998244353;
const ll INFF=0x3f3f3f3f3f3f3f3f;
#ifndef ONLINE_JUDGE
#define debug(...)
#include<debug>
#else
#define debug(...)
#endif
const int N=2003;
int a[N]={0,5,2,3,1,4};
int n;
int res[N];
int cnt=0;
int ask(int l,int r)
{
if(l>=r) return 0;
cnt++;
cout<<"? "<<l<<" "<<r<<endl;
int ans=0;
// for(int i=l;i<=r;i++)
// {
// for(int j=i+1;j<=r;j++)
// {
// ans^=(a[i]>a[j]);
// }
// }
cin>>ans;
return ans;
}
int mir[N];
int s[N];
int get(int l,int r)
{
return (s[r]-s[l-1])&1;
}
// return the first i of [l,r],such as check(i)=true
// if no such i,return r+1
template<class T,class Func>
T binarySearchMinAnswer(T l,T r,Func check) {
T ans=r+1;
while(l<=r) {
T mid=l+(r-l)/2;
if(check(mid)) {
ans=mid;
r=mid-1;
}else l=mid+1;
}
return ans;
}
// return the last i of [l,r],such as check(i)=true
// if no such i,return l-1
template<class T,class Func>
T binarySearchMaxAnswer(T l,T r,Func check) {
T ans=l-1;
while(l<=r) {
T mid=l+(r-l)/2;
if(check(mid)) {
ans=mid;
l=mid+1;
}else r=mid-1;
}
return ans;
}
int main()
{
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
auto f=[&](int L,int R){
return ask(L,R)^ask(L+1,R)^get(L,R-1)^get(L+1,R-1);
};
vector<int> ver;
ver.push_back(1);
for(int i=2;i<=n;i++)
{
for(int j=1;j<=i-1;j++) s[j]=s[j-1]+mir[j];
int l=0,r=ver.size()-1;
//找到最大的<a[i]的位置
auto check=[&](int mid){
return !f(ver[mid],i);
};
l=binarySearchMaxAnswer(l,r,check);
ver.push_back(0);
for(int j=ver.size()-2;j>=l+1;j--)
{
mir[ver[j]]++;
ver[j+1]=ver[j];
}
// cerr<<l<<endl;
ver[l+1]=i;
}
// for(int i=0;i<n;i++)
// {
// cerr<<ver[i]<<endl;
// }
// cerr<<cnt<<endl;
assert(cnt<=40000);
for(int i=0;i<n;i++)
{
res[ver[i]]=i+1;
}
cout<<"! ";
for(int i=1;i<=n;i++) cout<<res[i]<<" ";
cout<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3