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的更多相关文章

随机推荐

  1. nvm-windows 之nodejs 版本管理

    前言   最近准备学习后端相关的东西,但是公司目前的node版本是偏低的,但是现在的node版本变化太快.刚好也有nvm这种版本管理器的存在,简直了都.兴奋之后发现,不支持windows系统,此处~~ ...

  2. 短URL系统、301/302重定向

    短 URL 系统是怎么设计的?https://yq.aliyun.com/articles/87600 短网址(short URL)系统的原理及其实现 https://hufangyun.com/20 ...

  3. css3之文本text-overflow 与 word-wrap, word-break

    CSS3 Text Overflow属性 CSS3文本溢出属性指定应向用户如何显示溢出内容 语法: text-overflow:clip | ellipsis 但是text-overflow只是用来说 ...

  4. Ubuntu安装CUDA9.2(不更新驱动)

    1.先装驱动,以为安装CUDA时安装最新驱动导致CUDA用不了 sudo apt-get install nvidia-396 2.参考这,安装好CUDA 9.2 https://developer. ...

  5. 2019-9-11-完整的-P2P-应用需要包含哪些功能

    title author date CreateTime categories 完整的 P2P 应用需要包含哪些功能 lindexi 2019-9-11 9:0:55 +0800 2019-09-05 ...

  6. CodeChef TRIPS-Children Trips 树上分块

    参考文献国家集训队2015论文<浅谈分块在一类在线问题的应用>-邹逍遥 题目链接 题目大意 一棵n个节点的树,树的每条边长度为1或2,每次询问x,y,z. 要求输出从x开始走,每次只能走到 ...

  7. Cyclic GCDs

    Cyclic GCDs 题目链接 题面描述 有\(n\)个点,每个点有权值. 现有排列\(P\),\(p_i\)表示\(i\)个点向\(p_i\)连了一条边. 显然会形成若干个简单环.每个简单环的权值 ...

  8. UOJ261 【NOIP2016】天天爱跑步 LCA+动态开点线段树

    UOJ261 [NOIP2016]天天爱跑步 Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.天天爱跑步是一个养成类游戏,需要玩家每天按时上线, ...

  9. html常用标签详解3-a标签

    a标签 1.a标签的属性 a标签属于行内元素标签,双标签<a></a> href:a标签的跳转地址 target:打开方式(_self自身:_blank:新窗口) title: ...

  10. SQL有意思的面试题

    1.中软国际 SQL行转列  变成   --数据准备create table t_test( year int, month int, sale int, primary key (year, mon ...