• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            oyjpArt ACM/ICPC算法程序設(shè)計(jì)空間

            // I am new in programming, welcome to my blog
            I am oyjpart(alpc12, 四城)
            posts - 224, comments - 694, trackbacks - 0, articles - 6

            Asked by nsdy.wu on PKU2738

            Posted on 2006-08-17 00:12 oyjpart 閱讀(858) 評論(6)  編輯 收藏 引用

            呵呵 居然有人問我題目啊~ 倍感榮幸!~
            ?這可是我人生中的第一次!~
            不能辜負(fù)了人家的期望呀,拼命也要做做-_-|||
            題目:
            PKU 2738

            Two Ends
            Time Limit:1000MS? Memory Limit:65536K
            Total Submit:605 Accepted:262

            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.

            Source
            East Central North America 2005

            思維:
            看到這題首先分析情形。初看似乎可以貪心(即每人2次的的一個(gè)回合內(nèi)),偶WA了一次貪心。但是WA后,發(fā)現(xiàn)貪心出不了最優(yōu)解(因?yàn)榭赡軙卸嘟M相同的解).
            搜索顯然不行 ,1000的長度 + multiple test case => absolutely TLE.
            于是考慮DP.
            是否滿足最優(yōu)子結(jié)構(gòu)?恩。因?yàn)槿肿顑?yōu)解包含局部最優(yōu)解.
            是否滿足無后效性? 恩。當(dāng)前所作決策可由當(dāng)前狀態(tài)唯一確定.
            OK.DP.
            首先是狀態(tài).不用說,用d[i][j]表示當(dāng)前i->j的最大可能值(即2號選手少的分)
            接著是狀態(tài)轉(zhuǎn)移.d[i][j]可能是兩個(gè)方向轉(zhuǎn)過來的,即選了最前面一個(gè)或最后面一個(gè).然后2nd player也應(yīng)該會有一個(gè)相應(yīng)的選擇.(具體見程序)
            做好初始化的工作,就OK啦

            Solution:

            #include <stdio.h>
            #include <string.h>
            #include <math.h>

            int a[1010], n, d[1010][1010];

            int SecondDecision(int i, int j) //return which the Second player would like to get
            {
            if(a[i] >= a[j]) return i;
            return j;
            }

            int Cal() //Dynamic Programming
            {
            int l, i, j, temp = 0;
            for(i=1; i<n; i++)
            d[i][i+1] = abs(a[i] - a[i+1]);
            for(l = 4; l <= n; l += 2) //case length=2 has been calculated
            for(i=1; i<=n-l+1; i++)
            {
            j = i+l-1;
            if(SecondDecision(i+1, j) == j && d[i+1][j-1] >= 0)
            {
            temp = d[i+1][j-1] + a[i] - a[j];
            d[i][j] = temp > d[i][j] ? temp : d[i][j];
            }
            else if(d[i+2][j] >= 0)
            {
            temp = d[i+2][j] + a[i] - a[i+1];
            d[i][j] = temp > d[i][j] ? temp : d[i][j];
            }

            if(SecondDecision(i, j-1) == j-1 && d[i][j-2] >= 0)
            {
            temp = d[i][j-2] + a[j] - a[j-1];
            d[i][j] = temp > d[i][j] ? temp : d[i][j];
            }
            else if(d[i+1][j-1] >= 0)
            {
            temp = d[i+1][j-1] + a[j] - a[i];
            d[i][j] = temp > d[i][j] ? temp : d[i][j];
            }
            }
            return d[1][n];
            }

            int main()
            {
            // freopen("ends.in", "r", stdin);
            int i, ntc = 0;
            while(scanf("%d", &n), n>0)
            {
            ntc++;
            memset(a, 0, sizeof(a));
            memset(d, -1, sizeof(d));
            for(i=1; i<=n; i++)
            scanf("%d", &a[i]);
            printf("In game %d, the greedy strategy might lose by as many as %d points.\n", ntc, Cal());
            }
            return 0;
            }
            //代碼寫的不好 將就著看吧
            呵呵 總算沒辜負(fù)人家期望啊~ 好開心~

            Feedback

            # re: Asked by nsdy.wu on PKU2738   回復(fù)  更多評論   

            2006-08-17 03:48 by
            強(qiáng)!~

            # re: Asked by nsdy.wu on PKU2738   回復(fù)  更多評論   

            2006-08-17 12:04 by cainiao
            thank you for your answer ~~~

            # re: Asked by nsdy.wu on PKU2738   回復(fù)  更多評論   

            2006-08-17 15:50 by 踏雪赤兔
            哈哈~~好心情,分享了

            # re: Asked by nsdy.wu on PKU2738   回復(fù)  更多評論   

            2006-08-19 12:54 by 偉莉
            竟是別有洞天!果真狡兔三窟。
            遇上這樣的程癡,自覺慚愧 -_-||
            ANYWAY,加油!^^

            # re: Asked by nsdy.wu on PKU2738   回復(fù)  更多評論   

            2006-08-27 10:37 by 路過
            牛人!!!!!!!!

            # re: Asked by nsdy.wu on PKU2738   回復(fù)  更多評論   

            2006-08-27 10:38 by 路過
            郵箱:zhangbin602@163.com

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久久久国色AV免费看图片| 久久人人超碰精品CAOPOREN| 色婷婷久久综合中文久久蜜桃av| 久久午夜无码鲁丝片秋霞 | 色婷婷噜噜久久国产精品12p| 久久夜色精品国产亚洲| 久久人人爽人人爽AV片| 无码人妻久久一区二区三区| 精品免费久久久久久久| 久久露脸国产精品| 久久天天躁狠狠躁夜夜网站| 久久久久久久久久久免费精品| 一本色综合网久久| 久久国产成人| 国内精品久久久人妻中文字幕| 国产三级久久久精品麻豆三级| 深夜久久AAAAA级毛片免费看| 久久久久亚洲AV无码永不| 无码乱码观看精品久久| 曰曰摸天天摸人人看久久久| 日日躁夜夜躁狠狠久久AV| 国产免费久久久久久无码| 久久精品国产亚洲AV久| 国产成人精品久久| 99久久免费国产特黄| 色狠狠久久AV五月综合| 久久亚洲日韩看片无码| 色狠狠久久综合网| 亚洲精品第一综合99久久| 久久精品草草草| 欧美熟妇另类久久久久久不卡| 国产美女亚洲精品久久久综合| 久久婷婷色香五月综合激情| 色婷婷久久久SWAG精品| 无码乱码观看精品久久| 青春久久| 久久综合综合久久狠狠狠97色88| 久久精品国产99久久久| 亚洲av日韩精品久久久久久a | 97久久超碰成人精品网站| 欧洲精品久久久av无码电影|