麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Codeforces 672C Recycling Bottles【極限思維+貪心枚舉】

2019-11-14 09:05:35
字體:
供稿:網(wǎng)友

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.

題目大意:

現(xiàn)在有兩個(gè)人,分別站在(ax,ay)和(bx,by)處,現(xiàn)在有N個(gè)垃圾,需要處理,垃圾箱位于(tx,ty)處,這兩個(gè)人每次只能拿一個(gè)垃圾,并且拿到垃圾之后必須放到垃圾箱中,對(duì)于兩個(gè)人來講,每個(gè)人的行動(dòng)都是相對(duì)獨(dú)立的。問兩個(gè)人的路徑和最小是多少,就能夠?qū)⑺欣既拥嚼渲小?/p>

思路:

1、這種思維題還是靠某些典型題的體型所影響。比如51nod 1487 占領(lǐng)資源(topcoder上的題)這道題:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1487

利用到這種極限思維的題還是蠻經(jīng)典的。可以規(guī)劃到一類去深刻記憶。

因?yàn)樽龀鰜磉@個(gè)題的時(shí)候感覺這種極限思維還是蠻有趣的,記憶比較深刻。

2、對(duì)于A.B兩個(gè)人來講,如果一開始都在垃圾箱處出發(fā),那么ans=2*Σdis(xi,yi)---->(tx,ty);

對(duì)于兩個(gè)人A.B,每個(gè)人只要都選了一個(gè)垃圾去撿,并且都回到了垃圾箱處,那么接下來的路程就相當(dāng)于一個(gè)人來回走。

那么對(duì)于暴力思維來講,那就是直接O(n^2)枚舉兩個(gè)垃圾,過程維護(hù)最小值。顯然這么做是會(huì)超時(shí)的。

那么接下里從貪心的角度出發(fā),對(duì)于ans=2*Σdis(xi,yi)->(tx,ty)-A選擇的第一個(gè)垃圾的點(diǎn)到垃圾箱的距離+A選擇的第一個(gè)垃圾的點(diǎn)到A初始位子的距離-B選擇的第一個(gè)垃圾的點(diǎn)到垃圾箱的距離+B選擇的第一個(gè)垃圾的點(diǎn)到A初始位子的距離;

我們肯定是希望后邊這一大長串描述中,A選擇的第一個(gè)垃圾的點(diǎn)到A初始位子的距離以及B選擇的第一個(gè)垃圾的點(diǎn)到B初始位子的距離的和盡可能的小,然而對(duì)于不同的選擇方式,最終答案肯定是不同的。

那么我們不妨設(shè)定兩個(gè)數(shù)組A【】,B【】,一個(gè)按照點(diǎn)與A的距離從小到大排序,一個(gè)按照與B的距離從小到大排序。

接下來我們可以暴力枚舉兩項(xiàng),分別作為A選擇的點(diǎn)以及B選擇的點(diǎn),當(dāng)然不能忘記,有可能一個(gè)人一直不動(dòng)是正解,這種情況也要枚舉。

3、通過思考和簡單枚舉不難發(fā)現(xiàn)對(duì)于枚舉的量,其實(shí)只要足夠大,就一定不會(huì)影響結(jié)果。

所以在第一次提交的時(shí)候,我A,B數(shù)組都枚舉了1000的量,結(jié)果是Ac的。(再之后交了一發(fā)100的枚舉量,也是Ac的);

4、這種極限思維還是蠻實(shí)用的。

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);    }}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产91亚洲精品久久久 | 一级电影免费在线观看 | 日本在线播放一区二区三区 | 日本高清一级片 | a免费视频| 日韩a毛片免费观看 | 国产精品无码久久久久 | 成人一区久久 | v11av在线视频成人 | 舌头伸进添的我好爽高潮网站 | 日本精品二区 | 一区二区三区无码高清视频 | 久久久久久麻豆 | 成人免费观看49www在线观看 | 黄色av网站在线观看 | 久久精品探花 | 新久草在线视频 | 久综合色 | 欧美亚洲综合网 | 欧美人与牲禽动交精品一区 | 国产日韩三区 | 98色视频 | 欧美日韩国产综合网 | 免费放黄网站在线播放 | 91九色蝌蚪在线 | 一区二区久久久久草草 | 成人男女啪啪免费观看网站四虎 | 国产pron | 男女污视频在线观看 | 一级大黄毛片免费观看 | 中文字幕在线观看国产 | 欧美在线观看视频一区 | 黄色国产在线观看 | 中文字幕22页 | 欧美日韩亚洲在线观看 | 精品在线视频观看 | 大学生一级毛片在线视频 | 久久精品视频网址 | 日本成人二区 | 综合日韩欧美 | 精品一区二区三区中文字幕老牛 |