算法學習【15】—— 1176. Two Ends

題目來源:http://soj.me/1176

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.

思路:剛剛開始就走進一個誤區,以爲是找規律,從而推測出判斷偶數位牌和奇數位牌的差,就是所要求的,結果WA了。後來,覺得確實不妥,這不是最大的。就採用動規了。largestScroe[a][b]記錄從第a張牌到第b張牌開始挑選,可以取得的最大分數。(對方用貪心)這樣就有了dp函數中的判斷。只有兩種情況,拿左邊的或者拿右邊的,找出最大值即可。

代碼:

/*
Link: http://soj.me/1176
Author: BetaBin
Date: 2012/07/25
*/
#include <stdio.h>

int cards[1005];
int largestScore[1005][1005];
int cardNum;
int cardSum;
int gameId;

int i,j;
int bestScore;

int dp(int begin, int end)
{
    int situation_left;
    int situation_right;

    if(end - begin <= 1)
    {
        return (cards[begin] > cards[end]) ? cards[begin] : cards[end];
    }

    if(-1 != largestScore[begin][end])
    {
        return largestScore[begin][end];
    }

    if(cards[begin + 1] >= cards[end])
    {
        situation_left = cards[begin] + dp(begin + 2, end);
    }
    else
    {
        situation_left = cards[begin] + dp(begin + 1, end - 1);
    }

    if(cards[begin] >= cards[end - 1])
    {
        situation_right = cards[end] + dp(begin + 1, end - 1);
    }
    else
    {
        situation_right = cards[end] + dp(begin, end - 2);
    }

    largestScore[begin][end] = (situation_left > situation_right) ? situation_left : situation_right;
    return largestScore[begin][end];
}

int main()
{
    gameId = 0;
    while(scanf("%d", &cardNum) && 0 != cardNum)
    {
        ++gameId;
        cardSum = 0;
        for(i = 1; i <= cardNum; ++i)
        {
            scanf("%d", &cards[i]);
            cardSum += cards[i];
            for(j = 1; j < i; ++j)
            {
                largestScore[j][i] = -1;
            }
        }
        bestScore = dp(1, cardNum);
        printf("In game %d, the greedy strategy might lose by as many as %d points.\n", gameId, bestScore + bestScore - cardSum);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章