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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章