Codeforces_388C_Fox and Card Game(博弈、贪心、排序)

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 are n 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 next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1c2, ...,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.

Sample test(s)
input
2
1 100
2 1 10
output
101 10
input
1
9 2 8 6 5 9 4 7 1 3
output
30 15
input
3
3 1 3 2
3 5 4 6
2 8 7
output
18 18
input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 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堆牌,并给出每堆牌的张数以及每张上的数字。

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

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

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


分析:

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

所以最后的情况是:

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

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

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

这样就可以贪心处理了。


代码:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int n;
int s[123];
int a[123];
int last[123];

bool cmp(int a,int b){
    return a>b;
}

int main(){
    while(~scanf("%d",&n)){
        int alice=0,bob=0;
        int half,cnt=0;
        int get = 0;
        for(int i=0;i<n;i++){
            scanf("%d",&s[i]);
            for(int j=0;j<s[i];j++){
                scanf("%d",&a[j]);
            }
            half = s[i]/2;
            //一人一半
            if(s[i]%2==0){
                for(int j=0;j<half;j++){
                    alice+=a[j];
                    get++;
                }
                for(int j=half;j<s[i];j++){
                    bob+=a[j];
                    get++;
                }
            }
            //剩了一个
            else{
                for(int j=0;j<half;j++){
                    alice+=a[j];
                    get++;
                }
                last[cnt++] = a[half];
                for(int j=half+1;j<s[i];j++){
                    bob+=a[j];
                    get++;
                }
            }
        }
        if(cnt!=0){
            sort(last,last+cnt,cmp);
            //alice first
            if(get%2==0){
                for(int i=0;i<cnt;i++){
                    if(i%2==0){
                        alice+=last[i];
                    }
                    else{
                        bob+=last[i];
                    }
                }
            }
            //bob first
            else{
                for(int i=0;i<cnt;i++){
                    if(i%2==1){
                        alice+=last[i];
                    }
                    else{
                        bob+=last[i];
                    }
                }
            }
        }
        printf("%d %d\n",alice,bob);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值