CF1252J Tiling Terrace
CF1252J Tiling Terrace
题目描述
Talia has just bought an abandoned house in the outskirt of Jakarta. The house has a nice and long yard which can be represented as a one-dimensional grid containing 1 \times N1×N cells. To beautify the house, Talia is going to build a terrace on the yard by tiling the cells. Each cell on the yard contains either soil (represented by the character '.') or rock (represented by the character '#'), and there are at most 5050 cells containing rocks.
Being a superstitious person, Talia wants to tile the terrace with mystical tiles that have the power to repel ghosts. There are three types of mystical tiles:
- Type-1: Covers 1 \times 11×1 cell and can only be placed on a soil cell (".").
- Type-2: Covers 1 \times 21×2 cells and can only be placed on two consecutive soil cells ("..").
- Type-3: Covers 1 \times 31×3 cells and can only be placed on consecutive soil-rock-soil cells (".#.").
Each tile of Type-1, Type-2, and Type-3 has the power to repel G_1G1 , G_2G2 , and G_3G3 ghosts per day, respectively. There are also some mystical rules which must be followed for the power to be effective:
- There should be no overlapping tiles, i.e. each cell is covered by at most one tile.
- There should be at most KK tiles of Type-1, while there are no limitations for tiles of Type-2 and Type-3.
Talia is scared of ghosts, thus, the terrace (which is tiled by mystical tiles) should be able to repel as many ghosts as possible. Help Talia to find the maximum number of ghosts that can be repelled per day by the terrace. Note that Talia does not need to tile all the cells on the yard as long as the number of ghosts that can be repelled by the terrace is maximum.
输入格式
Input begins with a line containing five integers: NN KK G_1G1 G_2G2 G_3G3 ( 1 \le N \le 100,0001≤N≤100000 ; 0 \le K \le N0≤K≤N ; 0 \le G_1, G_2, G_3 \le 10000≤G1,G2,G3≤1000 ) representing the number of cells, the maximum number of tiles of Type-1, the number of ghosts repelled per day by a tile of Type-1, the number of ghosts repelled per day by a tile of Type-2, and the number of ghosts repelled by a tile of Type-3, respectively. The next line contains a string of NN characters representing the yard. Each character in the string is either '.' which represents a soil cell or '#' which represents a rock cell. There are at most 5050 rock cells.
输出格式
Output in a line an integer representing the maximum number of ghosts that can be repelled per day.
输入输出样例
输入 #1复制
输出 #1复制
输入 #2复制
输出 #2复制
输入 #3复制
输出 #3复制
说明/提示
Explanation for the sample input/output #1
Let "A" be a tile of Type-1, "BB" be a tile of Type-2, and "CCC" be a tile of Type-3. The tiling "ACCCBB" in this case produces the maximum number of ghosts that can be repelled, i.e. 10 + 40 + 25 = 7510+40+25=75
Explanation for the sample input/output #2
This sample input has the same yard with the previous sample input, but each tile of Type-2 can repel more ghosts per day. The tiling "BB#BBA" or "BB#ABB" produces the maximum number of ghosts that can be repelled, i.e. 100 + 100 + 10 = 210100+100+10=210 . Observe that the third cell is left untiled.
Explanation for the sample input/output #3
The tiling "ACCCA.#", "ACCC.A#", or ".CCCAA#" produces the maximum number of ghosts that can be repelled, i.e. 30 + 100 + 30 = 16030+100+30=160 . Observe that there is no way to tile the last cell.
题解:
非常容易判断是一道\(DP\)的题目。
联想一下这道题目:CF358D Dima and Hares
那么我们分方式决策,可以看出:第一种和第二种的选择都仅包含".",只有第三种选择有"#",那么我们可以考虑分段进行决策:将整个序列分成以"#"相隔开的段,并用\(a[i]\)数组保存\(i\)段中"."的个数。
那么我们设置状态\(dp[i][j][0/1]\)表示前\(j\)段用了\(i\)次方式\(1\),后面的\(0/1\)表示最后的"#"有没有用方式\(3\)所获得的最大价值。
那么我们转移的时候就只需要考虑方式二的使用次数。
转移方程需要分类讨论。另外地,还有一种情况是不选择完这\(n\)个字符。这个东西可以在枚举方式1的时候用除以二自动取整来实现。
可以结合代码理解:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
const int maxn=1e5+10;
int n,k,g1,g2,g3,tot,ans;
char s[maxn];
int a[maxn],dp[60][maxn][5];
//dp[i][j][0/1]表示前j段使用i次方式1,最后一个"#"是否选用方式3所得的最大利益。
signed main()
{
scanf("%I64d%I64d%I64d%I64d%I64d",&n,&k,&g1,&g2,&g3);
scanf("%s",s+1);
tot=1;
for(int i=1;i<=n;i++)
{
if(s[i]=='#')
tot++;
else
a[tot]++;
}
while(tot && !a[tot])
tot--;
if(!tot)
{
puts("0");
return 0;
}
memset(dp,0xcf,sizeof(dp));
dp[0][0][0]=0;
for(int i=1;i<=tot;i++)
for(int j=0;j<=k;j++)
{
int t=min(a[i],j);
for(int l=0;l<=t;l++)
{
if(l<=a[i])
dp[i][j][0]=max(dp[i][j][0],dp[i-1][j-l][0]+(a[i]-l)/2*g2+l*g1);
if(l<=a[i]-1)
{
dp[i][j][0]=max(dp[i][j][0],dp[i-1][j-l][1]+(a[i]-l-1)/2*g2+g3+l*g1);
dp[i][j][1]=max(dp[i][j][1],dp[i-1][j-l][0]+(a[i]-l-1)/2*g2+l*g1);
}
if(l<=a[i]-2)
dp[i][j][1]=max(dp[i][j][1],dp[i-1][j-l][1]+(a[i]-l-2)/2*g2+g3+l*g1);
}
}
for(int i=0;i<=k;i++)
ans=max(ans,dp[tot][i][0]);
printf("%I64d",ans);
return 0;
}
CF1252J Tiling Terrace的更多相关文章
- Tiling Terrace CodeForces - 1252J(dp、贪心)
Tiling Terrace \[ Time Limit: 1000 ms\quad Memory Limit: 262144 kB \] 题意 给出一个字符串 \(s\),每次可以选择三种类型来获得 ...
- 【CF1252J】Tiling Terrace(DP)
题意:有一个长为n的串,每个字符是#或者.中的一个,#不超过50个 有3种覆盖串的方式:(.),(..),(.#.),分别能获得g1,g2,g3的收益,覆盖之间不能重叠 第一种方式不能使用超过K次,问 ...
- 2019-2020 ICPC, Asia Jakarta Regional Contest
目录 Contest Info Solutions A. Copying Homework C. Even Path E. Songwriter G. Performance Review H. Tw ...
- Texture tiling and swizzling
Texture tiling and swizzling 原帖地址:http://fgiesen.wordpress.com If you’re working with images in your ...
- 图文详解Unity3D中Material的Tiling和Offset是怎么回事
图文详解Unity3D中Material的Tiling和Offset是怎么回事 Tiling和Offset概述 Tiling表示UV坐标的缩放倍数,Offset表示UV坐标的起始位置. 这样说当然是隔 ...
- POJ3420Quad Tiling(矩阵快速幂)
Quad Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3740 Accepted: 1684 Descripti ...
- Tri Tiling[HDU1143]
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Tiling 分类: POJ 2015-06-17 15:15 8人阅读 评论(0) 收藏
Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8091 Accepted: 3918 Descriptio ...
- Tiling Up Blocks_DP
Description Michael The Kid receives an interesting game set from his grandparent as his birthday gi ...
随机推荐
- JavaScript内置对象及常见API
一.全局属性 Infinity:表示正无穷大 NaN:非数字值 undefined:未定义的值 decodeURI():对encodeURI()转义的字符串解码 decodeURIComponent( ...
- JavaScript中随机打乱一个数组
JavaScript中随机打乱一个数组 function shuffle(arr) { let i = arr.length; while (i) { let j = Math.floor(Math. ...
- VIJOS-P1167 南蛮图腾
洛谷 P1498 南蛮图腾 洛谷传送门 JDOJ 1325: VIJOS-P1167 南蛮图腾 JDOJ传送门 Description 自从到了南蛮之地,孔明不仅把孟获收拾的服服帖帖,而且还发现了不少 ...
- AtCoder Grand Contest 040
Preface 今年准备省选啥都不说了,省选题基本上都做过一遍了,开始尝试板刷AGC 这场做完就从AGC001开始吧,感觉以我的速度和来机房的频率一个礼拜做一场都谢天谢地了 A - >< ...
- 不用输入ssh -i命令行即可携带pem文件快速登录的方法
如果要登录的服务器只允许pem认证 每次输入ssh -i xxxx.pem 用户@ip 地址 就很烦 这里有个一劳永逸的方法: 进入到自己的用户目录,例如/home/me 把pem文件放在当前目录 ...
- Python变量与内存管理
Python变量与内存管理 –与C语言中的变量做对比,更好的理解Python的变量. 变量 变量在C语言中 全局变量:其存放在内存的静态变量区中. 局部变量:代码块中存放在内存的代码区当中,当被调 ...
- es6入门7--Set Map数据结构
本文作为ES6入门第十三章的学习整理笔记,可能会包含少部分个人的理解推测,若想阅读更详细的介绍,还请阅读原文ES6入门 一.set数据结构 1.set不接受重复值 ES6新增了Set构造函数用于创建s ...
- 前端优化,包括css,jss,img,cookie
前端优化,来自某懒观看麦子学院视频的笔记. 尽可能减少HTTP的请求数 使用CDN 添加Expirs头,或者Cache-control Gzip组件压缩文件内容 将CSS放在页面上方 将脚本放到页面下 ...
- 15-scrapy-redis两种形式分布式爬虫
什么叫做分布式爬虫? 分布式爬虫,就是多台机器共用一个scrapy—redis程序高效爬取数据, 为啥要用分布式爬虫? 其一:因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器 ...
- Dubbo从入门到实战:实战篇
一.加入 zookeeper 作为注册中心 在前面的案例中,我们没有使用任何的注册中心,而是用一种直连的方式进行的.但是,实际上很多时候,我们都是使用 dubbo + zookeeper 的方式,使用 ...