Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_problem&problem=4134
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!
Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.
2 1 2 3 3 1 10 10
1666.6667 1000.0 1666.6667 0.0
给移动之后的点标上坐标序号,然后求移动之前的点的坐标到最近坐标的最小距离和。
体会下面计算k这个值的好处。
完整代码:
/*0.009s*/ #include<cstdio> #include<cmath> int main(void) { double n, m; while (~scanf("%lf%lf", &n, &m)) { double k = 1.0 + m / n, ans = 0.0; for (int i = 1; i < n; ++i) //第一个(标号为0)不用移动 { double pos = k * i; ans += fabs(pos - round(pos)); } printf("%.4f\n", ans * 10000 / (n + m)); } return 0; }