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;}新聞熱點
疑難解答