#include <bits/stdc++.h> #include "cielib.h" using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) //#define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define ALL(G) (G).begin(),(G).end() #if 0 #define DEB printf #else #define DEB(...) #endif typedef long long ll; typedef long long LL; typedef pair<int,int> pii; typedef vector<int> vi; const int inft = 1000000009; const int mod = 1000000007; const int MAXD = 506; int query[MAXD],a[MAXD],b[MAXD],ans[MAXD], diff[MAXD]; int QUERY,d,R; bool ask(int *A){ QUERY++; fru(i,d)query[i]=A[i]; DEB("query: "); fru(i,d)DEB("%d ",query[i]);DEB("\n"); return czyCieplo(query); } bool les(int *A,int *B){ //is a<b bool same=1; fru(i,d)if(B[i]!=query[i]){same=0;break;} ask(B); return ask(A); } void answer(int *A){ fru(i,d)query[i]=A[i]; znalazlem(query); exit(0); } void setone(int *A,int pos,int value,int rest){ fru(i,d)A[i]=rest; A[pos]=value; } vi W,M,SR; void setapart(int *A,int pos,int value,int rest){//set M to 0, W to R rest fru(i,d)A[i]=rest; tr(it,M)A[*it]=0; tr(it,W)A[*it]=R; A[pos]=value; } void setshift(int *A,int x){ tr(it,W)A[*it]=R-x-diff[*it]; tr(it,M)A[*it]=x+diff[*it]; tr(it,SR)A[*it]=ans[*it]; } void find(int p1,bool m1,int p2,int m2){ //find diff p2 wrt p1 setapart(a,p1,m1?R:0,R/2); setapart(b,p2,m2?R:0,R/2); int p=0,k=R/2+1; bool amnb=les(a,b); DEB("find %d %d $ %d %d--- %d k=%d \n",p1,m1,p2,m2,amnb,k); while(k-p>1){ int m=(p+k)/2; if(amnb){ setapart(a,p1,m1?R:0,R/2); setapart(b,p2,m2?R-m:m,R/2); }else{ setapart(a,p1,m1?R-m:m,R/2); setapart(b,p2,m2?R:0,R/2); } if(les(a,b)){ if(amnb)p=m;else k=m; } else { if(amnb)k=m;else p=m; } } DEB("---> %d\n",p); if(amnb)diff[p2]=-k; else diff[p2]=p; } int main() { QUERY=0; d = podajD(); R=podajR(); fru(i,d)ans[i]=-1; fru(i,d){ setone(a,i,0,R/2); setone(b,i,R,R/2); if(les(a,b))M.pb(i); else if(les(b,a))W.pb(i); else SR.pb(i); } DEB("-----------\n"); //check sr tr(it,SR){ if(R%2==0)ans[*it]=R/2; else{ setapart(a,*it,0,R/2); setapart(b,*it,R,R/2); if(les(a,b))ans[*it]=R/2; else ans[*it]=R/2+1; } } // tr(it,W)DEB("%d ",*it);DEB("\n"); // tr(it,M)DEB("%d ",*it);DEB("\n"); DEB("-----------\n"); if(SR.size()==d)answer(ans); int ind=-1,mn=1; if(!W.empty()){ind=W[0];mn=0;} else ind=M[0]; //find differences wrt. ind tr(it,W)find(ind,mn,*it,0); tr(it,M)find(ind,mn,*it,1); fru(i,d)DEB("%d ",ans[i]);DEB("\n"); DEB("ind=%d \n",ind); fru(i,d)DEB("%d ",diff[i]);DEB("\n"); //ternary search int p=0,k=R; fru(i,d)if(ans[i]==-1){ p=max(p,-diff[i]); k=min(k,R-diff[i]); } DEB("interval: %d %d \n",p,k); while(k-p>4){ int m1=p+(k-p)/3; int m2=p+2*(k-p)/3; setshift(a,m1); setshift(b,m2); if(les(a,b))k=m2; else p=m1; } DEB("ending : %d %d \n",p,k); //brute force int best=p; for(int i=p+1;i<=k;i++){ setshift(a,i); setshift(b,best); if(les(a,b))best=i; } DEB("best %d\n",best); setshift(ans,best); answer(ans); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #include <bits/stdc++.h> #include "cielib.h" using namespace std; #define fru(j,n) for(int j=0; j<(n); ++j) #define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it) //#define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it) #define x first #define y second #define pb push_back #define ALL(G) (G).begin(),(G).end() #if 0 #define DEB printf #else #define DEB(...) #endif typedef long long ll; typedef long long LL; typedef pair<int,int> pii; typedef vector<int> vi; const int inft = 1000000009; const int mod = 1000000007; const int MAXD = 506; int query[MAXD],a[MAXD],b[MAXD],ans[MAXD], diff[MAXD]; int QUERY,d,R; bool ask(int *A){ QUERY++; fru(i,d)query[i]=A[i]; DEB("query: "); fru(i,d)DEB("%d ",query[i]);DEB("\n"); return czyCieplo(query); } bool les(int *A,int *B){ //is a<b bool same=1; fru(i,d)if(B[i]!=query[i]){same=0;break;} ask(B); return ask(A); } void answer(int *A){ fru(i,d)query[i]=A[i]; znalazlem(query); exit(0); } void setone(int *A,int pos,int value,int rest){ fru(i,d)A[i]=rest; A[pos]=value; } vi W,M,SR; void setapart(int *A,int pos,int value,int rest){//set M to 0, W to R rest fru(i,d)A[i]=rest; tr(it,M)A[*it]=0; tr(it,W)A[*it]=R; A[pos]=value; } void setshift(int *A,int x){ tr(it,W)A[*it]=R-x-diff[*it]; tr(it,M)A[*it]=x+diff[*it]; tr(it,SR)A[*it]=ans[*it]; } void find(int p1,bool m1,int p2,int m2){ //find diff p2 wrt p1 setapart(a,p1,m1?R:0,R/2); setapart(b,p2,m2?R:0,R/2); int p=0,k=R/2+1; bool amnb=les(a,b); DEB("find %d %d $ %d %d--- %d k=%d \n",p1,m1,p2,m2,amnb,k); while(k-p>1){ int m=(p+k)/2; if(amnb){ setapart(a,p1,m1?R:0,R/2); setapart(b,p2,m2?R-m:m,R/2); }else{ setapart(a,p1,m1?R-m:m,R/2); setapart(b,p2,m2?R:0,R/2); } if(les(a,b)){ if(amnb)p=m;else k=m; } else { if(amnb)k=m;else p=m; } } DEB("---> %d\n",p); if(amnb)diff[p2]=-k; else diff[p2]=p; } int main() { QUERY=0; d = podajD(); R=podajR(); fru(i,d)ans[i]=-1; fru(i,d){ setone(a,i,0,R/2); setone(b,i,R,R/2); if(les(a,b))M.pb(i); else if(les(b,a))W.pb(i); else SR.pb(i); } DEB("-----------\n"); //check sr tr(it,SR){ if(R%2==0)ans[*it]=R/2; else{ setapart(a,*it,0,R/2); setapart(b,*it,R,R/2); if(les(a,b))ans[*it]=R/2; else ans[*it]=R/2+1; } } // tr(it,W)DEB("%d ",*it);DEB("\n"); // tr(it,M)DEB("%d ",*it);DEB("\n"); DEB("-----------\n"); if(SR.size()==d)answer(ans); int ind=-1,mn=1; if(!W.empty()){ind=W[0];mn=0;} else ind=M[0]; //find differences wrt. ind tr(it,W)find(ind,mn,*it,0); tr(it,M)find(ind,mn,*it,1); fru(i,d)DEB("%d ",ans[i]);DEB("\n"); DEB("ind=%d \n",ind); fru(i,d)DEB("%d ",diff[i]);DEB("\n"); //ternary search int p=0,k=R; fru(i,d)if(ans[i]==-1){ p=max(p,-diff[i]); k=min(k,R-diff[i]); } DEB("interval: %d %d \n",p,k); while(k-p>4){ int m1=p+(k-p)/3; int m2=p+2*(k-p)/3; setshift(a,m1); setshift(b,m2); if(les(a,b))k=m2; else p=m1; } DEB("ending : %d %d \n",p,k); //brute force int best=p; for(int i=p+1;i<=k;i++){ setshift(a,i); setshift(b,best); if(les(a,b))best=i; } DEB("best %d\n",best); setshift(ans,best); answer(ans); } |