CodeForces 388C Fox and Card Game 解题报告

D - Fox and Card Game
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Description
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 c1, 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.

Sample Input
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

Hint
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.

首先题意就是两个人在放了许多堆牌的桌面拿牌,每张牌都有每张牌的值,并且Ciel 只能从牌堆顶上拿牌,Jiro 只能从牌堆的底部拿牌,问如果两个人拿牌得当的话,最终每人拿到的牌的值得总和是多少。

很明显我们要分析对于偶数堆牌,如果牌堆的上半部分比下半部分总和大,那Jiro肯定不会浪费次数来拿拿不到的牌 ,肯定会去抢奇数堆牌的中间大的那堆,同时Ciel 也知道,对面如果要抢偶数堆牌的上半部分,自己只要跟着他那就行了,这样对面就拿不到了,所以,每个人要抢的其实只有奇数堆牌中间的那一张牌,因为除去那一张牌后,奇数堆牌会变得和偶数堆牌一样对半分。

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int st[105];

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

int main()
{
    int n,x,y;
    int l = 0,r = 0,cnt = 0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        int tmp ;
        if(x&1) tmp = (x+1)/2;
        else tmp = x/2;

        for(int j=1;j<=x;j++){
            scanf("%d",&y);
            if(j<=tmp) l+=y;
            else r+= y;

            if(x&1 && j==tmp){
                st[cnt++] = y;
            }
        }
        if(x&1) l -= st[cnt-1];
    }
    sort(st,st+cnt,cmp);
    for(int i=1;i<=cnt;i++){
        if(i&1) l += st[i-1];
        else r += st[i-1];
    }
    printf("%d %d\n",l,r);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值