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;

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <algorithm>
  11. #include <set>
  12. using namespace std;
  13. typedef long long ll;
  14. typedef unsigned long long Ull;
  15. #define MM(a,b) memset(a,b,sizeof(a));
  16. #define SC scanf
  17. #define CT continue
  18. const int inf = 0x3f3f3f3f;
  19. const double pi=acos(-1);
  20. const int mod=100000000;
  21.  
  22. const int N=5*1e4+5;
  23.  
  24. const double eps=1e-8;
  25. int dcmp(double x)
  26. {
  27. if(fabs(x)<eps) return 0;
  28. else return x>0?1:-1;
  29. }
  30.  
  31. double x[N],sum[N],sum2[N];
  32.  
  33. int main()
  34. {
  35. int cas,n,k;
  36. SC("%d",&cas);
  37. while(cas--)
  38. {
  39. SC("%d%d",&n,&k);
  40. for(int i=1;i<=n;i++) SC("%lf",&x[i]);
  41. sort(x+1,x+n+1);
  42. double ans=-1;
  43. int m=n-k;
  44. if(m<=1) {printf("0\n");CT;}
  45. for(int i=1;i<=n;i++) {
  46. sum2[i]=sum2[i-1]+x[i]*x[i];
  47. sum[i]=sum[i-1]+x[i];
  48. }
  49. for(int i=m;i<=n;i++){
  50. double tmp=0,e=(sum[i]-sum[i-m])/m;
  51. tmp+=m*e*e;
  52. tmp-=2*e*(sum[i]-sum[i-m]);
  53. tmp+=sum2[i]-sum2[i-m];
  54. if(dcmp(ans+1)==0) ans=tmp;
  55. else ans=min(ans,tmp);
  56. }
  57. printf("%.12f\n",ans);
  58. }
  59. return 0;
  60. }

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. Caesar's Legions(CodeForces-118D) 【DP】

    题目链接:https://vjudge.net/problem/CodeForces-118D 题意:有n1名步兵和n2名骑兵,现在要将他们排成一列,并且最多连续k1名步兵站在一起,最多连续k2名骑兵 ...

  2. # 滚动Hash

    滚动Hash 假设字符串\(C=c_1*c_2*...c_m\),定义Hash函数\(H(C)=(C_1*b^{m-1}+C_2*b^{m-2}+...C_m*b^{0})mod\; h\) 从k开始 ...

  3. 第十三章 ZYNQ-MIZ701 TIMER定时器中断

      上篇文章实现了了PS接受来自PL的中断,本片文章将在ZYNQ的纯PS里实现私有定时器中断.每隔一秒中断一次,在中断函数里计数加1,通过串口打印输出. 本文所使用的开发板是Miz701 PC 开发环 ...

  4. 消息服务百科全书——Kafka基本原理介绍

    架构 1.1 总体架构 因为Kafka内在就是分布式的,一个Kafka集群通常包括多个代理. 为了均衡负载,将话题分成多个分区,每个代理存储一或多个分区.多个生产者和消费者能够同时生产和获取消息. 一 ...

  5. WMIC命令的利用技巧

    WMIC扩展WMI(Windows Management Instrumentation,Windows管理工具),提供了从命令行接口和批命令脚本执行系统管理的支持.在WMIC出现之前,如果要管理WM ...

  6. Wannafly挑战赛23

    B. 游戏 大意: $n$堆石子, 第$i$堆初始$a_i$, 每次只能选一堆, 假设一堆个数$x$, 只能取$x$的约数, 求先手第一步必胜取法. SG入门题, 预处理出所有$SG$值. 先手要必胜 ...

  7. docker部署redis

    镜像获取 docker pull redis:4.0 ##当前最新版本 docker images 启动 docker run --name redis-huiyuan -p : -v $PWD/da ...

  8. Qt之QTableWidget

    学习QTableWidget就要首先看看QTableView控件(控件也是有”家世“的!就像研究人一样一样的),因为QTableWidget继承于类QTableView. 两者主要区别是QTableV ...

  9. 基于Hadoop生态SparkStreaming的大数据实时流处理平台的搭建

    随着公司业务发展,对大数据的获取和实时处理的要求就会越来越高,日志处理.用户行为分析.场景业务分析等等,传统的写日志方式根本满足不了业务的实时处理需求,所以本人准备开始着手改造原系统中的数据处理方式, ...

  10. vue中的绑定class和微信小程序中的绑定class的区别

    微信小程序 小程序里面的class与style绑定,遵循HTML特性绑定,有关于HTML绑定.在进行class与style绑定时,可以直接绑定,也可以带上逻辑与,或者三元运算进行条件控制 JS dat ...