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 The input file contains several test cases, each of them consists of a a line that 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 For each test case, write to the output a line with a single real number — the minimal sum of travel distances of all statues (in feet). The answer must be precise to at least 4 digits after decimal point. Note: 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

【题解】

一定有一个不动。可以想如果他动了,那么可以转回来,移动的长度不变。

蓝书上用了很巧的一个技巧去做。

把总长度看做1,然后分成n分,第i个就在i/n的位置,然后扩大(n + m)

倍,得到总长度(n + m)时第i个所在的位置,移动到整数位的最近距离就是

四舍五入后的数与该数的差,之后除以(n + m),恢复到总长度1

由于原长为10000,最后乘10000即可

不卡精度这样写美滋滋

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} int n,m;
double ans; int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
ans = ;
for(register int i = ;i < n;++ i)
{
double tmp = (double)i / n * (n + m);
ans += fabs(tmp - (int)(tmp + 0.5)) / (n + m);
}
printf("%.4lf\n", ans * );
}
return ;
}

LA1347

UvaLive1347的更多相关文章

随机推荐

  1. Joomla - 模块系统(新建模块、模块类别、自定义模块)

    Joomla - 模块系统,模块配合模板的布局设置.菜单分配.权限分配能创建出一个内容丰富且易于管理的高度自定义前端页面架构 一.新建模块 进入后台,点击顶栏菜单 扩展管理 -> 模块管理 ,进 ...

  2. iOS程序两中启动图方式和一些坑LaunchImage 和 Assets.xcassets(Images.xcassets)

    一.通过LaunchScreen.storyboard 作启动图 1>在LaunchScreen.storyboard中拖拽一个imageView放上启动图片 注意:记得勾选右边的 User a ...

  3. H-ui 前端框架

    H-ui 前端框架 架起设计与后端的桥梁轻量级前端框架,简单免费,兼容性好,服务中国网站. 首个付费版产品 H-ui.admin.Pro,盘他!

  4. python 描述器

    语法简析 一般来说,描述器(descriptor)是一个有”绑定行为”的对象属性(object attribute),它的属性访问被描述器协议方法重写.这些方法是 __get__(). __set__ ...

  5. redux在react项目中的应用

    今天想跟大家分享一下redux在react项目中的简单使用 1 1.redux使用相关的安装 yarn add redux yarn add react-redux(连接react和redux) 2. ...

  6. SPRINGBOOT配置MYSQL,MYBATIS,DRUID

    配置 DRUID连接池 MYSQL数据库 MYBATIS持久层框架 添加依赖 <dependency> <groupId>mysql</groupId> <a ...

  7. sql 2000 or sql2005 数据库日志删除

    数据库日志清理(sql 2000 or sql2005)DUMP TRANSACTION crm WITH NO_LOGBACKUP LOG crm WITH NO_LOGDBCC SHRINKDAT ...

  8. springcloud之配置中心用法

    一.配置文件服务器server端 1.构建server端所需jar <dependencies> <dependency> <groupId>org.springf ...

  9. php表单 - 验证邮件和URL

    PHP - 验证名称 以下代码将通过简单的方式来检测 name 字段是否包含字母和空格,如果 name 字段值不合法,将输出错误信息: $name = test_input($_POST[" ...

  10. Touching segments(数据结构)

    题目链接 Problem Statement Your Maths professor is a very nice guy, but he sometimes comes up with not s ...