codeforces Sereja and Dima 题解

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

时间: 2024-12-16 05:24:45

codeforces Sereja and Dima 题解的相关文章

codeforces A. Supercentral Point 题解

One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points(x1,?y1),?(x2,?y2),?...,?(xn,?yn). Let's define neighbors for some fixed point from the given set (x,?y): point (x',?y') is (x,?y)'s right neighbor,

codeforces New Year Present 题解

The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception. Vasily knows that the best present is (no, it's not a contest) money. He's put n empty wallets from left to right in a r

codeforces D. Ice Sculptures 题解

The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus n ice sculptures were erected. The sculptures

Educational Codeforces Round 64部分题解

Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点个数是否有限,如果有限,输出. 很明显当正方形套三角形或者三角形套正方形是交点个数是无限的(因为有一条边相交) 其他图形的嵌套交点个数比较好判断,不多赘述 但是注意坑点: 当按照矩形,园,三角这样的顺序是,三角与圆的一个交点是与圆和正方形的交点重合的,判一下就好了 #include<cstdio>

Codeforces 7E - Defining Macros 题解

目录 Codeforces 7E - Defining Macros 题解 前言 做法 程序 结尾 Codeforces 7E - Defining Macros 题解 前言 开始使用博客园了,很想写点东西.(逃 这是一道Codeforces题目. 做法 一道大模拟 相信大家都看得懂题目意思,我就不赘述了. 首先我们读进来\(n\)行,对于每一行,我们把它初步分成一下四块内容: #号 define 宏的名字 宏的表达式 注意:#和define中间可能会有空格,define和宏的名字,宏的名字和宏

Codeforces Round #616 部分题解

老年选手诈尸? A,B 咕了. C - Prefix Enlightenment 很容易看出这个限制条件可以推出每个点最多被两个集合包含.按照套路,很容易联想到给这两个集合连一条边,表示他们的状态要相同/不同. 因为保证了有解,所以从左往右扫的时候拿并查集维护一下每个连通块的二分图情况,选较小的那一边. 如果只被一个集合覆盖,那么就相当于强制这个集合选/不选,在做并查集的时候特判一下即可. 代码咕了. D - Coffee Varieties (hard version) 作为一名憨憨,做法当然

【codeforces】【比赛题解】#854 CF Round #433 (Div.2)

cf一如既往挺丧 看丧题点我! [A]分数 Petya是数学迷,特别是有关于分数的数学.最近他学了所谓一个分数被叫做"真分数"当且仅当其分子小于分母,而一个分数被叫做"最简分数"当且仅当其分子分母互质.在闲暇时间,Petya在用计算器研究:如何把最简真分数转换为小数等问题.有一天他不小心把除号(÷)按成了加号(+),导致他得到了分子与分母的和.Petya想要得到他原来的分数,但他很快发现这不是唯一的.所以现在他想要知道最大的最简真分数使得其分子与分母的和为n. 输入

Codeforces #Round 376 部分题解

A: 题目传送门:http://codeforces.com/problemset/problem/731/A 直接根据题意模拟即可 1 #include "bits/stdc++.h" 2 3 using namespace std ; 4 typedef long long QAQ ; 5 6 char s[ 1010 ] ; 7 8 inline int Calc ( const char x , const char y ) { 9 if ( x > y ) return

codeforces 366C C. Dima and Salad(dp)

题目链接: codeforces 366C 题目大意: 给出n个物品,有两个属性,问最后第一个属性的总和是第二个属性的k倍的时候,第一个属性最大是多少. 题目分析: 我们将物品做一个变形,重量为a[i]-b[i]*k,收益是a,那么我们只需要对重量为正的做一遍背包,对质量为负的取绝对值做一遍背包,然后重量相等的背包的收益之和就是当前重量下的最大收益. AC代码: #include <iostream> #include <cstring> #include <cstdio&g