Codeforces 388C Fox and Card Game (贪心博弈)
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 (贪心博弈)的更多相关文章
- codeforces 388C Fox and Card Game
刚刚看到这个题感觉是博弈题: 不过有感觉不像,应该是个贪心: 于是就想贪心策略: 举了一个例子: 3 3 1 2 3 4 3 4 1 2 5 4 1 2 5 8 如果他们两个每次都拿对自己最有利的那个 ...
- 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 ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)
[BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...
- CodeForces 462B Appleman and Card Game(贪心)
题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...
- 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 ...
- Codeforces Round #646 (Div. 2) C. Game On Leaves (贪心,博弈)
题意:给你一棵树,每次可以去掉叶节点的一条边,Ayush先开始,每回合轮流来,问谁可以第一个把\(x\)点去掉. 题解:首先如果\(x\)的入度为\(1\),就可以直接拿掉,还需要特判一下入度为\(0 ...
- 【贪心+博弈】C. Naming Company
http://codeforces.com/contest/794/problem/C 题意:A,B两人各有长度为n的字符串,轮流向空字符串C中放字母,A尽可能让字符串字典序小,B尽可能让字符串字典序 ...
随机推荐
- 测开之路四十九:用Django实现扑克牌游戏
用Django实现和之前flask一样的扑克牌游戏 项目结构 html <!DOCTYPE html><html lang="en"><head> ...
- 【python】 读写文件
#标准输出 sys.stdout.write() sys.stderr.write() #标准输入 while True : try: line = raw_input().rstrip(); exc ...
- Java并发AtomicLongArray类
java.util.concurrent.atomic.AtomicLongArray类提供了可以原子读取和写入的底层long类型数组的操作,并且还包含高级原子操作. AtomicLongArray支 ...
- Vue 在手机上键盘把底部菜单顶上去的解决方案
Vue 在手机上键盘把底部菜单顶上去的解决方案 ios和安卓的键盘的区别 ios和安卓的键盘的区别弹起方式不同, ios直接弹出键盘, 不影响页面, 而安卓键盘弹起时会把页面顶起来, 这样就会把底部菜 ...
- SpringMVC学习(9):实现注解式权限验证
对大部分系统来说都需要权限管理来决定不同用户可以看到哪些内容,那么如何在Spring MVC中实现权限验证呢?当然我们可以继续使用servlet中的过滤器Filter来实现.但借助于Spring MV ...
- [Java 教程 04] Java基础语法
在上一篇文章中我们已经运行了个简单的java程序,但是没有给大家讲解代码部分的内容与含义.学习,我们要做到知其然而知其所以然,所以本篇文章我们就来讲解java程序的基本语法,学完这篇文章你再回头看上篇 ...
- java命令-jps
jps命令,查看当前用户所有java进程pid 可进入/tmp/hsperfdata_xxx(登录用户名)路径下,可查看当前用户下所有的Java进程.jps.jconsole.jvisualvm等工具 ...
- js实用小函数收集
格式化金额 var val='212312.235423' var rex = /\d{1,3}(?=(\d{3})+$)/g; val.replace(/^(-?)(\d+)((\.\d+)?) ...
- 使用ubuntu的一些操作笔记20191203
前言 环境: virtualbox + Ubuntu 16.04 情况: 可以进入虚拟机中Ubuntu系统的桌面,但是外部可以访问到 ssh,输入正确的用户名和密码无法登录 无法正常启动 Apache ...
- 割边的tarjan算法
与割点唯一一点不同是low[v]>=dfn[u]变为low[v]>dfn[u] 代码如下: bool vis[maxn]; int dfn[maxn],low[maxn]; int cnt ...