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值并不是按顺序的,,,所以要先排序

最新文章

  1. &lt;&lt;&lt; Java生成Md5
  2. 【javascript基础】3、变量和作用域
  3. JAVA学习Swing绝对局部简单学习
  4. AVR 定点数运算程序设计及数制转换
  5. 自学了三天的SeaJs学习,解决了前端的一些问题,与小伙伴们一起分享一下!
  6. 【JS跨域请求】Ajax跨域请求JSONP
  7. bestcoder单调区间
  8. sql 添加约束
  9. Day1_算法分析方法
  10. IntelliJ&nbsp;IDEA导出Java&nbsp;可执行Jar包
  11. Mybatis 一对一,一对多,多对一,多对多的理解
  12. Floyd算法(最短路)
  13. php curl模拟登陆抓取数据
  14. vue组件-构成组件-父子组件相互传递数据
  15. MVC从Controller到View的呈现
  16. C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出
  17. 现在有两个变量,分别是a = 3, b = 4,那么我们不用第三个变量来调换a和b的值。
  18. pilicat-dfs 霹雳猫-分布式文件系统
  19. POJ 3076 Sudoku
  20. MySQL(二) MySQL基本操作

热门文章

  1. 剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
  2. 内存溢出,内存泄漏,CPU溢出区别
  3. Pycharm 配置houdini
  4. S03_CH03_AXI_DMA_OV7725摄像头采集系统
  5. k8s集群升级
  6. 常用bat文件
  7. JavaScript基本使用
  8. zxx.cms.app 开发中的一些git命令
  9. boost交叉编译
  10. Java BIO、NIO、AIO 基础,应用场景