时限:1000ms 内存限制:10000K 总时限:3000ms
描述:
在一个周长为10000的圆上等距分布着n个点,即这n个点是一个正n边形的顶点。现在要另加m个点到圆上,新加的m个点可以任意选择位置(可以与原有的点重合)。然后将这n+m个点中的一些点延圆周移动,最终使n+m个点均匀分布,即在一个正n+m边形的顶点上。输出最小总移动距离。
输入:
输入两个整数 n, m。 (2≤n≤1000, 1≤m≤1000).
输出:
输出最小总移动距离,保留4位小数。
输入样例:
sample input #1
2 1
sample input #2
2 3
sample input #3
3 1
sample input #4
10 10
输出样例:
sample output #1
1666.6667
sample output #2
1000.0
sample output #3
1666.6667
sample output #4
0.0
图对应前3个样例
思路:为方便计算,将圆的周长转换为增加点后每个点之间距离为单位长度
- 总周长为n+m,有n+m个点,那么每个点距离长度为1,且分布都在整数点上
- 在总长为n+m下,有n个点的时候,位置分别pos = (n+m)/n*i,i为[0, n-1]
- 只需要将pos移动到距离最近的整数点上,floor(pos+0.5),求出移动的距离和ans
- 那么长度为10000时,移动距离为ans*10000/(double)(n+m)
第一个点位于0处,pos在两个整数点的中点处,会取偏右侧的整数点,从而保证不存在两个点竞争的情况
程序:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
double abs(double x , double y)
{
if(x > y)
{
return x-y;
}
else
{
return y-x;
}
}
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));
}
}
样例有问题……另外G++才能AC,C++会WA……