HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)
You can rotate 1-3 consecutive digits up or down in one step.
For examples:
567890 -> 567901 (by rotating the last 3 digits up)
000000 -> 000900 (by rotating the 4th digit down)
Given the current state and the secret password, what is the minimum amount of steps you have to rotate the locker in order to get from current state to the secret password?
For each case, two strings with equal length (≤ 1000) consists of only digits are given, representing the current state and the secret password, respectively.
题目大意:从一串数字转成另一串数字,每次操作可以把连续的1~3个数字都+1或者-1(9+1=0,0-1=9),问最少须要多少次操作。
思路:DP。dp[i][x][y]表示,把第一个字符串变为第 i 位是 x,第 i-1 位是 y, i-1前面的全部都与第二个字符串相同,最少须要多少步。
假设第一串数字是a[],第二串数字是b[]
那么状态转移就是,对于每一个dp[i][x][y],判断从a[i]须要转多少步操作才能到x(两种操作都要枚举),设为k,然后枚举 i-1 跟着转多少步才能到达 y,设为p(p≤k),再枚举 i-2 须要跟着转多少步才能到达b[i],设为q(q≤p)。详见代码。
最终答案为dp[n][b[n]][b[n-1]]
代码(203MS):
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXN = ; int a[MAXN], b[MAXN], n;
int dp[MAXN][][];
char s[MAXN]; inline int make(int x) {
if(x < ) x += ;
if(x > ) x -= ;
return x;
} inline void update_min(int &a, const int &b) {
if(a > b) a = b;
} inline int calc(int x, int y) {
if(x > y) y += ;
return y - x;
} int solve() {
memset(dp, 0x3f, sizeof(dp));
dp[][a[]][] = ;
for(int i = ; i <= ; ++i) {
update_min(dp[][make(a[] + i)][], i);
update_min(dp[][make(a[] - i)][], i);
}
for(int i = ; i <= n; ++i) {
for(int x = ; x <= ; ++x) {
for(int y = ; y <= ; ++y) {
int k = calc(a[i], x);
for(int p = ; p <= k; ++p) {
for(int q = ; q <= p; ++q)
update_min(dp[i][x][y], dp[i - ][make(y - p)][make(b[i - ] - q)] + k);
} k = - k;
for(int p = ; p <= k; ++p) {
for(int q = ; q <= p; ++q)
update_min(dp[i][x][y], dp[i - ][make(y + p)][make(b[i - ] + q)] + k);
}
}
}
}
return dp[n][b[n]][b[n - ]];
} int main() {
while(scanf("%s", s) != EOF) {
n = strlen(s);
for(int i = ; i < n; ++i) a[i + ] = s[i] - '';
scanf("%s", s);
for(int i = ; i < n; ++i) b[i + ] = s[i] - '';
printf("%d\n", solve());
}
}
HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)的更多相关文章
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)
Problem Description There's a queue obeying the first in first out rule. Each time you can either pu ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
- HDU 4431 Mahjong(枚举+模拟)(2012 Asia Tianjin Regional Contest)
Problem Description Japanese Mahjong is a four-player game. The game needs four people to sit around ...
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...
- HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP
意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...
- HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)
Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...
- HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)
Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...
- HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...
随机推荐
- 菜鸟笔记 -- Chapter 4.7 代码注释与编码规范
4.7 代码注释与编码规范 在程序代码中适当的添加注释可以提高程序的可读性和可维护性.好的编码规范可以使程序更易阅读和理解.下面我们将介绍几种代码注释,以及应该注意的编码规范. 4.7.1 代码注释 ...
- Java经典书籍-PDF
密码:rhgr https://pan.baidu.com/s/17MkdVFS9JrsbseMveQePOQ
- Flask—06-理解掌握flask数据模型(02)
数据模型 模型关系 一对多(使用最多) 一:学生(Student) 需要添加反向引用 多:文章(Article) 需要添加外键关联 一对一 一:学生(Student),主表 需要添加反向引用,在一对多 ...
- noip2018 洛谷 P1969积木大赛
1 //一定不要忘记这句话 “连续区间 ”!! #include<bits/stdc++.h> using namespace std; int main(){ int n, h;//n是 ...
- OI 刷题记录——每周更新
每周日更新 2016.05.29 UVa中国麻将(Chinese Mahjong,Uva 11210) UVa新汉诺塔问题(A Different Task,Uva 10795) NOIP2012同余 ...
- <CPP学习>第一天 第一个CPP程序 hello word
由于我是计算机类嵌入式专业的大一学生,之前一直使用的是生万物的C语言,了解了其过程性语言的特性及其基础语法,在大一下学期期末阶段想自学一下C++,其实在开学初就买了一本C++ Primer,但由于各种 ...
- VC中edit控件使用
SetSel(start,end)作用:定制EDIT的所选择内容.间接地可以用于定位光标位置. 使用例子:EXP1:设置光标CEdit* pEdit=(CEdit*)GetDlgItem(I ...
- MySQL——用户与密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码.通过下面的方式找到root默认密码,然后登录mysql进行修改: grep 'temporary p ...
- 详解Linux运维工程师
运维工程师是从一个呆逼进化为苦逼再成长为牛逼的过程,前提在于你要能忍能干能拼,还要具有敏锐的嗅觉感知前方潮流变化.如:今年大数据,人工智能比较火……(相对表示就是 Python 比较火) 之前写过运维 ...
- kali linux 安装谷歌浏览器
kali linux 版本 2018.2 先下载谷歌浏览器安装包 wget https://dl.google.com/linux/direct/google-chrome-stable_curren ...