Codeforces Round #352 (Div. 2) C. Recycling Bottles __ geometry, greedy and pretreat

来源:互联网 发布:返利网淘宝购物车付款 编辑:程序博客网 时间:2024/05/23 18:34

C. Recycling Bottles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Choose to stop or to continue to collect bottles.
  2. If the choice was to continue then choose some bottle and walk towards it.
  3. Pick this bottle and walk to the recycling bin.
  4. 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.

Input

First line of the input contains six integers axaybxbytx 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.

Output

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 031 12 12 3
output
11.084259940083
input
5 0 4 2 2 055 23 05 53 53 3
output
33.121375178000
Note

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.


Source

Codeforces Round #352 (Div. 2) C. Recycling Bottles


My Solution

geometry, greedy and pretreat.


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!

                                                                                                                                               ------from ProLights
0 0
原创粉丝点击