POJ 3154 Graveyard【多解，数论，贪心】

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## POJ 3154 Graveyard【多解，数论，贪心】

angel_kitty 2017-07-19 09:17:00 浏览872

## Graveyard

 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1707 Accepted: 860 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

然后我们沿着这个重合的点，将圆环剪开，注意首尾相同，颜色没有变哦。

你有看出什么东西吗？好吧，如果没有，那么请你看下一步，将AB‘，B’D , DC'，C'A线段（即所有的蓝色点间隔形成的线段）重合。

```for（int i=0;i<n;i++）
a[i] = i*k%n;
sort（a,a+n）;```

,

``` 1 #include <stdio.h>
2 #define min(a,b) (a)>(b)?(b):(a)
3 int gcd(int a,int b)
4 {
5     return b==0?a:gcd(b,a%b);
6 }
7 int main()
8 {
9     int n,m;
10     while(scanf("%d%d",&n,&m)!=EOF)
11     {
12         int temp=gcd(n,m);
13         n/=temp;
14         m/=temp;
15         int ans=0;
16         for(int i=1;i<n;i++)
17             ans+=min(i,n-i);
18         printf("%.4lf\n",(double)ans/(n*(n+m))*10000);
19     }
20     return 0;
21 }```

lrj解法(直接贪心，然后求最小距离(按比例缩小))：

``` 1 #include <stdio.h>
2 #include <math.h>
3 int main()
4 {
5     int n,m;
6     while(scanf("%d%d",&n,&m)!=EOF)
7     {
8         double ans=0.0;
9         for(int i=1;i<n;i++)
10         {
11             double pos=(double)i/n*(n+m);
12             ans+=fabs(pos-floor(pos+0.5))/(n+m);
13         }
14         printf("%.4lf\n",ans*10000);
15     }
16     return 0;
17 }```

angel_kitty
+ 关注