AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划
题目相关
题目链接
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:
- Choose three consecutive empty cells, and put a coin on each of the three cells.
- 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 —— 动态规划相关推荐
- AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S
题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...
- AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering
题目相关 题目链接 AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d. Proble ...
- AtCoder题解 —— AtCoder Beginner Contest 187 —— B - Gentle Pairs —— 暴力
题目相关 题目链接 AtCoder Beginner Contest 187 B 题,https://atcoder.jp/contests/abc187/tasks/abc187_b. Proble ...
- AtCoder题解—— AtCoder Beginner Contest 181 —— B - Trapezoid Sum
题目相关 题目链接 AtCoder Beginner Contest 181 B 题,https://atcoder.jp/contests/abc181/tasks/abc181_b. Proble ...
- AtCoder题解——AtCoder Regular Contest 107——B - Quadruple
题目相关 题目链接 AtCoder Regular Contest 107 B 题,https://atcoder.jp/contests/arc107/tasks/arc107_b. Problem ...
- Atcoder题解与视频集
开启Atcoder之路 开启Atcoder之路_sortmin的博客-CSDN博客_atcoder怎么用 atcoder心得 atcoder心得_404REN的博客-CSDN博客_atcoder怎么用 ...
- 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...
- AtCoder Grand Contest 008: Contiguous Repainting(思维)
Contiguous Repainting 时间限制: 2 Sec 内存限制: 256 MB 提交: 69 解决: 22 [提交][状态][讨论版][命题人:admin] 题目描述 There a ...
- AtCoder Grand Contest 017
AtCoder Grand Contest 017 A - Biscuits 有\(n\)个数,问有多少个集合的数的和模\(2\)余\(P\). 随便\(dp\)一下就好了. #include< ...
最新文章
- Symantec:揭秘Hidden Lynx组织的APT***行动
- 双声道录制的混动波形信号
- oracle11g到底是什么6,Oracle11g六个重要进程
- C/C++程序内存分配详解
- 使用RunTime添加动态方法、方法交换、获取所有属性来重写归档解档
- php操作redis phpredis扩展
- Angular:Use function as controller
- linux系统下top命令的详细用法、参数详解、以及模式配置
- ubuntu nginx配置负载均衡篇(二)
- VMware Workstation 8 技巧集
- 点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则
- 曼昆 宏观经济学 笔记
- 中基鸿业投资理财好习惯
- google三大论文之--MapReduce:超大机群上的简单数据处理
- RS-485电路设计及接口防护
- 从消费价值角度破解小红书爆文密码
- Java 面试,创建了几个String 对象? 我让问!让你问!让你问!
- Android源码备用
- idea 2020取消Import Maven projects automatically
- android 自动恢复,Android的状态保存和恢复