Codeforces 388 C. Fox and Card Game

C. Fox and Card Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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


Ciel总是可以抢到前一半,Jiro抢到后一半。。。最后留下奇数堆中剩下的中间的值。。。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int c[120][120],ct[120],n;
int ccc[120],Ciel,Jiro,cnt;

int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",ct+i);
		int p1=ct[i]/2-1,p2=ct[i]-ct[i]/2;
		for(int j=0;j<ct[i];j++)
		{
			scanf("%d",&c[i][j]);
			if(j<=p1)
				Ciel+=c[i][j];
			else if(j>=p2)
				Jiro+=c[i][j];
			else ccc[cnt++]=c[i][j];
		}
	}
	sort(ccc,ccc+cnt);
	for(int i=cnt-1;i>=0;i--)
	{
		if((cnt-i)%2) Ciel+=ccc[i];
		else Jiro+=ccc[i];
	}

	printf("%d %d\n",Ciel,Jiro);
	return 0;
}



/**
 * Created by ckboss on 14-10-7.
 */
import java.util.*;

public class FoxandCardGame {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int[][] pile = new int[n+10][111];
        int[] qiang = new int[n+10];
        int qn=0;
        int SumA=0,SUM=0;
        for(int i=1;i<=n;i++){
            pile[i][0]=in.nextInt();
            for(int j=1;j<=pile[i][0];j++){
                pile[i][j]=in.nextInt();
                SUM+=pile[i][j];
                if(j<=pile[i][0]/2) SumA+=pile[i][j];
            }
            if(pile[i][0]%2==1){
                qiang[qn++]=pile[i][pile[i][0]-pile[i][0]/2];
            }
        }
        Arrays.sort(qiang,0,qn);
        for(int i=qn-1,j=0;i>=0;i--,j++){
            if(j%2==0)
                SumA+=qiang[i];
        }
        System.out.println(SumA+" "+(SUM-SumA));
    }
}



你可能感兴趣的:(Codeforces 388 C. Fox and Card Game)