codeforces 725F

来源:互联网 发布:mac两倍速度播放器 编辑:程序博客网 时间:2024/06/06 01:54

F. Family Photos

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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

题目大意:

有n对照片,A和B轮流取,获得不同的喜悦值,每对照片只有当第一张照片被取走后才能取第二张,当轮到一个人时,他可以选择不取,当所有照片都被取完或当连续两个人都不取时,游戏结束。A和B都希望自己的喜悦值和对方的喜悦值差值最大,假设两人都采用最佳策略,求A和B的喜悦值的差值。

解题思路:

分三种情况:
1.a1<=b2&&b1<=a2,负收益可忽略
2.a1>b2或b1>a2,且a1+b1<=a2+b2;保证正收益选择先手
3.其余情况,物品价值a1+b1(第二轮a2+b2),优先队列贪心

#include<iostream>#include<queue>using namespace std;typedef long long LL;int n;struct node{    int a1, b1, a2, b2;    int tag;};bool operator< (const node& a, const node& b){    return a.a1+a.b1<b.a1+b.b1;//大的先出队}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    priority_queue<node> pq;    while(cin>>n)    {        while(!pq.empty())        pq.pop();        LL ans=0;        int a1, b1, a2, b2;        for(int i=1;i<=n;i++)        {            cin>>a1>>b1>>a2>>b2;            if(a1<=b2&&b1<=a2) continue;            else if(a1+b1<a2+b2)            {                if(a1>b2) ans+=a1-b2;                else ans+=a2-b1;            }            else            {                node tmp;                tmp.a1=a1, tmp.b1=b1, tmp.a2=a2, tmp.b2=b2;                tmp.tag=1;                pq.push(tmp);            }        }        int cnt=0;        while(!pq.empty())        {            cnt++;            node tmp=pq.top();            pq.pop();            if(cnt%2)            {                ans+=tmp.a1;            }            else ans-=tmp.b1;            if(tmp.tag==1)            {                tmp.tag=2;                tmp.a1=tmp.a2;                tmp.b1=tmp.b2;                tmp.a2=0;                tmp.b2=0;                pq.push(tmp);            }        }        cout<<ans<<endl;    }    return 0;}
1 0