1176. Two Ends(搜索破解此题)

来源:互联网 发布:茶叶网络推广 编辑:程序博客网 时间:2024/04/30 14:33
Two Ends
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2304 Accepted: 1092

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 48 1 2 3 4 5 6 7 88 2 2 1 5 3 8 7 30

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
分析:此题原则为DP,但我学的是搜索,对搜索感觉敏锐一点,就用搜索给做了,做完看题解,发现是DP,仔细一想,果然是DP(话说本人也有一点DP基础),
我只说搜索题解,有兴趣的可以可以再看DP题解一起研究下。
done[i][j]存卡片被取的剩下第i张到第j张的最高点数,等到该层结束(层即回合数,队列中同一层都都是连续的)该层的done数组更新结束后,我们会发现
队列中点数小于done中存储的最大值的都不必继续算下去,因为同样被取到剩下第i张到第j张,点数较小的最终结果不可能最优,此时剪掉该枝(其实还有同样也为最大值的点没被剪掉,但对此题来讲,这样就可以AC,加强剪枝可以做到媲美DP)。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;int da[1005],done[1005][1005];int n,ans;struct node{    int l,r,sum;    node(int ll=0,int rr=0,int ss=0)    {        l=ll;r=rr;sum=ss;    }};void bfs(){    queue<node>q;    memset(done,0xbf,sizeof done);    q.push(node(1,n,0));    while(!q.empty())    {        node x=q.front();q.pop();        if(x.sum<done[x.l][x.r])continue;//剪掉值较小的        int st=x.l,en=x.r,sum=x.sum;        if(st==en)        {            if(sum+da[st]>ans)ans=sum+da[st];            continue;        }        if(st>en)        {            if(sum>ans)ans=sum;            continue;        }        if(da[st]>=da[en-1])        {            if(sum+da[en]-da[st]>done[st+1][en-1])                done[st+1][en-1]=sum+da[en]-da[st],q.push(node(st+1,en-1,sum+da[en]-da[st]));        }        else        {            if(sum+da[en]-da[en-1]>done[st][en-2])                done[st][en-2]=sum+da[en]-da[en-1],q.push(node(st,en-2,sum+da[en]-da[en-1]));        }        if(da[st+1]>=da[en])        {            if(sum+da[st]-da[st+1]>done[st+2][en])                done[st+2][en]=sum+da[st]-da[st+1],q.push(node(st+2,en,sum+da[st]-da[st+1]));        }        else        {            if(sum+da[st]-da[en]>done[st+1][en-1])                done[st+1][en-1]=sum+da[st]-da[en],q.push(node(st+1,en-1,sum+da[st]-da[en]));        }    }    return ;}int main(){    int m,i,cas=0;    while(~scanf("%d",&n)&&n)    {        for(i=1;i<=n;i++)            scanf("%d",&da[i]);        ans=-INF;        bfs();        printf("In game %d, the greedy strategy might lose by as many as %d points.\n",++cas,ans);    }    return 0;}

AC代码:

0 0
原创粉丝点击