zoukankan      html  css  js  c++  java
  • 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 0
    3
    1 1
    2 1
    2 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
    ",ans1);
        //cout << setprecision(10) << setiosflags(ios::scientific) << ans1 << endl;
        return 0;
    }
  • 相关阅读:
    openlayers5之ol.proj坐标转换
    bootstrap模态框可拖动 --------------------- 本文来自 gisdoer 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/gisdoer/article/details/82838312?utm_source=copy
    bootstrap模态框可拖动
    leaflet实用插件整理
    面试题经常问的闭包问题
    撩课-Java每天5道面试题第15天
    撩课-Web大前端每天5道面试题-Day4
    撩课-Python-每天5道面试题-第4天
    撩课-Java每天5道面试题第14天
    撩课-Web大前端每天5道面试题-Day3
  • 原文地址:https://www.cnblogs.com/zxhl/p/5485579.html
Copyright © 2011-2022 走看看