time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 to 1000.

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
input
7
1 2 3 4 5 6 7
output
16 12
Note

In the first sample Sereja will take cards with numbers 10 and 2, so
Sereja's sum is 12. Dima will take cards with numbers 4 and 1,
so Dima's sum is 5.

解题思路:看似博弈论,事实上就是个贪心。因为每次仅仅能取最左边的或最右边的,则仅仅需开两个指针i = n-1,j = 0,两人轮流取,每次若a[i] > a[j] , 则 i --;否则 j ++;直到i == j时,结束。

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff int a[1005]; int main()
{
#ifdef sxk
freopen("in.txt","r",stdin);
#endif
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0; i<n; i++)
cin>>a[i];
int ans = 0, anss = 0;
int t = 0;
for(int i=n-1, j=0; i>=j; ){ //开两个指针i,j
if(a[i] > a[j]){
if(t%2) anss += a[i];
else ans += a[i];
i --;
}
else{
if(t%2) anss += a[j];
else ans += a[j];
j ++;
}
t ++;
}
cout<<ans<<" "<<anss<<endl;
}
return 0;
}

Codeforces Round #223 (Div. 2)--A. Sereja and Dima的更多相关文章

  1. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  2. Codeforces Round #223 (Div. 2) A

    A. Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes map

    B. Sereja and Suffixes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  4. Codeforces Round #215 (Div. 1) B. Sereja ans Anagrams 匹配

    B. Sereja ans Anagrams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  5. Codeforces Round #223 (Div. 2) C

    C. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #215 (Div. 2) D. Sereja ans Anagrams

    http://codeforces.com/contest/368/problem/D 题意:有a.b两个数组,a数组有n个数,b数组有m个数,现在给出一个p,要你找出所有的位置q,使得位置q  q+ ...

  7. Codeforces Round #243 (Div. 1)A. Sereja and Swaps 暴力

    A. Sereja and Swaps time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #243 (Div. 2) C. Sereja and Swaps

    由于n比较小,直接暴力解决 #include <iostream> #include <vector> #include <algorithm> #include ...

  9. Codeforces Round #243 (Div. 2) B. Sereja and Mirroring

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

随机推荐

  1. ACdream 1135(MST-最小生成树边上2个值,维护第一个最小的前提下让还有一个最小)

    F - MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...

  2. HttpGet协议与正则表达

    使用HttpGet协议与正则表达实现桌面版的糗事百科   写在前面 最近在重温asp.net,找了一本相关的书籍.本书在第一章就讲了,在不使用浏览器的情况下生成一个web请求,获取服务器返回的内容.于 ...

  3. achieve aop through xml

    The main way to achive AOP is deploying a xml file. Now a xml file is presented to be a explanation ...

  4. 黑马程序猿_ 利用oc的协议实现代理模式

    先说下代理模式是什么吧 定义: 为其它对象提供一种代理以控制对这个对象的訪问.在某些情况下,一个对象不适合或者不能直接引用还有一个对象 而代理对象能够在client和目标对象之间起到中介的作用. 在看 ...

  5. 【从翻译mos文章】SGA_TARGET与SHMMAX关系

    SGA_TARGET与SHMMAX关系 参考原始: Relationship Between SGA_TARGET and SHMMAX (文件 ID 1527109.1) 申请: Oracle Da ...

  6. 阿里云免费试用之体验——阿里云serverECS试用心得

    自上次參加了阿里云的开发人员大会回来 心里就一直惦记着阿里云 由于曾经各种各样什么的server也用了不少 年前開始接触阿里云 一直没有给予很多其它的关注 參加了这次的开发人员大会后 就想更进一步的了 ...

  7. 如何做程序猿SOHO它定购家庭赚外快?

    做为一名程序猿.我想大多数人除了平时削尖了脑袋研究各种各样的技术之外. ArticleId=28404183" width="1" height="1" ...

  8. HDU3537-Daizhenyang&#39;s Coin(博弈SG-打表)

    <span style="color: green; font-family: Arial; font-size: 12px; background-color: rgb(255, 2 ...

  9. 用VS2005编译生成Lua库文件和解释器

    TMD,本来很简单的东西,网上说的乱七八糟,说的也不明白,大家抄来抄去,估计都不自己实践的..花了半个下午研究了一下,总结一下. 1)下载lua工程文件,地址为http://www.lua.org/f ...

  10. SWT的TreeViewer和TableViewer的交互

    左边是一个TreeViewer,右边是一个TableViewer.当点击左边的treeitem的时候,右边的tableViewer要将该item的子节点信息显示出来.就像这样: 左边的treeView ...