URAL 2000. Grand Theft Array V(贪心啊)

来源:互联网 发布:组三组六计划软件 编辑:程序博客网 时间:2024/04/29 07:25

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2000


2000. Grand Theft Array V

Time limit: 0.5 second
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 ≤ 105). 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

inputoutput
101 2 3 4 5 6 7 8 9 04 8
21 24
Problem Author: Ilya Kuchumov. (Prepared by Kirill Devyatkin)
Problem Source: Ural Regional School Programming Contest 2013

题意:

两个人从给出的两个点出发,每次只能向左或向右移一步!每个人所得分数就是移到的单元格的分数,然后把所在单元格的分数设置为零!

求两个人分别能得到的最大分数!

PS:

贪心原则!最开始一定是先向对方所在的位置移动!移动到两个人初始位置距离的一半的时候在往回走! 这样对方永远不可能追上你!

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){    int n;    int a[100047];    while(~scanf("%d",&n))    {        int sum = 0;        for(int i = 1; i <= n; i++)        {            scanf("%d",&a[i]);            sum += a[i];        }        int p1, p2;        scanf("%d%d",&p1,&p2);        int sum1 = 0, sum2 = 0;        int ans = 0;        if(p1 > p2)        {            int tt = (p1-p2)/2;//            for(int i = 1; i <= p2+tt; i++)//            {//                sum2 += a[i];//            }            for(int i = p1-tt; i <= n; i++)            {                sum1 += a[i];            }            ans = sum1;//            ans = max(sum1, sum2);        }        else if(p1 == p2)        {            for(int i = 1; i <= p1; i++)            {                sum1 += a[i];            }            for(int i = p2; i <= n; i++)            {                sum2 += a[i];            }            ans = max(sum1, sum2);        }        else if(p1 < p2)        {            int tt = (p2-p1)/2;            for(int i = 1; i <= p1+tt; i++)            {                sum1 += a[i];            }//            for(int i = p2-tt+1; i <= n; i++)//            {//                sum2 += a[i];//            }//            ans = max(sum1, sum2);            ans = sum1;        }        printf("%d %d\n",ans,sum-ans);    }    return 0;}/*51 2 3 4 52 351 2 3 4 52 4*/


1 0
原创粉丝点击