Codeforces Round #223 (Div. 2)--A. Sereja and Dima
2014-10-29 07:02
489 查看
Sereja and Dima
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
output
input
output
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代码:
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
- codeforces #233 (Div2) A. Sereja and Dima【模拟】+B. Sereja and Stairs【暴力】
- Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs
- Codeforces Round #223 (Div. 2)--A. Sereja and Dima
- Codeforces Round #223 (Div. 1) C. Sereja and Brackets
- codeforces Round #223 (Div.1) C. Sereja and Brackets【线段树】
- Codeforces Round #214 (Div. 2) C. Dima and Salad 背包
- Codeforces Round #223 (Div. 2) B. Sereja and Stairs
- Codeforces Round #167 (Div. 2) 272E Dima and Horses
- Codeforces Round #223 (Div. 2)——B. Sereja and Stairs
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
- Codeforces Round #208 (Div. 2)E. Dima and Kicks
- cf-Round #214 (Div. 2)-D-Dima and Trap Graph-dfs+二分
- Codeforces Round #208 (Div. 2) Dima and Text Messages
- Codeforces Round #243 (Div. 2)——Sereja and Table
- Codeforces Round #243 (Div. 1)——Sereja and Squares
- CodeForces Round 213 Div 2 E Sereja and Brackets 线段树
- Codeforces Round #223 (Div. 2)-C. Sereja and Prefixes
- Codeforces Round #223 (Div. 1) C. Sereja and Brackets
- codeforces #262 DIV2 B题 Little Dima and Equation