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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

TZOJ 5101 A Game(区间DP)

發(fā)布時間:2023/11/29 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TZOJ 5101 A Game(区间DP) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

描述

Consider the following two-player game played with a sequence of N positive integers (2 <= N <= 100) laid onto a 1 x N game board. Player 1 starts the game. The players move alternately by selecting a number from either the left or the right end of the gameboar. That number is then deleted from the board, and its value is added to the score of the player who selected it. A player wins if his sum is greater than his opponents.

Write a program that implements the optimal strategy. The optimal strategy yields maximum points when playing against the "best possible" opponent. Your program must further implement an optimal strategy for player 2.

輸入

Line 1: ? ?N, the size of the board ? ?

Line 2-etc: ? ?N integers in the range (1..200) that are the contents of the game board, from left to right ??

輸出

Two space-separated integers on a line: the score of Player 1 followed by the score of Player 2.

樣例輸入

6
4 7 2 9
5 2

樣例輸出

18 11

題意

1*N的游戲盤,每個格子都有價值,玩家一次拿最左或最右,拿了刪掉這個格子,問玩家1先拿,玩家2后拿,問倆人都最優(yōu)可以拿多少分

題解

dp[i][j]=區(qū)間[i,j]先手-后手的差值

很容易推出dp[i][j]可由小區(qū)間得到

dp[i][j]=a[i]-dp[i+1][j];

dp[i][j]=a[j]-dp[i][j-1];

最終我們得到dp[1][n]為先手-后手的差值

設(shè)玩家1拿了V1,玩家2拿了V2

V1+V2=Σa;

V1-V2=dp[1][n];

求得

V1=(Σa+dp[1][n])/2;

V2=(Σa-dp[1][n])/2;

代碼

1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 int n; 7 while(scanf("%d",&n)!=EOF) 8 { 9 int a[105],dp[105][105]={0},sum=0; 10 for(int i=1;i<=n;i++) 11 scanf("%d",&a[i]),sum+=a[i]; 12 for(int len=1;len<=n;len++) 13 for(int i=1;i<=n-len+1;i++) 14 { 15 int j=i+len-1; 16 dp[i][j]=max(a[i]-dp[i+1][j],a[j]-dp[i][j-1]); 17 } 18 printf("%d %d\n",(sum+dp[1][n])/2,(sum-dp[1][n])/2); 19 } 20 return 0; 21 }

轉(zhuǎn)載于:https://www.cnblogs.com/taozi1115402474/p/10269781.html

總結(jié)

以上是生活随笔為你收集整理的TZOJ 5101 A Game(区间DP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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