思路:一个比较经典的题目,考虑每一堆,如果该堆为偶数,那么最终肯定是对半平分的,若为奇数,则会多出一个,那么显然只有那一个会影响这一堆的情况,将它们单独出来,排个序然后轮流取就可以了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n, m,c[maxn];
vector<int>cc;
bool cmp(int a,int b){return a>b;}
int main ()
{
int p=0,q=0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &m);
int tmp = m / 2;
for (int j = 0; j < m; j++)
{
scanf("%d", &c[j]);
if (j < tmp) p += c[j];
else q += c[j];
}
if (m & 1)
{
q -= c[tmp];
cc.push_back(c[tmp]);
}
}
sort(cc.begin(),cc.end(),cmp);
for (int i = 0;i<cc.size();i++)
{
if (i & 1) q +=cc[i];
else p += cc[i];
}
printf("%d %d\n", p, q);
}
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?
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.
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
2 1 100 2 1 10
101 10
1 9 2 8 6 5 9 4 7 1 3
30 15
3 3 1 3 2 3 5 4 6 2 8 7
18 18
3 3 1000 1000 1000 6 1000 1000 1000 1000 1000 1000 5 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.