Good news for us: to release the financial pressure, the government started selling galaxies and we can
buy them from now on! The first one who bought a galaxy was Tianming Yun and he gave it to Xin
Cheng as a present.
To be fashionable, DRD also bought himself a galaxy. He named it Rho Galaxy. There are n stars
in Rho Galaxy, and they have the same weight, namely one unit weight, and a negligible volume. They
initially lie in a line rotating around their center of mass.
Everything runs well except one thing. DRD thinks that the galaxy rotates too slow. As we know,
to increase the angular speed with the same angular momentum, we have to decrease the moment of
inertia.
The moment of inertia I of a set of n stars can be calculated with the formula
I =
∑n
i=1
wi
· d

i
,
where wi
is the weight of star i, di
is the distance form star i to the mass of center.
As DRD’s friend, ATM, who bought M78 Galaxy, wants to help him. ATM creates some black holes
and white holes so that he can transport stars in a negligible time. After transportation, the n stars
will also rotate around their new center of mass. Due to financial pressure, ATM can only transport
at most k stars. Since volumes of the stars are negligible, two or more stars can be transported to the
same position.
Now, you are supposed to calculate the minimum moment of inertia after transportation.
Input
The first line contains an integer T (T ≤ 10), denoting the number of the test cases.
For each test case, the first line contains two integers, n (1 ≤ n ≤ 50000) and k (0 ≤ k ≤ n),
as mentioned above. The next line contains n integers representing the positions of the stars. The
absolute values of positions will be no more than 50000.
Output
For each test case, output one real number in one line representing the minimum moment of inertia.
Your answer will be considered correct if and only if its absolute or relative error is less than 1e-9.
Sample Input

3 2
-1 0 1
4 2
-2 -1 1 2
Sample Output

0.5

题意:有n(n<=5e4)个质点位于一维直线上,现在你可以任意移动其中k个质点,且移动到任意位置,设移动后的中心为e,求最小的I=(x[1]-e)^2+(x[2]-e)^2+(x[3]-e)^2.....(x[n]-e)^2;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define SC scanf
#define CT continue
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int mod=100000000; const int N=5*1e4+5; const double eps=1e-8;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x>0?1:-1;
} double x[N],sum[N],sum2[N]; int main()
{
int cas,n,k;
SC("%d",&cas);
while(cas--)
{
SC("%d%d",&n,&k);
for(int i=1;i<=n;i++) SC("%lf",&x[i]);
sort(x+1,x+n+1);
double ans=-1;
int m=n-k;
if(m<=1) {printf("0\n");CT;}
for(int i=1;i<=n;i++) {
sum2[i]=sum2[i-1]+x[i]*x[i];
sum[i]=sum[i-1]+x[i];
}
for(int i=m;i<=n;i++){
double tmp=0,e=(sum[i]-sum[i-m])/m;
tmp+=m*e*e;
tmp-=2*e*(sum[i]-sum[i-m]);
tmp+=sum2[i]-sum2[i-m];
if(dcmp(ans+1)==0) ans=tmp;
else ans=min(ans,tmp);
}
printf("%.12f\n",ans);
}
return 0;
}

1.对于给出的式子可以发现拆开后,只要预处理出两个前缀和和求出中心e就可以了

2.关键是求出e:可以发现每次移动k个后,对于剩下的m=n-k个,要让这k个I值尽可能小,那么这k个最好是连续的,且移动的k个应放在原来m个的e处,方能保证I值尽可能小,m*e^2-2*e*(x[1]+x[2]+..x[m])+(x[1]^2+x[2]^2+...x[m]^2);可以发现

当e=(x[1]+x[2]...+x[m])/m才能取得最小值,这样可以在O(1)时间求出当前枚举的K的I值

3,。因为输入的x值并不是按顺序的,,,所以要先排序

hdu 5073 Galaxy 数学 铜牌题的更多相关文章

  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 5073 Galaxy(2014acm鞍山亚洲分部 C)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)   ...

  3. hdu 5073 Galaxy(2014acm鞍山亚洲分部 D)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others)   ...

  4. HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)

    Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total S ...

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

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

  6. HDU - 5073 Galaxy(数学)

    题目 题意:n个点,运行移动k个点到任何位置,允许多个点在同一位置上.求移动k个点后,所有点到整体中心的距离的平方和最小. 分析:这题题目真的有点迷...一开始看不懂.得知最后是选取一个中心,于是看出 ...

  7. ACM学习历程—HDU 5073 Galaxy(数学)

    Description Good news for us: to release the financial pressure, the government started selling gala ...

  8. HDU 5073 Galaxy (数学)

    Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

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

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

随机推荐

  1. gcc命令-更新中....

    下载安装MinGW 1.编译c 使用gcc xx.c命令,将文件编译为a.exe.或使用gcc xx.c -o xx.exe命令,将文件编译为xx.exe 2.编译c++ 使用g++ xx.cpp命令 ...

  2. DEDE调用列表页,前台titlelen显示不全问题解决

    一. 问题描述:{dede:list titlelen='100'}标题长度设为100后,但前台调用显示最长却只有60. 二. 产生原因:经过我多度查找,最终导致标题显示不全的原因是在DEDE生数据库 ...

  3. 8.perf top系统性能分析工具

    perf 是一个调查 Linux 中各种性能问题的有力工具. # perf --help  usage: perf [--version] [--help] COMMAND [ARGS]  The m ...

  4. C++反汇编第一讲,不同作用域下的构造和析构的识别

    目录大纲: 1.全局(静态)对象的识别,(全局静态全局一样的,都是编译期间检查,所以当做全局对象看即可.) 1.1 探究本质,理解构造和析构的生成,以及调用方式(重要,如果不想知道,可以看总结.) 2 ...

  5. SimpleDateFormat线程安全问题

    今天线上出现了问题,从第三方获取的日期为 2019-12-12 11:11:11,通过SimpleDateFormat转换格式后,竟然出现完全不正常的日期数据,经百度,得知SimpleDateForm ...

  6. CPU vector operations

    CPU vector operations 原文:https://blog.csdn.net/wangeen/article/details/8602028 vector operations 是现代 ...

  7. Springboot+mybatis+druid 配置多数据源

    项目结构 application.yml配置文件 spring: application: name: service datasource: primary: jdbc-url: jdbc:orac ...

  8. Java基础第一天--继承、修饰符

    继承 继承的概述: 继承是面向对象三大特征之一.可以使得子类具有父类的属性和方法,还可以在子类中重新定义,追加属性和方法. //创建父类 public class Fu{ public void sh ...

  9. 基于SDP的提议/应答(offer/answer)模型简介

    1.引入 在松耦合会议中,会话参数完全由会议创建者来确定,参与者能做的仅仅是根据这些会话参数来加入会议(当然也可以选择不加入).这种情况下,主要要做的就是会话描述,在这里SDP本身就足够了. 但是在更 ...

  10. 对于div里面内容过大根据长度或者宽度进行适配,然后可以滚轮缩放的功能

    在做3000的项目中,因为页面的svg很大,但是做的只是适配电脑,打开肯定是看不全的,要看全就必须进行滚动,可是客户提出了将页面放在电视机上面,用电视输入网址直接访问,这样问题就来了,电视上怎么进行滚 ...