polygon-Graveyard(poj3154)

来源:互联网 发布:angularjs 数组排序 编辑:程序博客网 时间:2024/06/03 17:38

描述:

在一个周长为10000的圆上等距分布着n个点,即这n个点是一个正n边形的顶点。现在要另加m个点到圆上,新加的m个点可以任意选择位置(可以与原有的点重合)。然后将这n+m个点中的一些点延圆周移动,最终使n+m个点均匀分布,即在一个正n+m边形的顶点上。输出最小总移动距离。

输入:

输入两个整数 n, m。 (2≤n≤1000, 1≤m≤1000).

输出:

输出最小总移动距离,保留4位小数。

输入样例:

sample input #12 1sample input #22 3sample input #33 1sample input #410 10

输出样例:

sample output #11666.6667sample output #21000.0sample output #31666.6667sample output #40.0



思路:为方便计算,将圆的周长转换为增加点后每个点之间距离为单位长度


  1. 总周长为n+m,有n+m个点,那么每个点距离长度为1,且分布都在整数点上
  2. 在总长为n+m下,有n个点的时候,位置分别pos =  (n+m)/n*i,i为[0, n-1]
  3. 只需要将pos移动到距离最近的整数点上,floor(pos+0.5),求出移动的距离和ans
  4. 那么长度为10000时,移动距离为ans*10000/(double)(n+m)

第一个点位于0处,pos在两个整数点的中点处,会取偏右侧的整数点,从而保证不存在两个点竞争的情况


#include <iostream>#include <cmath>#include <cstdio>using namespace std;int main(){    int n, m;    double pos, ans;    while(cin >> n >> m)    {        ans = 0;        for(int i = 0; i < n; i++)        {            pos = (double)(n+m)/n*i;    //原位置            ans += abs(floor(pos+0.5)-pos);        }        printf("%.4f\n", ans*10000/(double)(n+m));    }}


0 0
原创粉丝点击