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 ...
随机推荐
- boost::function和boost::bind 介绍
一. boost::function介绍 原文:http://www.cnblogs.com/sld666666/archive/2010/12/16/1907591.html 本片文章主要介绍boo ...
- CountDownLatch、CyclicBarrier、Samephore浅谈三大机制
CountDownLatch.CyclieBarrier与SamePhore都可用来控制线程的执行,那么他们之间有什么区别呢 CountDownLatch CountDowenlatch可以看成一个线 ...
- IOS 后台保持连接
当iphone应用程序进行网络编程时,切到后台后,socket连接会断掉,ios的设计就是这样. 但是好在apple公司也没有那么绝,还是有一些东西可以在后台运行的(backgroundmodes), ...
- BumpMapping [转]
http://fabiensanglard.net/bumpMapping/index.php Fabien Sanglard's Website Home About FAQ Email Rss T ...
- C#报错"线程间操作无效: 从不是创建控件“XXX”的线程访问它"--解决示例
C# Winform程序中,使用线程对界面进行更新需要特殊处理,否则会出现异常“线程间操作无效: 从不是创建控件“taskView”的线程访问它.” 在网文“http://www.cnblogs.co ...
- Git以及github的使用方法(五),暂存区和工作区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- mysql 数据迁移时遇到 外键限制
禁用外键约束 SET FOREIGN_KEY_CHECKS=0; ......数据迁移........ 启动外键约束 SET FOREIGN_KEY_CHECKS=1;
- javascript 怎么操纵OGNL标签
吧ONGL标签放到html标签中,来操作html的标签就能够了 样例代码: html <div id="categoryid" style="display:non ...
- table 设置边框
本文引自:https://www.cnblogs.com/leona-d/p/6125896.html 示例代码: <!DOCTYPE html> <html lang=" ...
- ActiveX控件打包成Cab置于网页中自动下载安装
[背景] http://www.360doc.com/content/13/1120/20/10159093_330853247.shtml 做过ActiveX控件的朋友都知道,要想把自己做的Acti ...