"CF 731E" Funny Game (DP)
Main topic:
n number of words in a row, two people to play games.
The rule of the game is that each time you can select more than 2 numbers from the leftmost merge, the score is the number of selected and the new number generated after the merge is the selected number.
Until there's a number left, the game is over.
Two people want to be with each other's final score difference is as large as possible, ask how much the difference between the final score and the opponent. The two are smart enough.
The idea of game set on DP
Consider Dp[i] to pre-merge the 1~i numbers, generate a new game situation, start playing from the current situation, the difference between the initiator and the final score.
Sum[i] is the number of 1~i and
So Dp[i]=max (Sum[j]−dp[j]) (i<j≤n) dp[i] = max (Sum[j]-dp[j]) (i
Because for Dp[i], the initiator will consider the best scenario for merging 1~j numbers. The combined initiator score is sum[j], and then the solution is poor, whichever is best.
Run from N to 1 O (n) solve
The code is as follows:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack&
Gt #include <list> #include <algorithm> #include <map> #include <set> #define LL Long Long #define Pr pair<int,int> #define FREAD (CH) freopen (CH, "R", stdin) #define FWRITE (CH) freopen (CH, "w", stdout) using namespace s
td
const int INF = 0X3F3F3F3F;
const int mod = 1E9+7;
Const double EPS = 1e-8;
const int MAXN = 212345;
int NUM[MAXN];
LL SUM[MAXN],DP[MAXN];
int main () {//fread ("");
Fwrite ("");
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];
} LL mx = sum[n];
Dp[n] = Sum[n];
for (int i = n-1; I >= 1; i.) {dp[i] = mx;
MX = max (mx,sum[i]-dp[i]); } PrintF ("%lld\n", dp[1]);
return 0; }