Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.

There are n stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks.

Each photo is described by two non-negative integers a and b, indicating that it is worth a units of happiness to Alice and b units of happiness to Bonnie. Values of a and b might differ for different photos.

It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively.

The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has x happiness and Bonnie has y happiness at the end, you should print x - y.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the number of two-photo stacks. Then follow n lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers a1, b1, a2 and b2, each not exceeding 109. a1 and b1 describe the top photo in the stack, while a2 and b2 describe the bottom photo in the stack.

Output

Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.

Examples
Input
2
12 3 4 7
1 15 9 1
Output
1
Input
2
5 4 8 8
4 12 14 0
Output
4
Input
1
0 10 0 10
Output
-10

这题是神贪心。。

考虑一对照片(a,b)--(c,d),其中(a,b)在上

那么对于A,如果他先取得a,那么B会取到d,这样是a-d。否则B先取到b,A再取到c,这样是c-b。如果A要先取,应当是a-d>=c-b的,为了不让B获得更多价值,A才会先取。a-d>=c-b移项得到a+b>=c+d

同样对于B,如果他先取得b,那么A会取到c,这样是b-c。否则A先取到a,B再取到d,这样是d-a。同样的,B要先取应当满足b-c>=d-a。移项变成a+b>=c+d,结果竟然跟A先取的条件一样了

这说明对于a+b>=c+d的照片,A、B都会想要先取以拉开差距。

对于a+b<c+d的,A、B先手取都不利于获得差值,显然都希望对方先取。如果a+d>=b+c的都取完了,大家都不先手取了,那么实际上此时a+b<c+d的先手取还是有一点关系的。

如果a>d,说明A先取还是能拉开一点差距的,虽然不如B先取A再取能拉开更大差距,但是B显然也不可能先取的。

同样的,如果b>c,说明B先取也能拉开一点差距。

这些照片可以在a+b>=c+d的取完之后再考虑取。

最后,如果有a+d<b+c&&a<=d&&b<=c的,显然A和B取了都不优,所以扔掉。

再把一张照片(x,y)做变换成((x+y)/2,(x+y)/2),答案预先加上个(x-y)/2。这样取了A,答案贡献是(x-y)/2+(x+y)/2=x,取了B,答案贡献是(x-y)/2-(x+y)/2=-y。这样每张照片对A和B价值都是一样的,可以排序完A和B轮流去取。要注意除2之后可能有0.5,要用double

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
double p[];
int n,cnt;
double ans;
inline void solve(int a,int b)
{
ans+=(double)(a-b)/;
p[++cnt]=(double)(a+b)/;
}
int main()
{
n=read();
for (int i=;i<=n;i++)
{
LL a=read(),b=read(),c=read(),d=read();
if (a+b>=c+d)
{
solve(a,b);
solve(c,d);
}else
{
if (a>=d)solve(a-d,d-a);
if (b>=c)solve(c-b,b-c);
}
}
sort(p+,p+cnt+,greater<double>());
for (int i=;i<=cnt;i++)
{
if (i&)ans+=p[i];
else ans-=p[i];
}
printf("%.0f\n",ans);
}

cf 725F

cf725F Family Photos的更多相关文章

  1. Say goodbye to my photos&videos

    刚刚得知一个悲惨的消息:虽然2012已经过去了,但是世界末日并未过去.嗯,我不是来严肃的,我是来搞笑的.毕竟,我已经如此伤心了.中午结束考试,下午看了一半的电影然后躺室友的床上睡了一觉,醒来看到阿姨发 ...

  2. iOS Photos.framework框架

    链接: iOS8.0 使用Photos.framework对相册的常用操作 iOS AssetsLibrary和Photos的使用总结: 权限及相册的获取 iOS 开发之照片框架详解 iOS Asse ...

  3. 利用Photos 框架搭建美图秀秀相册选择器

    简介:Photos框架是iOS8.0后推出的一个新的用于对系统相册进行相关操作的,在iOS8.0之前,开发中只能使用AssetsLibrary框架来访问移动设备的图片库.本文中不再对AssetsLib ...

  4. iOS之Photos:访问某个相册通过collectionView显示

    文中相关知识点较多,只记载重点思路,相关部分都有对应注释说明,部分还需要优化,只是工作学习的一种思路. @import AVFoundation; @import Photos;   导入框架 - ( ...

  5. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  6. codeforces Gym 100187H H. Mysterious Photos 水题

    H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  7. JavaScript网站设计实践(五)编写photos.html页面,实现点击缩略图显示大图的效果

    一.photos.html页面,点击每一张缩略图,就在占位符的位置那里,显示对应的大图. 看到的页面效果是这样的: 1.实现思路 这个功能在之前的JavaScript美术馆那里已经实现了. 首先在页面 ...

  8. iOS英文 汉化,如调用相册,相机改“cancel”,“photos”为“取消”,“相机”

    调用系统相册.相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文,纠结半天,最终在info.plist设置解决问题. 只需要改三个地方: 1.plist文件中: 2. ...

  9. Automatically watermark all uploaded photos (给所有上传的相片加水印)

    Hello, This mod automatically watermark all uploaded photos. Price: FREE, enjoy. You will have to ed ...

随机推荐

  1. SQL中的动态语句执行--exec(@sqlstr)

    begin drop table #tmptable declare @money ut_money set @money=1.2345 create table #tmptable ( je ut_ ...

  2. COGS 2688. 鱼的感恩

    ★   输入文件:fool.in   输出文件:fool.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 从前有一个渔夫抓到了一条特别的鱼,放走了. 渔夫再次抓到了这条 ...

  3. SAP成都研究院飞机哥:程序猿和飞机的不解之缘

    今天的文章来自Jerry的老同事张航. 张航和Jerry一样于2007年毕业后加入SAP成都研究院工作至今.进入SAP后的第一个开发部门是SAP Business by Design Infrastr ...

  4. SQLite连接

    SQLite -连接 SQLite的联接子句用于从数据库中的两个或多个表合并的记录.JOIN是用于通过使用共同的每个值从两个表结合域的装置. SQL定义了三个主要类型的连接: CROSS JOIN I ...

  5. DRBD+NFS+Keepalived高可用环境

    1.前提条件 准备两台配置相同的服务器 2.安装DRBD [root@server139 ~]# yum -y update kernel kernel-devel [root@server139 ~ ...

  6. 多线程下载命令--axel

    axel是轻量级多线程下载工具,对比传统下载工具curl和wget,下载速度上提升不少 centos安装如下 yum install -y axel 命令行用法 axel [option] url1 ...

  7. Php教程

    第一部:PHP基础部分(131集,发布完毕) 讲html与PHPt基础,PHP环境搭建,与留言本编写. 下载地址:① HTML视频[2014新版] http://pan.baidu.com/s/1ve ...

  8. 欧几里得(辗转相除gcd)、扩欧(exgcd)、中国剩余定理(crt)、扩展中国剩余定理(excrt)简要介绍

    1.欧几里得算法(辗转相除法) 直接上gcd和lcm代码. int gcd(int x,int y){ ?x:gcd(y,x%y); } int lcm(int x,int y){ return x* ...

  9. ubuntu命令行卸载并清理软件

    卸载软件,可以使用下面这两种方式之一: sudo apt-get remove --purge [software name] sudo apt-get autoremove --purge [sof ...

  10. Idea 搭建Maven--web项目(MVC)

    小编最近正在学习使用MVC框架,在搭建Maven的项目过程中,遇到了很多问题,上网搜了很多材料才找到答案,为了小编以后查起来方便,也为了向广大小伙伴分享,写了这部片博文,敬我昨天一天的学习结果! 步骤 ...