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

C. Recycling Bottles

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

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.

 题意:

  给你A,B,C三个点的坐标,C表示垃圾桶,A,B表示人

  在给你n个垃圾的坐标,问你两人每次最多可以携带一个垃圾到达垃圾桶

  将这n个垃圾全部扔进垃圾桶最少走多少路

  这里n个垃圾可以完全由其中一个或两个人捡起

题解:

  在人到达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[N],y[N];
struct ss{
 double x,y,c,d;int ho,id;}p[N],P[N];
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;
}
时间: 2024-08-24 17:24:17

Codeforces Round #352 (Div. 2) C. Recycling Bottles 贪心的相关文章

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

思路:分析完这道题后会发现  当两个人捡到第一个瓶子后, 之后走的路的最小值都是不会变的了. 所以只要找出两个走到各自的第一个瓶子是最小值的情况的时候(其中还有一个人不走,一个人走的情况).   如果当有两个人或有一个人到其第一个瓶子的权值大于瓶子到回收点时,选取权值小的那个. 而且计算最小值的时候 只需取出两个权值数组中最小的两个数 即可 不用全部遍历来选取. #include<iostream> #include<cstdio> #include<cmath> #i

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #352 (Div. 2) ABCD

Problems # Name     A Summer Camp standard input/output 1 s, 256 MB    x3197 B Different is Good standard input/output 2 s, 256 MB    x2870 C Recycling Bottles standard input/output 2 s, 256 MB    x664 D Robin Hood standard input/output 1 s, 256 MB  

Codeforces Round #352 (Div. 2) D. Robin Hood

题目连接请戳此处 题意:n个人每人有一定的财富值ci,每天Robin Hood从最富的人手中取财富1分给最穷的人(取后最穷, 即可以退回),若有多个最穷的人,任选一个给出财富值.问k天后最富的人和最穷的人财富差值为多少. 1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109 1 ≤ ci ≤ 109 分析: 每一天随着财富值的取和给,最穷的人和最富的人都在动态更新. 最后要求的是  (richest-poorest)min,那么  要使这个等式最小,只需求出k天后richest的最小值和po

Codeforces Round #352 (Div. 2) D. Robin Hood (二分法+判断平衡态)

解题思路: 由求最小值和求最大值各自二分. 由l*(a[l+1]-a[l]):求取填 1~l 到a[l+1]的高度需要多少的钱,如果大于剩余的k 则可执行 若否 判断剩余的k是否为l,若否最小值为a[l],否则 为a[l]+k/l; 由(n-r+1)*(a[r]-a[r-1]):求去掉n~n-r+1的这部分需要多少钱,如果大于剩余的k 则执行 若否 判断剩余的k是否为n-r+1,若是最大值为a[n-r+1]-k/(n-r+1); 如果最大值小于最大值,直接printf 如果最小值大于等于最大值,

Codeforces Round #352 (Div. 2) B - Different is Good

A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different. Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants allsubstrings 

Codeforces Round #352 (Div. 2) A Summer Camp

Every year, hundreds of people come to summer camps, they learn new algorithms and solve hard problems. This is your first year at summer camp, and you are asked to solve the following problem. All integers starting with 1 are written in one line. Th

Codeforces Round #140 (Div. 1) Naughty Stone Piles 贪心

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn = 1000010; __int64 sum[maxn] ; __int64 num[maxn]; __int64 ans[maxn]; __int64 vis[maxn] ; __int64 qu

Codeforces Round #140 (Div. 1)D The table 贪心

#include<iostream> #include<cstdio> #include<cstring> using namespace std ; const int maxn = 110 ; int sum_r[maxn] ; int sum_c[maxn] ; int vis_c[maxn] ;int vis_r[maxn] ; int ans_c[maxn] ;int ans_r[maxn] ; int table[maxn][maxn] ; void ini