UVa11555 - Aspen Avenue

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   strong   

今晚CF GYM A題,神坑。。

原題:


Aspen Avenue

 

``Phew, that was the last one!‘‘ exclaimed the garden helper Tim as he threw the last tree plant to the ground. His employer, countess Esmeralda Hunt who owned the estate, has ordered him to arrange an avenue of aspen trees along both sides of the front road leading up to the house. The first trees in the avenue are supposed to be planted at the very beginning of the road, and the last trees of the avenue at the very end of the road. Tim, who was constantly being reminded of her sense of accuracy, knew that the countess would insist on the trees being placed in perfectly aligned tree pairs, one on each side of the road, and with exactly the same spacing between the pairs along the road. However, when bringing the tree plants to the estate, Tim had just dropped them arbitrarily along the left side of the road and was now facing the task of moving the trees to their correct positions to meet the countess‘s requirements. Being stronger in mind than in arms, and since it was time for a coffee break anyway before he started digging, he sat down to figure out which trees to move to which positions so as to minimize the total (Euclidean) distance that he had to move the trees.

 

Input 

The input file contains several test cases, each of them as described below.

The input starts with a positive even integer N between 4 and 2000 (inclusive), giving the total number of trees in the avenue. The next line contains two integers L and W , where 1L10000 is the length of the road, in meters, and 1W20 is the width of the road, in meters. The next N lines each describe where Tim had dropped of the trees. Each such line contains an integer 0pL indicating the position of a tree plant along the left side of the road, measured in meters from the start of the road.

 

Output 

For each test case, write to the output the smallest total number of meters the tree plants need to be moved, on a line by itself. The answer should be given with an absolute or relative error of at most 10-6 .

 

Sample Input 
410 1101010610 1093556

 

Sample Output 
2.41421356249.2853832858

 

題意:

  一條長為L,寬為W的道路,兩旁共需要種N棵樹。每兩棵樹作為一對,嚴格戳在距起點相同距離的道路兩旁。每相鄰兩對樹之間的距離要求嚴格相等。前兩棵在0處,最後兩棵在L處。

  初始時所有樹都在道路的左側亂序散布(輸入位置),問最少需要將樹總共移動多少歐氏距離才能把他們移動到規定的位置。

 

思路:

  開始會想到sort一下,然後依次讓兩棵樹跑到距起點最近的沒有樹的位置去,稍微計算一下,能發現將距離規定位置最近的那棵樹放在道路左邊,將另一棵樹放到道路右邊是對這兩棵樹來說最優的。但是連WA 4發以後放棄了。。。

  最後確定DP來做,但由於是樹形展開(from God crccw),所以直接for是不大行的,用dfs記憶化搜尋可以解決。

  dp[i][j]=dfs(i,j)=min(dfs(i+1,j)+dis(tree[i+j],左側i+1位置) ,dfs(i,j+1)+dis(tree[i+j],右側j+1位置));

  tips:dp[i][j]初始化為INF;下標從0開始

 

代碼:

 

 1 #include<stdio.h> 2 #include<math.h> 3 #include<algorithm> 4 using namespace std; 5  6 const double INF = 100000000000; 7 int n; 8 double l,w; 9 double a[3000];10 double itv;11 double dp[3000][3000];12 bool fact[3000][3000];13 14 void ini()15 {16     for(int i=0;i<=n;i++)17         for(int j=0;j<=n;j++)18         {19             dp[i][j]=INF;20             fact[i][j]=0;21         }22 }23 24 double dfs(int l,int r)25 {26     if(l>=n&&r>=n) return 0;27     if(fact[l][r]) return dp[l][r];28     fact[l][r]=1;29 30     if(l<n) dp[l][r]=min(dfs(l+1,r)+fabs(a[l+r]-l*itv),dp[l][r]);31     if(r<n) dp[l][r]=min(dfs(l,r+1)+sqrt((a[l+r]-r*itv)*(a[l+r]-r*itv)+w*w),dp[l][r]);32 33     return dp[l][r];34 }35 36 int main()37 {38     scanf("%d",&n);39     scanf("%lf%lf",&l,&w);40     for(int i=0;i<n;i++)41     {42         scanf("%lf",&a[i]);43     }44     ini();45     sort(a,a+n);46     n=n/2;47     itv=l/((double)n-1);48 49     printf("%.10f\n",dfs(0,0));50     return 0;51 }

 

 

至於開始那種貪心做法為何不對,經過一番多次平方的不等式計算應該可以得到。

 

題外話:

今天在缺少雷神的極其惡劣的情況下,神隊友crccw帶著我做了zoj monthly march 2013和CF GYM,GYM是NCPC2008的題目,明明是A題,看起來也不是很難,,WA了5發,看了大牛部落格才過。。也是學習了。。。

 

自己太弱,不過只要臉皮厚,就能繼續學習。。多靠神隊友。

 

晚安!

UVa11555 - Aspen Avenue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.