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
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
Output
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
- UVA 3708<数学题><在一个集合中挑选出与另外一个集合相同数量的点求两者之前的差的绝对值最小>
- 在数组中取一个位置,让这个位置之前的树的和与之后的和的差绝对值最小
- 证明两个集合的划分最小绝对值差问题
- 求一个排序数组中绝对值最小的数
- 求一个数组差的绝对值的最小值
- 编程题--任意给出一个四位数, 把它重新组成一个四位的最大数和一个最小数, 算出两者间的差
- 任意给出一个四位数, 把它重新组成一个四位的最大数和一个最小数, 算出两者间的差.
- 求一个集合的子集
- 一个工具类-----集合的交并差
- 求一个数的绝对值
- 求一个数的绝对值
- 求一个数的绝对值
- 求一个数的绝对值
- 百度面试题:求一个已排序的数组中绝对值最小的元素
- 求一个已排序的数组中绝对值最小的元素
- 百度面试题:求一个已排序的数组中绝对值最小的元素
- 已知元素从小到大排列的两个数组x[]和y[],请写出一个程序算出两个数组彼此之间差的绝对值中最小的一个,这叫做数组的距离
- 有3个集合, 从其中一个集合中删除同时存在于另外两个集合的元素
- Linux内存:内存管理的实质
- C语言中变长数组的使用方法
- Duilib学习笔记《05》— 消息响应处理
- 【Android知识点精讲】(9)WebView
- Duilib学习笔记《06》— 窗体基类WindowImpBase
- UVA 3708<数学题><在一个集合中挑选出与另外一个集合相同数量的点求两者之前的差的绝对值最小>
- __weak & __strong
- Duilib学习笔记《07》— 资源加载
- 桌面图标打不开问题
- WiresShark使用初探
- Duilib扩展《01》— 双击、右键消息扩展
- centos6.0安装中文输入法
- Android——4.2 - 3G移植之路之 AT 通信 (四)
- CUDA Thread Block