您的位置:首页 > 其它

codeforces389 E. Fox and Card Game【博弈 + 贪心】

2018-03-21 19:47 429 查看

E. Fox and Card Game

time limit per test1 second

memory limit per test256 megabytes

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 si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, 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.

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.

题意: 给你几堆牌,每张牌都一个数字,第一个人在这几堆顶挑一张牌,第二个在堆底挑一张牌,直到没牌了,每个人都想使手里的牌最大,问你最后这两人的牌上数子总和分别多少

分析: 因为我博弈的题做的不多,也不怎么会做,我一看到这个题我以为贪心,用了双向队列来模拟的,结果wa11了,后来看了题解才知道是个简单博弈,我们这样想,他们二人是以不让对手拿牌为代价的话就简单了,如果只有一堆的话,那么两人只能分别从两端拿,如果这堆是奇数的话,肯定第一个人多拿一个,如果是偶数的话,拿的个数一样多。如果是多堆,当第一个人在任意一堆拿了一个数时,第二个人仅考虑当前堆的最大化,从而去和第一个人去竞争,毕竟整体的最大化是由局部的最大化得来的,这样的话我们只需要考虑每堆牌的奇偶性即可,也就是说他们就是竞争所有奇数堆的中间那个数的最大化,我们遍历完所有的堆,把那个数取出,然后贪心的来取即可

总结: 还是自己做的这方面的题少,没有及时把握题的方向,博弈还得多做..

参考代码

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e3 + 10;

vector<int> a;

int main() {
int n;cin>>n;
int s1 = 0,s2 = 0;
for (int i = 0; i < n; i++) {
int t; cin>>t;
for (int j = 0; j < t; j++) {
int x; cin>>x;
if((t & 1) && j == t / 2) {
a.push_back(x);continue;
}
if(j < t / 2) s1 += x;
else s2 += x;
}
}
sort(a.begin(),a.end(),greater<int>());
for (int i = 0; i < a.size(); i++) {
if(i & 1) s2 += a[i];
else s1 += a[i];
}
cout<<s1<<' '<<s2<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: