日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【LightOJ - 1031】Easy Game (区间dp,博弈)

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LightOJ - 1031】Easy Game (区间dp,博弈) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

You are playing a two player game. Initially there are?n?integer numbers in an array and player?A?and?B?get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player?A?starts the game then how much more point can player?A?get than player?B?

Input

Input starts with an integer?T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer?N (1 ≤ N ≤ 100)?denoting the size of the array. The next line contains?Nspace separated integers. You may assume that no number will contain more than?4?digits.

Output

For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input

2

?

4

4 -10 -20 7

?

4

1 2 3 4

Sample Output

Case 1: 7

Case 2: 10

?

解題報告:

? ? 做過的原題【UVA - 10891 Game of Sum 】【HRBUST - 1622】 Alice and Bob 不解釋了、、

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; const int INF = 0x3f3f3f3f; ll dp[205][205]; ll sum[205],a[205]; ll dfs(int l,int r) {if(dp[l][r] != -1) return dp[l][r];if(l == r) return dp[l][l]=a[l];ll maxx = -INF;for(int i = l+1; i<=r; i++) {maxx = max(maxx , sum[r] - sum[l-1] - dfs(i,r));}for(int i = r-1; i>=l; i--) {maxx = max(maxx , sum[r] - sum[l-1] - dfs(l,i));}maxx = max(maxx,sum[r] - sum[l-1]);return dp[l][r] = maxx; } int main() {int t,n;cin>>t;int iCase = 0;while(t--) {scanf("%d",&n);memset(sum,0,sizeof sum);memset(dp,-1,sizeof dp);for(int i = 1; i<=n; i++) scanf("%lld",&a[i]),sum[i] = sum[i-1] + a[i];printf("Case %d: %lld\n",++iCase,2*dfs(1,n) - sum[n]);}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【LightOJ - 1031】Easy Game (区间dp,博弈)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。