首页 > ACM题库 > HDU-杭电 > Hdu 1679 Moogle 待解决 [解题报告] C++

Hdu 1679 Moogle 待解决 [解题报告] C++


问题描述 :

You got the original idea of making map software, called Moogle Maps, for the new cool Maple mPhone. It will even be capable of indicating the location of a house address like ”Main Street 13”. However, since the mPhone has limited storage capacity, you need to reduce the data amount. You don’t want to store the exact location of every single house number. Instead only a subset of the house numbers will be stored exactly, and the others will be linearly interpolated.
So you want to select house numbers that will minimise the average interpolation error, given how many house locations you have capacity to store. We view the street as a straight line, and you will always store the first and the last house location.
Given that you’ve stored the locations xi and xj for the houses with numbers i and j respectively, but no other house in between, the interpolated value for a house with number k with i < k < j is xi + (xj – xi) * (k-i)/(j-i).


The first line of input gives a single integer, 1 <= t <= 50, the number of test cases.
For each test case, there are two lines. The first contains 2 <= h <= 200 and 2 <= c <= h, where h is the number of houses in the street and c is the number of house locations that can be stored. The second contains h integers in increasing order giving the location of the h houses. Each location is in the interval [0, 1000000].


For each test case, output the average interpolation error over all the h houses for the optimal selection of c house locations to store. The output should be given with four decimal places, but we will accept inaccuracies of up to ±0.001.


4 3
0 9 20 40
10 4
0 10 19 30 40 90 140 190 202 210



  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c