Mines

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1110    Accepted Submission(s): 280

Problem Description
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.

 
Input
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.

 
Output
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.
 
Sample Input
3
0 0 0
1 1 2
2 2 2
3
1
2
3
0
 
Sample Output
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=;
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=,int n=):y(y),n(n){}
bool operator<(const Mine &A) const//对y重载小于号
{
return y<A.y;
}
};
multiset<Mine>s[maxn]; void fun()
{
int k,ans=,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,));//返回的是指针
yr=s[i].upper_bound(Mine(p[k].y+dy,));
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=;i<N;i++)
{
p[i]=read_point();
f[i]=p[i].x;
}
sort(f,f+N);
X=unique(f,f+N)-f;
for(i=;i<X;i++) s[i].clear();
for(i=;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=;
while(scanf("%d",&N),N)
{
printf("Case #%d:\n",++icase);
solve();
}
return ;
}
 
 

hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)的更多相关文章

  1. HDU 4620 Fruit Ninja Extreme 暴搜

    题目大意:题目就是描述的水果忍者. N表示以下共有 N种切水果的方式. M表示有M个水果需要你切. W表示两次连续连击之间最大的间隔时间. 然后下N行描述的是 N种切发 第一个数字C表示这种切法可以切 ...

  2. 紫书 习题7-14 UVa 307(暴搜+剪枝)

    这道题一开始我想的是在排序之后只在头和尾往中间靠近来找木块, 然后就WA, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...

  3. hdu4982 暴搜+剪枝(k个数和是n,k-1个数的和是平方数)

    题意:       给你两个数n,k问你是否怎在这样一个序列:      (1)这个序列有k个正整数,且不重复.      (2)这k个数的和是n.      (3)其中有k-1个数的和是一个平方数. ...

  4. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  5. HDU 5961:传递(暴搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=5961 题意:中文题意.给出两个图,判断这个两个图是否都是传递的.注意一下传递的定义要看清,一开始没看清连样例都看 ...

  6. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  7. POJ 1167 The Buses 暴搜+剪枝

    思路: 先把能选的路线都预处理出来 按照能停的车的多少排个序 (剪枝1) 搜搜搜 如果当前剩的车÷当前能停车的多少+deep>=ans剪掉 (剪枝2) //By SiriusRen #inclu ...

  8. 洛谷 1312 Mayan游戏——暴搜+剪枝

    题目:https://www.luogu.org/problemnew/show/P1312 自己写了很久.又T又WA的. 发现对题理解有误.改完后应该只有T了,但还是T的. 自己写了许多剪枝,很鸡肋 ...

  9. CF1340B Nastya and Scoreboard(暴搜剪枝/dp)

    Question 一个n个数码位的分数板,每一个数码位都是一个七段数码管,现在给出每个数码位的显示情况,问再点亮k段数码管的话能显示的最大的数是多少,如果不能构成一串数字,就输出-1 Solution ...

随机推荐

  1. Rop实战之利用VirtualProtect绕过DEP

    CVE-2011-0065 Firefox mChannel UAF漏洞 为了实现任意代码执行,需要在mChannel对象释放后,用可控数据“占坑”填充它,因此,可在onChannelRedirect ...

  2. 抓取oracle建表语句及获取建表ddl语句

    抓取oracle建表语句及获取建表ddl语句 1.抓取代码如下: 1.1.产生表的语法资料 DECLARE-- v_notPartTable VARCHAR2(1000):= '&2'; -- ...

  3. iOS动画之iOS UIBezierPath类 介绍

    感谢:http://blog.csdn.net/crayondeng/article/details/11093689 使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类 ...

  4. 【STL学习笔记】一、STL体系

    目录 1.标准库以header files形式呈现 2.namespce命名空间 3.STL与OO 4.STL六组件及其关系 5.STL组件例子 6.range-based for statement ...

  5. C++内存管理(effective c++ 04)

    阅读effective c++ 04 (30页) 提到的static对象和堆与栈对象.看了看侯老师的内存管理视频1~3.有点深. 了解一下. 目录 1 内存管理 1.1 C++内存管理详解 1.1.1 ...

  6. 二叉搜索树详解(Java实现)

    1.二叉搜索树定义 二叉搜索树,是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值: 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根 ...

  7. 【贪心】10.24assassin

    题目分析 没有题目分析…… 寄存一下神奇反悔贪心 #include<bits/stdc++.h> ; struct node { int a,b; node(, ):a(x),b(y) { ...

  8. k8s搭建WebUI--Dashborad管理界面

    k8s的webUI管理界面可以更好更直观更便捷的让我们去管理我们的k8s集群. 我们知道,由于某些原因我们无法直接拉取dashboard的镜像,但是国内有些人已经将镜像下载到dockerhub中可以给 ...

  9. charles 模拟手机弱网、修改请求参数、修改返回值

    1.charles模拟弱网(断网) 2.charles修改请求参数 (1)先访问一次需要改的请求,在charles上找到相应的请求地址 (2)然后在需要打断点的请求上右键,勾选[Breakpoints ...

  10. destoon修改手机端分页

    1. global.func.php pages函数和listpages函数 函数开头增加 $DT_TOUCH,$newsamplepages变量 global $DT_URL, $DT, $L,$D ...