zoukankan      html  css  js  c++  java
  • cf725F Family Photos

    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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<deque>
     9 #include<set>
    10 #include<map>
    11 #include<ctime>
    12 #define LL long long
    13 #define inf 0x7ffffff
    14 #define pa pair<int,int>
    15 #define mkp(a,b) make_pair(a,b)
    16 #define pi 3.1415926535897932384626433832795028841971
    17 using namespace std;
    18 inline LL read()
    19 {
    20     LL x=0,f=1;char ch=getchar();
    21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    23     return x*f;
    24 }
    25 double p[200010];
    26 int n,cnt;
    27 double ans;
    28 inline void solve(int a,int b)
    29 {
    30     ans+=(double)(a-b)/2;
    31     p[++cnt]=(double)(a+b)/2;
    32 }
    33 int main()
    34 {
    35     n=read();
    36     for (int i=1;i<=n;i++)
    37     {
    38         LL a=read(),b=read(),c=read(),d=read();
    39         if (a+b>=c+d)
    40         {
    41             solve(a,b);
    42             solve(c,d);
    43         }else
    44         {
    45             if (a>=d)solve(a-d,d-a);
    46             if (b>=c)solve(c-b,b-c);
    47         }
    48     }
    49     sort(p+1,p+cnt+1,greater<double>());
    50     for (int i=1;i<=cnt;i++)
    51     {
    52         if (i&1)ans+=p[i];
    53         else ans-=p[i];
    54     }
    55     printf("%.0f
    ",ans);
    56 }
    cf 725F
  • 相关阅读:
    判断文件结束,feof……
    第五篇 分治思想(例子待加)
    第四篇 枚举思想
    第三篇 贪心思想
    第二篇 递归思想
    第一篇 递推思想
    爬虫系列
    整数划分问题
    html中a标签做容器的问题
    H5学习小结——div+css创建电子商务静态网页
  • 原文地址:https://www.cnblogs.com/zhber/p/7283672.html
Copyright © 2011-2022 走看看