Graveyard(poj3154)
Graveyard
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 1289 | Accepted: 660 | Special Judge |
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
2 1
2 3
3 1
10 10
Sample Output
1666.6667
1000.0000
1666.6667
0.0000
Hint

Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.
ps:http://poj.org/problem?id=3154
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std; double nmin(double x,double y)
{
return x<y?x:y;
}
int main()
{
int n,m,i,j;
double ans,s,o,temp;
double a[],c[];
while(scanf("%d%d",&n,&m)!=EOF)
{
s=1.0/(double)n;
o=1.0/(double)(n+m);
for(i=;i<n;i++)
a[i]=i*s;
for(j=;j<m+n;j++)
c[j]=j*o;
ans=;
for(i=;i<n;i++)
{
for(j=;j<m+n;j++)
{
if(a[i]<=c[j])
break;
}
int loc=j;
temp=fabs(a[i]-c[j]);
// printf("temp = %lf\n",temp);
if(loc>)
temp=nmin(temp,fabs(a[i]-c[loc-]));
if(loc<n+m-)
temp=nmin(temp,fabs(a[i]-c[loc+]));
ans+=temp;
} printf("%.4f\n",ans*);
}
return ;
}
然后下面是大神的代码:
#include<cstdio>
#include<cmath>
using namespace std; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
double ans=;
for(int i=;i<n;i++)
{
double pos=(double)i/n*(n+m); //计算每个需要移动是雕塑的坐标
ans+=fabs(pos-floor(pos+0.5))/(n+m); //累加移动距离
}
printf("%.4f\n",ans*);//等比例扩大坐标
//第一次交是lf。。。结果就错了,可能是精度问题吧。
}
return ;
}
Graveyard(poj3154)的更多相关文章
- 1388 - Graveyard(数论)
题目链接:1388 - Graveyard 题目大意:在一个周长为10000的圆形水池旁有n个等距离的雕塑,现在要再添加m个雕塑,为了使得n + m个雕塑等距离,需要移动一些雕塑,问如何使得移动的总位 ...
- 【贪心】【POJ3154】墓地雕塑(Graveyard, NEERC 2006, LA 3708)需要稍稍加工的(先贪心,再确保能这样贪(可行性&&如果可行必定最优&&非证明最优性)的题)(K)
例题4 墓地雕塑(Graveyard, NEERC 2006, LA 3708) 在一个周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周 ...
- [技术翻译]Guava-libraries(一): 用户指导
用户指导 本文翻译自http://code.google.com/p/guava-libraries/wiki/GuavaExplained,由十八子将翻译,发表于博客园 http://www.cnb ...
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
- Angular2学习笔记(1)
Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...
- ASP.NET Core 之 Identity 入门(一)
前言 在 ASP.NET Core 中,仍然沿用了 ASP.NET里面的 Identity 组件库,负责对用户的身份进行认证,总体来说的话,没有MVC 5 里面那么复杂,因为在MVC 5里面引入了OW ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- Online Judge(OJ)搭建(第一版)
搭建 OJ 需要的知识(重要性排序): Java SE(Basic Knowledge, String, FileWriter, JavaCompiler, URLClassLoader, Secur ...
- 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑
阅读目录 前言 场景1的思考 场景2的思考 避坑方式 实践 结语 一.前言 在上一篇中(如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成),有一行注释的代码: public interfa ...
随机推荐
- linux03
linux day31.正则表达式 \* ------重复0无数次 \+ ------重复1 无数次 ^ -------开头 $ -------结尾 | ------或 & ----- 与 ( ...
- vue项目axios请求接口,后端代理请求接口404,问题出现在哪?
在vue项目中,列表数据需要用到qq音乐接口中的数据,但是直接请求不行,有host及referer限制,需要采用后端代理的方式.借助axios及node的express,在dev-server.js中 ...
- 社会地位即服务, Status as a Service (二): 社交网络的投资回报率 (ROI)
本文作者 Eugene Wei 是 Amazon 战略部门的第一位分析师,后来还担任过 Flipboard, Hulu, Oculus 的产品负责人. 原文标题是 Status as a Servic ...
- SQL面试题之行转列
典型的课程表: mysql> select * from course; +----+------------+----------+------------+ | id | teacher_i ...
- info.plist的选项含义
1. Application does not run in background(鍵名:UIApplicationExistsOnSuspend)自從iOS4.0之後,當你在應用程式執行的時 ...
- rabbitmq系列五 之远程过程调用(RPC)
1.远程过程调用(RPC) 在第二篇教程中我们介绍了如何使用工作队列(work queue)在多个工作者(woker)中间分发耗时的任务. 可是如果我们需要将一个函数运行在远程计算机上并且等待从那儿获 ...
- (转)Python 实现双向链表(图解)
原文:https://blog.csdn.net/qq490691606/article/details/49948263 Python 实现双向链表(图解)双向链表双向链表也叫双链表,是链表的一种, ...
- storm_分组策略
注意1:原始的案例 spout 和bolt都是1个并发 所以文件中30条日志 从spout发出以后 bolt接受到30条
- SSH框架整合详细分析【执行流程】
struts1和spring有两种整合的方法 一种是action和spring bean映射:一种是将action交给spring初始化 第一种方式:访问.do的URL->tomcat接收到r ...
- 使用vue开发微信公众号下SPA站点的填坑之旅
原文发表于本人博客,点击进入使用vue开发微信公众号下SPA站点的填坑之旅 本文为我创业过程中,开发项目的填坑之旅.作为一个技术宅男,我的项目是做一个微信公众号,前后端全部自己搞定,不浪费国家一分钱^ ...