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

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

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

2019-11-14 08:45:56
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(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è)人來(lái)講,每個(gè)人的行動(dòng)都是相對(duì)獨(dú)立的。問(wèn)兩個(gè)人的路徑和最小是多少,就能夠?qū)⑺欣既拥嚼渲小?/p>

思路:

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

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

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

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

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

那么對(duì)于暴力思維來(lái)講,那就是直接O(n^2)枚舉兩個(gè)垃圾,過(guò)程維護(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初始位子的距離;

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

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

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

3、通過(guò)思考和簡(jiǎn)單枚舉不難發(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ā)表
主站蜘蛛池模板: 在线成人免费视频 | 色人阁在线视频 | 欧美韩国日本在线 | 国产一区精品在线观看 | av电影在线观看免费 | 999久久久久久 | 一级视频网站 | 精品一区二区免费 | 久久国产亚洲精品 | 日本一区二区免费在线观看 | 欧美精品成人 | 亚洲国产馆 | 亚洲一二区视频 | 国产一区精品在线观看 | 久久久久久久久久综合 | av在线中文 | 午夜免费一区 | 成人精品 | 久久久久久久免费看 | 精品中文视频 | 亚州成人在线观看 | 亚洲福利在线视频 | 亚洲aⅴ免费在线观看 | 在线播放污 | 色99999| 视频在线中文字幕 | 九九福利视频 | 爱唯侦察 国产合集 亚洲 | 亚洲成人国产综合 | 国产日产精品久久久久快鸭 | 成人午夜视频在线观看免费 | 国产精品av久久久久久久久久 | 精品国产一区二区久久 | 国产一级在线观看视频 | 国产一区二区在线免费播放 | 日韩视频一区在线 | 国产免费高清 | 97久久日一线二线三线 | av在线免费观看不卡 | 麻豆蜜桃在线观看 | 亚洲爱爱图 |