首页 > 学院 > 开发设计 > 正文

Codeforces 672C Recycling Bottles【极限思维+贪心枚举】

2019-11-14 08:45:56
字体:
来源:转载
供稿:网友

C. Recycling Bottlestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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:

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.

Input

First line of the input contains six integers ax,ay,bx,by,tx andty (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 integersxi andyi (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 exceed10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury isb. The checker program will consider your answer correct if.

ExamplesInput
3 1 1 2 0 031 12 12 3Output
11.084259940083Input
5 0 4 2 2 055 23 05 53 53 3Output
33.121375178000Note

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.

题目大意:

现在有两个人,分别站在(ax,ay)和(bx,by)处,现在有N个垃圾,需要处理,垃圾箱位于(tx,ty)处,这两个人每次只能拿一个垃圾,并且拿到垃圾之后必须放到垃圾箱中,对于两个人来讲,每个人的行动都是相对独立的。问两个人的路径和最小是多少,就能够将所有垃圾都扔到垃圾箱中。

思路:

1、这种思维题还是靠某些典型题的体型所影响。比如51nod 1487 占领资源(topcoder上的题)这道题:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1487

利用到这种极限思维的题还是蛮经典的。可以规划到一类去深刻记忆。

因为做出来这个题的时候感觉这种极限思维还是蛮有趣的,记忆比较深刻。

2、对于A.B两个人来讲,如果一开始都在垃圾箱处出发,那么ans=2*Σdis(xi,yi)---->(tx,ty);

对于两个人A.B,每个人只要都选了一个垃圾去捡,并且都回到了垃圾箱处,那么接下来的路程就相当于一个人来回走。

那么对于暴力思维来讲,那就是直接O(n^2)枚举两个垃圾,过程维护最小值。显然这么做是会超时的。

那么接下里从贪心的角度出发,对于ans=2*Σdis(xi,yi)->(tx,ty)-A选择的第一个垃圾的点到垃圾箱的距离+A选择的第一个垃圾的点到A初始位子的距离-B选择的第一个垃圾的点到垃圾箱的距离+B选择的第一个垃圾的点到A初始位子的距离;

我们肯定是希望后边这一大长串描述中,A选择的第一个垃圾的点到A初始位子的距离以及B选择的第一个垃圾的点到B初始位子的距离的和尽可能的小,然而对于不同的选择方式,最终答案肯定是不同的。

那么我们不妨设定两个数组A【】,B【】,一个按照点与A的距离从小到大排序,一个按照与B的距离从小到大排序。

接下来我们可以暴力枚举两项,分别作为A选择的点以及B选择的点,当然不能忘记,有可能一个人一直不动是正解,这种情况也要枚举。

3、通过思考和简单枚举不难发现对于枚举的量,其实只要足够大,就一定不会影响结果。

所以在第一次提交的时候,我A,B数组都枚举了1000的量,结果是Ac的。(再之后交了一发100的枚举量,也是Ac的);

4、这种极限思维还是蛮实用的。

Ac代码:

#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>using namespace std;#define ll __int64struct node{    ll x,y;    double dis0;    double disa;    double disb;    int pos;}a[100060],b[100060];double dis(ll a,ll b,ll c,ll d){    return sqrt((a-c)*(a-c)+(b-d)*(b-d));}int cmp(node a,node b){    if(a.disa!=b.disa)    return a.disa<b.disa;    else return a.disb<b.disb;}int cmp2(node a,node b){    if(a.disb!=b.disb)    return a.disb<b.disb;    else return a.disa<b.disa;}int main(){    ll ax,ay,bx,by,tx,ty;    while(~scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&ax,&ay,&bx,&by,&tx,&ty))    {        ll n;        scanf("%I64d",&n);        double minna=-1,minna0;        double minnb=-1,minnb0;        double sum=0;        for(int i=0;i<n;i++)        {            scanf("%I64d%I64d",&a[i].x,&a[i].y);            a[i].pos=i;            b[i].pos=i;            a[i].dis0=dis(a[i].x,a[i].y,tx,ty);            a[i].disa=dis(a[i].x,a[i].y,ax,ay);            a[i].disb=dis(a[i].x,a[i].y,bx,by);            sum+=2*a[i].dis0;            b[i].dis0=a[i].dis0,b[i].disa=a[i].disa;b[i].disb=a[i].disb;        }        double ans=2000000000000000000;        sort(a,a+n,cmp);        sort(b,b+n,cmp2);        for(int i=0;i<n&&i<100;i++)        {            for(int j=0;j<n&&j<100;j++)            {                if(a[i].pos==b[j].pos)                {                    ans=min(ans,min(sum-a[i].dis0+a[i].disa,sum-b[j].dis0+b[j].disb));                    continue;                }                ans=min(ans,sum-a[i].dis0-b[j].dis0+a[i].disa+b[j].disb);            }        }        printf("%.12lf/n",ans);    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表