hdu stl的运用。

题意: 给出若干个炸弹的位置, 位置的坐标都是大于等于0的整数,并且给出了每个炸弹可以轰炸的距离范围(曼哈顿距离),并且如果一个炸弹爆炸后波及到另外一个炸弹的话这个炸弹也会爆炸。现在给出了点燃炸弹的次序方案,问点燃当前炸弹的时候爆炸的炸弹数目是多少。 如果该炸弹已经爆炸了, 输出0. n=100000. 

时间限制是5s。

 

思路:开始看到这道题就想用暴力,但是果断不行。 

这道题是先对横坐标离散化, 然后对于每一个横坐标位置将属于这个横坐标的炸弹全放进去。

每次给出一个炸弹,先判断是否还存在,如果不存在则直接输出0,否则就进行bfs,对于每一个出来的点先确定它能炸到炸弹横坐标的左边界和右边界, 然后对于这个区间中的点, 用吗,multiset的方法直接找到满足情况的区间, 然后进行判断。

stl很强大。

AC代码:

 

View Code
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <string>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <set>
  8 using namespace std;
  9 const int N = 110000;
 10 struct node
 11  {
 12      int y, id;
 13      node (int a=0, int b=0)
 14       {
 15           y = a;
 16           id = b;
 17       }
 18      bool friend operator < (node A, node B)
 19       {
 20           return  A.y < B.y;
 21       }
 22  };
 23 typedef multiset<node>::iterator itr;
 24 
 25 struct point
 26  {
 27      int x, y, d;
 28  }p[N];
 29 
 30 int hash[N], n, m;
 31 bool used[N];
 32 multiset<node>g[N];
 33 
 34 void init()
 35  {
 36      for(int i=0; i<n; i++)
 37       {
 38           scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].d);
 39           hash[i] = p[i].x;
 40           g[i].clear();
 41       }
 42      sort(hash, hash+n);
 43      m = unique(hash, hash+n) - hash;
 44      int k;
 45      for(int i=0; i<n; i++)
 46       {
 47           k = lower_bound(hash, hash+m, p[i].x) - hash;
 48           g[k].insert(node(p[i].y, i));
 49       }
 50  }
 51 
 52 int myabs(int u)
 53  {
 54      return u>0 ? u:-u;
 55  }
 56 
 57 void solve()
 58  {
 59      int k, a, b, ans, l, r, y;
 60      itr il, ir, ii;
 61      queue<int>q;
 62      memset(used, 0, sizeof(used));
 63      scanf("%d", &k);
 64      while(k--)
 65       {
 66           scanf("%d", &a);
 67           a--;
 68           if(used[a])
 69            {
 70                puts("0");
 71                continue;
 72            }
 73           used[a] = 1;
 74           while( !q.empty() ) q.pop();
 75           q.push(a);
 76           ans = 0;
 77           while(!q.empty())
 78            {
 79                a = q.front();
 80                q.pop();
 81                ans++;
 82                l = lower_bound(hash, hash+m, p[a].x - p[a].d) - hash;
 83                r = upper_bound(hash, hash+m, p[a].x + p[a].d) - hash;
 84                for(int i=l; i<r; i++)
 85                 {
 86                     y = p[a].d - myabs(p[a].x - hash[i]);
 87                     il = g[i].lower_bound(node(p[a].y-y,0));
 88                     ir = g[i].upper_bound(node(p[a].y+y,0));
 89                     for(ii=il; ii!=ir; ii++)
 90                      {
 91                          if(!used[ii->id])
 92                           {
 93                               used[ii->id] = 1;
 94                               q.push(ii->id);
 95                           }
 96                      }
 97                     g[i].erase(il,ir);
 98                 }
 99            }
100          printf("%d\n", ans);
101       }
102  }
103 
104 int main()
105  {
106     int t = 0;
107     while(scanf("%d", &n) != EOF && n)
108      {
109          init();
110          printf("Case #%d:\n", ++t);
111          solve();
112      }
113     return 0;
114  }

 

 

 

 

 

posted @ 2012-10-02 12:07  Gu Feiyang  阅读(256)  评论(0)    收藏  举报