http://acm.hdu.edu.cn/showproblem.php?pid=5073

就是给你 n 个数,代表n个星球的位置,每一个星球的重量都为 1

开始的时候每一个星球都绕着质心转动,那么质心的位置就是所有的星球的位置之和 / 星球的个数

现在让你移动 k 个星球到任意位置(多个星球可以在同一个位置并且所有的星球在同一直线上)

移动之后那么它们质心的位置就可能发生变化,求 I = sum(di^2) (di表示第i个星球到达质心的距离)最小

根据方差的性质(越密越小)得知贪心策略就是,n-k序列一定连续,那么随即求连续n-k序列里面方差最小的那个

设连续长度为k

avg = sum/k;

有 求和(pi - avg)^2 = (求和pi^2)   -  sum^2/k;

然后贪心模拟一遍即可

输入序列式无序的,样例坑到死,不sort或者sum^2/k精度问题WA到死

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%I64d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 5e4+5;
const double INF = 1e20;
int n,k;
LL p[maxn],sum[maxn],sq[maxn];
int main() {
int _;RD(_);while(_--){
RD2(n,k);
p[0] = sum[0] = sq[0] = 0;
for(int i = 1;i <= n;++i)
RD(p[i]);
double ans = INF;
k = n - k;
if(k == 0 || k == 1){
puts("0");
continue;
}
sort(p+1,p+n+1);
for(int i = 1;i <= n;++i)
sum[i] = p[i] + sum[i-1],sq[i] = p[i]*p[i] + sq[i-1];
for(int i = k;i <= n;++i)
ans = min(ans,(double)(sq[i] - sq[i - k])*(double)k - (double)(sum[i] - sum[i-k])*(double)(sum[i] - sum[i-k]));
printf("%.10lf\n",ans/(double)k);
}
return 0;
}

hdu 5073 有坑+方差贪心的更多相关文章

  1. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  2. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  3. 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy

    Galaxy Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...

  4. ACM/ICPM2014鞍山现场赛D Galaxy (HDU 5073)

    题目链接:pid=5073">http://acm.hdu.edu.cn/showproblem.php?pid=5073 题意:给定一条线上的点,然后能够去掉当中的m个,使剩下的到重 ...

  5. HDU 5281 Senior's Gun (贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5281 贪心题目,但是看看我的博客里边相关贪心的题解实在是少的可怜,这里就写出来供大家一起探讨. 题意还 ...

  6. HDU 4952 Poor Mitsui(贪心)

    HDU 4957 Poor Mitsui pid=4957" style="">题目链接 思路:利用相邻交换法去贪心就可以.注意容积为0的情况,这是个坑点 代码: ...

  7. hdu 5073

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 思路:一开始忘了排序,wa了好几发...选择区间长度为N - K的连续的数, 然后其余的K个数都 ...

  8. HDU 5073 Galaxy(2014鞍山赛区现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...

  9. HDU 5813 Elegant Construction (贪心)

    Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

随机推荐

  1. Android.DebugTools.Traceview & dmtracedump

    1. Android 调试工具之Traceview http://www.cnblogs.com/devinzhang/archive/2011/12/18/2291592.html TraceVie ...

  2. nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator

    该问题是少了一个spring-tx-的jar包,把该包加入到buildpath中就行了. 参考链接:http://blog.csdn.net/Rongbo_J/article/details/4666 ...

  3. BZOJ1178或洛谷3626 [APIO2009]会议中心

    BZOJ原题链接 洛谷原题链接 第一个问题是经典的最多不相交区间问题,用贪心即可解决. 主要问题是第二个,求最小字典序的方案. 我们可以尝试从\(1\to n\)扫一遍所有区间,按顺序对每一个不会使答 ...

  4. Codeforces 749D. Leaving Auction set+二分

    D. Leaving Auction time limit per test: 2 seconds memory limit per test:256 megabytes input:standard ...

  5. AX_Args

    Args args; FormRun formRun; ; args = new Args(); args.name(formstr(FormName)); args.caller(); args.r ...

  6. RNA-Seq数据去接头(Adapter)

    1.adapter是一段短的序列已知的核酸链,用于链接序列未知的目标测序片段. 2.barcode,也称为index,是一段很短的寡居核酸链,用于在多个样品混合测序时,标记不同的样品. 3.inser ...

  7. Anaconda 3中配置OpenCV

    平台:win10 x64+Anaconda 3(64-bit)+opencv_python-3.4.5+contrib-cp37-cp37m-win_amd64 一.OpenCV下载 Python环境 ...

  8. Sprign中常用注解

    1.@Component 创建类对象,相当于配置<bean/> 2.@Service 与 @Component功能相同 2.1写在ServiceImpl类上 (建议在ServiceImpl ...

  9. 34、iOS App图标和启动画面尺寸

    注意:iOS所有图标的圆角效果由系统生成,给到的图标本身不能是圆角的. 1. 桌面图标 (app icon) for iPhone6 plus(@3x) : 180 x 180 for iPhone ...

  10. 2018.11.01 bzoj4325: NOIP2015 斗地主(贪心+搜索)

    传送门 原来一直以为是一道大模拟. 没想到是一道搜索+最优性剪枝 如何搜最优呢? 我们考虑怎么最快出完. 大概是应该尽量出当前能出出去最多的吧. 于是我们选择优先出顺子. 这样做有什么好处呢? 我们会 ...