Uva11555-aspen Avenue

Source: Internet
Author: User

Tonight CF GYM a question, God pit.

Original question:


Aspen Avenue

"Phew, that's the last one!" exclaimed the garden helper Tim as he threw the last of the tree plant to the ground. His employer, Countess Esmeralda Hunt who owned the estate, had 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 is supposed to being planted at the very beginning of the road, and the last trees of the Aven UE at the very end of the road. Tim, who was constantly being reminded of hers sense of accuracy, knew that the Countess would insist on the trees being PL aced in perfectly aligned tree pairs, one on all side of the road, and with exactly the same spacing between the pairs al Ong the road. However, when bringing the tree plants to the estate, Tim had just dropped them arbitrarily along the left side of the ROA D and is 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 is 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 mini Mize the total (Euclidean) distance This he had to move the trees.

Input

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

The input starts with a positive even integer N between 4 and (inclusive), giving the total numb Er of trees in the avenue. The next line contains-integers L and W , where 1L10000 is the length of the RO AD, in meters, and 1Ware 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 S IDE 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 being moved, on a line by itself. The answer should is given with an absolute or relative error of in most 10-6.

Sample Input
410 1101010610 1093556

Sample Output
2.41421356249.2853832858

Test instructions

A road with a length of L and a width of w is required to plant n trees on both sides. Each tree as a pair, strictly stamped at the same distance from the beginning of the road on both sides. The distance between each adjacent pair of trees is strictly equal. The first two trees are at 0 and the last two are in L.

Initially all trees are scattered in the left side of the road (input position), asking the minimum number of Euclidean distances required to move the tree in order to move them to the specified position.

Ideas:

Start thinking sort, then let the two trees run to the nearest to the beginning of the location of the no tree, a little calculation, you can find the closest to the specified location of the tree on the left side of the road, the other tree on the right side of the road is the best for the two trees. But even WA 4 hair after give up ...

Finally determine the DP to do, but because it is a tree expansion (from God CRCCW), so the direct for is not very line, with DFS memory search can be resolved.

Dp[i][j]=dfs (i,j) =min (Dfs (I+1,J) +dis (Tree[i+j], left i+1 position), DFS (i,j+1) +dis (Tree[i+j], right j+1 position));

TIPS:DP[I][J] initialized to INF; subscript starting from 0

Code:

1#include <stdio.h>2#include <math.h>3#include <algorithm>4 using namespacestd;5 6 Const DoubleINF =100000000000;7 intN;8 Doublel,w;9 Doublea[ the];Ten DoubleITV; One Doubledp[ the][ the]; A BOOLfact[ the][ the]; -  - voidINI () the { -      for(intI=0; i<=n;i++) -          for(intj=0; j<=n;j++) -         { +dp[i][j]=INF; -fact[i][j]=0; +         } A } at  - DoubleDfsintLintR) - { -     if(l>=n&&r>=n)return 0; -     if(Fact[l][r])returnDp[l][r]; -fact[l][r]=1; in  -     if(l<n) Dp[l][r]=min (DFS (l +1, R) +fabs (a[l+r]-l*ITV), Dp[l][r]); to     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]); +  -     returnDp[l][r]; the } *  $ intMain ()Panax Notoginseng { -scanf"%d",&n); thescanf"%LF%LF",&l,&W); +      for(intI=0; i<n;i++) A     { thescanf"%LF",&a[i]); +     } - ini (); $Sort (a,a+n); $n=n/2; -itv=l/((Double) N-1); -  theprintf"%.10f\n", DFS (0,0)); -     return 0;Wuyi}

As to the beginning of the greedy practice is not right, after a number of square inequality calculation should be available.

Off Topic:

Today in the absence of the Thunder God's extremely bad situation, God teammate CRCCW with me did Zoj monthly March 2013 and CF Gym,gym is NCPC2008 the topic, clearly is a problem, it does not look very difficult, WA 5 hair, see Daniel Blog before. Also study ...

I am too weak, but as long as the thick skin, you can continue to study. Rely more on God's teammates.

Good night!

Uva11555-aspen Avenue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.