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

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 (贪心博弈)的更多相关文章

  1. codeforces 388C Fox and Card Game

    刚刚看到这个题感觉是博弈题: 不过有感觉不像,应该是个贪心: 于是就想贪心策略: 举了一个例子: 3 3 1 2 3 4 3 4 1 2 5 4 1 2 5 8 如果他们两个每次都拿对自己最有利的那个 ...

  2. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  3. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  4. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  5. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

  6. CodeForces 462B Appleman and Card Game(贪心)

    题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...

  7. Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心

    A. Fox and Box Accumulation 题目连接: http://codeforces.com/contest/388/problem/A Description Fox Ciel h ...

  8. Codeforces Round #646 (Div. 2) C. Game On Leaves (贪心,博弈)

    题意:给你一棵树,每次可以去掉叶节点的一条边,Ayush先开始,每回合轮流来,问谁可以第一个把\(x\)点去掉. 题解:首先如果\(x\)的入度为\(1\),就可以直接拿掉,还需要特判一下入度为\(0 ...

  9. 【贪心+博弈】C. Naming Company

    http://codeforces.com/contest/794/problem/C 题意:A,B两人各有长度为n的字符串,轮流向空字符串C中放字母,A尽可能让字符串字典序小,B尽可能让字符串字典序 ...

随机推荐

  1. 大数据学习笔记之Hadoop(三):MapReduce&YARN

    文章目录 一 MapReduce概念 1.1 为什么要MapReduce 1.2 MapReduce核心思想 1.3 MapReduce进程 1.4 MapReduce编程规范(八股文) 1.5 Ma ...

  2. 判断是否是NaN

    if (isNaN(parseInt(x))) { alert("非数字"); } else{ alert("数字"); }

  3. 更新python的依赖包,亲测!

    输入pip install --upgrade pandas 无需卸载,让它自己更新就好 pandas可以改成别的包

  4. js中浅谈this对象(未补充完整)

    什么是this? 1.javascript语言中,一切皆为对象(除了 undefined 和 null 之外),运行环境也是对象,所以函数都是在某个对象之中运行,this就是这个对象(环境). 2.t ...

  5. mysql学习(1)----------基础语法

    进入mysql mysql -u用户名 -p密码 初始用户为root   初始密码为空   status; 查看当前用户,以及数据库的字符集和其他参数的设置 set db  characterset= ...

  6. shell脚本对代码执行时间的计时

    时间秒: $(date +%s) 算数表达式: $(($cost_time/60)) $(($cost_time%60)) #!/bin/bash # ccache is from epel mirr ...

  7. c# WInform 自定义导航布局

    问题形成原因:软件一般都是左侧树导航或上部菜单导航,做好一个软件后,有的客户可能想用一个页面做导航图像,而各个客户用的功能可能不同,所以导航布局需要自定义. 思路:1.把菜单列出来 2.双击菜单生成一 ...

  8. Linux 环境下安装rlwrap工具

    rlwrap项目是一个“readline包装器”,它使用GNU readline库来编辑任何其他命令的键 盘输入.通过rlwrap可以进行命令的上下切换,类似历史命令. 1.下载rlwrap rpm ...

  9. 好用的打包工具webpack

    <什么是webpack> webpack是一个模块打包器,任何静态资源(js.css.图片等)都可以视作模块,然后模块之间也可以相互依赖,通过webpack对模块进行处理后,可以打包成我们 ...

  10. Git忽略已经跟踪的文件 转摘:http://blog.csdn.net/huguohuan/article/details/7380349

    某工程project用Git管理代码,但是在他的根目录下有个配置文件,比如project.iws是不需要git每次跟踪它的修改记录的. 一般做法是在.gitignore文件中添加一行 project. ...