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. android 视频文件不能进行幻灯片的播放

    packages\apps\Gallery2\src\com\android\gallery3d\app\SlideshowPage.java中:   1.加入: import com.android ...

  2. [Cocos2d-x]节点之间的相互通讯

    在做.NET开发时,对象之间的相互通讯一般使用事件(event)实现,事件概念是.NET对Delegate的封装. 在Cocos2d-x开发过程中,对象之间的通讯刚开始时不知道如何实现,于是想到c++ ...

  3. webservice之cxf样例

    整理參考于网上资源: http://wenku.baidu.com/link?url=MbPPOKCficQCAwSZduszpMFSD3f8xCKeNz6YUtwFS1TkHharz1aPPfkXD ...

  4. uva315(求割点数目)

    传送门:Network 题意:给出一张无向图,求割点的个数. 分析:模板裸题,直接上模板. #include <cstdio> #include <cstring> #incl ...

  5. html练习(5)

    这个练习主要简单的展示了据对定位和相对定位: 在此说下html的定位: 1.static定位 这个是默认的方式.对static而言.left和right是不生效的. 2.relative定位(相对定位 ...

  6. configure: error: zlib not installed

    export LDFLAGS="-L/usr/local/zlib/lib" export CPPFLAGS="-I/usr/local/zlib/include&quo ...

  7. poj3414(bfs)

    题目链接:http://poj.org/problem?id=3414 题意:给你两个容器 A  B 问是否能够经过有限的步骤倒水,得到容量为 C 的水,输出最小的步数,同时输出每一步的操作.如果不能 ...

  8. Effective C++ -- 构造析构赋值运算

    05.了解C++默默编写并调用哪些函数 编译产生的析构函数时non-virtual,除非这个类的基类析构函数为virtual 成员变量中有引用和const成员时,无法自己主动生成copy assign ...

  9. 【Ruby】Ruby的model学习——Active Record Associations

    在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 一.怎样定义关联 两个model之间经常会存在关联关系,为了解决这些关联引起的复 ...

  10. adt-bundle更新eclipse,以及搭建android环境

    曾经开发一直去android官网下载adt-bundle的.里面已经包括了eclipse和android SDK,搭建android环境特别方便,仅仅须要3步:1.下载并安装jdk(也就是jar se ...