Galaxy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4991    Accepted Submission(s): 1215
Special Judge

Problem Description
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


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
2
3 2
-1 0 1
4 2
-2 -1 1 2
 
Sample Output
0
0.5

题意:

  有n个点,可以删去k个点,使得公式 I 的值最小。

题解:

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
double a[maxn];
double sum[maxn];
double sum_pow[maxn];
void solve()
{
ms(sum, );ms(sum_pow, );
int n, k;scanf("%d%d", &n, &k);
for(int i = ;i<=n;i++) scanf("%lf", &a[i]);
sort(a+, a++n);
for(int i = ;i<=n;i++){
sum[i] = sum[i-] + a[i];
sum_pow[i] = sum_pow[i-] + a[i]*a[i];
}
if(n-k<=){
printf("0.0000000000\n");return;
}
double ans = (double)INF;
for(int i = n-k;i<=n;i++){
double A = (double)(n-k);
double B = -2.0*(sum[i]-sum[i-(n-k)]);
double C = sum_pow[i] - sum_pow[i-(n-k)];
double x = -B/(2.0*A);
// printf("%f %f %f %f\n", A, B, C, x);
// printf("%f\n", A*x*x + B*x + C); ans = min(ans, A*x*x + B*x + C);
}
printf("%.10f\n", ans);
return;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// IOS
int t;scanf("%d", &t);
while(t--)
solve();
return ;
}

最新文章

  1. iOS编译FFmpeg、kxmovie实现视频播放 (转载)
  2. org.hibernate.AssertionFailure:collection[......] was not processed by flush()
  3. php知识案列
  4. ACM : Travel-并查集-最小生成树 + 离线-解题报告
  5. iOS 横竖屏切换(应对特殊需求)
  6. [ubuntu14.04 amd64 ]搜狗拼音輸入法安裝
  7. github常见操作和常见错误!错误提示:fatal: remote origin already exists.
  8. hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)这题有点意思
  9. 理解PHP 依赖注入|Laravel IoC容器
  10. apache 提示You don't have permission to access /test.php on this server.怎样解决
  11. Idea 设置根目录
  12. c# 去除文本的html标签
  13. CSS.01 -- 选择器及相关的属性文本、文字、字体、颜色、
  14. [填坑]树上差分 例题:[JLOI2014]松鼠的新家(LCA)
  15. java:list排序
  16. c# 上传图片到一个外链相册服务器
  17. Mybatis核心配置文件SqlMapConfig.xml
  18. echarts柱状图鼠标移动在柱状图上显示数据给数据添加单位
  19. Protobuf3 语法指南
  20. (网页)logback的使用和logback.xml详解(转)

热门文章

  1. [ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Cannot access nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public) in off
  2. [LeetCode] 30. 串联所有单词的子串
  3. 安装Pycharm(方便编辑代码的IDE(编辑器))以及 使用Pycharm新建项目
  4. 使用pdfjs插件在线预览PDF文件
  5. python3中编码与解码的问题
  6. jQuery学习总结06-插件开发
  7. 第三次作业—Wordcount
  8. hdu2955_Robberies 01背包
  9. 前端错误监控上报公共方法,可在父页面及iframe子页面同时使用
  10. Insomni&#39;hack teaser 2019 - Misc - echoechoechoecho