Two students are playing the following game. There are 2· n points on the plane, given with their coordinates ( xiyi). Each move player paints the point with his own color (first with white, second with black). The first student makes odd moves, second student makes even moves. When all points are painted (each student made n moves), the game finishes. Each student gets amount of points (real number) that equals to the sum of all distances among pairs of points, colored with his color. Student who get more points becomes a winner. The students play optimally. Find and print the difference between points amount of winner and looser.

Input

Contains multiple test cases. The first line of each case contains positive integer number n ( n ≤ 500). Next 2· n lines contain points' coordinates ( x 1, y 1), ( x 2, y 2), …, ( x 2n, y 2n).

Output

For each test case output the difference between the points of winner and looser. Output the difference with three digits after decimal point.

Example

input output
2
0 0
0 1
1 0
1 1
2
0 0
1 0
0 3
1 5
0.000
1.937

思路:之前遇到了类似的题。由于这样的题比较抽象,当时我是陷入了矛盾的,A既要让自己越大越好,又要使B越小越好(B同理),这两个标准会不会又矛盾呢,该遵循哪一个呢。

此题可以推出来,二则在使自己大的时候,也约束 了对方更小。

val A= Sum( distant(pi,pj) ) { i<j && i,j belong to A}   - Sum( distant(pi,pj) ) {i<j && i,j belong to B}

= Sum( distant(pi,pj) ) {i,j belong to Q}  - Sum( distant(pi,pj) ) { i belong to B && j belong to Q}

(其中Q是全集。

我们令dis是点到其他所有点的距离之和,那么A和B都按照dis从大到小选,既能要自己更大,也能让对方更小。

(下次再遇到这样的题,就用公式,把他们的标准统一就好了(如果可以的话)。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
double x[maxn],y[maxn],dis[maxn],ans;int p[maxn];
bool cmp(int w,int v){ return dis[w]>dis[v]; }
double dist(int u,int v){
return sqrt((x[u]-x[v])*(x[u]-x[v])+(y[u]-y[v])*(y[u]-y[v]));
}
int main()
{
int N;
while(scanf("%d",&N)==){ N<<=;
for(int i=;i<=N;i++) scanf("%lf%lf",&x[i],&y[i]);
for(int i=;i<=N;i++) p[i]=i;
for(int i=;i<=N;i++){
dis[i]=.;
for(int j=;j<=N;j++) dis[i]+=dist(i,j);
}
sort(p+,p+N+,cmp); ans=;
for(int i=;i<=N;i++){
double tmp=0.0;
for(int j=i+;j<=N;j+=) tmp+=dist(p[i],p[j]);
if(i&) ans+=tmp; else ans-=tmp;
}
printf("%.3lf\n",ans);
}
return ;
}

最新文章

  1. 巡检脚本OS+Oracle
  2. 移动web app开发必备 - zepto事件问题
  3. 利用jQuery和CSS实现环形进度条
  4. rsync排除文件同步
  5. ASP.NET中gridview获取当前行的索引值
  6. ACM 素数
  7. WPF笔记
  8. 【原】android本地推送
  9. 【从0到1】android网络框架的选型参考
  10. egrep 查找IP
  11. UVA 10294 等价类计数
  12. Java - 选择性排序 PHP || Java 代码对比
  13. Eclipse用法和技巧二十六:浅谈快捷键
  14. Learning Cocos2d-x for WP8(4)——中文显示
  15. Linux环境Perl链接MS Sql Server数据库
  16. Android开发模板代码(一)——简单打开图库选择照片
  17. 根据展示文字自适应 cell 高度,实现点击cell的伸缩扩展
  18. Eureka的工作原理以及它与ZooKeeper的区别
  19. 使用c++如何实现在gRPC中传输文件
  20. if-else(职责链)

热门文章

  1. Android AlarmManager 的使用
  2. Android仿QQ微信开场导航以及登陆界面
  3. Codeforces 595B - Pasha and Phone
  4. LncRNA
  5. 『Scrapy』爬取斗鱼主播头像
  6. python-day67--MTV之Template
  7. Netty高性能编程备忘录(上)
  8. ReactJS环境搭建
  9. Oracle连接知识
  10. 推荐十款java开源中文分词组件