UVA 3708<数学题><在一个集合中挑选出与另外一个集合相同数量的点求两者之前的差的绝对值最小>

来源:互联网 发布:java的if else 编辑:程序博客网 时间:2024/06/08 03:45

这是一道数学题,题意是在10000的圆上面等距分布着n个雕塑,现在又有哪个雕塑要放进去,问原来的雕塑要怎样移动可以使得后加入的能防止进去,同时移动的距离最小,输出这个距离

设原来的位置分别是0,x,2x,3x,4x,,,,,(n-1)x  A*

设后来的位置分别是0,y,2y,3y,4y,,,,,,(n+m-1)y  B*

很明显现在要求的是在B*中挑选出n个数使得与A*中的每一个数的差的绝对值最小,也即求每一个A*中的数在B*中最靠近其的数。这时候就可以用表达式ix/y结果的上取整和下取整求得在A*中第i个数的在B中大小是其附近的数的位序,很明显如果对ix/y进行四舍五入就是离他最近的那个数的位序,但是这时候就会出现一个问题,是否会出现一个问题是否会出现两个A*中的数同时取到了在B*的数呢。事实证明这中情况是不会出现的如果两个点选到了同一个点说明他们两个的距离必定会比y要小,但是n < (n+m),这与条件不符所以这种情况是不会发生的

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <climits>#include <cstdlib>#include <ctime>using namespace std;//ios::sync_with_stdio(false);int main(){int n,m;while(cin >> n >> m){if(m % n == 0){printf("0.0\n");}else{double ans = 0;double x = 10000.0 / n;double y = 10000.0 / (n+m);for(int i = 0; i < n;i++){ans += fabs((i*x) - (int)(i*x/y + 0.5)*y);}if((ans-(int)ans)<1e-6){printf("%.1f\n",ans);}elseprintf("%.4f\n",ans);}}return 0;}
UVALive - 3708

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

Submit Status

Description

Download as PDF

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

0 0