http://codeforces.com/contest/133/problem/E

题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意,要求输出能走到最远是哪里。

首先可以保证的是,他最远走的可以默认是向右走,因为,如果你说是向左走的话,我可以设置相反的开始face,就是开始的时候面向那里,从而得到相反的结论。所以就能得到向左走,是-1,向右走,是+1

那么从最终状态考虑,

最后肯定是走完了整段序列,然后改变了n次,face是那里还不清楚的,所以就是dp[i][j][face]能表达完状态。

face : 0 or 1

dp[i][j][face]表示走完前i个,改变了j次,最后face向哪里的时候的最优解

那么转移过来的时候:

因为一个点可以改变很多次,所以。

for (int i = 1; i <= lenstr; ++i) //枚举整段序列(这是必须的)

  for (int j = 0; j <= n; ++j) //枚举前i个位置一共改变了多少次

    for (int h = 0; h <= j; ++h) //枚举第i个位置改变了多少次(因为可以重复改变)

如果同一个点改变了奇数次,相当于没变。以此类推

所以就能从dp[i - 1][j - h][face]这个转移到下一个

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int dp[maxn][maxn][];
char str[maxn];
void work() {
int n;
for (int i = ; i <= maxn - ; ++i) {
for (int j = ; j <= maxn - ; ++j) {
for (int k = ; k < ; ++k) {
dp[i][j][k] = -inf;
}
}
}
dp[][][] = ;
dp[][][] = ;
scanf("%s%d", str + , &n);
for (int i = ; str[i]; ++i) {
for (int j = ; j <= n; ++j) {
for (int h = ; h <= j; ++h) {
if (str[i] == 'T') {
if (h & ) {
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] + );
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] - );
} else {
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
}
} else { // 'F'
if (h & ) { //to 'T'
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][]);
} else {
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] + );
dp[i][j][] = max(dp[i][j][], dp[i - ][j - h][] - );
}
}
}
}
}
int lenstr = strlen(str + );
int ans = max(dp[lenstr][n][], dp[lenstr][n][]);
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp的更多相关文章

  1. Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP

    题目链接:http://codeforces.com/contest/132/problem/C C. Logo Turtle time limit per test 2 seconds memory ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Beta Round #96 (Div. 2) (A-E)

    写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...

  5. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  7. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

随机推荐

  1. struts2 validate手动验证

    我们前面学习struts2知道,struts2通过拦截器实现了一些验证操作. 比如,如果是不能转换的类型在action中接受的话会跳转到错误页面,错误信息中会包含对应的错误信息,例如: 首先我们了解一 ...

  2. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  3. arm-linux-gcc4.4.3编译s3c2410平台linux内核

    写在前面:2.6.14版本的内核用arm-linux-gcc4.4.3没有编译成功,下载2.6.37版本的内核用arm-linux-gcc4.4.3编译通过. 一.首先下载linux内核: linux ...

  4. AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  5. 向nexus远程仓库里面添加JAR

    向nexus远程仓库里面添加JAR 远程仓库:http://10.1.252.21:8081/nexus/index.html admin/admin123 方法一:手动 在左侧选择:Reposito ...

  6. CentOS6下用yum升级系统最新内核版本

    首先当你决定升级内核时,要想清楚为什么升级内核,因为升级内核会带来很多麻烦.所以这种事情能避免就避免 导入 Public Key rpm --import https://www.elrepo.org ...

  7. Git查询

    Git查询 查询分支 git branch # 查询本地分支 git branch -a # 查询所有分支 $ git branch -a * master remotes/origin/HEAD - ...

  8. 模板 - SPFA

    SPFA可以用来判断负环或者计算带负权的最短路. 其实带负权的最短路可以用带势Dijkstra计算-- 所以SPFA基本就拿来判负环了-- #include<bits/stdc++.h> ...

  9. 315. Count of Smaller Numbers After Self(Fenwick Tree)

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  10. iTween研究院之学习笔记Move移动篇(一)

    http://www.xuanyusong.com/archives/2052 iTween.MoveTo(): 让模型移动到一个位置,它的底层函数是通过动态的修改模型每一帧的transform.po ...