您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐