题目相关

题目链接

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

Problem Statement

NNN cells are arranged in a row. The cells are numbered 111 through NNN 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 aia_iai​ 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=92+3+4=92+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=63+(−1)+4=63+(−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≤5003≤N≤5003≤N≤500
  • −100≤ai≤100−100≤a_i≤100−100≤ai​≤100
  • All values in the input are integers.

题解报告

题目翻译

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

题目分析

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

数据范围分析

本题的 NNN 最大值是 500500500,aia_iai​ 的最大值是 100100100,因此最大的分数为 500×100=5∗104500 \times 100=5*10^4500×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 __LOCALconst 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);
#endifint 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");
#endifreturn 0;
}

时间复杂度

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

空间复杂度

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

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

AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划相关推荐

  1. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  2. AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering

    题目相关 题目链接 AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d. Proble ...

  3. AtCoder题解 —— AtCoder Beginner Contest 187 —— B - Gentle Pairs —— 暴力

    题目相关 题目链接 AtCoder Beginner Contest 187 B 题,https://atcoder.jp/contests/abc187/tasks/abc187_b. Proble ...

  4. AtCoder题解—— AtCoder Beginner Contest 181 —— B - Trapezoid Sum

    题目相关 题目链接 AtCoder Beginner Contest 181 B 题,https://atcoder.jp/contests/abc181/tasks/abc181_b. Proble ...

  5. AtCoder题解——AtCoder Regular Contest 107——B - Quadruple

    题目相关 题目链接 AtCoder Regular Contest 107 B 题,https://atcoder.jp/contests/arc107/tasks/arc107_b. Problem ...

  6. Atcoder题解与视频集

    开启Atcoder之路 开启Atcoder之路_sortmin的博客-CSDN博客_atcoder怎么用 atcoder心得 atcoder心得_404REN的博客-CSDN博客_atcoder怎么用 ...

  7. 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...

  8. AtCoder Grand Contest 008: Contiguous Repainting(思维)

    Contiguous Repainting 时间限制: 2 Sec  内存限制: 256 MB 提交: 69  解决: 22 [提交][状态][讨论版][命题人:admin] 题目描述 There a ...

  9. AtCoder Grand Contest 017

    AtCoder Grand Contest 017 A - Biscuits 有\(n\)个数,问有多少个集合的数的和模\(2\)余\(P\). 随便\(dp\)一下就好了. #include< ...

最新文章

  1. Symantec:揭秘Hidden Lynx组织的APT***行动
  2. 双声道录制的混动波形信号
  3. oracle11g到底是什么6,Oracle11g六个重要进程
  4. C/C++程序内存分配详解
  5. 使用RunTime添加动态方法、方法交换、获取所有属性来重写归档解档
  6. php操作redis phpredis扩展
  7. Angular:Use function as controller
  8. linux系统下top命令的详细用法、参数详解、以及模式配置
  9. ubuntu nginx配置负载均衡篇(二)
  10. VMware Workstation 8 技巧集
  11. 点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则
  12. 曼昆 宏观经济学 笔记
  13. 中基鸿业投资理财好习惯
  14. google三大论文之--MapReduce:超大机群上的简单数据处理
  15. RS-485电路设计及接口防护
  16. 从消费价值角度破解小红书爆文密码
  17. Java 面试,创建了几个String 对象? 我让问!让你问!让你问!
  18. Android源码备用
  19. idea 2020取消Import Maven projects automatically
  20. android 自动恢复,Android的状态保存和恢复

热门文章

  1. 如何在 Excel 中查找合并的单元格(并删除它们)
  2. 2021牛客暑期多校训练营2 F-Girlfriend
  3. php数组时按值传递还是按地址传递
  4. LaTex常用包及其功能
  5. 医院需要什么样的集成平台
  6. Impala-shell卡顿分析——记一次曲折的Debug经历
  7. java 对数和指数计算
  8. 计算机考研之方向解读
  9. MCAD MCSD和MCPD,究竟该考哪一个
  10. 【大数据分析专业毕设之基于python爬虫的电影票房大数据预测分析+大屏可视化分析