Codeforces---Fox and Card Game

来源:互联网 发布:扫码 销售 软件 编辑:程序博客网 时间:2024/05/21 17:59
 Fox and Card Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Fox Ciel is playing a card game with her friend Fox Jiro. There aren piles of cards on the table. And there is a positive integer on each card.

The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.

Suppose Ciel and Jiro play optimally, what is the score of the game?

Input

The first line contain an integer n (1 ≤ n ≤ 100). Each of the nextn lines contains a description of the pile: the first integer in the line issi (1 ≤ si ≤ 100) — the number of cards in thei-th pile; then followsi positive integersc1, c2, ...,ck, ...,csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.

Output

Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.

Examples
Input
21 1002 1 10
Output
101 10
Input
19 2 8 6 5 9 4 7 1 3
Output
30 15
Input
33 1 3 23 5 4 62 8 7
Output
18 18
Input
33 1000 1000 10006 1000 1000 1000 1000 1000 10005 1000 1000 1000 1000 1000
Output
7000 7000
Note

In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.

In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.



题型:博弈论


题意:

甲乙俩人玩游戏,有n堆牌,并给出每堆牌的张数以及每张上的数字。

甲先抽,甲可以抽任意一堆的最上面一张,乙后抽,乙可以抽任意一张的最下面一张。

俩人轮流抽,直至牌全部抽完。

两人都想使自己的最后的牌上的数字的和最大,求出最后两人各得了多少分。


分析:

从博弈的角度讲,甲乙都不想让对方得到更多的分,但是由于规则限制只能一个从上往下抽一个从下往上抽,那么为了抑制对方,唯一的办法就是防止对方拿到每一堆中超过一半的牌。

所以最后的情况是:

如果当前这一堆牌是偶数张,那么甲乙各拿一半;

如果当前这一堆牌是奇数张,那么最后这堆牌只剩中间一张。

然后就剩下了一些中间牌,从大到小排一下序,两人按顺序拿完就可以了。

这样就可以贪心处理了。


<span style="font-size:12px;">#include <iostream>#include <algorithm>#include <deque>using namespace std;bool cmp(int a,int b){    return a>b;}int main(){    int n,i,k,j,x,y;    cin>>n;    long long a=0,b=0;    int s[n];    k=0;    for(i=0;i<n;i++)    {        cin>>x;        if(x%2==0)         for(j=0;j<x;j++)        {            cin>>y;            if(j<x/2) a+=y;             else b+=y;        }        else        {            for(j=0;j<x;j++)            {                cin>>y;                if(j<x/2) a+=y;                else if(j==(x-1)/2) s[k++]=y;                else b+=y;            }        }    }    sort(s,s+k,cmp);    for(i=0;i<k;i++)        if(i%2==0) a+=s[i];    else b+=s[i];    cout<<a<<" "<<b<<endl;    return 0;}</span>



0 0
原创粉丝点击