【codeforces 672 C】【计算几何+贪心】Recycling Bottles【有两个人和一个垃圾桶,n个瓶子,把每个瓶子捡起来然后扔到垃圾箱中,每人最多带一个瓶子,求最短总距离】

本文介绍了一种解决回收日问题的算法,通过坐标平移建立坐标系并计算两人的最短总行走距离来收集所有瓶子并投入回收箱。考虑了各种特殊情况,并提供了详细的代码实现。

传送门:http://codeforces.com/contest/672/problem/C

题意:有两个人和一个垃圾桶,n个瓶子, 现在要人去把每个瓶子捡起来然后扔到垃圾箱中,每人最多带一个瓶子,求最短总距离


思路:

我先用坐标平移使得垃圾桶作为零点建立坐标系

假设人都在垃圾箱上,那么总距离为2 * 每个瓶子到垃圾桶的距离

现在人不在上面,所以只要求出瓶子到垃圾桶的距离 - 人到瓶子的距离的最大值

然后再用总和减去上面一行所求即可

 注意:肯定要有一个人先捡瓶子再回垃圾箱的,如果另一个人的最大值小于0,那么久不需要他捡了, 还有注意只有1个瓶子的情况。


PS:这题不仅细节多,而且考验码力,还是要多多练习


代码:

#include <bits/stdc++.h>
using  namespace  std;

const int inf=0x3f3f3f3f;
const int N=1e5+10;

double dis(double x1, double y1, double x2, double y2)  
{  
    return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) * (y1 - y2));  
}  

struct node
{
    double x,y; 
    int id;
}a[N], b[N];
int vis[N];
double ax, ay, bx, by, tx, ty, sum;

bool cmp1(node a, node b){
    return dis(a.x, a.y, 0, 0) - dis(a.x, a.y, ax, ay) > dis(b.x, b.y, 0, 0) - dis(b.x, b.y, ax, ay);
}

bool cmp2(node a, node b){
    return dis(a.x, a.y, 0, 0) - dis(a.x, a.y, bx, by) > dis(b.x, b.y, 0, 0) - dis(b.x, b.y, bx, by);
}

int  main(){
    cin>>ax>> ay>> bx>> by>> tx>> ty;
    ax -= tx;//把垃圾桶作为坐标原点,坐标变换
    ay -= ty;
    bx -= tx;
    by -= ty;
    int n;
    cin>>n;
    for(int i=1; i<=n; i++){
        double x, y;
        cin>>x>>y;
        b[i].x=a[i].x=x-tx;
        b[i].y=a[i].y=y-ty;
        a[i].id=b[i].id=i;
        sum += 2*(dis(a[i].x, a[i].y, 0, 0));
    }

    double mx=0, a1, a2, b1,  b2;

    sort(a+1, a+n+1, cmp1); vis[a[1].id]=1;
    a1=dis(a[1].x, a[1].y, 0, 0) - dis(a[1].x, a[1].y, ax, ay);
    a2=dis(a[2].x, a[2].y, 0, 0) - dis(a[2].x, a[2].y, ax, ay);

    sort(b+1, b+n+1, cmp2); 
    b1=dis(b[1].x, b[1].y, 0, 0) - dis(b[1].x, b[1].y, bx, by);
    b2=dis(b[2].x, b[2].y, 0, 0) - dis(b[2].x, b[2].y, bx, by);

    if(n==1){
        mx=max(a1, b1);
    }
    else{
        if(vis[b[1].id])
            mx = max(max(a1, b2)+max(min(a1, b2), 0.0), max(a2, b1)+max(min(a2, b1), 0.0));//注意要和0比较  
        else
            mx = max(a1, b1)+max(min(a1, b1), 0.0);
    }
    printf("%.12lf\n", sum-mx);
    return 0;
}

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 0
3
1 1
2 1
2 3
output
11.084259940083
input
5 0 4 2 2 0
5
5 2
3 0
5 5
3 5
3 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.





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值