sicily 1176. Two Ends

1176. Two Ends

Constraints

Time Limit: 1 secs, Memory Limit: 64 MB

Description

In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest -- we'll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)
3 2 10 4
You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.

Input

There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.

Output

For each test case you should print one line of output of the form:
In game m, the greedy strategy might lose by as many as p points.
where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player's score and second player's score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.

Sample Input

4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

Sample Output

In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

题目分析

a任意取数,b取最大(默认左边),求最大的差值

一开始没有记录中间结果超时了

后来看别人代码做了一点优化

maxAScore[l][r]记录a在[l,r]区间能获得的最大和,

每轮取数只要计算a的得分,最后将a的得分*2-总分即可获得最大的差值

减少了运算,在做回滚的时候更容易理解代码


#include <stdio.h>
#include <memory.h>

int digit[1001];
int maxAScore[1001][1001];

int search(int left, int right) {
  if (right - left == 1) {
    maxAScore[left][right] = digit[left] > digit[right] ? digit[left] : digit[right];
  }
  if (maxAScore[left][right]!=-1)
    return maxAScore[left][right];
  int temp1 = digit[left];
  temp1 += digit[left+1] < digit[right] ? search(left+1, right-1) : search(left+2, right);
  int temp2 = digit[right];
  temp2 += digit[left] < digit[right-1] ? search(left, right-2) : search(left+1, right-1);
  maxAScore[left][right] = temp1 >= temp2 ? temp1 : temp2;
  return maxAScore[left][right];
}

int main()
{
  int num;
  int id = 1;
  while (scanf("%d", &num)) {
    if (num == 0)
      break;
    int sum = 0;
    for (int i = 1; i <= num; ++i) {
      scanf("%d", &digit[i]);
      sum += digit[i];
    }
    memset(maxAScore, -1, sizeof(maxAScore));
    int max = search(1, num) * 2 - sum;
    printf("In game %d, the greedy strategy might lose by as many as %d points.\n", id++, max);
  }
}



你可能感兴趣的:(sicily 1176. Two Ends)