ZCMU-1031-Graveyard

1031: Graveyard

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 134   Solved: 63
[ Submit][ Status][ Web Board]

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.

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

Sample Input

2 1 
2 3
3 1 
10 10

Sample Output

1666.6667
1000.0000
1666.6667
0.0000


【解析】
我觉得这道题很有必要的说一下虽然我也仅仅是理解,不过感觉这个套路很好用,这道题的意思就是在一个圆当中本来是有n个点,点与点的距离相等,现在多了m个点,要求这n+m个点之间点与点的距离都是相等的。这样的话,其实按照我们这样子,原来呢就看成在1个长度内均分,它们的坐标是多少,然后给他放大在n+m个长度当中它的坐标是多少然后我们再找离它最近的点,这个最近的点怎么找用floor(k+0.5))取不超过k+0.5的最大整数然后这里我们要注意了因为我们已经把长度放大到n+m的所以在这里面n+m个点所处的都是整数的坐标,所以我们这里fabs(k-floor(k+0.5))最后除n+m是最后缩小回去。我们举个例子吧,比如说我们n取4.m取1,第一个点我们把它设定为原点不动的,它在n+m个长度当中相当于0,第二个点我们算它左边是1.25,因为现在长度为5,然后5/4就是1.25,之后呢我们就要算它离那个点最近了,注意在n+m长度中一定是整数点,所以我们这里可以用floor(k+0.5)所取的一定是最小的
#include<stdio.h>
#include<math.h>
int main()
{
    int n,m;
    double sum,k;
    while(~scanf("%d%d",&n,&m))
    {
        sum=0;
        for(int i=1;i<n;i++)//一个点不动
        {
            k=(double)i/n*(n+m);//算这个点在n+m个长度当中的坐标
            sum+=fabs(k-floor(k+0.5))/(n+m);//距离相减
        }
        printf("%.4f\n",sum*10000);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值