hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)
2014-06-01 01:48
260 查看
Mines
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1110 Accepted Submission(s): 280
[align=left]Problem Description[/align]
Terrorists put some mines in a crowded square recently. The police evacuate all people in time before any mine explodes. Now the police want all the mines be ignited. The police will take many operations to do the job. In each operation, the police will ignite one mine. Every mine has its "power distance". When a mine explodes, any other mine within the power distance of the exploding mine will also explode. Please NOTE that the distance is Manhattan distance here.
More specifically, we put the mines in the Cartesian coordinate system. Each mine has position (x,y) and power distance d.
The police want you to write a program and calculate the result of each operation.
[align=left]Input[/align]
There are several test cases.
In each test case:
Line 1: an integer N, indicating that there are N mines. All mines are numbered from 1 to N.
Line 2…N+1: There are 3 integers in Line i+1 (i starts from 1). They are the i-th mine’s position (xi,yi) and its power distance di. There can be more than one mine in the same point.
Line N+2: an integer M, representing the number of operations.
Line N+3...N+M+2 : Each line represents an operation by an integer k meaning that in this operation, the k-th mine will be ignited. It is possible to ignite a mine which has already exploded, but it will have no effect.
1<=M<=N<=100000,0<=xi,yi<=10^9,0<=di<=10^9
Input ends with N=0.
[align=left]Output[/align]
For each test case, you should print ‘Case #X:’ at first, which X is the case number starting from 1. Then you print M lines, each line has an integer representing the number of mines explode in the correspondent operation.
[align=left]Sample Input[/align]
3
0 0 0
1 1 2
2 2 2
3
1
2
3
0
[align=left]Sample Output[/align]
Case #1:
1
2
0
/* 曼哈顿距离为两坐标的轴绝对值和即:abs(a.x-b.x)+abs(a.y-b.y) 直接暴搜时间复杂度O(N*M)会超时 对它进行优化剪枝,先对x值离散化,再通过二分查找范围, */ #include <iostream> #include <cstdio> #include <cstring> #include <set> #include <queue> #include <cmath> #include <algorithm> using namespace std; const int maxn=100010; int N,M,X,f[maxn]; bool vis[maxn];//标记数组 struct Point { int x,y,d;//x坐标,y坐标,曼哈顿距离 }p[maxn]; Point read_point() { Point t; scanf("%d %d %d",&t.x,&t.y,&t.d); return t; } struct Mine { int y,n;//y坐标,编号 Mine(){} Mine(int y=0,int n=0):y(y),n(n){} bool operator<(const Mine &A) const//对y重载小于号 { return y<A.y; } }; multiset<Mine>s[maxn]; void fun() { int k,ans=0,l,r,yl,yr,i; scanf("%d",&k);k--; if(vis[k]) { printf("0\n"); return ; } queue<int> Q; Q.push(k);vis[k]=true; while(!Q.empty()) { ans++; k=Q.front();Q.pop(); l=lower_bound(f,f+X,p[k].x-p[k].d)-f;//二分查找大于等于val的下标 r=upper_bound(f,f+X,p[k].x+p[k].d)-f;//二分查找“元素值>查找值”的第一个元素的位置 for(i=l;i<r;i++) { int dy=p[k].d-abs(p[k].x-f[i]); multiset<Mine>::iterator it,yl,yr; yl=s[i].lower_bound(Mine(p[k].y-dy,0));//返回的是指针 yr=s[i].upper_bound(Mine(p[k].y+dy,0)); for(it=yl;it!=yr;it++) { if(!vis[it->n]) { vis[it->n]=true; Q.push(it->n); } } s[i].erase(yl,yr); } } printf("%d\n",ans); } void solve() { int i,j; for(i=0;i<N;i++) { p[i]=read_point(); f[i]=p[i].x; } sort(f,f+N); X=unique(f,f+N)-f; for(i=0;i<X;i++) s[i].clear(); for(i=0;i<N;i++) { j=lower_bound(f,f+X,p[i].x)-f; s[j].insert(Mine(p[i].y,i)); } memset(vis,false,sizeof(vis)); scanf("%d",&M); while(M--) fun(); } int main() { int icase=0; while(scanf("%d",&N),N) { printf("Case #%d:\n",++icase); solve(); } return 0; }
相关文章推荐
- 非C#的文章,不过大家可以借鉴一下其中思想--用Java实现断点续传(HTTP)
- 本机用PL/SQL可以连上, 在其他机器上连本机的时候总提示TNS连接超时.
- HDU 3681 Prison Break (二分答案+状压DP+bfs预处理)
- hdu 2717 Catch That Cow(BFS,剪枝)
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
- 06年的CS Sub,挺像考研考纲的。。平时学习的时候,可以参考一下~
- sql语句语法介绍全记录,好东西,备用(用到的时候不会可以来查一下!)
- Asp.Net各种超时问题总结 (感觉挺有用的 就借鉴了一下)
- poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下
- 已超过了锁请求超时时段” 直接把下在面的存储过程 放到你的Master数据库执行一下就可以了
- 给学生写了个Socket小程序,大家可以借鉴一下
- HDU_1598 find the most comfortable road(二分+bfs)
- 有惊无险——学习一下oracle误删除表数据后的恢复,适当时候可以躲过主管一劫
- hdu 4090 GemAnd Prince dfs+bfs+最优性剪枝
- 【总结】搜索的剪枝二分预处理和离散化等优化
- 一些可以借鉴一下的实现方式
- 关于本科学习GIS的一点感悟,不知道自己该干些什么的学弟学妹们可以借鉴一下……
- HDU 4282 A very hard mathematic problem [剪枝/二分]
- hdu 2337 Escape from Enemy Territory (presolve bfs +bfs+二分)
- 终于可以用Java写A+B了,发博客纪念一下(Hdu 1002)