POJ 2738 Two Ends(记忆化搜索+dp)

落日映苍穹つ 2022-06-10 05:18 266阅读 0赞

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

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

Sample Output

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

题解:

题意:

两个人都从数列两边取数,第一个人吃了聪明豆,会为长远考虑,第二个人是脑残,只选两端大的,每个人每次结果累加,问他们的最大差值为多少

思路:

因为我不是我们队搞dp的,所以完全没有hhhhh,因为老赵一直在怼A题,我又把我该做的都做完了,所以我就硬着头皮上了,看了题解看懂了就照着打了一遍qwq

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<deque>
  12. #define M (t[k].l+t[k].r)/2
  13. #define lson k*2
  14. #define rson k*2+1
  15. #define ll long long
  16. #define INF 100861111;
  17. using namespace std;
  18. int a[1005];
  19. int dp[1005][1005],n;
  20. int done(int x,int y)
  21. {
  22. if(dp[x][y]!=-1)//如果已经有值了
  23. return dp[x][y];
  24. if(x+1==y)//如果选到了最后两个
  25. return dp[x][y]=abs(a[x]-a[y]);
  26. int sa,sb;
  27. if(a[x+1]>=a[y])//如果a取x处的情况
  28. sa=done(x+2,y)+a[x]-a[x+1];
  29. else
  30. sa=done(x+1,y-1)+a[x]-a[y];
  31. if(a[x]<a[y-1])//如果a取y处的情况
  32. sb=done(x,y-2)+a[y]-a[y-1];//递归+差值
  33. else
  34. sb=done(x+1,y-1)+a[y]-a[x];
  35. return dp[x][y]=max(sa,sb);//取最优
  36. }
  37. int main()
  38. {
  39. int i,j=1;
  40. while(scanf("%d",&n)!=EOF&&n)
  41. {
  42. for(i=0;i<n;i++)
  43. scanf("%d",&a[i]);
  44. memset(dp,-1,sizeof(dp));
  45. printf("In game %d, the greedy strategy might lose by as many as %d points.\n",j,done(0,n-1));
  46. j++;
  47. }
  48. return 0;
  49. }

发表评论

表情:
评论列表 (有 0 条评论,266人围观)

还没有评论,来说两句吧...

相关阅读

    相关 hdu 1078 (dp记忆搜索

    题意:有一个n\n的方格,每个格子有一个价值,经过这个格子就可以得到这个价值,每次可以横着或者竖着一步最多走K格,并且每次走的格子要比上一个格子的价值大,问现在从位置(1,1)