Graveyard

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1707   Accepted: 860   Special Judge

Description

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

Input file 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

Write a single real number to the output file — the minimal sum of travel distances of all statues (in feet). The answer must be precise to at least 4 digits after decimal point.

Sample Input

sample input #1
2 1sample input #2
2 3sample input #3
3 1sample input #4
10 10

Sample Output

sample output #1
1666.6667sample output #2
1000.0sample output #3
1666.6667sample output #4
0.0

Hint

Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.

Source

Northeastern Europe 2006
题目链接:http://poj.org/problem?id=3154

题意:一个圆上原先均匀分布着n个点(可移动),现要加入新的m个点,使(n+m)个点仍然均匀分布在圆上,m个点可任意加在任意位置,求重新均匀分布时,原来n个点的移动距离的和的最小值。

这是lrj大白书上的一道题目(P7)。

数论解法:

证明思路,证明这个算法需要证明两个结论:

结论一:无论新的方案和旧的方案的相对位置是什么样的,对于原先的n个点来说,最好的策略就是往最近的新点位置上走。

这个结论唯一一个需要想一下的地方是,两个旧点会不会站到一个坑里去。可以证明这是不会的,设旧的方案两个点之间距离为L,新的方案两个点之间距离为X,若两个旧点A,B跑到了同一个新点O上,就说明,|AO| < x/2 && |AB| < x/2 ,则L = |AB| <= |AO| + |BO| < 2*(X/2)  = X ,但事实上,L  > X。反正就是不会碰在一起。

结论二:在结论一的前提下,有至少一个点重合的方案是最佳方案。

首先,一开始是n个点,后来是n+m个点,就是说一开始间隔是1/n,后来是1/(n+m)。为了但是用分数看实在不方便,我们取1/(n*(n+m))为基本单位,这样,旧的间隔是n+m,后来的间隔是n。在这里请假设n与n+m互质,如果不是互质,我们就进行约分,原因后来会知道。

然后,我们先假设有新的方案和旧的方案有一个点是重合的,其中红色的线段和点是旧的方案,蓝色的是新的方案;

然后我们沿着这个重合的点,将圆环剪开,注意首尾相同,颜色没有变哦。

你有看出什么东西吗?好吧,如果没有,那么请你看下一步,将AB‘,B’D , DC',C'A线段(即所有的蓝色点间隔形成的线段)重合。

好吧,让我们从代数的角度重新审视一下上面这个东西。设k = n+m,已经保证了k和n是互质的。看下面这段代码。

for(int i=0;i<n;i++)a[i] = i*k%n;
sort(a,a+n);

在这之后,如果你输入数组a的值,你会得到0,1,2,3....n-1,这样的序列。这是由两个数互质的性质决定的,具体证明可以采用反证法。

然后说了那么多,这个和我们的结论有什么关系呢?

既然所有的节点都到了一段区间,我们就不用管它到底是一个圆还是一条线段,从上面那个图上可以直接看出,B应该往左靠,C应该往右靠。

如果在这种情况下,我们选择转动A点,没转动x,A到最近点的距离增大x,B到最近点的距离增大x,C到最近点的距离减少x,B,C的影响抵消,所以总距离增大,这个增大的趋势一直到A,B,C三个点移动到了四等分该线段的时候,

,

然后A的最短距离增大,B,C的最短距离减少,总体距离减少。但是最终再次形成重合的时候,又是一样的。所以先增后尖,最小值在两端。

综合结论1,2该问题圆满解决。也因此获得了上面那个算法,一开始就将所有的关系投影到一个线段上。那个累加ans过程就来自以上分析。

值得一提的是,如果不进行约分的话,在很多情况下,答案也是对的,得益于映射到线段时良好的对称性。但是n和m存在倍数关系就不行了。

 1 #include <stdio.h>
 2 #define min(a,b) (a)>(b)?(b):(a)
 3 int gcd(int a,int b)
 4 {
 5     return b==0?a:gcd(b,a%b);
 6 }
 7 int main()
 8 {
 9     int n,m;
10     while(scanf("%d%d",&n,&m)!=EOF)
11     {
12         int temp=gcd(n,m);
13         n/=temp;
14         m/=temp;
15         int ans=0;
16         for(int i=1;i<n;i++)
17             ans+=min(i,n-i);
18         printf("%.4lf\n",(double)ans/(n*(n+m))*10000);
19     }
20     return 0;
21 }

lrj解法(直接贪心,然后求最小距离(按比例缩小)):

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main()
 4 {
 5     int n,m;
 6     while(scanf("%d%d",&n,&m)!=EOF)
 7     {
 8         double ans=0.0;
 9         for(int i=1;i<n;i++)
10         {
11             double pos=(double)i/n*(n+m);
12             ans+=fabs(pos-floor(pos+0.5))/(n+m);
13         }
14         printf("%.4lf\n",ans*10000);
15     }
16     return 0;
17 }

转载于:https://www.cnblogs.com/ECJTUACM-873284962/p/7203943.html

POJ 3154 Graveyard【多解,数论,贪心】相关推荐

  1. 算法经典“钓鱼”问题详解 基于贪心算法 C语言描述

    算法经典"钓鱼"问题详解 基于贪心算法 初始条件 在一条水平路边,有 n 2 ≤ n ≤ 25个钓鱼池,从左到右编号为1.2.3.--.n.小明有H1 ≤ H ≤ 16个小时的空余 ...

  2. 【贪心】 POJ 1456 Supermarket 详解 (并查集/二叉堆优化)

    题目链接:POJ-1456 题目大意 有n件商品,每件商品的利润为,销售日期的截止时间为(即只能在时间前销售该物品).一天只能销售一件物品.问这n件商品的最大利润为多少 方案一 分析 有两种贪心策略, ...

  3. 《挑战程序设计竞赛》--初级篇习题POJ部分【穷竭搜索+贪心】

    最近看了<挑战程序设计竞赛>初级篇,这里总结一下部分poj上的练习题,主要涉及方面为: 穷竭搜索 and 贪心算法 具体题目: 简单导航 一.穷竭搜索 二.贪心算法 一.穷竭搜索 穷竭搜索 ...

  4. 【POJ - 1328】Radar Installation(贪心+计算几何)安装雷达辐射岛屿

    题干: Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the ot ...

  5. POJ 2586 Y2K Accounting Bug(贪心)

    题目连接:http://poj.org/problem?id=2586 题意:某公司要统计全年盈利状况,对于每一个月来说,如果盈利则盈利S,如果亏空则亏空D.公司每五个月进行一次统计,全年共统计8次( ...

  6. poj 1088 滑雪 详解

    http://poj.org/problem?id=1088 这是一道dp入门题,不过我一直没想明白应该怎么dp.今天,在做自己学校oj的算法基础题时看到这题,标注着dp的分类,加上我一直都比较喜欢做 ...

  7. POJ 1320 Street Numbers 解佩尔方程

    传送门 Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2529   Accepted: 140 ...

  8. CodeForces - 1333F Kate and imperfection(数论+贪心)

    题目链接:点击查看 题目大意:初始时有一个 1 ~ n 共 n 个元素的集合,现在需要选出恰好有 k 个元素的一个子集,使得子集中两两元素进行 gcd 运算后的最大值最小,分别输出 k ∈[ 2 , ...

  9. POJ 1042 Gone Fishing【枚举+贪心】

    题意: 有 n 个池塘,只能从第一个池塘开始走,可以在每个池塘中钓鱼,而且知道了每个池塘每五分钟钓鱼的数量都会下降一定额数值,且从池塘到下一个池塘之间都有一定的距离,知道了每个池塘走到下一个池塘的时间 ...

最新文章

  1. python发邮件包含表格,在Python中在电子邮件正文中包含Excel表
  2. sql语句数据行操作-虽然一般不用
  3. 架构周报:微信后台系统的演进之路
  4. 机器学习你必须知道的几点知识
  5. CH - 0501 货仓选址(中位数)
  6. 食物链 POJ - 1182
  7. 玩ts要注意什么_番禺三维创意拍摄要注意什么
  8. 36岁自学C语言,C语言的数据类型
  9. python的类中包含什么_Python中的类(中)
  10. 队列的基本操作c语言代码大全,数据结构――队列(循环队列)的基本操作(实现链队列逐一取出c语言代码)...
  11. 关于mqtt+js前端中mqtt服务器关闭重连服务器后js前端接收不到消息的问题
  12. 兼容android 11 唤起系统相机拍照并显示
  13. Busting Frame Busting: a Study of Clickjacking Vulnerabilities on Popular Sites
  14. 工信部确定5G使用频段 产业链即将迎来变革
  15. opencv手势识别(2_KNN算法识别)
  16. h5调取摄像头实时显示并点击按钮拍照
  17. 下载本地pdf并压缩为zip文件
  18. 风口猪炒股指标操作复盘 2021-01-20
  19. Android蜘蛛网评分
  20. sality感染型病毒

热门文章

  1. 7-42 行编辑器 (10 分)
  2. oracle安全性规则,[ORACLE ]安全性
  3. 上位机获取单片机发来的数据并进行检验(完整版,附完整源码)
  4. shell 清楚空格_Cygwin系列(五):Shell命令行初体验
  5. fragment怎么获得上下文环境_Flask 源码剖析 (三):Flask 的上下文机制 (上)
  6. java对象布局查看工具_Java 查看对象布局工具 - Java Object Layout
  7. 【Python学习笔记】Coursera课程《Python Data Structures》 密歇根大学 Charles Severance——Week6 Tuple课堂笔记...
  8. 对volley的初步分析第一篇
  9. 【吃炸弹的鸽子UVA10765-双联通模板】
  10. ViewPage最全解析