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. Java字节流实现文件夹的拷贝

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  2. xhr文件类型说明

    2017-08-08 18:31:08 xhr : XML HTTP Request,是一种在后台与服务器进行交互的数据.这意味着可以在不加载整个网页的情况下,对网页中的部分内容进行更新. 这是Aja ...

  3. python-GUI,生成ssn

    第一次做这个, 样子有点丑,主要是实现功能,做测试的时候,经常要用到身份证号.手机号.姓名等,这里先生成ssn,后续研究怎么做成客户端 代码: from tkinter import * from u ...

  4. English trip -- Review Unit 9 Daily living 日常生活

    主要讲了一个时态:现在进行时   Be动词+Ving  需要记住的有6种规律 1.直接单词后面 + ing    e.g.     watch -> watching 2.是ie结尾的单词,变y ...

  5. Confluence 6 安装 Active Directory 证书服务器

    如果证书服务器已经安装了的话,跳过这一步骤,直接进入下一步.下面步骤中的屏幕截图是从 Windows 2008 服务器版上安装的截图,针对 2000 和 2003 安装过程是一样的. 作为系统管理员登 ...

  6. bzoj2595: [Wc2008]游览计划 斯坦纳树

    斯坦纳树是在一个图中选取某些特定点使其联通(可以选取额外的点),要求花费最小,最小生成树是斯坦纳树的一种特殊情况 我们用dp[i][j]来表示以i为根,和j状态是否和i联通,那么有 转移方程: dp[ ...

  7. hdu-6301-贪心

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. ASP.NET的内置对象 —— Response 对象

    在 ASP.NET 中,页面实际上是一个输出 HTML 的可执行对象. 传统的 ASP 程序通常将 ASP 代码和 HTML 标签集成在一个文件中,或者将其分离于多个文 件. 当执行文件时,服务器回从 ...

  9. 阿里云ECS安装最新版本Node.js

    原文  http://www.w3ctech.com/topic/1610 主题 Node.js操作系统服务器 我的ECS实例是Ubuntu操作系统,直接使用 apt-get install node ...

  10. SQL调优(SQL TUNING)之远程支持完成性能大幅优化

    前几天,一个朋友找到我,说一个SQL性能有问题,看看能不能优化,下面为过程: 雪豹 9:35:10 在吗 兰花岛主 15:07:39 忙忘了,有事儿? 雪豹 15:07:49 嗯 雪豹 15:07:5 ...