HDU-4145 模拟题 数学题
2015-05-16 02:49
387 查看
部分转载:http://blog.csdn.net/niushuai666/article/details/7174884
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4145
题目大意:
给你2个大炮和一些敌人,每个大炮的攻击半径为r,A大炮和B大炮的攻击范围分别为r1和r2。问怎样才能使r1*r1 + r2*r2最小。。。
解题思路:
这道题刚开始想错了,以为枚举每个敌人,比较它到2个大炮的距离,如果距离A近,则把这个距离与上个距离A近的敌人的距离比较,取较大者。反之同理处理B。这样就得到A和B炮的半径,进而得到答案。但是这样是错的。比如AB距离为100,他们中点旁边各有1个敌人,根据这种思路得到的距离为49*49 + 49 * 49 = 4802,而如果只用A,则只需要51 * 51 = 2601.所以这种思路是错误的。
正确思路应该是:
将所有敌人距离A的距离从大到小排序,然后枚举A的半径,则B的半径就是A无法覆盖的点中距离B最远的距离。然后用一个变量维护A和B距离的最小值即可。
代码如下:
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4145
题目大意:
给你2个大炮和一些敌人,每个大炮的攻击半径为r,A大炮和B大炮的攻击范围分别为r1和r2。问怎样才能使r1*r1 + r2*r2最小。。。
解题思路:
这道题刚开始想错了,以为枚举每个敌人,比较它到2个大炮的距离,如果距离A近,则把这个距离与上个距离A近的敌人的距离比较,取较大者。反之同理处理B。这样就得到A和B炮的半径,进而得到答案。但是这样是错的。比如AB距离为100,他们中点旁边各有1个敌人,根据这种思路得到的距离为49*49 + 49 * 49 = 4802,而如果只用A,则只需要51 * 51 = 2601.所以这种思路是错误的。
正确思路应该是:
将所有敌人距离A的距离从大到小排序,然后枚举A的半径,则B的半径就是A无法覆盖的点中距离B最远的距离。然后用一个变量维护A和B距离的最小值即可。
代码如下:
/* * Author: NICK WONG * Created Time: 2015/5/16 2:06:02 * File Name: j.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; #define out(x) cout<<#x<<": "<<x<<endl const double eps(1e-8); const int maxn=101000; const long long maxint=-1u>>1; const long long maxlong=maxint*maxint; typedef long long lint; struct point { int x,y,da,db; } p[maxn],a,b; int t,cases,n,max_b; int dist(point a, point b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } void init() { cin>>a.x>>a.y>>b.x>>b.y; cin>>n; for (int i=1; i<=n; i++) { scanf("%d%d",&p[i].x,&p[i].y); p[i].da=dist(p[i],a); p[i].db=dist(p[i],b); } } bool cmp(point a, point b) { return a.da>b.da; } void work() { sort(p+1,p+1+n,cmp); max_b=0; int ans=maxint; for (int i=1; i<=n; i++) { ans=min(ans,p[i].da+max_b); max_b=max(max_b,p[i].db); } ans=min(ans,max_b); cout<<ans<<endl; } int main() { t=0; cin>>cases; while(cases--) { init(); work(); } return 0; }
相关文章推荐
- HDU 3880 Lucky Numbers 数学题
- HDU 1332(模拟题,电子数字)
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
- HDU 5879 Cure(数学)
- HDU 4452 Running Rabbits (模拟题)
- HDU 2197 本原串 (数学+容斥)
- HDU 1215 七夕节 数学题~
- HDU 4121 Xiangqi 模拟题
- hdu 2566 统计硬币 数学解法
- HDU_5810_数学,概率,方差
- HDU 1071 The area (数学水题)
- HDU 6116 路径计数(组合数学+NTT)
- HDU--IMNU集训三:夹角有多大(数学能力+数据分析能力)
- hdu-4535-吉哥系列故事——礼尚往来-组合数学-错排-java
- HDU 6033(Add More Zero) 简单数学题 Java
- hdu 5826 physics (物理数学)
- hdu 1220 Cube (数学)
- HDU 4565 So Easy!(数学+矩阵快速幂)
- hdu 1755 数学 ***
- hdu 3501 数学题