AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

题目相关

题目链接

AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b

Problem Statement

N N N cells are arranged in a row. The cells are numbered 1 1 1 through N N N from left to right.
Initially, all cells are empty. You can perform the following two types of operation arbitrary number of times in arbitrary order:

  1. Choose three consecutive empty cells, and put a coin on each of the three cells.
  2. Choose three consecutive cells with coins, and remove all three coins on the chosen cells.

After you finish performing operations, if the i-th cell contains a coin, you get a i a_i ai​ points. Your score is the sum of all points you get from the cells with coins.
Compute the maximum possible score you can earn.

Input

Input is given from Standard Input in the following format:

N
a1
.
.
an

Output

Print the answer.

Sample 1

Sample Input 1

4
1
2
3
4

Sample Output 1

9

Explaination

We represent a cell with a coin as o and a cell without a coin as . (a dot). One optimal way is as follows:
… →.ooo
We get 2 + 3 + 4 = 9 2+3+4=9 2+3+4=9 points this way.

Sample 2

Sample Input 2

6
3
-2
-1
0
-1
4

Sample Output 2

6

Explaination

One optimal way is as follows:
… →ooo… → oooooo →o…oo
We get 3 + ( − 1 ) + 4 = 6 3+(−1)+4=6 3+(−1)+4=6 points this way.

Sample 3

Sample Input 3

10
-84
-60
-41
-100
8
-8
-52
-62
-61
-76

Sample Output 3

0

Constraints

  • 3 ≤ N ≤ 500 3≤N≤500 3≤N≤500
  • − 100 ≤ a i ≤ 100 −100≤a_i≤100 −100≤ai​≤100
  • All values in the input are integers.

题解报告

题目翻译

水平方向有 N N N 个房间,从左到右编号从 1 1 1 到 N N N。开始的时候,每个房间都是空的。你可以随机的任意次数进行下面两个操作。
1、选择 3 3 3 个连续的,而且都为空的房间,在这三个房间内放入金币。
2、选择 3 3 3 个连续的,而且都放着金币的房间,在这三个房间内的金币清空。
当完成这些操作后,如果第 i i i 个房间内有金币,你将能到 a i a_i ai​ 分数。总分将是这 N N N 个房间的总和。
请问,可以得到的最大分数是多少。

题目分析

本题是一个典型的动态规划题目。
本题的实际问题就是:给了序列 { x 1 , ⋯   , x k x_1, \cdots, x_k x1​,⋯,xk​},其中 x 1 < ⋯ < x k x_1<\cdots<x_k x1​<⋯<xk​,要求我们能否可能找到这样的状态: k k k 个房间有金币,其他房间没有金币。
由于本题是对连续 3 3 3 个房间进行操作。因此,我们考虑 4 4 4 个房间的变化:
…o → oooo → o…
这样,我们就实现了反序。

数据范围分析

本题的 N N N 最大值是 500 500 500, a i a_i ai​ 的最大值是 100 100 100,因此最大的分数为 500 × 100 = 5 ∗ 1 0 4 500 \times 100=5*10^4 500×100=5∗104,因此 int 够用了。

AC 参考代码

//https://atcoder.jp/contests/agc050/tasks/agc050_b
//B - Three Coins
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

const int MAXN=5e2+10;
int a[MAXN];
int dp[MAXN][MAXN];
int ans[MAXN];

int main() {
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    int n;
    cin>>n;
    for (int i=0; i<n; i++) {
        cin>>a[i];
    }
    
    //初始化DP数组
    for (int i=0; i<MAXN; i++) {
        for (int j=0; j<MAXN; j++) {
            dp[i][j]=INT_MIN;
            if (i==j) {
                dp[i][j]=0;
            }
        }
    }
    //计算DP数组
    for (int d=1; d<=n; d++) {
        if (0==d%3) {
            for (int l=0; l+d<=n; l++) {
                int r=l+d;
                if (3==d) {
                    dp[l][r] = max(0, a[l]+a[l+1]+a[l+2]);
                } else {
                    int e=0;
                    for (int m=l+3; m<r; m+=3) {
                        e = max(e, dp[l][m]+dp[m][r]);
                    }
                    for (int m=l+1; m<r; m++) {
                        if (0==(m-l-1)%3) {
                            e = max(e, dp[l+1][m]+dp[m+1][r-1]);
                            e = max(e, dp[l+1][m]+dp[m+1][r-1]+a[l]+a[m]+a[r-1]);
                        }
                    }
                    dp[l][r]=e;
                }
            }
        }
    }
	//获取结果
    for (int i=1; i<=n; i++) {
        ans[i]=ans[i-1];
        for (int l=i-3; l>=0; l-=3) {
            ans[i]=max(ans[l]+dp[l][i], ans[i]);
        }
    }
    cout<<ans[n]<<"\n";

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

时间复杂度

O ( N 3 ) O(N^3) O(N3)。

空间复杂度

O ( N 2 ) O(N^2) O(N2)。

P.S.
发现写 AC 代码比写题解简单。

上一篇:CF819B Mister B and PR Shifts


下一篇:The 2020 ICPC Asia Taipei-Hsinchu Site Programming Contest