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 ...
随机推荐
- Spring IOC 容器源码分析 - 循环依赖的解决办法
1. 简介 本文,我们来看一下 Spring 是如何解决循环依赖问题的.在本篇文章中,我会首先向大家介绍一下什么是循环依赖.然后,进入源码分析阶段.为了更好的说明 Spring 解决循环依赖的办法,我 ...
- 【BZOJ3709】 [PA2014]Bohater(贪心)
传送门 BZOJ Solution 考虑如果可以回血肯定要打,那么就是按照伤害值从小到大排个序能打就打,不能打就\(NIE\). 接着看不能够回血的,emmm,把这个过程反着看一下就是打一个怪扣\(a ...
- .gitignore文件常用写法
一般的项目代码中会涉及到密码.key/secret等隐私内容,不适合上传github公开.这时可以使用.gitignore文件来屏蔽这些文件的提交. 你可能用到的写法如下 写法 含义 /build/ ...
- 一些浩辰设置及它的bug?
gcad执行快捷键有问题?尝试修改Setvar("autocompletemode", "19");原因是’输入按键时显示建议列表’这个项打钩了,这里首先捕捉的 ...
- git add .添加不成功
情景: 我首先在一个有许多文件的文件夹中 git init 创建一个git管理仓库 之后 git add . 之后 git commit -m "提交" 发现提交不成功,文件没 ...
- Java Listener中Spring接口注入的使用
在项目中使用Spring通常使用他的依赖注入可以很好的处理,接口与实现类之间的耦合性,但是通常的应用场景中都是Service层和DAO层,或者web层的话, 也是与Strust2来整合,那么如何在Li ...
- Swift5 语言指南(二十) 类型转换
类型转换是一种检查实例类型的方法,或者将该实例视为与其自己的类层次结构中的其他位置不同的超类或子类. Swift中的类型转换是使用is和as运算符实现的.这两个运算符提供了一种简单而富有表现力的方法来 ...
- powerDesigner 把name项添加到注释(comment)
第一次写博客,分享一点经验吧,平时大家用powerDesigner的时候,pd是不会把name项默认添加到comment的,所以生成的数据库表里面也没有中文字段的注释. 我在网上查了一下.有解决方案了 ...
- Java入门-类HelloWorld是公共的,应在名为HelloWorld.java的文件中声明
开始学习java了,搭好环境,notepad++中新建一个java文件,新建一个HelloWorld类, public class HelloWorld { public static void ma ...
- 怎么把excel表格内的数据导入数据库?
第一种方法: 思路:想要把excel表格内的数据直接导入数据库不是那么容易,可以把excel表格另存为.csv格式的文档(特点:内容以逗号分割):然后通过一系列的文档操作函数处理成为一个二维数组,然后 ...