Codeforces Round #352 (Div. 2) C. Recycling Bottles 贪心
2016-05-12 14:32
579 查看
[b]C. Recycling Bottles[/b]
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.
We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each.
For both Adil and Bera the process looks as follows:
Choose to stop or to continue to collect bottles.
If the choice was to continue then choose some bottle and walk towards it.
Pick this bottle and walk to the recycling bin.
Go to step 1.
Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.
They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.
[b]Input[/b]
First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively.
The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground.
Then follow n lines, each of them contains two integers xi and yi (0 ≤ xi, yi ≤ 109) — position of the i-th bottle.
It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.
[b]Output[/b]
Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if
.
Examples
input
[b]output[/b]
[b]Note[/b]
Consider the first sample.
Adil will use the following path:
.
Bera will use the following path:
.
Adil's path will be
units long, while Bera's path will be
units long.
[b] 题意:[/b]
给你A,B,C三个点的坐标,C表示垃圾桶,A,B表示人
在给你n个垃圾的坐标,问你两人每次最多可以携带一个垃圾到达垃圾桶
将这n个垃圾全部扔进垃圾桶最少走多少路
这里n个垃圾可以完全由其中一个或两个人捡起
[b]题解:[/b]
在人到达C后,我们知道之后这个人捡垃圾都是从C到垃圾距离*2
我们假设所有垃圾都是从C出发捡的
那么最优解就是从A,B出发所节省的,前提是至少有一个人出发了,按照节省多少进行排序
找到节省最多的至多的两个点,更新答案
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.
We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each.
For both Adil and Bera the process looks as follows:
Choose to stop or to continue to collect bottles.
If the choice was to continue then choose some bottle and walk towards it.
Pick this bottle and walk to the recycling bin.
Go to step 1.
Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.
They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.
[b]Input[/b]
First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively.
The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground.
Then follow n lines, each of them contains two integers xi and yi (0 ≤ xi, yi ≤ 109) — position of the i-th bottle.
It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.
[b]Output[/b]
Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if
.
Examples
input
3 1 1 2 0 0 3 1 1 2 1 2 3
[b]output[/b]
11.084259940083
[b]Note[/b]
Consider the first sample.
Adil will use the following path:
.
Bera will use the following path:
.
Adil's path will be
units long, while Bera's path will be
units long.
[b] 题意:[/b]
给你A,B,C三个点的坐标,C表示垃圾桶,A,B表示人
在给你n个垃圾的坐标,问你两人每次最多可以携带一个垃圾到达垃圾桶
将这n个垃圾全部扔进垃圾桶最少走多少路
这里n个垃圾可以完全由其中一个或两个人捡起
[b]题解:[/b]
在人到达C后,我们知道之后这个人捡垃圾都是从C到垃圾距离*2
我们假设所有垃圾都是从C出发捡的
那么最优解就是从A,B出发所节省的,前提是至少有一个人出发了,按照节省多少进行排序
找到节省最多的至多的两个点,更新答案
#include<bits/stdc++.h> using namespace std; const int N = 1e6+20, M = 1e7, mod = 1e9+7; typedef long long ll; double dis( double a, double b, double x, double y) { return (a-x)*(a-x)+(b-y)*(b-y); } double X,Y,xx,yy,xh,yh,xxx,yyy; int n; double x ,y ; struct ss{ double x,y,c,d;int ho,id;}p ,P ; int cmp1(ss s1,ss s2) { return s1.c<s2.c; } int main() { cin>>X>>Y>>xx>>yy>>xh>>yh; // scanf("%lf%lf%lf%lf%lf%lf",&X,&Y,&xx,&yy,&xh,&yh); scanf("%d",&n); double ans = 0.0; int cnt = 0; for(int i=1;i<=n;i++) { cin>>xxx>>yyy; if(xxx==xh&&yyy==yh) continue; p[++cnt].x = xxx; p[cnt].y = yyy; // scanf("%lf%lf",&p[i].x,&p[i].y); } n = cnt; for(int i=1;i<=n;i++) { ans = ans + sqrt(dis(xh,yh,p[i].x,p[i].y))*2.0; } double ans1 = 1e18; for(int i=1;i<=n;i++) { p[i].c = sqrt(dis(X,Y,p[i].x,p[i].y)) - sqrt(dis(p[i].x,p[i].y,xh,yh)); p[i].id = i; } for(int i=1;i<=n;i++) { P[i].id = p[i].id; P[i].c = sqrt(dis(xx,yy,p[i].x,p[i].y)) - sqrt(dis(p[i].x,p[i].y,xh,yh)) ; } double t; for(int i=1;i<=n;i++) { t = sqrt(dis(xx,yy,p[i].x,p[i].y)); ans1 = min(ans1,ans+t - sqrt(dis(p[i].x,p[i].y,xh,yh))); t = sqrt(dis(X,Y,p[i].x,p[i].y)); ans1 = min(ans1,ans+t - sqrt(dis(p[i].x,p[i].y,xh,yh))); } sort(p+1,p+n+1,cmp1); sort(P+1,P+n+1,cmp1); for(int i=1;i<=min(100,n);i++) { for(int j=1;j<=min(100,n);j++) { if(p[i].id==P[j].id) continue; double tmp1 = p[i].c; double tmp2 = P[j].c; ans1 = min(ans1,ans + tmp1 + tmp2); } } printf("%.10f\n",ans1); //cout << setprecision(10) << setiosflags(ios::scientific) << ans1 << endl; return 0; }
相关文章推荐
- 【算法题】Leedcode 335. Self Crossing 自交问题
- 趣学 C 语言(七)—— 文件读写操作
- JS中数组去重及求量数组中的相同字符个数
- mac 系统不区分大小写,linux 服务器区分大小写
- MAKEWORD
- 另类新浪微博基本数据采集方法(值得深思)
- Systemd 入门教程:实战篇
- hibernate id生成策略
- 查看数据库表的数据量和SIZE大小的脚本修正
- 类型构造器
- CentOS7中安装subversion
- iOS 关于IMP指针的作用
- PowerDesigner连接MySQL,建立逆向工程图解
- MAC下Eclipse的常用快捷键
- Android Fragment 生命周期onCreatView、onViewCreated
- freemarker自定义指令及方法
- Canvas与Image互相转换
- Mysql跨平台(Windows,Linux,Mac)使用与安装
- 程序员装逼指南
- 图表控件,官方AchartEngine控件学习