UvaLive1347

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. Note: 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.0 1666.6667 0.0

 

【题解】

一定有一个不动。可以想如果他动了,那么可以转回来,移动的长度不变。

蓝书上用了很巧的一个技巧去做。

把总长度看做1,然后分成n分,第i个就在i/n的位置,然后扩大(n + m)

倍,得到总长度(n + m)时第i个所在的位置,移动到整数位的最近距离就是

四舍五入后的数与该数的差,之后除以(n + m),恢复到总长度1

由于原长为10000,最后乘10000即可

不卡精度这样写美滋滋

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cmath>
 7 #define max(a, b) ((a) > (b) ? (a) : (b))
 8 #define min(a, b) ((a) < (b) ? (a) : (b))
 9 inline void swap(int &a, int &b)
10 {
11     int tmp = a;a = b;b = tmp;
12 }
13 inline void read(int &x)
14 {
15     x = 0;char ch = getchar(), c = ch;
16     while(ch < '0' || ch > '9')c = ch, ch = getchar();
17     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
18     if(c == '-')x = -x;
19 }
20 
21 int n,m;
22 double ans;
23 
24 int main()
25 {
26     while(scanf("%d %d", &n, &m) != EOF)
27     {
28         ans = 0;
29         for(register int i = 1;i < n;++ i)
30         {
31             double tmp = (double)i / n * (n + m);
32             ans += fabs(tmp - (int)(tmp + 0.5)) / (n + m); 
33         }
34         printf("%.4lf\n", ans * 10000);
35     }
36     return 0;
37 }
LA1347

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/7673830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值