cug oj 1479 Treasure Chest Lock (区间dp 思维)
1479: Treasure Chest Lock
Time Limit: 1 Sec  Memory Limit: 128 MB id=1479">Submit id=1479">Status
Submit: 7  Solved: 5
[
 Board]
Description
Vic has a treasure chest. And there is a lock on the treasure chest. The lock contains a sequence of wheels. Each wheel has the 26 letters of the English alphabet
‘a’ through ‘z’, in order. If you move a wheel up, the letter it shows changes to the next letter in the English alphabet (if it was showing the last letter ‘z’, then it
changes to ‘a’). If you move the wheel down, it changes to show the previous letter in the English alphabet (if it was showing ‘a’, then it changes to ‘z’).
It is also possible to move any subsequence of contiguous wheels in the same direction with only one movement. This has the same effect of moving each of the
wheels within the subsequence on that direction, but saves the effort of doing that one wheel at a time.The lock openswhen the wheels show a secret sequence of l
etters. Currently all wheels are showing the letter ‘a’. Vic wants to know the minimum number of movements you need to open the lock.
Input
The input has several test cases. Each of them is given in exactly one line containing a nonempty string of at most 1000 lowercase letters. The string represents
the secret sequence of letters that opens the lock.
Output
For each test case, output a line containing a single integer with the minimum number of movements to open the lock.
Sample Input
abcxyz
abcdefghijklmnopqrstuvwxyz
aaaaaaaaa
zzzzzzzzz
zzzzbzzzz
*
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 1005
#define MAXN 200005
#define INF 0x3f3f3f3f
#define mod 20140518
#define eps 1e-6
const double pi=acos(-1.0);
typedef long long ll;
using namespace std; int n,m;
int up[maxn][40],dn[maxn][40];
char s[maxn]; void solve()
{
int i,j,k;
n=strlen(s+1);
memset(dn,0x3f,sizeof(dn));
memset(up,0x3f,sizeof(up));
dn[1][0]=s[1]-'a';
up[1][0]='a'+26-s[1];
for(i=1; i<n; i++)
{
for(j=0; j<30; j++)
{
if(s[i+1]==s[i])
{
dn[i+1][j]=dn[i][j];
up[i+1][j]=up[i][j];
}
else if(s[i+1]<s[i])
{
if(dn[i][j]<INF)
{
dn[i+1][j]=min(dn[i+1][j],dn[i][j]);
if(j>0) dn[i+1][j-1]=min(dn[i+1][j-1],dn[i][j]);
dn[i+1][j+1]=min(dn[i+1][j+1],dn[i][j]+s[i+1]-s[i]+26);
up[i+1][0]=min(up[i+1][0],dn[i][j]+'a'+26-s[i+1]);
}
if(up[i][j]<INF)
{
up[i+1][j]=min(up[i+1][j],up[i][j]+s[i]-s[i+1]);
if(j>0) up[i+1][j-1]=min(up[i+1][j-1],up[i][j]);
up[i+1][j+1]=min(up[i+1][j+1],up[i][j]+s[i]-s[i+1]+26);
dn[i+1][0]=min(dn[i+1][0],up[i][j]+s[i+1]-'a');
}
}
else
{
if(dn[i][j]<INF)
{
dn[i+1][j]=min(dn[i+1][j],dn[i][j]+s[i+1]-s[i]);
if(j>0) dn[i+1][j-1]=min(dn[i+1][j-1],dn[i][j]);
dn[i+1][j+1]=min(dn[i+1][j+1],dn[i][j]+s[i+1]-s[i]+26);
up[i+1][0]=min(up[i+1][0],dn[i][j]+'a'+26-s[i+1]);
}
if(up[i][j]<INF)
{
up[i+1][j]=min(up[i+1][j],up[i][j]);
if(j>0) up[i+1][j-1]=min(up[i+1][j-1],up[i][j]);
up[i+1][j+1]=min(up[i+1][j+1],up[i][j]+s[i]-s[i+1]+26);
dn[i+1][0]=min(dn[i+1][0],up[i][j]+s[i+1]-'a');
}
}
}
}
int ans=min(dn[n][0],up[n][0]);
printf("%d\n",ans);
}
int main()
{
while(~scanf("%s",s+1))
{
if(s[1]=='*') break ;
solve();
}
return 0;
}
/*
mtezqh
ans 30 e -26-e
*/
cug oj 1479 Treasure Chest Lock (区间dp 思维)的更多相关文章
- 【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)
		http://www.lydsy.com/JudgeOnline/problem.php?id=2101 这个dp真是神思想orz 设状态f[i, j]表示i-j先手所拿最大值,注意,是先手 所以转移 ... 
- Light OJ 1031 - Easy Game(区间DP)
		题目大意: 给你一个n,代表n个数字,现在有两个选手,选手A,B轮流有有一次机会,每个选手一次可以得到一个或者多个数字,从左侧或者右侧,但是不能同时从两边取数字,当所有的数字被取完,那么游戏结束.然后 ... 
- codeforces 1140D(区间dp/思维题)
		D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ... 
- BZOJ 2121: 字符串游戏 区间DP + 思维
		Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对 于一个在集合S中的字符串p,如果p在L中出现,BX就可以选择是否将其删 ... 
- Light oj 1044 - Palindrome Partitioning(区间dp)
		题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 dp[i][j]表示i到j直接的最小回文区间个数,直接看代码 #include ... 
- HDU 2476 String painter(区间DP+思维)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ... 
- 题解——洛谷P4767 [IOI2000]邮局(区间DP)
		这题是一道区间DP 思维难度主要集中在如何预处理距离上 由生活经验得,邮局放在中间显然最优 所以我们可以递推求出\( w[i][j] \)表示i,j之间放一个邮局得距离 然后设出状态转移方程 设\( ... 
- 区间DP小结
		也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ... 
- BZOJ 2101 [Usaco2010 Dec]Treasure Chest 藏宝箱:区间dp 博弈【两种表示方法】【压维】
		题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2101 题意: 共有n枚金币,第i枚金币的价值是w[i]. 把金币排成一条直线,Bessie ... 
随机推荐
- linux下java命令行引用jar包
			一般情况下: 如果java 文件和jar 包在同一目录 poi-3.0-alpha3-20061212.jar testTwo.java 编译: javac -cp poi-3.0-alpha3-2 ... 
- poj3255 Roadblocks 次短路
			Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10098 Accepted: 3620 Descr ... 
- 九度oj 题目1079:手机键盘
			题目描述: 按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次. 如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,k ... 
- POJ 1656 Counting Black
			Counting Black Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9772 Accepted: 6307 De ... 
- Welcome-to-Swift-07闭包(Closures)
			闭包是自包含的函数代码块,可以在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks)以及其他一些编程语言中的 lambdas 函数比较相似. 闭包可 ... 
- hdu5730 Shell Necklace  【分治fft】
			题目 简述: 有一段长度为n的贝壳,将其划分为若干段,给出划分为每种长度的方案数,问有多少种划分方案 题解 设\(f[i]\)表示长度为\(i\)时的方案数 不难得dp方程: \[f[i] = \su ... 
- mybatis学习(六)——参数处理
			先总结一下,后面再一个个解释: 单个参数:直接使用#{参数名}进行取值,mybatis没做特殊处理,参数名可以随便写. 多个参数:使用#{param1},#{param2}取值 命名参数:通过@par ... 
- 解决 Jackson反序列化 Unexpected token ... , expected VALUE_STRING: need JSON String that contains type id (for subtype of ...)
			首先检查是否是 objectMapper.enableDefaultTyping(); 的受害者.优先考虑删除该配置. 使用Jackson把数组的json字符串反序列化为List时候报了个JsonMa ... 
- Springboot 版本+ jdk 版本 + Maven 版本的匹配
			Spring boot 版本 Spring Framework jdk 版本 maven 版本 1.2.0 版本之前 6 3.0 1.2.0 4.1.3+ 6 3.2+ 1.2.1 4.1.3+ 7 ... 
- bzoj 4009 接水果 整体二分
			Description 先给出一些盘子, 用路径x-y表示, 有权值 再有Q个询问, 表示水果, 用路径x-y表示 如果盘子是水果的子路径, 可以接住 对于每个水果, 输出可以接住它的盘子的第k小权 ... 
