Description
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 Input
4 4 1 2 10
12 5
7 1 2 3 4 5 6 7
16 12
Hint
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.
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,i,num[1010],ssum,dsum,l,r;
while(cin>>n){
ssum=dsum=0;
for(i=1;i<=n;i++){
cin>>num[i];
}
l=1;
r=n;
for(i=1;i<=n;i++){
if(i%2==1){
if(num[l]>=num[r]){
ssum+=num[l];
l++;
}
else{
ssum+=num[r];
r--;
}
}
else{
if(num[l]>=num[r]){
dsum+=num[l];
l++;
}
else{
dsum+=num[r];
r--;
}
}
}
cout<<ssum<<" "<<dsum<<endl;
}
return 0;
}
本文介绍了一个简单的游戏策略模拟问题,玩家轮流从两端取走带有不同数值的卡片,目标是使自己的分数最大化。通过一种贪婪策略来决定每轮选择哪张卡片,并最终计算两个玩家的得分。
322

被折叠的 条评论
为什么被折叠?



