Galaxy

Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5073


Mean:

在一条数轴上,有n颗卫星,现在你可以改变k颗卫星的位置,使得剩下的n-k颗卫星到某个点(不固定)的距离的平方和最小。

抽象成数学语言后等价于:数轴上有n个点,现在去掉k个点,使得剩下的n-k个点的方差最小,求方差*n的值。

analyse:

一道让人很容易想偏的数学题。首先说一下我的思路:

1)我们最终的目的是让这n-k个点尽量的集中,所以去掉的这k个点必须是位于两边的点(想不通的请自行补脑);

2)剩下的事情就是枚举两边的数量了,但是在枚举这一步,怎样才不超时呢?咳咳,这题的关键来了。

我们可以先来推一下公式:

设Fn为这n个数的方差,d为这n个数的平均数,那么:

Fn=[(x1-d)^2+(x2-d)^2+......(xn-d)^2]/n;

 =[sum(xi^2)+n*d*d-2*d*sum(xi)]/n;

根据这个公式来枚举前后个数就简单多了,详见代码。

Time complexity: O(n)

Source code: 

//  Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-11-16-22.59
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define LL long long
using namespace std;
#define N 50000+10
double a[N],sum1[N],sum2[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
memset(sum1,0,sizeof sum1);
memset(sum2,0,sizeof sum2);
double tmp1,tmp2;
tmp1=tmp2=0.0;
for(int i=1;i<=n;++i)
scanf("%lf",&a[i]);
sort(a+1,a+1+n);
for(int i=1;i<=n;++i)
{
sum1[i]=sum1[i-1]+a[i];
sum2[i]=sum2[i-1]+a[i]*a[i];
}
if(n==k)
{
puts("0.0000000000000");
continue;
}
int m=n-k; /**< 需要选的人数 */
double d;
double tmp;
int sta,en;
double res=1000000000000000000.0;
// cout<<setprecision(10)<<res<<endl;
for(int i=1;i+m-1<=n;++i)
{
sta=i;
en=i+m-1;
d=(sum1[en]-sum1[sta-1])/m;
tmp=(sum2[en]-sum2[sta-1])+m*d*d-2*d*(sum1[en]-sum1[sta-1]);
if(tmp<res)
{
res=tmp;
}
}
printf("%.9lf\n",res);
}
return 0;
}

  

2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy的更多相关文章

  1. dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku

    Hatsune Miku Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5074 Mean: 有m种音符(note),现在要从 ...

  2. 2014 Asia AnShan Regional Contest --- HDU 5078 Osu!

    Osu! Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5078 Mean: 略. analyse: 签到题,直接扫一遍就得答 ...

  3. HDU 5073 Galaxy 2014 Asia AnShan Regional Contest 规律题

    推公式 #include <cstdio> #include <cmath> #include <iomanip> #include <iostream> ...

  4. HDU 5074 Hatsune Miku 2014 Asia AnShan Regional Contest dp(水

    简单dp #include <stdio.h> #include <cstring> #include <iostream> #include <map> ...

  5. hdu5071 2014 Asia AnShan Regional Contest B Chat

    模拟题: add的时候出现过的则不再添加 close的时候会影响到top rotate(Prior.Choose)的时候会影响到top /*============================== ...

  6. hdu5072 2014 Asia AnShan Regional Contest C Coprime

    最后一次参加亚洲区…… 题意:给出n(3 ≤ n ≤ 105)个数字,每个数ai满足1 ≤ ai ≤ 105,求有多少对(a,b,c)满足[(a, b) = (b, c) = (a, c) = 1] ...

  7. 2014 ACM-ICPC Asia Anshan Regional Contest(Online Version)

    题目I - Osu! - HDU 5078 题目分析:最水的一道题吧,求两点间的距离和时间差值的最大比值 #include<stdio.h> #include<math.h> ...

  8. UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  9. UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

随机推荐

  1. Infer初体验 for Android

    安装Python Infer下载地址:https://github.com/facebook/infer 配置环境变量 vi ~/.bash_profile #infer export PATH=${ ...

  2. 使用 jackson 解析 json 演示样例

    首先须要下载3个包,下载地址在Github FasterXML,这三个核心模块各自是: Streaming ("jackson-core") defines low-level s ...

  3. 提示Can't load package:dclite70.bpl解决方法

    64位系统安装Delphi7提示Can’t load package:dclite70.bpl 2015年04月05日 ⁄ 杂谈 ⁄ 共 392字 ⁄ 字号 小 中 大 ⁄ 暂无评论 ⁄ 阅读 1,7 ...

  4. IOS , plist 配置项说明

    本文转载至 http://blog.csdn.net/fengsh998/article/details/8307424 Key:Application can be killed immediate ...

  5. Linux和UNIX监控

    Linux和UNIX上的数据库监控工具包括监控CPU.内存.磁盘.网络.安全性和用户的监控工具.下面罗列了我们找到的有用工具及其简单描述. ps           显示系统上运行的进程列表 top ...

  6. Could not find the Visual SourceSafe Internet Web Service connection information for the specified database Would you like to launch the Visual sourceSafe connection wizard?

    今天同事遇到个奇怪问题,以前也遇到过,不过重新绑定一下就OK了,不知道为什么今天不行了. 错误提示:===============================================Cou ...

  7. 安装cvxpy遇到的问题与解决方案(ubuntu14.10,python 2.7.8)

    应该说,cvxpy的安装说明是很棒的,一步一步非常清楚,www.cvxpy.org/en/latest/install/index.html 可是,我照着做完之后,还是不能import cvxpy,不 ...

  8. CLR via C#深解笔记一 - CLR & C# 基础概念

    写在前言   .Net Framework并不是Win 32 API 和COM上的一个抽象层.   某种程度上,它是自己的操作系统,有自己的内存管理器,自己的安全系统,自己的文件加载器,自己的错误处理 ...

  9. 在使用sqlite时淌过的坑

    以前一直用sqlite.net 1.0.66.0版本,在.net4下面程序写好了部署到目的地机器时winform程序总是出现缺少运行时的问题.有时装了运行时也还是出问题,后来发现是混合模式的问题,当时 ...

  10. 【eoe 6】ActionBar的使用

    一. Action Bar 一般位于屏幕顶部,包括四个可操作区域: 应用图标或LOGO区域,用于视图控制的Spinner下拉菜单或TAB控件区域, Action button(也称为Action It ...