• sicily 1176 Two Ends


    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.

    分析:

    如果只是为了正确,深搜是种可行的办法,但是显然数据规模不小,深搜要超时。用空间换时间,用DP的办法做,也算是中记忆化搜索。每次抽牌方法需要比较的是抽出两端任意一张后的取法最大所得,而另一人取法是贪心,递归可求。

    代码:

    // Problem#: 1176
    // Submission#: 1914687
    // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
    // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
    // All Copyright reserved by Informatic Lab of Sun Yat-sen University
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    #define MAX 1000
    
    int num[MAX];
    int buf[MAX][MAX];
    
    inline int max(int a, int b) { return a > b ? a : b; }
    
    int dp(int a, int b) {
        if (a == b-1) return buf[a][b] = max(num[a], num[b]);
        if (buf[a][b]) return buf[a][b];
        int t1, t2;
        t1 = num[a];
        t1 += (num[a + 1] >= num[b]) ? dp(a + 2, b) : dp(a + 1, b - 1);
        t2 = num[b];
        t2 += (num[a] >= num[b - 1]) ? dp(a + 1, b - 1) : dp(a, b - 2);
        return buf[a][b] = max(t1, t2);
    }
    
    int main(void) {
        int n, s, cnt = 1;
        while (cin >> n && n) {
            s = 0;
            for (int i = 0; i < n; ++i) {
                cin >> num[i];
                s += num[i];
            }
            memset(buf, 0, sizeof(buf));
            cout << "In game " << cnt++ << ", the greedy strategy might lose by as many as " << 2 * dp(0, n - 1) - s << " points." << endl;
        }
        return 0;
    }
  • 相关阅读:
    Delphi 10.1.2 berlin开发跨平台APP的几点经验
    (矫情)关于编程(能力第一,其它不要多想)
    Xdite:永葆热情的上瘾式学习法(套路王:每天总结自己,反省自己的作息规律,找到自己的幸运时间、幸运方法,倒霉时间、倒霉方法。幸运是与注意力挂钩的。重复才能让自己登峰造极,主动去掉运气部分来训练自己。游戏吸引自己的几个原因非常适合训练自己)good
    无锡美新赵阳:创业18年,一辈子做好一家企业(创业是一种生活方式;为了赚钱而创业,那是扯淡”。最重要的是做自己喜欢做的事情)
    Delphi中流对象的应用
    多样性的配置来源
    WeText项目的服务端
    集线器,交换机,路由器
    不知道的JavaScript
    IPerf网络测试工具
  • 原文地址:https://www.cnblogs.com/ciel/p/2883650.html
Copyright © 2020-2023  润新知