"Simulation competition 20181025": Game Theory + dp simple Optimization

Source: Internet
Author: User
Description

Yasuo and riven start to practice for a row of \ (n \) dummy persons. Kill the \ (I \) dummy and get \ (C_ I. The two sides took turns and learned from each other in their exercises, so their swords became stronger and stronger. Based on the number of counterfeits killed by the peer \ (k \), you can kill the number of consecutive counterfeits in the range [1, 2 K] \ at the top of the remaining counterfeits. Yasuo first made a move to kill \ (1 \) or \ (2 \) counterfeits. Yasuo secretly called you aside and asked how much essence he could get when both parties adopt the best strategy.

Input

The first line is a positive integer \ (n \), indicating the number of dummy persons.
Next, for the \ (n \) Row, each row has a positive integer \ (C_ I \), which indicates the essence of killing each dummy.

Output

A positive integer indicates the maximum number of excellent values that Yasuo can obtain.

Sample Input
513172
Sample output
9
Example

Yasuo chopped \ (1 \), riven chopped \ (2 \), Yasuo chopped \ (3, 4 \), riven chopped \ (5.

Data range

For the previous \ (10 \ % \) data, \ (n \ Leq 10 \)
For the previous \ (40 \ % \) data, \ (n \ Leq 500 \)
For \ (100 \ % \) data, \ (5 \ Leq n \ Leq 5000, CI \ Leq 10 ^ 9 \)

Question

First, let's talk about the background of the question, and talk about the author who moved the question and made the magic change.

Simple game theory \ (DP \) involves almost no knowledge of game theory.
Obviously, the two are equivalent. \ (F [I] [J] \) indicates the \ (I \) dummy at the end of the current record, the last knife is the maximum value of \ (J \) dummy. Obviously, we can enumerate the number of people \ (k \ In [1, 2j] \) and \ (DP \) states to be transferred. Unfortunately, this complexity is \ (O (N ^ 3) \) and cannot pass all test points.
For optimization, let's write down the \ (DP \) format:
\ (F [I] [J] = max (s [I]-f [I-K] [k]) \), where \ (k \ In [1, 2j] \), \ (s [I] \) represents the sum of \ (I \) Personal \ (C.
The following formula is used:
\ (F [I] [J] = max (F [I] [J-1], s [I]-f [i-2j] [2j], s [I]-f [i-2j + 1] [2j-1]) \)
And then it's gone ......
\ (Code :\)

#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define N 5005#define ll long long#define inf (1ll << 50)template<typename Mytype>void Read(Mytype &p){    p = 0;    char c = getchar();    for (; c < '0' || c > '9'; c = getchar());    for (; c >= '0' && c <= '9'; c = getchar())p = p * 10 + c - '0';}ll s[N];ll f[5005][5005];int n, A[N];int main(){    Read(n);    for (int i = 1; i <= n; i++)        Read(A[i]), s[n - i + 1] = A[i];    for (int i = 1; i <= n; i++)        s[i] += s[i - 1];    for (int i = 1; i <= n; i++)    {        for (int j = 1; j <= n; j++)        {            ll ans1 = -inf, ans2 = -inf;            if (i >= (2 * j - 1))                ans1 = s[i] - f[i - (2 * j - 1)][2 * j - 1];            if (i >= (2 * j))                ans2 = s[i] - f[i - 2 * j][2 * j];            f[i][j] = max(max(ans1, ans2), f[i][j - 1]);        }    }    printf("%lld\n", f[n][1]);}

"Simulation competition 20181025": Game Theory + dp simple Optimization

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.