UVA LA3708 Graveyard

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

The input file contains several test cases, each of them consists of a a line that 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

For each test case, write to the output a line with a single real number — the minimal sum of travel distances of all statues (in feet). The answer must be precise to at least 4 digits after decimal point.

Solution

1.刚开始我想无非就是选择一个点坐标不变,剩下的n-1个坐标每个移动 1 n − 1 n + m \frac{1}{n}-\frac{1}{n+m} n1n+m1 的距离。但最后看了答案之后发现是错的,毕竟不一定对应的点就是最近的坐标。

2.本题的核心就是保持一个点不变,剩下n-1个点找到与之最近的点。

3.我们可以将其看作为一个周长为n的圆,现在要把它变为周长为n+m的圆。计算那n-1个点的坐标在n+m的圆中的对应坐标 ,四舍五入找到与其最近的点(因为在n+m的圆中,n+m个点的坐标为1,2,……,n+m)。

4.对于情况3,不会出现两个点同时占同一个点的证明:在经过从n到n+m的转换后,坐标值无疑是变大的,而原坐标值之间的差分为1,经过转换后差分为 1 + m n > 1 1+\frac{m}{n} > 1 1+nm>1 。对于最坏的情况下,令x=1.5,y=2.499999999……,此时y-x还是小于1的,故不成立,故证明成立。

Code

#include <bits/stdc++.h>

#define MOD 1000000007

using namespace std;

int n,m;

void solve()
{
    
    
    double ans = 0.0;
    //计算n-1个点移动的距离
    for(int i=1; i<=n-1; i++)
    {
    
    
        //将n圆移动到n+m圆的新坐标
        double value = 1.0*i/n*(n+m);
		//新坐标与n+m圆上的坐标的差值
        double gap = fabs(floor(value+0.5)-value)/(n+m);
        //加到答案上
        ans += gap;

    }
	//记得乘回题目描述的10000圆
    printf("%.4lf\n", ans*10000);
}   

int main(void)
{
    
    
    while(scanf("%d%d",&n, &m) == 2)
        solve();

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45812280/article/details/121339693