LA3708 Graveyard (暴力)

来源:互联网 发布:安装linux系统分3个区 编辑:程序博客网 时间:2024/05/01 17:25

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 nd 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 le 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.
Note:
Pictures show the rst three examples. Marked circles denote original statues, empty circles denote
new equidistant places, arrows denote movement plans for existing statues.

这道题的意思就是说一个圆上,本来有n个点,现在加上m个点,求原来的点至少要移动多少距离?
我们可以考虑暴力,先求出原来的位置,然后求出加上m之后的位置,两者比较,在后面的位置中找到离原来位置最近的,累加即可。

#include<cstdio>#include<stdlib.h>#include<algorithm>using namespace std;#include<iostream>#include<cstring>#include<cmath>const int maxn=2e3+5;double a[maxn];double dp[maxn];bool used[maxn];int main() {    double n,m;    while(~scanf("%lf%lf",&n,&m)) {        double could=10000.00/(n+m);        double s=10000.00/n;        for(int i=0; i<n; ++i) {            a[i]=i*s;        }        for(int i=0; i<m+n; ++i) {            dp[i]=i*could;        }        memset(used,false,sizeof(used));        double ans=0.00;        for(int i=0; i<n; ++i) {            double t=10000000000.00;            int best=-1;            for(int j=0; j<m+n; ++j) {                if(!used[j]) {                    if(t>fabs(a[i]-dp[j])) {                        best=j;                        t=fabs(a[i]-dp[j]);                    }                }            }            used[best]=true;            ans+=t;        }        printf("%.4lf\n",ans);    }    return 0;}
1 0
原创粉丝点击