Codeforces 388C Fox and Card Game (贪心博弈)

Codeforces 388C Fox and Card Game (贪心博弈),第1张

概述Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game 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

Codeforces Round #228 (Div. 1)

题目链接:C. Fox and Card Game

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 \(s_i (1?≤?s_i?≤?100)\) — the number of cards in the \(i\)-th pile; then follow \(s_i\) positive integers \(c_1,c_2,...,c_k,c_{s_i} (1?≤?c_k?≤?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

2 1 1002 1 10

output

101 10

input

1 9 2 8 6 5 9 4 7 1 3

output

30 15

input

33 1 3 23 5 4 6 2 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.

Solution 题意

给定 \(n\) 叠牌,第 \(i\) 叠牌有 \(s_i\) 张,第 \(k\) 张牌的值为 \(c_k\)

CIEl 先手,每次选择一叠牌,拿走最上面的一张牌,Jiro 后手,每次选择一叠牌,拿走最下面的一张牌。

求两者在采取最优策略的情况下各自的分数。

题解

贪心博弈。如果一叠牌的数量是偶数,那么两个人各自取一半,如果是奇数,则中间的一叠牌单独取,其余的牌一人一半。

对所有的中间的牌排序后再轮流取。

Code
#include <bits/stdc++.h>using namespace std;vector<int> a;int main() {    ios::sync_with_stdio(false);    cin.tIE(0);    int n;    cin >> n;    int sum1 = 0,sum2 = 0;    for(int i = 0; i < n; ++i) {        int k,x;        cin >> k;        for(int j = 1; j <= k / 2; ++j) {            cin >> x;            sum1 += x;        }        if(k & 1) {            cin >> x;            a.push_back(x);        }        for(int j = 1; j <= k / 2; ++j) {            cin >> x;            sum2 += x;        }    }    sort(a.begin(),a.end(),[](int a,int b){return a > b;});    for(int i = 0; i < a.size(); ++i) {        if(i & 1) {            sum2 += a[i];        } else {            sum1 += a[i];        }    }    printf("%d %d\n",sum1,sum2);    return 0;}
总结

以上是内存溢出为你收集整理的Codeforces 388C Fox and Card Game (贪心博弈)全部内容,希望文章能够帮你解决Codeforces 388C Fox and Card Game (贪心博弈)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1210836.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存