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即可
不卡精度这样写美滋滋
#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
UvaLive1347的更多相关文章
随机推荐
- 莫烦PyTorch学习笔记(四)——回归
下面的代码说明个整个神经网络模拟回归的过程,代码含有详细注释,直接贴下来了 import torch from torch.autograd import Variable import torch. ...
- 聊聊MVC和模块化以及MVVM和组件化
原文链接 小寒的博客,带你理解更深的世界 面向对象,模块化和MVC 面向对象是指把写程序映射到现实生活,从而一来逻辑性更强,更容易写好代码,二来代码很贴切,通俗易懂,更被人理解,三来更加容易拓展和管理 ...
- 70 二叉树的层次遍历 II
原题网址:http://www.lintcode.com/zh-cn/problem/binary-tree-level-order-traversal-ii/ 给出一棵二叉树,返回其节点值从底向上的 ...
- IoC深入理解
1. IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机 ...
- java编程题古典算法之兔子问题
1.题目如下. 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 分析每个月的兔子对数: 1---- ...
- python3.6.4安装错误0x80072efd
是因为翻不了墙,请求URL错误,所以报错那么去掉Download debug sysbom选项
- Linux常用技巧
1.解决不能中文显示 xshell 终端语言显示选择UTF-8 #yum groupinstall chinese-support 2.heredocument报错“unexpected end of ...
- PHP SSH2 不支持 IdentityFile
有的情况下 我们会用到 类似命令行 sftp -o IdentityFile=.ssh/identity username@host方式 登陆, 想用php 操作, 但是 php 现在看是不支持的, ...
- 全栈之路-微信小程序-架构总览
第一阶段是用来学习小程序开发的,这个就相当于PC端的网站吧,只不过现在依靠微信强大的流量来将业务搬移到小程序中,对于企业来说,这是一种很好的发展方向,既减少了开发成本,又减少了推广成本,小程序是很被人 ...
- python验证码识别PIL+pytesseract
1.需要模块安装 在python安装目录scripts即: 执行pip install pillow 下载tesseract-ocr-setup-4.00.00dev.exe 安装,我的目录在C盘默认 ...