POJ 3154 Graveyard (思维)

Graveyard
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1199   Accepted: 610   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 1

sample input #2
2 3

sample input #3
3 1

sample input #4
10 10

Sample Output

sample output #1
1666.6667

sample output #2
1000.0

sample output #3
1666.6667

sample 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


题意:

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

思路:

请仔细看样例。3个样例具有一个共同的特点:有一个雕塑没有移动。如果该特点在所有情况下都成立,则所有雕塑的最终位置(称为“目标点”)实际上已经确定。为了简单起见,我们把没有动的那个雕塑作为座标原点,其他雕塑按照逆时针顺序标上到原点的距离标号,如下图:

注意,这里的距离并不是真实距离,而是按比例缩小以后的距离。接下来,我们把每个雕塑移动到离它最近的位置。如果没有两个雕塑移动到相同的位置,那么这样的移动一定是最优的。


/*************************************************************************
	> File Name: poj3154.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年09月23日 星期一 00时03分40秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif



int main(int argc, char** argv) {
    //read;
    int n,m;
    double ans,pos;
    while( scanf("%d%d",&n,&m) != EOF ) {
        ans = 0;
        for( int i=1; i<=n; i++ ) {
            pos = (double)i / n * (n+m);           //计算每个需要移动的雕塑的座标
            ans += fabs( pos - floor(pos + 0.5) ); //floor返回小于或者等于指定表达式的最大整数
        }
        printf("%.4f\n",ans * 10000 / ( n+m ));    //等比例扩大座标
    }
    return 0;
}


发布了45 篇原创文章 · 获赞 6 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章