UESTC 1170 红与蓝 计算几何、贪心、红蓝点对
来源:互联网 发布:沈阳优化 编辑:程序博客网 时间:2024/04/28 09:00
D - EN TARO Artanis
Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %lluDescription
平面上有N个红点和N个蓝点,求红点到蓝点的最近距离
Input
第一行为一个整数N
接下来第N行每行两个整数xi,yi,表示第i个红点的坐标
接下来第N行每行两个整数xi,yi,表示第i个蓝点的坐标(
Output
红点到蓝点的最短距离
Sample Input
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
Sample Output
1.414
Hint
Source
UESTC 1170
Source
贪心的做法
预处理所有Ai到O的距离, 然后根据距离排序,
之后依次对每个Bi也求出BiO, 然后二分查找, 然后处理最近的+-100个点, 答案必定在这100或者1000个点之内
比赛的时候是+-100 共200个点Accepted的
然后比赛结束后试了一下, 我这样的方法至少要+-40 共80个点才能通过那个题目的数据测试
好像还有专业计算几何的算法 红蓝点对 ☺☺
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 1e5 + 8;struct p{ int x, y; double dist;} val[maxn];inline bool cmp(const p& a, const p& b){ return a.dist < b.dist;}int main(){ #ifdef LOCAL freopen("a.txt", "r", stdin); //freopen("b.txt", "w", stdout); int T = 2; while(T--){ #endif // LOCAL int n, x, y, a, b; double dist, ans = maxn; scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%d%d", &val[i].x, &val[i].y); val[i].dist = sqrt(val[i].x*val[i].x + val[i].y*val[i].y); } sort(val, val + n, cmp); for(int i = 0; i < n; i++){ scanf("%d%d", &x, &y); dist = sqrt(x*x + y*y); int m; a = 0, b = n - 1; while(a < b){ m = a + (b - a)/2; if(val[m].dist >= dist) b = m; else a = m + 1; } for(int j = max(0, a - 100); j < min(n, a + 100); j++){ ans = min(ans, sqrt((x - val[j].x)*(x - val[j].x) + (y - val[j].y)*(y - val[j].y))); } } printf("%.3f", ans); #ifdef LOCAL printf("\n"); } #endif // LOCAL return 0;}
Thank you!
------from ProLights
1 0
- UESTC 1170 红与蓝 计算几何、贪心、红蓝点对
- CDOJ 1170 红与蓝 (几何+二分+枚举)
- UESTC 1170 红蓝点对
- 计算几何-UESTC 484 Division
- 计算几何(point&line)红书模板
- UESTC oj 797 Eh? Beautiful Stone!(计算几何)
- uestc oj Justice is Given by Light(计算几何)
- 计算几何-最近点对
- 3716: [PA2014]Muzeum 计算几何+贪心+set
- poj1328 Radar Installation 贪心 几何计算
- 【UESTC 1334 郭大侠与Rabi-Ribi】+ 贪心 + 优先队列
- CQOI2016 K远点对 计算几何
- The 14th UESTC Programming Contest Final I - Intersection 计算几何、积分、精度
- Poj1696计算几何的叉积+极角排序+贪心
- uva 1531 & poj 1518 Problem Bee(几何计算+贪心)
- Little Zu Chongzhi's Triangles(计算几何加贪心)
- Network(计算几何:推规律+暴力+贪心)
- Wunder Fund Round 2016 C. Constellation(贪心+计算几何)
- Handler消息传递机制
- java中的static用法
- Sonar初识
- Java 过滤器读取配置文件实现过滤编码
- 单生产者 单消费者
- UESTC 1170 红与蓝 计算几何、贪心、红蓝点对
- maven基础
- 十五、解释器模式Interpreter(行为型)
- 2440: [中山市选2011]完全平方数
- Thinkphp3.2+PHPQRCode 二维码生成示例
- 原型继承例子,看各自的prototype和constructor
- Docker镜像与仓库
- 简单易懂的背包问题
- Android中Handler造成内存泄露解决方法