LA 3708 - Graveyard

原创 2015年07月09日 10:12:16

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$ \le$n$ \le$1000, 1$ \le$m$ \le$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.

\epsfbox{p3708.eps}

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
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const double LEN = 10000;
int main()
{
    int n, m, i, j;
    double l, nl, ans, p1, p2;
    while(scanf("%d%d",&n,&m)!=EOF)
    { 
      ans = 0;
      l = LEN/n;
      nl = LEN/(n+m);
      for(i=1, j=1; i<n; i++)
      {
               while( (l*i - j*nl) * (l*i - (j+1)*nl) > 1e-8)
               {
                      j++;
               }
               p1 = l*i - j*nl;
               p2 = (j+1)*nl - l*i;
             //  cout << p1 << " ***** " << p2 << endl;
               if(p1 < p2)
               {
                   ans += p1;
                   
               }else{
                   ans += p2;
                   
                   
               }
      }
      printf("%0.4f\n", ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LA 3708- Graveyard

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

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

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

【思维】POJ3154 LA3708Graveyard NEERC2006

题目链接 题目描述:在一个周长为10000的圆上,均匀放置nn个雕塑。现要添加mm个雕塑,使得这n+mn+m个雕塑均匀分布。求这nn个雕塑移动的总距离。看看样例说明,感觉有一个雕塑木有移动。接下来证...
  • cqbztsy
  • cqbztsy
  • 2015年08月13日 08:32
  • 329

UVALive 3708 Graveyard

Graveyard Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu ...

UVALive 3708_Graveyard

题目描述: 有个1000步距离的圆环,环上的两点之间的距离是一样的,及环上的点平分周长。题目给出初始点个数和将要加上点的个数,把初始的点调整下位置,然后把要加入点的接上,求总调整距离的最小值。 解题方...

UVaLive 3708 Graveyard (构造法求解最优化问题)

来源:《算法竞赛入门经典训练指南》第1章例题4、UVaLive 3708、POJ3154 这里只给出确实有一个最优解,其中必定有一个墓碑不动.至于其他的内容,网上茫茫多的解释我也想不出什么更有新意...

UVAlive3708 UVA1388 POJ3154 Graveyard【水题】

Graveyard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1715   Acce...

墓碑移动 LA3708(圆上点的移动)

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

ZOJ 3708 Density of Power Network(水题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3708 题意:     题意挺简单的,就是数一下有多少种线路,...

Graveyard C语言

  • 2014年12月01日 13:32
  • 433B
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LA 3708 - Graveyard
举报原因:
原因补充:

(最多只允许输入30个字)