POJ - 3154 Graveyard

来源:互联网 发布:java集合教务管理系统 编辑:程序博客网 时间:2024/06/05 03:56
Graveyard
Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

Description

Programming contests became so popular in the year 2397 that the governor of New Earck — the largest human-inhabited planet of the galaxy — opened a special Alley of Contestant Memories (ACM) at the local graveyard. The ACM encircles a green park, and holds the holographic statues of famous contestants placed equidistantly along the park perimeter. The alley has to be renewed from time to time when a new group of memorials arrives.

When new memorials are added, the exact place for each can be selected arbitrarily along the ACM, but the equidistant disposition must be maintained by moving some of the old statues along the alley.

Surprisingly, humans are still quite superstitious in 24th century: the graveyard keepers believe the holograms are holding dead people souls, and thus always try to renew the ACM with minimal possible movements of existing statues (besides, the holographic equipment is very heavy). Statues are moved along the park perimeter. Your work is to find a renewal plan which minimizes the sum of travel distances of all statues. Installation of a new hologram adds no distance penalty, so choose the places for newcomers wisely!

Input

Input file contains two integer numbers: n — the number of holographic statues initially located at the ACM, and m — the number of statues to be added (2 ≤ n ≤ 1000, 1 ≤ m ≤ 1000). The length of the alley along the park perimeter is exactly 10 000 feet.

Output

Write a single real number to the output file — the minimal sum of travel distances of all statues (in feet). The answer must be precise to at least 4 digits after decimal point.

Sample Input

sample input #1 2 1 sample input #2 2 3 sample input #3 3 1 sample input #4 10 10

Sample Output

sample output #1 1666.6667 sample output #2 1000.0 sample output #3 1666.6667 sample output #4 0.0

Hint

Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.

Source

Northeastern Europe 2006




大意:
在一个周长为10000的圆上等距分布着n个雕塑,现在又要向圆中加入m个雕塑,并且这m+n个雕塑依旧需要等距分布,求原n个雕塑需要移动的最小距离,能使m+n个雕塑在圆上等距分布。

思路:
将原有的每个雕塑的坐标位置,映射在一个 [0,m+n-1] 的数轴上(设第一个点的坐标为0,并保持不变),该数轴上每个整数端点为添加雕塑后每个雕塑的摆放位置(一共 m+n 个)。pos[i]代表原来的第i个点,用 pos[i]/n*(n+m) 即可得到映射在数轴上的坐标值。由于第一个雕塑的坐标保持为0,从第二个雕塑开始枚举,判断当前雕塑的坐标距离哪个整数的端点最近(用四舍五入判断),较近的这段距离,即为它所需要移动的距离,用一个变量来累加结果。在这里不可能出现两个雕塑都距离同一个整数端点较近的情况,因为现在有 m+n 个雕塑,每个雕塑之间的间隔为1,而之前只有 n 个雕塑,那么之前的雕塑之间的间隔一定大于1,所以不可能都靠近同一个整数端点。最后将总的移动距离 sum 转化为在周长为10000的圆上,用 sum/(m+n)*10000 即可。



下面是ac代码:
#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    int n,i,m;
    double pos;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        double ans=0.0;
        for(i=1;i<n;i++)
        {
            pos=(double)i/n*(n+m);//得到(n+m)个整数点,pos为原来位置放大(n+m)倍后的位置,这时应当取离整数点最近的靠近
            ans+=fabs(pos-floor(pos+0.5))/(n+m);//取离整数点最近的位置靠近,同时除以(n+m)还原。
        }
        printf("%.4lf\n",ans*10000);
    }
    return 0;
}




这是wa代码:
#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    double n,i,m,j;
    while(scanf("%lf%lf",&n,&m)!=EOF)
    {
        double ans=0.0;
        for(i=1;i<n;i++)
        {
            for(j=1;j<n+m;j++)
            {
                if(fabs(i/n-j/(n+m))<=fabs(i/n-(j-1)/(n+m))&&fabs(i/n-j/(n+m))<=fabs(i/n-(j+1)/(n+m)))//?????
                ans+=fabs(i/n-j/(n+m));//???
            }
        }
        printf("%.4lf\n",ans*10000);
    }
    return 0;
}



一直找不到wa的原因,惆怅。。。。。。待解决。或者有什么反例?虽然感觉这种解法不太对。
0 0