hdu 4666
题意:n个事件,op == 0 插入一个点,op == 1删除一个点,每个事件之后询问当前点里曼哈顿距离最大的值;
分析:曼哈顿距离等于abs(a.x - b.x) + abs(a.y - b.y);
就是c1*(a.x- b.x) + c2*(a.y - b.y),c1,c2取1或-1;显然只有当c1,c2去恰当值后值才是他们的曼哈顿距离,并且是最大的;
这样我们就有思路了,开1<<k个multiset,对于每个点都放入不同的multiset里,而在同一个muliset里,直接相减就是最大值;
其实就是高中的去绝对值的问题; 唉...
it1 = st.lower_bound(key); 注意st.erase(it1) 和st.erase(*it1)的区别;
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstring> 7 #include<cstdlib> 8 #include<cstdio> 9 #include<set> 10 #include<map> 11 using namespace std; 12 const int N = 1<<5; 13 int n,k; 14 struct node{ 15 int a[5]; 16 int s; 17 void init(){ 18 memset(a,0,sizeof(a)); 19 } 20 void sum(){ 21 s = 0; 22 for (int i = 0; i < 5; i++) s += a[i]; 23 } 24 bool operator < (const node &p) const{ 25 return s < p.s; 26 } 27 }nd[60000+10]; 28 29 multiset<node> st[N]; 30 multiset<node> :: iterator it1,it2; 31 void print(){ 32 int ans = 0; 33 for (int i = 0; i <(1<<k); i++) { 34 it1 = st[i].begin(); 35 if (it1 == st[i].end()) continue; 36 it2 = st[i].end(); 37 it2 --; 38 if (it2->s - it1->s > ans) ans = it2->s - it1->s; 39 } 40 printf("%d\n",ans); 41 42 } 43 int main(){ 44 45 while (~scanf("%d%d",&n,&k)) { 46 for (int i = 0; i < (1<<k) ; i++) st[i].clear(); 47 node t1,t2; 48 for (int w = 0; w < n; w++) { 49 int op; scanf("%d",&op); 50 if (op == 0) { 51 t1.init(); 52 for (int i = 0; i < k; i++) { 53 scanf("%d",&t1.a[i]); 54 } 55 nd[w+1] = t1; 56 t2.init(); 57 for (int i = 0; i < (1<<k); i++) { 58 for (int j = 0; j < k; j++) { 59 if (i & (1<<j)) { 60 t2.a[j] = -t1.a[j]; 61 }else t2.a[j] = t1.a[j]; 62 } 63 t2.sum(); 64 st[i].insert(t2); 65 } 66 print(); 67 }else { 68 int d; scanf("%d",&d); 69 t2.init(); 70 for (int i = 0; i < (1<<k); i++) { 71 72 for (int j = 0; j < k; j++) { 73 if (i & (1<<j)) { 74 t2.a[j] = -nd[d].a[j]; 75 }else t2.a[j] = nd[d].a[j]; 76 } 77 t2.sum(); 78 it1 = st[i].lower_bound(t2); 79 st[i].erase(it1); 80 } 81 print(); 82 } 83 } 84 } 85 86 return 0; 87 }