2000. Grand Theft Array V
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
A long anticipated game called Grand Theft Array V is about to appear in shops! What, haven’t you heard of it? Then we must tell you all about it!
The gameplay in GTA V takes place on a one-dimensional array of integers. The game has two players, each player has his own specified starting position. Players move in turns. During each turn a player takes a number written in his current cell, then writes a zero in it and moves to the left or right adjacent cell. Naturally, the player cannot move beyond the boundaries of the array. At some moment of time two players can be located in the same cell. A player’s score is the sum of all numbers he earns during the game. The game ends when zeroes are written in all cells of the array.
Now please calculate the maximum number of points the first and the second player can get (the first player moves first, naturally), if they play optimally well, that is, if they try to maximize their score and if there are multiple variants of maximizing one’s own score, they try to minimize the opponent’s score.
Input
The first line contains an integer
n that is the size of array (10 ≤
n ≤ 10
5). The second line contains
n integers representing the initial array. All elements of the array are non-negative and do not exceed 10 000. The third line contains two integers that are the starting positions of the first and the second player, correspondingly. The cells of the array are indexed starting from one.
Output
Output the score of the first and the second player correspondingly if both play optimally well.
Sample
input | output |
---|---|
10 1 2 3 4 5 6 7 8 9 0 4 8 | 21 24 |
题意:n表示有多少格子, 后面输入的是 f和s 所处的格子。
f和s轮流一步步走,f先走,格子经过的地方都算自己得到的分数。
输出各自最高得分。
做法:贪心, f和s之间的数是偶数就平分,靠近f的数归f靠近s的归s。 如果是基数,f多得一个数。然后 f 和s各自到终点和起点一侧的分数归自己。 直接输出就好了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define INF 999999999
#define eps 0.00001
#define LL __int64d
#define pi acos(-1.0)
int sum[100100],a[100100];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
sum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
sum[i]=a[i]+sum[i-1];
}
int f,s;
scanf("%d%d",&f,&s);
int sco_f,sco_s;
if(f==s)
{
if(sum[n]-sum[f]<sum[f-1])
{
printf("%d %d\n",sum[f-1]+a[f],sum[n]-sum[f]);
}
else
{
printf("%d %d\n",sum[n]-sum[f]+a[f],sum[f-1]);
}
}
else
{
int mid=(f+s)/2;
if(abs(f-s)%2==0)//oushu
{
if(f<s)
{
printf("%d %d\n",sum[mid],sum[n]-sum[mid]);
}
else
{
printf("%d %d\n",sum[n]-sum[mid-1],sum[mid-1]);
}
}
else
{
if(f<s)
{
printf("%d %d\n",sum[mid],sum[n]-sum[mid]);
}
else
{
printf("%d %d\n",sum[n]-sum[mid],sum[mid]);
}
}
}
}
return 0;
}