Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 651 Accepted Submission(s):
295
However, the device is in test phase, often in a unstable state. Mr.Smith worried that it may cause an explosion while testing it. The energy of the device is related to the maximum manhattan distance among particle.
Particles may appear and disappear any time. Mr.Smith wants to know the maxmium manhattan distance among particles when particle appears or disappears.
In each case: In the first line, there are two integer q(number of particle appear and disappear event, ≤60000) and k(dimensions of the hyperspace that the hyperspace the device generated, ≤5). Then follows q lines. In each line, the first integer ‘od’ represents the event: od = 0 means this is an appear
event. Then follows k integer(with absolute value less then 4 × 107). od = 1 means this is an disappear event. Follows a integer p represents the disappeared particle appeared in the pth event.
思路:求出所有的曼哈顿距离求最值,那就枚举一下(x-x')+(y-y')
max(x+y)-min(x'+y');也就是相同的组合,符号位置相同。
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<map> 5 #include<set> 6 #define FOR(i,a,b) for(int i=a;i<=b;++i) 7 #define ll(x) (1<<x) 8 using namespace std; 9 map<int,int>mp[17]; 10 multiset<int>has[17]; 11 multiset<int>::iterator is,it; 12 int f[6]; 13 int main() 14 { int n,k; 15 while(~scanf("%d%d",&n,&k)) 16 { 17 int op,a,b; 18 FOR(i,0,16)mp[i].clear(),has[i].clear(); 19 FOR(i,0,n-1) 20 { 21 scanf("%d",&op); 22 if(op==0) 23 { FOR(j,0,k-1) 24 scanf("%d",&f[j]); 25 FOR(j,0,ll(k-1)-1) 26 { 27 int ret=f[0]; 28 FOR(l,0,k-2) 29 if(ll(l)&j) 30 ret+=f[l+1]; 31 else ret-=f[l+1]; 32 has[j].insert(ret); 33 mp[j][i]=ret; 34 } 35 } 36 else 37 { 38 scanf("%d",&a);--a; 39 FOR(j,0,ll(k-1)-1) 40 if(mp[j].size()>0) 41 { 42 has[j].erase(has[j].find(mp[j][a])); 43 mp[j].erase(a); 44 } 45 } 46 int ans=0; 47 FOR(j,0,ll(k-1)-1) 48 if(has[j].size()>0) 49 { 50 is=has[j].begin();it=has[j].end(); 51 it--; 52 ans=max(ans,*it-*is); 53 } 54 printf("%d\n",ans); 55 } 56 } 57 return 0; 58 }