"CF 731E" Funny Game (DP)

Source: Internet
Author: User
Tags fread

"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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.