Graveyard

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 <tex2html_verbatim_mark>-- the number of holographic statues initially located at the ACM, and m <tex2html_verbatim_mark>-- the number of statues to be added (2n1000, 1m1000) <tex2html_verbatim_mark>. 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.

<tex2html_verbatim_mark>

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 题目大意:在一个周长为10000的圆上等距分布着n个雕塑。现在又有m个新雕塑加入(位置可以任意放),希望所有n+m个雕塑在圆上等距分布。这就需要移动一些原有的雕塑。要求n个雕塑移动的总距离尽量小。 分析:3个样例具有一个共同点:有一个雕塑没有移动。实际上根据中位数原理,该特点在所有情况下都成立,中位数原理证明见上上篇随笔。
  为了简单起见,我们把没动的那个雕塑作为坐标原点,其他雕塑按照逆时针顺序标上到原点的距离编号(新增雕塑之后,相邻的雕塑距离等于1)。注意,这里的距离并不是真实距离,而是按比例缩小后的距离。接下来,我们把每个雕塑移动到离它最近的位置。如果没有两个雕塑移动到相同的位置,那么这样的移动一定是最优的。 代码如下:
 #include<cstdio>
#include<cmath>
using namespace std; int main() {
int n, m;
while(scanf("%d%d", &n, &m) == ) {
double ans = 0.0;
for(int i = ; i < n; i++) {
double pos = (double)i / n * (n+m); //计算每个需要移动的雕塑的坐标
ans += fabs(pos - floor(pos+0.5)) / (n+m); //累加移动距离
}
printf("%.4lf\n", ans*); //等比例扩大坐标
}
return ;
}

注意:在代码中,坐标pos的雕塑移动到的目标位置是floor(pos+0.5),也就是pos四舍五入后的结果。这就是坐标缩小的好处。

  实际上确实没有两个雕塑移动到相同的位置,证明如下:在我们的程序中,当坐标系缩放之后,坐标为x的雕塑被移动到了x四舍五入后的位置。如果有两个坐标分别为x和y的雕塑被移动到了同一个位置,说明x和y四舍五入后的结果相同,换句话说,差距最大的情况是x=0.5,y=1.499999...。即便是这样的情况,y-x小于1,但是这是不可能的,因为新增雕塑之后,相邻的雕塑距离才等于1,之前雕塑的数目更少,距离应当更大才对。

												

LA 3708 Graveyard(推理 参考系 中位数)的更多相关文章

  1. [ACM_数学] LA 3708 Graveyard [墓地雕塑 圈上新加点 找规律]

    Description   Programming contests became so popular in the year 2397 that the governor of New Earck ...

  2. LA 3708 Graveyard 墓地雕塑 NEERC 2006

    在一个周长为 10000 的圆上等距分布着 n 个雕塑.现在又有 m 个新雕塑加入(位置可以随意摆放),希望所有 n + m 个雕塑能在圆周上均匀分布.这就需要移动一些原有的雕塑.要求 n 个雕塑移动 ...

  3. LA 3708 Graveyard

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  4. 【贪心】【POJ3154】墓地雕塑(Graveyard, NEERC 2006, LA 3708)需要稍稍加工的(先贪心,再确保能这样贪(可行性&&如果可行必定最优&&非证明最优性)的题)(K)

    例题4  墓地雕塑(Graveyard, NEERC 2006, LA 3708) 在一个周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周 ...

  5. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  6. UVALive.3708 Graveyard (思维题)

    UVALive.3708 Graveyard (思维题) 题意分析 这标题真悲伤,墓地. 在周长为1e4的圆周上等距分布着n个雕塑,现在要加入进来m个雕塑,最终还要使得这n+m个雕塑等距,那么原来的n ...

  7. LA 3708 && POJ 3154 Graveyard (思维)

    题意:在周长为10000的圆上等距分布着n个雕塑,现在又加入m个,现在让m+n个等距分布,那就得移动一些原有的雕塑,问你移动的最少总距离是多少. 析:首先我们可以知道,至少有一个雕塑是可以不用移动的, ...

  8. uvalive 3708 Graveyard

    https://vjudge.net/problem/UVALive-3708 题意: 一个长度为10000的圆环上放着n个雕塑,每个雕塑之间的距离均相等,即这个圆环被n个点均分.现在需要加入m个雕塑 ...

  9. Uva 3708 Graveyard

    题意:在周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周上均匀分布. 这就需要移动其中一些原有的雕塑.要求n个雕塑移动的距离最小. (2& ...

随机推荐

  1. 如何使用 RDP 或 SSH 连接到 Azure 虚拟机

    使用 RDP 或 SSH 连接到 Azure 虚拟机 本文简要概述了如何使用远程桌面控制协议 (RDP) 或安全外壳(Secure Shell,SSH)客户端登录 Azure 虚拟机.它还包括要求和故 ...

  2. Unity手游之路<六>游戏摇杆之Easy Touch 3教程

    之前已经介绍过Unity自带的摇杆Joystick,它用起来很简单.但是它也存在很多局限,不能全部满足普通mmo手游的一些需求,例如:要能方便地更好素材:能指定在某个区域显示,或者只有在该区域触摸时才 ...

  3. 数字信号处理与音频处理(使用Audition)

    前一阵子由于考博学习须要,看了<数字信号处理>,之前一直不清除这门课的理论在哪里应用比較广泛. 这次正巧用Audition处理了一段音频,猛然发现<数字信号处理>这门课还是很实 ...

  4. Geogebra里给带有曲线和直线混合边界的封闭区域填充颜色

    目的 用Geogebra绘制如图所看到的曲线,并填充如图边界的区域为实心: 用代码实现当然是能够的,可是,图形过于简单的时候用代码就不经济了.由于每个细小变动都还要调整改动代码并预览,非所见即所得.往 ...

  5. Eclipse使用新手教程

    说起java的IDE,朗朗上口的无非是Eclipse了,假若能熟练Eclipse,对于我们编写java程序会起到事半功倍的效果,大大提高我们工作效率.因此本篇博文,笔者仅仅是针对刚刚入门java的新手 ...

  6. phpmailer使用gmail SMTP的方法

    终于能够通过phpmailer使用gmail账号发送邮件了phpmailer(现在的版本是1.73)是一个很好用的工具,可以很方便的使用php语言发送邮件,支持smtp及验证,我们一直都用它. 但是, ...

  7. cocosbuilder中的Callbacks和sound effects

    cocosbuilder3中有增加了 Callback和sound effects 的timeline 这个东西用来在动画播放过程中控制音效和回调动作,非常方便 按住option键(alt), 点击t ...

  8. linux使用过程中遇到的问题和解决方法

      测试过程中,出现以下错误,导致远程ssh连接不上,最终导致测试结果失败.系统日志如下: Sep 1 03:15:03 node3 avahi-daemon[5834]: Invalid respo ...

  9. 高质量CSS编写规范

    ①id和class的命名在保持语义性的同时尽可能的短.不推荐的写法:#navigation {} .atr {}推荐的写法  : #nav {} .author {}可以缩写单词,但缩写后务必能让人明 ...

  10. Android开发之处理崩溃异常

    众所周知,android的设备千差万别,难免会发生崩溃异常等现象,这个时候就需要捕获哪些崩溃异常了,也就是捕获崩溃异常的相关信息,并记录下来,这样一来方便开发人员和测试人员的分析与调试. 1.首先我们 ...