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 server 数据库基础语句

    上篇介绍的是鼠标操作 遗漏两个知识: 主外键 两个列 数据类型 必须一致    //int类型不能约束nvarchar 类型      varchar类型不能约束nvarchar类型 varchar( ...

  2. 生鲜o2o配送应用系统,包括Android源码+SSH带后台管理系统

    前台功能划分    我的 登录            账户+密码 注册            订单管理 查看/删除(显示订单详情)                支付(提交订单)           ...

  3. COFF文件格式

    链接器 目录 一 COFF-Common Object File Format-通用对象文件格式... 3 COFF的文件格式与结构体... 4 文件头... 5 numberOfSections(区 ...

  4. adhoc无法下载应用程序 此时无法安装-解决

    解决方法 点击xcode,进入build setting,选择code signing,provisioning profile选择automatic 或者选择adhoc的provisioning p ...

  5. C# 重写(override)和覆盖(new)

    重写 用关键字 virtual 修饰的方法,叫虚方法.可以在子类中用override 声明同名的方法,这叫“重写”.相应的没有用virtual修饰的方法,我们叫它实方法.重写会改变父类方法的功能.   ...

  6. Maven01

    1. Maven简单介绍 Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念.  作用:完成项目的相关操作,如:编译,构建,单元测试,安装,网站生成和基于Maven部 ...

  7. html中常见符号的代码表示

    HTML中空格的集中代码表示: HTML中空格   不断行的空白(1个字符宽度)     半个空白(1个字符宽度)     一个空白(2个字符宽度)     窄空白(小于1个字符宽度)   其他常见的 ...

  8. 搭建SSI开发框架原理

    Spring2.5.Struts2.Ibatis开发框架搭建(一) ssi, ibatis 一.框架下载 1.1   Struts2框架 Struts2框架发展于WebWork,现在捐献给了Apach ...

  9. (1)JSTL的13个core标签库

     标准标签库JSTL的全名为:Java Server Pages Standard Tag Library. (jsp  standard tag library) JSTL主要提供了5大类标签库: ...

  10. XML解析(二) SAX解析

    XML解析之SAX解析: SAX解析器:SAXParser类同DOM一样也在javax.xml.parsers包下,此类的实例可以从 SAXParserFactory.newSAXParser() 方 ...