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尽可能让字符串字典序 ...
随机推荐
- java String 类特点
String的设计是一个典型的单一模式 String str1="AAAA":String str2="AAAA": 这生成两个对象吗?不是.在内存中,这是同一 ...
- Cocos2d-x打包安卓apk
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 下载安装安卓(android)环境 见http://www.cnblogs.com/geore/p/5793620.html,按照其 ...
- python-form表单
form表单 form属于块级标签 功能: 表单用于向服务器传输数据,从而实现用户与web服务器的交互 表单能够包含input系列标签,比如文本字段.复选框.单选框.提交按钮等等 表单还可以包含tex ...
- mysql 查看数据库最大连接数
show variables like '%max_connections%'; navicat 切换到命令行: navicat查看建表语句: 选中表,右键,对象信息,选择DDL
- 如何访问 Redis 中的海量数据,服务才不会挂掉?
来源:www.toutiao.com/i6697540366528152077 一.前言 有时候我们需要知道线上的Redis的使用情况,尤其需要知道一些前缀的key值,让我们怎么去查看呢?并且通常情况 ...
- luoguP3258 [JLOI2014]松鼠的新家 题解(树上差分)
P3258 [JLOI2014]松鼠的新家 题目 树上差分:树上差分总结 #include<iostream> #include<cstdlib> #include<c ...
- docker--build自己的image
通过container commit成image [root@localhost docker_test]# docker container commit #可以简写成docker commit a ...
- VINS 检测回环辅助激光建图
最近接到一个任务,在激光检测回环失败时,比如黑色物体多,场景大等,可否利用视觉进行回环检测.如果只是检测回环,现有的许多框架都可以使用.ORB-SLAM本身就有单目模式,且效果不错.但是发现ORB在检 ...
- IT面试技巧(1)
声明:以下面试技巧仅作参考,更多的时候还是要真实得表达自我,要保持一定的职业素养. 1.请你自我介绍一下你自己? 回答提示:一般人回答这个问题过于平常,只说姓名.年龄.爱好.工作经验,这些在简历上都有 ...
- Windows 命令提示符
命令提示符(cmd): 启动:Win+R ,输入cmd回车 切换盘符:盘符名称: 进入文件夹:cd 文件夹名称 进入多级文件夹:cd 文件夹1\文件夹2\文件夹3 返回上一级:cd .. 直接回根路径 ...