1031: Graveyard
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 178 Solved: 92
[ Submit][ Status][ Web Board]
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
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<=n<=1000, 1<=m<=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.
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
Sample Output
HINT
Source
初始n个石柱在一个圆中,相邻距离相等,加m个石柱进去要求达到n+m个石柱相邻距离相等,求最小移动距离。
相当于摆n+m个石柱在一个圆中,n+m个石柱有确定的位置,设最上方石柱为起始的石柱,且位置为0,接下来
的石柱位置就是10000.0/(n+m),由于初始有n个石柱了,且位置如上定义,对于第i个石柱总是要移动(在原地移动
也假设为移动)
问题转化为何时移动第i个石柱最优。显然在摆这n+m石柱的过程中,假定当前摆的是第j个石柱,那么在n个石柱中,
总有一个没有移动的石柱与其最近,那么是否要移动这个石柱去摆第j个石柱,或是直接放上去一个石柱,决定在于
这个石柱是否能够和第j+1个石柱更近,因为如果更近的话显然让它移动到第j+1个石柱的位置更好。
u#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define pi acos(-1)
int main(){
int n, m;
while (~scanf("%d%d", &n, &m)) {
double disn = 10000.0 / n;
double dism = 10000.0 / (n + m);
double s = 0;
double t = 0;
double sum = 0;
m = n + m;
while (m--) {
if (fabs(s - t) < fabs(s - (t + dism))) {
sum = sum + fabs(s - t);
s = s + disn;
}
t += dism;
}
printf("%.4lf\n", sum);
}
return 0;
}