Codeforces Round #352 (Div. 2) C. Recycling Bottles __ geometry, greedy and pretreat
来源:互联网 发布:返利网淘宝购物车付款 编辑:程序博客网 时间:2024/05/23 18:34
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.
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.
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 .
3 1 1 2 0 031 12 12 3
11.084259940083
5 0 4 2 2 055 23 05 53 53 3
33.121375178000
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.
First of all, the distant will be sigma{ 2*distant of Bottle to the recycling bin}, then the first choices of the bottle make it different.
As we can know, there at least one person should walk to a Bottle and put it to the bin, the dist is d1 + d2,
At the meantime we define the dist from bin to Bottle and go back to the bin as d2+d2;
Next if the first choice is this Bottle, a[ i ] = d1 + d2 - (d2 + d2) = d1 - d2;
b[] is the same as a[ i ];
so we can sort the array a[], then if the first is a negative value, we add it to the sigma{d2 + d2 }, then this point will change d2 + d2 to d2 + d2 + d1 - d2 == d1 + d2;and b[] is the same as a[ i ];then we get the answer;
What's more, what we should pay attention to is if a[0] < 0, and b[0] < 0, but they are the same bottle, we use this line to solve it
ans += min(a[0].abdis + b[1].abdis, min(a[1].abdis + b[0].abdis, min(a[0].abdis, b[0].abdis)));
else if a[0] < 0, add it to the sigma, the same as b.
else if a[0] >= 0, and b[0] >= 0, we add the mallest one to the sum, the other be still, becase at least one person should pick the Bottle
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn = 100000 + 8;double dis[maxn];struct p{ int abi; double abdis;} a[maxn], b[maxn];bool cmp(const p& x, const p& y){ return x.abdis < y.abdis;}int main(){ #ifdef LOCAL freopen("a.txt", "r", stdin); #endif // LOCAl int ax, ay, bx, by, tx , ty, n, x, y; scanf("%d%d%d%d%d%d", &ax, &ay, &bx, &by, &tx , &ty); scanf("%d", &n); int Adis, Ai, Bdis, Bi; for(int i = 0; i < n; i++){ scanf("%d%d", &x, &y); dis[i] = sqrt(pow(x-tx, 2) + pow(y-ty, 2)); a[i].abdis = sqrt(pow(x-ax, 2) + pow(y-ay, 2)) - dis[i]; b[i].abdis = sqrt(pow(x-bx, 2) + pow(y-by, 2)) - dis[i]; a[i].abi = i, b[i].abi = i; } sort(a, a + n, cmp); sort(b, b + n, cmp); double ans = 0; if(a[0].abdis < 0 && b[0].abdis >= 0) ans += a[0].abdis; if(a[0].abdis >= 0 && b[0].abdis < 0) ans += b[0].abdis; if(a[0].abdis < 0 && b[0].abdis < 0){ if(a[0].abi != b[0].abi){ans += a[0].abdis;ans += b[0].abdis;} else{ ans += min(a[0].abdis + b[1].abdis, min(a[1].abdis + b[0].abdis, min(a[0].abdis, b[0].abdis))); } } if(a[0].abdis >= 0 && b[0].abdis >= 0){ ans += min(a[0].abdis, b[0].abdis); } for(int i = 0; i < n; i++){ ans += 2*dis[i]; } printf("%.12f", ans); return 0;}
You can view this blog in my Codeforces blog, too. ☺☺
My Solution for Codeforces Round #352 (Div. 2) C. Recycling Bottles
Thank you!
- Codeforces Round #352 (Div. 2) C. Recycling Bottles __ geometry, greedy and pretreat
- Codeforces 672 C. Recycling Bottles(Codeforces Round #352 (Div. 2))
- Codeforces Round #352 (Div. 2) C. Recycling Bottles
- Codeforces Round #352 (Div. 2) C - Recycling Bottles
- Codeforces Round #352 (Div. 2)C. Recycling Bottles
- Codeforces Round #352 (Div. 2) C. Recycling Bottles
- Codeforces Round #352 (Div. 2) C. Recycling Bottles
- Codeforces Round #352 (Div. 2) C. Recycling Bottles
- Codeforces Round #352 (Div. 2) C. Recycling Bottles (几何)
- Codeforces Round #345 (Div. 2) D. Image Preview __ two pointers and pretreat
- Codeforces Round #352 (Div. 1) A. Recycling Bottles
- Codeforces Round #352 (Div. 1) A. Recycling Bottles 暴力
- Codeforces #352 Recycling Bottles
- Codeforces #352 Div2 C Recycling Bottles
- codeforces#352-C - Recycling Bottles-贪心
- Codeforces Round #345 (Div. 2) A. Joysticks __ greedy and simulation
- Codeforces Round #345 (Div. 2) B. Beautiful Paintings __ greedy and Bucket_sort
- codeforces 672C. Recycling Bottles
- debugfs dump usb device info
- 苹果新贵 Swift 之前世今生
- mysql通过拷贝数据文件的方式进行数据库迁移
- 2016-5-12-1
- 图解Session(会话)
- Codeforces Round #352 (Div. 2) C. Recycling Bottles __ geometry, greedy and pretreat
- WIndwos7-64bit注册
- new (nothrow) T() 的原理
- ElasticSearch(二)--分布式集群
- RedHat安装yum的方法总结
- SSM学习之路---工具安装
- iOS开发中常见的宏
- Python实现对linux的监控(PROC)
- ViewPager滑动事件调用机制简单概述