Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP
题目链接:http://codeforces.com/contest/132/problem/C
2 seconds
256 megabytes
standard input
standard output
A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn
around") and "F" ("move 1 unit forward").
You are given a list of commands that will be given to the turtle. You have to change exactly n commands from the list (one command
can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?
The first line of input contains a string commands — the original list of commands. The string commands contains
between 1 and 100 characters, inclusive, and contains only characters "T" and "F".
The second line contains an integer n (1 ≤ n ≤ 50)
— the number of commands you have to change in the list.
Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the
modified list.
FT
1
2
FFFTFFF
2
6
In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units.
In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.
题解:
方法一(四维dp):
1.dp[i][j][dir][dis]表示:执行到第i个指令,修改了j个指令,前进方向为dir,且到达了dis的地方的情况是否存在。其值为0或1。
2.枚举已有状态,推出下一步状态。(与常见的dp不同,常见的dp为枚举可能的状态,然后看他能从那些状态转移过来)。
3.由于结束点可能在左边,即距离为负数,为了防止溢出,将起始点往右移100。
类似的做法的题:http://blog.csdn.net/dolfamingo/article/details/73903530
易错点:
写成dp[i][j][dir][dis] 或者dp[i][j][dis][dir]都可以,但是写成dp[dir][i][j][dis] 或者dp[dis][i][j][dir]等等就不行了,因为枚举的顺序不对。递推必须自底向上,如果“底”都没推出来,那“上”自然也推不出来了。
学习之处:
一:目前了解到的递推式DP有两种:
1.当前状态(可能不存在)能从哪些状态转移过来。 被赋值的状态是当前状态。
2.当前状态(已存在)能推出哪些状态。 被赋值的状态是被推出来的状态。
二:
当设定的k维dp不好递推时,如果再加多一维(这一维是数值,且范围很小)仍不会超时,那么就可以改写成k+1维的dp,这样所有可能的状态都通过dp数组的下标体现出来了,所以要做的就是:递推出存在的状态,然后在这些状态中找出最优结果。
四维DP:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int n, m, dp[][][][];
char s[maxn]; void init()
{
scanf("%s%d",s+, &m);
n = strlen(s+); dp[][][][] = ; //起始点平移到100,防止下标溢出
for(int i = ; i<n; i++)
for(int j = ; j<=m; j++) //这样枚举,一个指令最多只能修改一次
for(int dir = ; dir<; dir++)
for(int dis = ; dis<=; dis++)
{
if(!dp[i][j][dir][dis]) continue; dp[i+][j+(s[i+]!='F')][dir][dis+(dir?-:)] = ;
dp[i+][j+(s[i+]!='T')][!dir][dis] = ;
}
} void solve()
{
int ans = -INF;
for(int j = m; j>=; j -= ) //一个指令可以修改多次
for(int dir = ; dir<; dir++)
for(int dis = ; dis<=; dis++)
if(dp[n][j][dir][dis])
ans = max(ans, abs(-dis)); //与100的距离,即为实际距离
cout<<ans<<endl;
} int main()
{
init();
solve();
}
三维DP:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; char s[];
int n,m, dp[][][]; int main()
{
scanf("%s%d",s+,&m);
n = strlen(s+);
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
for(int k = ; k<; k++)
dp[i][j][k] = -INF; dp[][][] = dp[][][] = ;
for(int i = ; i<n; i++)
for(int j = ; j<=m; j++)
for(int k = ; k<; k++)
{
dp[i+][j+(s[i+]!='F')][k] = max(dp[i+][j+(s[i+]!='F')][k],dp[i][j][k]+(k?:-));
dp[i+][j+(s[i+]!='T')][!k] = max(dp[i+][j+(s[i+]!='T')][!k],dp[i][j][k]);
} int ans = -INF;
for (int j = m; j>=; j -= )
ans = max(ans, max(dp[n][j][], dp[n][j][])); printf("%d\n",ans);
}
Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP的更多相关文章
- Codeforces Beta Round #96 (Div. 1) C. Logo Turtle DP
C. Logo Turtle A lot of people associate Logo programming language with turtle graphics. In this c ...
- Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp
http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...
- Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心
D. Constants in the language of Shakespeare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codef ...
- Codeforces Beta Round #96 (Div. 2) (A-E)
写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- 咦?Oracle归档文件存哪了?
实验环境:RHEL 5.4 + Oracle 11.2.0.3 现象:日志切换后没找到归档日志目录. 1.查看归档日志路径 2.日志切换后并未找到归档目录 3.创建归档目录后再次观察 引申知识 1.查 ...
- 空扫描Idle Scanning
空扫描Idle Scanning 空扫描Idle Scanning是一种借助第三方实施的端口扫描技术,可以很好的隐蔽扫描主机本身.它的实现基于以下两个TCP工作机制. (1)在TCP三次握手阶 ...
- 同源策略Same-origin policy
同源策略Same-origin policy 同源策略Same-origin policy是Web应用的一种安全基础策略.它规定同一源中,页面包含的脚本可以访问该源下的其他页面的数据.只有当网址中的 ...
- [ZJOI 2016] 小星星
4455: [Zjoi2016]小星星 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 653 Solved: 400[Submit][Status] ...
- [转] 常用SQL查询语句
sunada 的原文地址 常用SQL查询语句 一.简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指 ...
- Unity -- 入门教程三
进入这个页面,按编译器版本进行下载,我用的是2010,所以要下载这个. 安装就不用我教了,下面开始看我是如何导入Unity VS的. 点击Import之后我们会发现并没有发生什么,但是接下来我们按一下 ...
- install nfs and share file
介绍一下NFS的安装,以及共享文件 NFS(Net File System),通过使用NFS,可以像使用本地文件一样访问远程文件. 它主要解决了数据共享的问题,可以备份容灾. 安装配置 1.以linu ...
- C#遇见的函数
1.类Stopwatch 提供一组方法和属性,可用于准确地测量运行时间. 命名空间: System.Diagnostics Stopwatch timePerParse = Stopwatc ...
- 树莓派 Zero W——随身钥匙扣
前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正确的格式显示,请访问我的博客原文: http://www.cnblogs.c ...
- LINUX下目标文件的BSS段、数据段、代码段
http://blog.chinaunix.net/uid-27018250-id-3867588.html bss 未初始化的全局数据 data 已经初始化的全局数据 text 代码段,机器指令 r ...