C. Fox and Card Game
题目连接:
http://codeforces.com/contest/388/problem/C
Description
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.
Sample Input
2
1 100
2 1 10
Sample Output
101 10
Hint
题意
有n堆数,第一个人只能从头开始取
第二个人只能从尾部开始取
两个人都采取最优策略
问两个人能够拿到多少分
题解:
对于偶数个的列,一人拿一半
对于奇数的,中间那个会被剩下来
把剩下来的扔到一个vector里面去,排序,然后分奇数偶数拿就好了
代码
#include<bits/stdc++.h>
using namespace std;
int ans1,ans2,n,flag,x;
vector<int> tmp;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);flag=x%2;x/=2;
for(int j=0;j<x;j++)ans1+=read();
if(flag)tmp.push_back(-read());
for(int j=0;j<x;j++)ans2+=read();
}
sort(tmp.begin(),tmp.end());
for(int i=0;i<tmp.size();i++)
if(i%2)ans2-=tmp[i];
else ans1-=tmp[i];
cout<<ans1<<" "<<ans2<<endl;
}