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尽可能让字符串字典序 ...
随机推荐
- 12. Jmeter-断言
jmeter-断言介绍与使用 性能测试中较少用到断言.断言会增加脚本执行时间,但是接口测试中断言是必备的.什么是断言?其实就是功能测试中常说的预期结果和实际结果是否相等. 响应断言 JSON Asse ...
- C++怎样通过嵌入汇编写一个函数
参考:http://msdn.microsoft.com/en-us/library/h5w10wxs.aspx 普通的函数,Compiler会自动生成prologue和epilogue,但是通过在函 ...
- Cocos2d-x之String
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Cocos2d-x中能够使用的字符串constchar*.std::string和cocos2d::__String等,其中const ...
- Cocos2d-x之Menu
| 版权声明:本文为博主原创文章,未经博主允许不得转载. cocos2d-x菜单简介: 菜单也是游戏开发中的重要环节,一般游戏开始的第一个画面都是游戏主菜单,这些菜单包括,开始游戏,游戏设置,关卡 ...
- vue-router(路由)详细教程
vue-router(路由)详细教程:https://blog.csdn.net/wulala_hei/article/details/80488727 vue路由组件传参-页面通信:https:// ...
- c# 使用网站的身份验证及 Cookie 的获取与使用
C# 的 Http 访问可以使用 .net 自带的 HttpWebRequest, WebClient, HttpClient 类.也可以使用开源库 RestSharp . RestSharp 的优 ...
- linux起源及centos安装
第1章 Linux介绍 1.1 什么是操作系统 是一个人与计算机硬件的中介 Linux:内核+shell+扩展软件 操作系统,英文名称Operating System,简称OS,是计算机系统中必不可 ...
- go语言从例子开始之Example12.func函数
函数 是 Go 的中心.我们将通过一些不同的例子来进行学习. Example: package main import "fmt" //定义一个函数使用关键字func //函数名名 ...
- React全栈-社交网络程序 提交表单数据
1. 给每个input 表格添加change 事件 当input 变化时触发 <div className="form-group"> <input type= ...
- linux rsync 复制文件忽略文件夹
比如: /home/vagrant/test 目录下有 a,b,c 三个文件夹,只复制 c 文件夹下面的文件到/home/vagrant/test2 下 使用cp命令复制的时候,只能排除一个目录不被复 ...