码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces Sereja and Dima 题解

时间:2014-07-22 23:00:16      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   数据   

Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.

Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.

Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.

Input

The first line contains integer n (1?≤?n?≤?1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to1000.

Output

On a single line, print two integers. The first number is the number of Sereja‘s points at the end of the game, the second number is the number of Dima‘s points at the end of the game.

Sample test(s)
input
4
4 1 2 10
output
12 5


一个轮流取数的游戏,取得的值最大者胜,这里要求结果。

这里使用一下deque数据结构吧。当然这里使用一般数列,用two points的思想解决也是可以的。

deque是可以两头取数都很快的容器。很适合本题这样的情况


#include <deque>
#include <string>
#include <iostream>
using namespace std;

void SerejaandDima()
{
	int n, a;
	cin>>n;
	deque<int> deq;
	while (n--)
	{
		cin>>a;
		deq.push_back(a);
	}
	int Sereja = 0, Dima = 0;
	bool turn = true;
	while (deq.size())
	{
		if (deq.front() > deq.back())
		{
			if (turn) Sereja += deq.front();
			else Dima += deq.front();
			deq.pop_front();
		}
		else
		{
			if (turn) Sereja += deq.back();
			else Dima += deq.back();
			deq.pop_back();
		}
		turn = !turn;
	}
	cout<<Sereja<<‘ ‘<<Dima;
}




codeforces Sereja and Dima 题解,码迷,mamicode.com

codeforces Sereja and Dima 题解

标签:style   blog   color   使用   os   数据   

原文地址:http://blog.csdn.net/kenden23/article/details/24839585

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!