Codeforces Round #223 (Div. 2)--A. Sereja and Dima
1 second
256 megabytes
standard input
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.
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.
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.
4
4 1 2 10
12 5
7
1 2 3 4 5 6 7
16 12
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #223 (Div. 2) C
C. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 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+ ...
- 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 ...
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
由于n比较小,直接暴力解决 #include <iostream> #include <vector> #include <algorithm> #include ...
- Codeforces Round #243 (Div. 2) B. Sereja and Mirroring
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
随机推荐
- MVC多语言应用
MVC多语言应用 最近发现资源文件是个好东西, 用的好了可以给开发人员借阅不少的时间. 例如做一个多语言的网站, 资源文件就有不小的用处. 这里以MVC4模版项目的登录页为例, 简单说一下过程: 1. ...
- HttpGet协议与正则表达
使用HttpGet协议与正则表达实现桌面版的糗事百科 写在前面 最近在重温asp.net,找了一本相关的书籍.本书在第一章就讲了,在不使用浏览器的情况下生成一个web请求,获取服务器返回的内容.于 ...
- jQuery 自学笔记—5 遍历
什么是遍历? jQuery 遍历,意为“移动”,用于根据其相对于其他元素的关系来“查找”(或选取)HTML 元素.以某项选择开始,并沿着这个选择移动,直到抵达您期望的元素为止. 下图展示了一个家族树. ...
- 收集经常使用的.net开源项目
Json.NET http://json.codeplex.com/ Json.Net是一个读写Json效率比較高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Lin ...
- 怎样使用 iOS 7 的 AVSpeechSynthesizer 制作有声书(1)
原文: http://www.raywenderlich.com/64623/make-narrated-book-using-avspeechsynthesizer-ios-7 随着 PageVie ...
- SE 2014年3月31日
一. 描述OSPF划分区域的优势. OSPF划分区域的优势主要表现在以下几个方面: 1. 当网络中路由器的数量增大时,划分区域有利于减轻一部分性能较低的设备的处理和维护LSA数据库. 2. 区域的划分 ...
- Java调用cmd压缩文件
今天在做一个java调用windows的压缩命令时遇到一奇怪问题代码如下: String cmd ="C:/Program Files (x86)/WinRAR/rar.exe a c:/t ...
- 《JavaScript设计模式与开发实践》读书笔记之观察者模式
1.观察者模式 观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知. JavaScript中通常采用事件模型替代传统的观察者模式 1.1 逐步实现观 ...
- UVALive - 4621 Cav 贪心 + 分析
题目大意:有一张洞穴地图,要在这个洞穴里面存放水,要求水不能碰到洞穴顶部.如今给出每一个位置的顶部位置和地面高度.问最多能够放多少水 解题思路:根据物理定理,每一段有水的连续区间,水位高度必须相等 所 ...
- 潜水JVM
原文地址:http://blog.jamesdbloom.com/JVMInternals.html(转载请注明出处和本文地址英文原文) 本文简要解析JVM的内部结构.下图显示了一个典型的一块JVM( ...