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. dex2jar反编译大文件内存溢出的问题

    @echo off REM better invocation scripts for windows from lanchon, release in public domain. thanks! ...

  2. vue 点击其他地方隐藏dom

    document.addEventListener('click', function (e) { if (document.getElementsByClassName('keywordContai ...

  3. drf:筛选,序列化

    1.基础 restful规范: - url:一般用名词 http://www.baidu.com/article (面向资源编程) - 根据请求方式的不同做不同操作:get,post,put,dele ...

  4. day 67 Django的view 与路由

    一.Django中的视图 CBV和FBV 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的. url(r'^add_publisher/',views.AddPublis ...

  5. Java迭代器模式

    迭代器模式是Java和.Net编程环境中非常常用的设计模式.此模式用于以顺序方式访问集合对象的元素,而不需要知道其底层表示. 迭代器模式属于行为模式类别. 实现实例 在这个实例中,将创建一个Itera ...

  6. 第6章 RPC之道

    6.1 认识RPC 分布式.微服务的架构思维中都不能缺少 RPC 的影子 RPC(Remote Procedure Call)远程过程调用.通过网络在跨进程的两台服务器之间传输信息,我们使用的时候不用 ...

  7. 利用hover,制作点击有缩放效果

    .tab-pic-wrap .pic-wrap .list li a:hover img { transform: scale(1.03); } .tab-pic-wrap .pic-wrap .li ...

  8. vue - blog开发学7

    将基本的项目部署到linux上(前后台只是实现了基本的功能,本次只是记录一些基本的开发流程,完善,等后续) 1.linux环境准备(我用的是阿里云服务器) ①jre.mysql,Nginx基本上这些就 ...

  9. go语言从例子开始之Example6.if/else

    if 和 else 分支结构在 Go 中当然是直接了当的了. package main import "fmt" func main() { 这里是一个基本的例子. if 7%2 ...

  10. Codeforces 1195E OpenStreetMap 单调队列套单调队列

    题意:给你一个n * m的矩阵,问所有的a * b的子矩阵的最小的元素的和是多少.题目给出了矩阵中的数的数据生成器. 思路:如果这个问题是1维的,即求所有区间的最小元素的和,用单调队列O(n)就可以做 ...