hihocoder-Week173--A Game
hihocoder-Week173--A Game?
A Game
時間限制:10000ms 單點時限:1000ms 內存限制: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.
樣例輸入?
?
使用區間dp,
?
但是我的這種方法只ac了50%, 應該是dp[i][j][1] = max( dp[i][j][1] , ?min( dp[i+1][j][0] , dp[i][j-1][0])?
應該對方的策略不是讓我方最少,而是對方也取得最優。
?
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])?
雙方都在求最優,所以 dp[i][j] 指的是當前下手的選手,可以取得的最優成果。 所以當前狀態是依賴于前面的 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; }
?
轉載于:https://www.cnblogs.com/zhang-yd/p/7718448.html
總結
以上是生活随笔為你收集整理的hihocoder-Week173--A Game的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SaltStack匹配target-第六
- 下一篇: mysql慢查询工具