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. Jquery实现光棒效果

    <script type="text/javascript"> $(function () { var $bac; $("#d1 tr").hove ...

  2. Apache配置文件httpd.conf细说

    1.httpd.conf文件位于apache安装目录/conf下2.Listen 88表示监听端口88 此处可以连续写多个端口监听如下: Listen 88 Listen 809 3.目录配置如下: ...

  3. English trip M1 - PC9 Where am I Teacher:Jade

    In this lesson you will learn to ask for and give directions    # 在本课中,您将学习如何提出要求并给出指示 Words North  ...

  4. English trip -- Review Unit7 Shopping 购物

    Xu言: 今天,lamb老师帮我梳理的时候到时提醒了我件事,之前Jade老师也说过每个单元的课程其实有个大主题,我需要把这个单元上完以后全部好好的回顾,然后整理一下.把每个单元的主题以及主题(them ...

  5. Java使用POI读取和写入Excel指南

    Java使用POI读取和写入Excel指南 做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃 ...

  6. OAF中trunc函数的使用(转)

    原文地址:OAF中trunc函数的使用 需求:在做OAF开发时,经常会需要查询功能,由于需求的不同,往往不能使用OAF标准的查询功能,需要自己客户化实现查询功能,而在查询功能中,经常会遇到查询的时间范 ...

  7. OC MRC之 @property参数(代码分析)

    第一部分 // // main.m // 04-@property参数 // // Created by apple on 13-8-9. // Copyright (c) 2013年 itcast. ...

  8. sql Server如何执行批量插入和批量删除

    平时我们sql server执行查询语句都是通过 insert into 表名(字段名,字段名) values(插入值,插入值) --单条插入语句--- insert into Reader(read ...

  9. POJ 1860 Bellman-Ford算法

    转载链接:http://blog.csdn.net/lyy289065406/article/details/6645778 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币, ...

  10. SQL Server 调优系列玩转篇三(利用索引提示(Hint)引导语句最大优化运行)

    前言 本篇继续玩转模块的内容,关于索引在SQL Server的位置无须多言,本篇将分析如何利用Hint引导语句充分利用索引进行运行,同样,还是希望扎实掌握前面一系列的内容,才进入本模块的内容分析. 闲 ...