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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hihocoder-Week173--A Game

發(fā)布時(shí)間:2023/12/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hihocoder-Week173--A Game 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

hihocoder-Week173--A Game?

A Game

時(shí)間限制:10000ms 單點(diǎn)時(shí)限:1000ms 內(nèi)存限制:256MB

描述

Little Hi and Little Ho are playing a game. There is an integer array in front of them. They take turns (Little Ho goes first) to select a number from either the beginning or the end of the array. The number will be added to the selecter's score and then be removed from the array.

Given the array what is the maximum score Little Ho can get? Note that Little Hi is smart and he always uses the optimal strategy.?

輸入

The first line contains an integer N denoting the length of the array. (1 ≤?N?≤ 1000)

The second line contains?N?integers?A1,?A2, ...?AN, denoting the array. (-1000 ≤?Ai?≤ 1000)

輸出

Output the maximum score Little Ho can get.

樣例輸入
4 -1 0 100 2
樣例輸出
99

?

?

使用區(qū)間dp,

?

但是我的這種方法只ac了50%, 應(yīng)該是dp[i][j][1] = max( dp[i][j][1] , ?min( dp[i+1][j][0] , dp[i][j-1][0])?

應(yīng)該對(duì)方的策略不是讓我方最少,而是對(duì)方也取得最優(yōu)。

?

AC 50% Code:?

#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAXN = 1000 + 10; int n, num[MAXN], dp[MAXN][MAXN][2]; int main(){freopen("in.txt", "r", stdin); int n; scanf("%d", &n); for(int i=1; i<=n; ++i){scanf("%d", &num[i]); } memset(dp, 0, sizeof(dp)); for(int i=1; i<=n; ++i){dp[i][i][0] = num[i]; }for(int i=n; i>=1; --i){for(int j=i; j<=n; ++j){dp[i][j][0] = max( dp[i+1][j][1] + num[i], dp[i][j][0] ); dp[i][j][0] = max( dp[i][j-1][1] + num[j], dp[i][j][0] ); dp[i][j][1] = max( dp[i][j][1], min( dp[i+1][j][0] , dp[i][j-1][0] ) ); }} int ans = dp[1][n][0]; printf("%d\n", ans);return 0; }

  

?

?

所以,?

?dp[i][j] = max( sum(i,j) - dp[i+1][j], sum(i,j) - dp[i][j-1])?

雙方都在求最優(yōu),所以 dp[i][j] 指的是當(dāng)前下手的選手,可以取得的最優(yōu)成果。 所以當(dāng)前狀態(tài)是依賴于前面的 dp[i][j-1] 和 dp[i+1][j] ,?

?

AC Code?

#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAXN = 1000 + 10; int n, num[MAXN], sum[MAXN], dp[MAXN][MAXN]; int main(){int n; scanf("%d", &n); sum[0] = 0; for(int i=1; i<=n; ++i){scanf("%d", &num[i]); sum[i] = sum[i-1] + num[i]; } memset(dp, 0, sizeof(dp)); for(int i=1; i<=n; ++i){dp[i][i] = num[i]; }for(int i=n; i>=1; --i){for(int j=i+1; j<=n; ++j){dp[i][j] = (sum[j] - sum[i-1]) - min( dp[i+1][j], dp[i][j-1] ); }} int ans = dp[1][n]; printf("%d\n", ans);return 0; }

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhang-yd/p/7718448.html

總結(jié)

以上是生活随笔為你收集整理的hihocoder-Week173--A Game的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。