HDU 4145: Cover The Enemy
来源:互联网 发布:彩虹源码安装 编辑:程序博客网 时间:2024/05/19 03:23
题目链接 : HDU 4145 BUCT Coer 2082
Cover The Enemy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1176 Accepted Submission(s): 295
Problem Description
Now, the war is coming, no one want to fight with others, but you have to when you are facing.
As the king of a country, you should defend your country. The condition is that “There are N enemy armies around you, you have two towers and you want to build cannon on each tower to cover all the enemy armies”. The cannon has an “attack distance” R with a cost of R^2.
You are facing a problem, to cover all the armies and make the cost R1^2 + R2^2 minimal, where R1 and R2 are the attack distance of the two towers.
As the king of a country, you should defend your country. The condition is that “There are N enemy armies around you, you have two towers and you want to build cannon on each tower to cover all the enemy armies”. The cannon has an “attack distance” R with a cost of R^2.
You are facing a problem, to cover all the armies and make the cost R1^2 + R2^2 minimal, where R1 and R2 are the attack distance of the two towers.
Input
The first line contains an integer t, number of test cases, following with t groups of test cases described as following.
For each case, the first line contains four integers: x1, y1, x2, y2, the Coordinates of the two towers.
The next line is an integer n, the number of enemy armies, following with n lines of coordinates: x, y, which is the coordinate of a group of enemy army, where:
t <= 100
1 <= n <= 100000,
-1000 <= x, y <= 1000
-1000 <= x1, y1, x2, y2 <= 1000
For each case, the first line contains four integers: x1, y1, x2, y2, the Coordinates of the two towers.
The next line is an integer n, the number of enemy armies, following with n lines of coordinates: x, y, which is the coordinate of a group of enemy army, where:
t <= 100
1 <= n <= 100000,
-1000 <= x, y <= 1000
-1000 <= x1, y1, x2, y2 <= 1000
Output
The minimal cost of the two tower to cover all the enemy armies.
Sample Input
20 0 10 02-3 310 00 0 6 05-4 -2-2 34 06 -29 1
Sample Output
1830
Author
mark1989@BJTU
Source
2011百校联动“菜鸟杯”程序设计公开赛
本题应用穷举的方法计算出每种情况,选择最优解。
将所有敌人距离A的距离从大到小排序,然后枚举出A的半径,则B的半径就是A无法覆盖的点中距离B最远的距离。然后用一个变量维护A和B距离的最小值即可。
#define LOCAL#include<iostream>#include<algorithm>using namespace std;#define MAX 1000000int t,x[2],y[2];int n;struct Enemy{int x,y,da,db;};Enemy En[MAX];int Dist(int m,int n,int Flag)//点(m,n)距离A(Flag=0)或B(Flag=1)的距离的平方,即cost值{return ((m-x[Flag])*(m-x[Flag]) + (n-y[Flag])*(n-y[Flag]));}bool comp(Enemy a,Enemy b){return a.da<b.da;}int main(){#ifdef LOCALfreopen("test.txt","r",stdin);freopen("tested.txt","w",stdout); #endifcin>>t;while(t--){cin>>x[0]>>y[0]>>x[1]>>y[1];cin>>n;for(int i=0;i<n;i++){cin>>En[i].x>>En[i].y;En[i].da=Dist(En[i].x,En[i].y,0);//点到A的costEn[i].db=Dist(En[i].x,En[i].y,1);//点到B的cost}sort(En,En+n,comp);int ans=En[n-1].da;//只用Aint db_MAX=0;//db_MAX表示B的半径,A不覆盖的点中距离B最远的点的cost值for(int i=n-2;i>=0;i--){if(En[i+1].db>db_MAX) db_MAX=En[i+1].db;//维护db_MAXans=ans > En[i].da+db_MAX ? En[i].da+db_MAX : ans;//维护ans值}if(En[0].db > db_MAX) db_MAX=En[0].db;//不用Aif(db_MAX < ans) ans=db_MAX;cout<<ans<<endl;}return 0;}
0 0
- HDU-4145 Cover The Enemy
- hdu 4145 Cover The Enemy
- HDU 4145: Cover The Enemy
- Hdu 4145 Cover The Enemy(大开脑洞)
- hdu4145 Cover The Enemy(贪心+枚举)
- 2011百校联动“菜鸟杯”程序设计公开赛&&Cover The Enemy
- Sleeping with the Enemy
- Enemy at the Gateway
- Complexity is The Enemy
- Learning from the enemy
- poj - 3855 - Blast the Enemy!
- LA 4426 Blast the Enemy!
- UVALive 7146 Defeat the Enemy
- Cover the inline style
- ACdream1034:the cover circle
- XDOJ1204 - the cover circle
- HDU-4536-XCOM Enemy Unknown
- HDU 4536 XCOM Enemy Unknown
- 【Web优化】Yslow优化法则(四)启用Gzip压缩
- 腾讯SDK帮助文档
- MFC 字符串解析,分割字符串
- 如果预期的时间快到了还差很多的话就考虑跟上级反应
- CocoStudio sample讲解 SampleCollision骨骼动画与简单碰撞
- HDU 4145: Cover The Enemy
- 【算法编程】过河问题
- QT编译项目错误:lnk1123转换到coff期间失败文件无效或损坏
- 面试集锦
- Python源码 -- C语言实现面向对象编程(基类&派生类&多态)
- 2014年蓝桥杯程序设计大赛山东省赛区成绩公布
- OCP-1Z0-051 第100题 DECODE,NULLIF函数用法
- zeromq的几种模式
- 理解补码