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 ;
}

URAL - 1397:Points Game (博弈,贪心)的更多相关文章

  1. ural 1303 Minimal Coverage【贪心】

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...

  2. HDU 4023 (博弈 贪心 模拟) Game

    如果硬要说这算是博弈题目的话,那这个博弈是不公平博弈(partizan games),因为双方面对同一个局面做出来的决策是不一样的. 我们平时做的博弈都是公平博弈(impartial games),所 ...

  3. Codeforces C Match Points(二分贪心)

    题目描述: Match Points time limit per test 2 seconds memory limit per test 256 mega bytes input standard ...

  4. POJ 2505 A multiplication game(找规律博弈/贪心)

    题目链接 #include<iostream> #include<cstdio> using namespace std; typedef long long ll; int ...

  5. URAL 1203 Scientific Conference(贪心 || DP)

    Scientific Conference 之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校.补多校的题目.刷一下一直薄弱的DP.多校假设有计算几何一定要干掉-.- 题意:给 ...

  6. ural 2021 Scarily interesting!(贪心)

    2021. Scarily interesting! Time limit: 1.0 secondMemory limit: 64 MB This year at Monsters Universit ...

  7. Ural 1303 Minimal Coverage(贪心)

    题目地址:Ural 1303 先按每一个线段的左端点排序,然后设置一个起点s.每次都从起点小于等于s的线段中找到一个右端点最大的. 并将该右端点作为新的起点s,然后继续找. 从左到右扫描一遍就可以. ...

  8. URAL 1161 Stripies(数学+贪心)

    Our chemical biologists have invented a new very useful form of life called stripies (in fact, they ...

  9. URAL 1303 Minimal Coverage

    URAL 1303 思路: dp+贪心,然后记录路径 mx[i]表示从i开始最大可以到的位置 sufmx[i]表从1-i的某个位置开始最大可以到达的位置 比普通的贪心效率要高很多 代码: #inclu ...

随机推荐

  1. RabbitMQ入门_04_Exchange & Binding

    如果你比较细心,你会发现 HelloWorld 例子中的 Sender 只申明了一个 hello 队列,然后就开始向默认 Exchange 发送路由键为 hello 的消息.按照之前 AMQP 基本概 ...

  2. 算法笔记--lca倍增算法

    算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...

  3. Google chrome浏览器打不开网页,显示ERR_Failed...等问题的解决方法

    新装好的win7系统,打开Google浏览器,显示网页可能暂时无法连接,或者它已永久性的移动到了新地址.在网络搜索很多资料,发现解决方法如下,亲测成功. 原因,该服务依赖的TCP/IP 协议有问题. ...

  4. cmd 常用操作

    2017-08-24 16:05:28 cd : change directory(目录) 功能:显示当前目录 类型:内部命令 格式:CD[盘符:][路径名][子目录名] 说明:CD命令不能改变当前所 ...

  5. C语言的的free和c++的delete的区别

    首先free对应的是malloc:delete对应的是new:free用来释放malloc出来动态内存,delete用来释放new出来的动态内存空间. 应用的区别为: 1. 数组的时候int *p=( ...

  6. php项目------浏览器导航开发

    最近项目不是很急,把以前做的php项目分享一些给大家,希望对各位有所帮助:很简单的一个项目,本人用来练习ThinPHP框架的. 浏览器导航开发,php+mysql+apache,ThinkPHP3.2 ...

  7. Confluence 6 配置用户目录

    一个用户目录是你存储你的用户和用户组信息的地方.用户信息包括有用户的全名,用户名,密码和电子邮件地址以及其他的一些个人信息. 用户组包括有用户组名字,属于这个用户组的用户和有可能属于这个用户组的另一个 ...

  8. python中的注意事项

    .python 中的 and 从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值: or 也是从左到有计算表达式,返回第一个为真的值: 其中数字 0 是假,其他都是真: 字 ...

  9. MVC 模式——第3章

    在深入到 ASP.NET MVC 框架的细节之间,最好熟悉 MVC 的设计模式及其背后的思想.良好地理解 MVC 背后的内容,有助于在阅读本书的过程中将该框架的特性放到相关的情境之中. 3.2 理解 ...

  10. JS-图片控制-动画管理模块

    animateManage.js ;(function(window,document,undefined){ var _aniQueue = [], //动画队列 --- ani:动画,Queue: ...