【Codeforces725F】Family Photos {贪心}

来源:互联网 发布:华云数据 薪水不行 编辑:程序博客网 时间:2024/06/16 01:36

Description

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

Examples output

1

Examples input

2
5 4 8 8
4 12 14 0

Examples output

4

Examples input

1
0 10 0 10

Examples output

-10


【题目大意】有n堆照片,每堆两张,必须先取上面的照片后才能取下面的。有两个人A、B轮流取照片,这两个人对每张照片的开心度不同,每次可以取也可以跳过,当所有的照片都被取光或者两个人都选择跳过的时候游戏结束。A先手,两人都采取最优策略。问两人最后开心度的最大差值。

【题解】贪心//话说贪心真的难到炸了QAQ

(一)要把当前所有能导致跳过的操作先处理掉;
如果有一堆照片,A、B两人对于上下两张照片的开心度分别为(a1,a2), (b1,b2),此时有以下几种情况:
<1> a1+b1>=a2+b2 → a1-b2>=b1-a2
这种情况下,两人都不会跳过。因为A取的话能增大差值,而B取则能减小差值。这种情况已经满足。
<2> a1+b1< a2+b2
此时又能分成三种情况:
① a1>b2 此时,两人无论是谁先取都不会更优。所以一定会等到只剩下这样的情况时,A无法跳过,否则游戏结束,这种情况对答案的贡献为a1-b2;
② b1>a2 同上可得,这样的情况对答案的贡献为a2-b1;
③ a1< b2 且 b1< a2,这样的照片堆对答案没有贡献,可以直接删除。
经过以上讨论,我们可以处理掉情况<2>。

(二)目前仅剩下a1+b1>=a2+b2 的照片堆。考虑剩下照片对答案的贡献。若照片被A取走,对答案的贡献就是 a,若照片被B取走,对答案的贡献就是 -b。所以考虑将 a+b 从打到小排序,将第奇数张给A,第偶数张给B。即可得到答案。


#include <cstdio>#include <algorithm>#define LL long longLL n,m,ans,t[200005];int main(){    scanf("%lld\n",&n);    for (int i=1;i<=n;++i)    {        LL a,b,c,d;        scanf("%lld%lld%lld%lld\n",&a,&b,&c,&d);        if (a+b>=c+d) t[++m]=a+b,t[++m]=c+d,ans+=a+c;        else if (a>d) ans+=a-d;        else if (b>c) ans+=c-b;    }    std::sort(t+1,t+m+1);    for (int i=1;i<=m;i+=2) ans-=t[i];    printf("%lld\n",ans);     return 0;}

【题外话】贪心。。好难。。

0 0