Codeforces---Fox and Card Game
来源:互联网 发布:扫码 销售 软件 编辑:程序博客网 时间:2024/05/21 17:59
Fox Ciel is playing a card game with her friend Fox Jiro. There aren 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?
The first line contain an integer n (1 ≤ n ≤ 100). Each of the nextn lines contains a description of the pile: the first integer in the line issi (1 ≤ si ≤ 100) — the number of cards in thei-th pile; then followsi positive integersc1, c2, ...,ck, ...,csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
21 1002 1 10
101 10
19 2 8 6 5 9 4 7 1 3
30 15
33 1 3 23 5 4 62 8 7
18 18
33 1000 1000 10006 1000 1000 1000 1000 1000 10005 1000 1000 1000 1000 1000
7000 7000
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.
题型:博弈论
题意:
甲乙俩人玩游戏,有n堆牌,并给出每堆牌的张数以及每张上的数字。
甲先抽,甲可以抽任意一堆的最上面一张,乙后抽,乙可以抽任意一张的最下面一张。
俩人轮流抽,直至牌全部抽完。
两人都想使自己的最后的牌上的数字的和最大,求出最后两人各得了多少分。
分析:
从博弈的角度讲,甲乙都不想让对方得到更多的分,但是由于规则限制只能一个从上往下抽一个从下往上抽,那么为了抑制对方,唯一的办法就是防止对方拿到每一堆中超过一半的牌。
所以最后的情况是:
如果当前这一堆牌是偶数张,那么甲乙各拿一半;
如果当前这一堆牌是奇数张,那么最后这堆牌只剩中间一张。
然后就剩下了一些中间牌,从大到小排一下序,两人按顺序拿完就可以了。
这样就可以贪心处理了。
<span style="font-size:12px;">#include <iostream>#include <algorithm>#include <deque>using namespace std;bool cmp(int a,int b){ return a>b;}int main(){ int n,i,k,j,x,y; cin>>n; long long a=0,b=0; int s[n]; k=0; for(i=0;i<n;i++) { cin>>x; if(x%2==0) for(j=0;j<x;j++) { cin>>y; if(j<x/2) a+=y; else b+=y; } else { for(j=0;j<x;j++) { cin>>y; if(j<x/2) a+=y; else if(j==(x-1)/2) s[k++]=y; else b+=y; } } } sort(s,s+k,cmp); for(i=0;i<k;i++) if(i%2==0) a+=s[i]; else b+=s[i]; cout<<a<<" "<<b<<endl; return 0;}</span>
- Codeforces---Fox and Card Game
- Fox and Card Game CodeForces
- codeforces 388C Fox and Card Game
- Codeforces 388 C. Fox and Card Game
- CodeForces 388C Fox and Card Game
- Codeforces Round #228 Fox and Card Game 解题报告
- Codeforces 388C Fox and Card Game(贪心)
- Codeforces #228 D2E / D1C:Fox and Card Game
- Codeforces Round #228 (Div. 1) C. Fox and Card Game
- Codeforces 388C Fox and Card Game 博弈
- CodeForces 388C Fox and Card Game 解题报告
- Codeforces Round #228 (Div. 1)C. Fox and Card Game
- Codefrces 388C Fox and Card Game
- codeforces Fox and Number Game
- Codeforces Round #228 (Div. 2) E Fox and Card Game(贪心博弈)
- Appleman and Card Game CodeForces
- Codeforces 389A Fox and Number Game
- codeforces A. Fox and Number Game
- matlab-线性代数 提取出矩阵的一列
- Myeclipse写servlet程序启动tomcat后无法进入debug模式的解决方法
- matlab-线性代数 矩阵换行
- Python-装饰器;Decorator模式
- CTF领域指南
- Codeforces---Fox and Card Game
- mfc TreeCtrl 排序
- HTML+CSS
- 【web前端】前端项目组织结构
- JavaScript学习【持续更新】
- Android N新特性
- Ubuntu16.04 Eclispe 安装 SVN 插件 subclipse 时 JavaHL 报错的解决办法
- matlab-线性代数 矩阵换列
- 设计模式-Java中自带的观察者模式(Observer)