POJ 3154 贪心
来源:互联网 发布:画漫画的软件 编辑:程序博客网 时间:2024/06/10 08:17
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
题意,一开始环形墓地有n个墓碑等距分布,新加入m个墓碑,要求这n+m个墓碑等距分布,问原来n个墓碑的最小移动距离是多少。
思路,原来位置分布为a[n],加入新墓碑后位置分布为b[n],对于没来每个啊a[i]位置,找到它两边的b[j],b[j+1]位置,
sum+=min( a[i]-b[j],b[j+1]-a[i].
#include<cstdio>#include<algorithm>#include<string>#include<cstring>#include<sstream>#include<iostream>#include<cmath>#include<queue>#include<map>using namespace std;int main(){double n,m;cin>>n>>m;double a[2001],b[2001];for(int i=0;i/n<=1;i++){a[i]=i/n*10000;//printf("a[%d]=%.4f\n",i,a[i]);}for(int i=0;1/(n+m)*i<=1;i++){b[i]=i/(n+m)*10000;//printf("b[%d]=%.4f\n",i,b[i]);}double sum=0;int j=0;for(int i=0;i<n;i++){while(!(a[i]>=b[j] && a[i]<b[j+1])){j++;}sum+=min(b[j+1]-a[i],a[i]-b[j]);//printf("%.4f\n",min(b[j]-a[i],a[i+1]-b[j]));}printf("%.4f\n",sum);return 0;}
- poj 3154 Graveyard 贪心
- POJ 3154 贪心
- poj 1017 Packets【贪心】
- POJ 1017 Packets(贪心)
- POJ 1323 简单贪心
- POJ 1065 (贪心)
- POJ 1328 贪心
- POJ 1018 贪心思想
- POJ 1089 贪心算法
- poj 1017 Packets(贪心)
- poj 1017 贪心算法
- poj 1328 贪心
- POJ 3544 贪心
- POJ-1456 Supermarket【贪心】
- poj 1024 贪心、枚举
- Poj 1328 贪心
- POJ 1018 枚举+贪心
- poj 2709 贪心
- FTP和FXP的区别
- web前端js过滤敏感词
- 进程的虚拟地址空间4G划分
- 安卓手册 第六章(开发之EditText)
- 70. Climbing Stairs
- POJ 3154 贪心
- c语言==简介及相关常识(3)
- 经典蓝牙连接-蓝牙音箱或蓝牙耳机
- android学习14#--Handler消息传递机制实例应用
- scrollview嵌套gridview:scrollview不是置顶显示
- [iOS] 文字描边方法
- 关于android报表的制作
- 打电话,发短息,打开网页
- 求两个数的最大公约数