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即可

不卡精度这样写美滋滋

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} int n,m;
double ans; int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
ans = ;
for(register int i = ;i < n;++ i)
{
double tmp = (double)i / n * (n + m);
ans += fabs(tmp - (int)(tmp + 0.5)) / (n + m);
}
printf("%.4lf\n", ans * );
}
return ;
}

LA1347

最新文章

  1. 预习笔记 多态 --S2 4.3
  2. 内网渗透测试思路-FREEBUF
  3. shell 使用
  4. html img src base64
  5. js的元素对象
  6. Atmel Studio 6.0 重新安装
  7. 求教jsp 问题,能在一台电脑上运行,不能在另外一台上运行?
  8. jquery1.7.2的源码分析(六)基本功能
  9. A - Bi-shoe and Phi-shoe (欧拉函数打表)
  10. 带金属光泽的模型shader的实现
  11. 2017寒假零基础学习Python系列之函数之 编写函数
  12. hadoop2.5的伪分布式安装配置
  13. [SimplePlayer] 8. 音视频同步
  14. urllib库的应用及简单爬虫的编写
  15. BZOJ5018:[SNOI2017]英雄联盟(背包DP)
  16. commit your changes or stash them before you can merge
  17. python(35):多线程读取文件
  18. 使用nginx搭建tomcat集群配置
  19. CSS的定位问题总结
  20. Latex初学者入门(三)-- 用BibTeX生成参考文献

热门文章

  1. js 数据绑定
  2. FormData兼容IE10 360及DWR的异步上传原理
  3. Jetty启动配置解析
  4. Diff- Linux必学的60个命令
  5. 工控安全入门(四)—— DNP3协议
  6. C# 窗体内容显示不全
  7. response - 文件下载
  8. Ajax技术 - (Asynchronous JavaScript + XML)
  9. 浓缩版 《C和指针》基础篇(Chpt.1~Chpt.9)
  10. Redis 核心