Tiling Terrace

\[Time Limit: 1000 ms\quad Memory Limit: 262144 kB
\]

题意

给出一个字符串 \(s\),每次可以选择三种类型来获得价值

\(Type1:“.”\) 获得 \(w_1\) 元

\(Type2:“..”\) 获得 \(w_2\) 元

\(Type3:“.\#.”\) 获得 \(w_3\) 元

此外,还有两个限制条件

\(Limti1:Type1\) 至多只能选 \(K\) 个

\(Limit2:\) 每个字符只能被选择一次

问最多可以获得的价值。

思路

首先可以发现,对于两个相邻的 \(\#\),如果我们确定了这两个的状态,也就是不用或者当成 \(Type3\) 来用,那么我们就可以知道这两个 \(\#\) 之间可用 \(.\) 的数量。如果这个数量是奇数,那么意味着其中有一个 \(.\) 拿来用作 \(Type1\) 是必然不会亏的,也就是这个 \(.\) 是白嫖的。

令 \(dp[i][j][k][0/1]\) 表示到第 \(i\) 个 \(\#\) 号为止,白嫖了 \(j\) 个 \(Type1\),选了 \(k\) 个 \(Type3\),并且第 \(i\) 个 \(\#\) 是否当成 \(Type3\) 来用。

为了方便计算,我们可以在整个字符串的开头加入一个 \(\#\),整个字符串的结尾加入一个 \(\#\),那么整个的状态必然要从 \(dp[0][0][0][0]\) 开始递推,必然以 \(dp[\#_{number}][j][k][0]\) 结尾。

这样推出来以后,我们就知道了白嫖 \(a\) 个 \(Type1\),选 \(c\) 个 \(Type3\) 的情况下,最多可以获得多少个 \(b\)。最后对 \(a、b、c\) 贪心求答案,尝试在 \(Type1\) 不超过 \(K\) 的情况下把 \(Type2\) 换成 \(Type1\)。

我们可以发现,由于 \(\#\) 的个数最多就 \(50\) 个,那么白嫖的 \(Type1\) 必然不会超过 \(51\),所以整个 \(dp\) 的复杂度是 \(O\left(50^3\times4\right)\) 的

/***************************************************************
> File Name : J.cpp
> Author : Jiaaaaaaaqi
> Created Time : Tue 05 Nov 2019 10:00:31 PM CST
***************************************************************/ #include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int> typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; ll g1, g2, g3;
char s[maxn];
ll dp[60][60][60][2];
vector<int> vv; ll calc(ll a, ll b, ll c) {
if(a > m) a = m;
ll ans = a*g1 + c*g3;
ll tmp = min(b, (m-a)/2);
ll res = max(b*g2, (b-tmp)*g2 + tmp*2ll*g1);
if(a+tmp*2<m && b-tmp>0) res = max(res, res-g2+g1);
return ans+res;
} int main() {
// freopen("in", "r", stdin);
scanf("%d%d%lld%lld%lld", &n, &m, &g1, &g2, &g3);
scanf("%s", s+1);
vv.clear();
vv.pb(0);
for(int i=1; i<=n; i++) {
if(s[i]=='#') vv.pb(i);
}
vv.pb(n+1);
for(int i=0; i<60; i++) for(int j=0; j<60; j++)
for(int k=0; k<60; k++) for(int z=0; z<2; z++)
dp[i][j][k][z] = -INF;
dp[0][0][0][0] = 0;
int sz = vv.size()-1;
for(int i=0; i<sz; i++) {
int s = vv[i+1]-vv[i]-1;
for(int j=0; j<60; j++) {
for(int k=0; k<60; k++) {
if(dp[i][j][k][0] >= 0) {
if(s>=0) dp[i+1][j+s%2][k][0] = max(dp[i+1][j+s%2][k][0], dp[i][j][k][0] + s/2);
if(s>=1) dp[i+1][j+(s-1)%2][k][1] = max(dp[i+1][j+(s-1)%2][k][1], dp[i][j][k][0] + (s-1)/2);
}
if(dp[i][j][k][1] >= 0) {
if(s>=1) dp[i+1][j+(s-1)%2][k+1][0] = max(dp[i+1][j+(s-1)%2][k+1][0], dp[i][j][k][1] + (s-1)/2);
if(s>=2) dp[i+1][j+(s-2)%2][k+1][1] = max(dp[i+1][j+(s-2)%2][k+1][1], dp[i][j][k][1] + (s-2)/2);
}
}
}
}
ll ans = 0;
for(int i=0; i<60; i++) for(int j=0; j<60; j++) if(dp[sz][i][j][0]>=0)
ans = max(ans, calc(i, dp[sz][i][j][0], j));
printf("%lld\n", ans);
return 0;
}

Tiling Terrace CodeForces - 1252J(dp、贪心)的更多相关文章

  1. CF1252J Tiling Terrace

    CF1252J Tiling Terrace 洛谷评测传送门 题目描述 Talia has just bought an abandoned house in the outskirt of Jaka ...

  2. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  3. CodeForces - 158B.Taxi (贪心)

    CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...

  4. BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...

  5. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

  6. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

  7. Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)

    Problem  Codeforces #550 (Div3) - G.Two Merged Sequences Time Limit: 2000 mSec Problem Description T ...

  8. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...

  9. CodeForces - 940E - Cashback +贪心+DP

    传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删 ...

随机推荐

  1. [PKUSC2018]最大前缀和(状压DP)

    题目大意:求给定的 $n$ 个数的所有排列的最大前缀和(不能为空)之和对 $10^9+7$ 取模的值. $1\le n\le 20,1\le\sum|a_i|\le 10^9$. 神级DP.杂题选讲的 ...

  2. Paper | Predicting the Quality of Images Compressed After Distortion in Two Steps

    目录 1. 问题本质剖析 2. 方法细节 图像质量评估大佬AC Bovik的论文,发表在2019 TIP上. 考虑的问题:对于有参考图像质量评估(R-IQA)任务,参考图像有时是有损的.这会导致评估的 ...

  3. 被“org.springframework.boot.web.support.SpringBootServletInitializer;”耽搁的两天

    org.springframework.boot.web.support.SpringBootServletInitializer 改为: org.springframework.boot.conte ...

  4. 大话设计模式Python实现-适配器模式

    适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口. 下面是一个适配器模式的demo: #!/usr/bin/env python # -*- coding:u ...

  5. 模型的细致程度--Level of Development

    模型的细致程度,英文称作Level of Details,也叫作Level of Development.描述了一个BIM模型构件单元从最低级的近似概念化的程度发展到最高级的演示级精度的步骤.美国建筑 ...

  6. LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

    题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...

  7. kali渗透综合靶机(四)--node1靶机

    kali渗透综合靶机(四)--node1靶机 靶机下载地址::https://download.vulnhub.com/node/Node.ova 一.主机发现 1.netdiscover -i et ...

  8. 1. mvc 树形控件tree + 表格jqgrid 显示界面

    1.界面显示效果 2.资源下载 地址 1. jstree  https://www.jstree.com/   2.表格jqgrid  https://blog.mn886.net/jqGrid/  ...

  9. ICSharpCode.SharpZipLib 中文乱码问题

    今天在调用ICSharpCode.SharpZipLib解压zip文件时出现了中文文件乱码的问题. 解决过程如下: 1.判断是否压缩包本身问题.经查zip文件夹在本地直接解压打开时正确的中文名称,所以 ...

  10. Java自学-集合框架 LinkedList

    Java集合框架 LinkedList 序列分先进先出FIFO,先进后出FILO FIFO在Java中又叫Queue 队列 FILO在Java中又叫Stack 栈 示例 1 : LinkedList ...