QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621408#8553. Exchanging Kubicucup-team3651TL 26ms3664kbC++143.7kb2024-10-08 14:19:432024-10-08 14:20:05

Judging History

你现在查看的是最新测评结果

  • [2024-10-08 14:20:05]
  • 评测
  • 测评结果:TL
  • 用时:26ms
  • 内存:3664kb
  • [2024-10-08 14:19:43]
  • 提交

answer


#include<bits/stdc++.h>

#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array

#define N 5005
#define inf 0x3f3f3f3f3f3f3f3f

using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x*f;
}
void write(int x){
    if(x<0)x=-x,putchar('-');
    if(x/10)write(x/10);
    putchar(x%10+'0');
}

struct Node{
    int l,r,val;
    Node(int l=0,int r=0,int val=0):l(l),r(r),val(val){}
};
vector<Node> S;
int res[N];

void solve(){
    int n=read();S.clear();
    for(int i=1;i<=n;i++)res[i]=0;
    for(int i=1;i<=n;i++){
        cout<<'?'<<' '<<i<<' '<<i<<endl;
        res[i]=read();S.pb(Node(i,i,res[i]));
    }
    for(int i=0;i<S.size();i++){
        while(i+1<S.size() && (S[i+1].val<=0)==(S[i].val<=0)){
            S[i].r=S[i+1].r;S[i].val+=S[i+1].val;
            S.erase(S.begin()+i+1);
        }
    }
    while(S.size()!=1){
        if(S.back().val==0)S.pop_back();
        else if(S[0].val==0)S.erase(S.begin());
        else{
            pi minn=mp(inf,0);
            for(int i=0;i<S.size();i++){
                (minn<mp(S[i].val,i))?minn=mp(S[i].val,S[i].l):minn;
            }
            if(minn.se==0){
                cout<<'?'<<' '<<S[0].l<<' '<<S[2].r<<endl;
                int now=read();
                if(now!=S[0].val && now!=S[2].val){
                    res[S[1].l]=now-S[0].val-S[2].val;
                    int L=S[0].l,R=S[2].r;S.erase(S.begin());S.erase(S.begin());
                    S[0].l=L;S[0].val=now;
                }
                else{
                    res[S[1].l]=-S[0].val;S.erase(S.begin());S.erase(S.begin());
                }
            }
            else if(minn.se==S.size()-1){
                int las=S.size()-1;
                cout<<'?'<<' '<<S[las-2].l<<' '<<S[las].r<<endl;
                int now=read();
                if(now!=S[las-2].val && now!=S[las].val){
                    res[S[las-1].l]=now-S[las-2].val-S[las].val;
                    int L=S[las-2].l,R=S[las].r;S.pop_back();S.pop_back();
                    S[S.size()-1].r=R;S[S.size()-1].val=now;
                }
                else{
                    res[S[las-1].l]=-S[las].val;S.pop_back();S.pop_back();
                }
            }
            else{
                int pos=minn.se;
                cout<<'?'<<' '<<S[pos-2].l<<' '<<S[pos].r<<endl;
                int now=read();
                if(now!=S[pos-2].val && now!=S[pos].val){
                    res[S[pos-1].l]=now-S[pos].val-S[pos-2].val;
                    int R=S[pos].r;S.erase(S.begin()+pos-1);S.erase(S.begin()+pos-1);
                    S[pos-2].val=now;S[pos-2].r=R;continue;
                }
                cout<<'?'<<' '<<S[pos].l<<' '<<S[pos+2].r<<endl;
                if(now!=S[pos].val && now!=S[pos+2].val){
                    res[S[pos+1].l]=now-S[pos].val-S[pos+2].val;
                    int R=S[pos+2].r;S.erase(S.begin()+pos+1);S.erase(S.begin()+pos+1);
                    S[pos].val=now;S[pos].r=R;continue;
                }
                res[S[pos+1].l]=-S[pos].val;int R=S[pos+1].r;
                S.erase(S.begin()+pos);S.erase(S.begin()+pos);
                S[pos-1].r=R;
            }
        }
    }
    cout<<'!'<<' ';
    for(int i=1;i<=n;i++)write(res[i]),putchar(' ');
    cout<<endl;
}

int main(){
    int T=read();while(T--)solve();
    
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3604kb

input:

2
3
1
0
1
1
5
2
0
3
0
5
4
5

output:

? 1 1
? 2 2
? 3 3
? 1 3
! 1 -1 1 
? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 1 3
? 1 5
! 2 -1 3 -4 5 

result:

ok ok (2 test cases)

Test #2:

score: 0
Accepted
time: 26ms
memory: 3664kb

input:

10000
1
718876398
1
0
1
0
1
0
1
938356223
1
857157125
1
0
1
0
1
0
1
0
1
0
1
965894497
1
0
1
626061677
1
642094813
1
107398046
1
0
1
0
1
0
1
287188651
1
0
1
345108193
1
0
1
0
1
714952783
1
0
1
325760098
1
0
1
800487422
1
322806679
1
0
1
0
1
0
1
866952779
1
741483102
1
492063609
1
0
1
833448280
1
0
1
...

output:

? 1 1
! 718876398 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 938356223 
? 1 1
! 857157125 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 965894497 
? 1 1
! 0 
? 1 1
! 626061677 
? 1 1
! 642094813 
? 1 1
! 107398046 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 0 
? 1 1
! 287188651 
? 1 1
! 0 
? 1 1...

result:

ok ok (10000 test cases)

Test #3:

score: -100
Time Limit Exceeded

input:

1052
9
167536100
0
373372185
0
427949326
442758705
102715118
0
0
373372185
973423149
9
248442934
306962195
570791475
593033322
0
582850731
559429390
0
120053133

output:

? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 1 3
? 3 7
! 167536100 -167536100 373372185 -373372185 427949326 442758705 102715118 0 0 
? 1 1
? 2 2
? 3 3
? 4 4
? 5 5
? 6 6
? 7 7
? 8 8
? 9 9
? 0 5

result: