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. 微信公众号 SVG长按互动

    <section class="" style="display: block;width: 100%;height:667px;overflow:hidden;m ...

  2. 转-VS2010常用功能使用介绍

    原文链接:http://www.jizhuomi.com/software/27.html 1.几个基础概念 在讲VS2010之前先讲下程序开发过程中的几个基本概念:源程序.目标程序和翻译程序. 源程 ...

  3. MS-coco数据集下载及使用(转)

    先做个标记,改天研究下. 几个链接: MS coco数据集介绍及下载 Microsoft COCO 数据集 COCO Dataset 数据特点 COCO 数据集的使用

  4. L2-006 树的遍历 (层序遍历)

    根据访问根节点与左右子树的先后顺序,二叉树一般有三种遍历方式:先序遍历.中序遍历和后序遍历. 只要给定中序遍历序列与先序或后序中的一种,可以还原二叉树结构.学习数据结构课程时,一直都只会手动构建还原二 ...

  5. 2019牛客暑假多校赛(第二场) F和H(单调栈)

    F-Partition problem https://ac.nowcoder.com/acm/contest/882/F 题意:输入一个数n,代表总共有2n个人,然后每个人对所有人有个贡献值,然后问 ...

  6. layui -关闭窗口方法

    1.关闭当前窗口 var index=parent.layer.getFrameIndex(window.name); //获取当前窗口的nameparent.layer.close(index); ...

  7. Pandas对于CSV的简单操作

    Pandas对于CSV的简单操作 最近在研究pandas对于csv文件的读取以及一些操作,网上的信息比较乱,写篇博客记录一下,毕竟自己写的才是最适合自己的用法. 首先我们应该都知道,pandas是一个 ...

  8. 12_PCA之探究用户对物品类别的喜好细分降维

    案例: 探究:用户对物品类别的喜好细分降维. 背景:把用户分成几个类别,分类的依据是用户购买了哪些物品. 先看商品products.csv数据,有product_id,product_name,ais ...

  9. 构建HBase二级索引

  10. Mahout In Action-第一章:初识Mahout

    1. 初识Mahout 本章涵盖以下内容: Apache Mahout是什么? 现实中推荐系统引擎.聚类.分类概述 配置mahout 读者可能从本书的标题中猜测到,本书是一本讲解如何将mahout应用 ...