#include "communication.h"
#include <bits/stdc++.h>
using namespace std;
//
// --- Sample implementation for the task communication ---
//
// To compile this program with the sample grader, place:
// communication.h communication_sample.cpp sample_grader.cpp
// in a single folder, then open the terminal in this directory (right-click onto an empty spot in the directory,
// left click on "Open in terminal") and enter e.g.:
// g++ -std=c++17 communication_sample.cpp sample_grader.cpp
// in this folder. This will create a file a.out in the current directory which you can execute from the terminal
// as ./a.out
// See task statement or sample_grader.cpp for the input specification
//
typedef pair<int,int> ii;
typedef vector<ii> vii;
#define pf printf
#define pb push_back
#define sz(x) (int)x.size()
int getSize(vii &v){
int sum=0;
for(auto[a,b]:v)sum+=b-a+1;
return sum;
}
int inside(vii &v,int X){
int half=getSize(v)/2;
for(int i=0;i<sz(v);++i){
if(half==0)break;
auto[l,r]=v[i];
if(r-l+1<=half){
if(l<=X&&X<=r)return 1;
half-=r-l+1;
}
else{
if(l<=X&&X<=l+half-1)return 1;
half=0;
}
}
return 0;
}
void split(vii &v,vii &vi,vii &ve){
int half=getSize(v)/2;
for(int i=0;i<sz(v);++i){
auto[l,r]=v[i];
if(r-l+1<=half)vi.pb({l,r}),half-=r-l+1;
else if(half==0)ve.pb({l,r});
else vi.pb({l,l+half-1}),ve.pb({l+half,r}),half=0;
}
}
void encode(int N,int X){
vii corr,wrong;
corr.pb({1,N});
while(getSize(corr)+getSize(wrong)>3){
//pf("corr: ");for(auto[l,r]:corr)pf("(%d %d) ",l,r);pf("\n");
//pf("wrong: ");for(auto[l,r]:wrong)pf("(%d %d) ",l,r);pf("\n");
int res=send(inside(corr,X)||inside(wrong,X));
vector<ii> corrI,corrE,wrongI,wrongE;
split(corr,corrI,corrE);
split(wrong,wrongI,wrongE);
corr.clear();
wrong.clear();
if(res==1){
for(ii x:corrI)corr.pb(x);
for(ii x:wrongI)corr.pb(x);
for(ii x:corrE)wrong.pb(x);
}
else{
for(ii x:corrE)corr.pb(x);
for(ii x:wrongE)corr.pb(x);
for(ii x:corrI)wrong.pb(x);
}
}
vector<int> rem;
for(auto [l,r]:corr){
for(int i=l;i<=r;++i)rem.pb(i);
}
for(auto [l,r]:wrong){
for(int i=l;i<=r;++i)rem.pb(i);
}
//pf("rem: ");for(int i:rem)pf("%d ",i);pf("\n");
if(X==rem[0])send(0),send(0),send(0),send(0);
if(X==rem[1])send(1),send(1),send(0),send(0);
if(X==rem[2])send(1),send(1),send(1),send(1);
}
ii decode(int N){
vii corr,wrong;
corr.pb({1,N});
while(getSize(corr)+getSize(wrong)>3){
//pf("corr: ");for(auto[l,r]:corr)pf("(%d %d) ",l,r);pf("\n");
//pf("wrong: ");for(auto[l,r]:wrong)pf("(%d %d) ",l,r);pf("\n");
int res=receive();
vector<ii> corrI,corrE,wrongI,wrongE;
split(corr,corrI,corrE);
split(wrong,wrongI,wrongE);
corr.clear();
wrong.clear();
if(res==1){
for(ii x:corrI)corr.pb(x);
for(ii x:wrongI)corr.pb(x);
for(ii x:corrE)wrong.pb(x);
}
else{
for(ii x:corrE)corr.pb(x);
for(ii x:wrongE)corr.pb(x);
for(ii x:corrI)wrong.pb(x);
}
}
vector<int> rem;
for(auto [l,r]:corr){
for(int i=l;i<=r;++i)rem.pb(i);
}
for(auto [l,r]:wrong){
for(int i=l;i<=r;++i)rem.pb(i);
}
//pf("rem: ");for(int i:rem)pf("%d ",i);pf("\n");
int tot=0,tot2=0;
for(int i=0;i<4;++i){
int x=receive();
tot+=x;
if(i<2)tot2+=1-x;
else tot2+=x;
}
vector<int> pos;
if(tot<=2)pos.pb(rem[0]);
if(tot>=2)pos.pb(rem[2]);
if(tot2<=2)pos.pb(rem[1]);
pos.pb(1);
return {pos[0],pos[1]};
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
200 KB |
Not correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
404 ms |
200 KB |
Not correct |
2 |
Halted |
0 ms |
0 KB |
- |