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 ...
随机推荐
- Xcode DeviceSupport
问题:Could not locate device support files. This iPhone 6s is running iOS 12.1 (16B5059d), which may n ...
- HTML表格和表单
<table>格式: 注意:1. 合并单元格:COLSPAN(跨列)ROWSPAN(跨行) 2.cellspacing属性定义单元格之间的间距(以像素为单位). cellpadding属性 ...
- wubiuefi-支持新版本ubuntu的wubi
由于某些原因,ubuntu官方不再提供新版的wubi 这就使得部分想快速且安全尝试新版ubuntu的用户望而却步 最近在外文网站找到了wubi的新版本wubiuefi,支持最新版的ubuntu 目前支 ...
- IOS NSNotification 通知
一. 先看下官方对NSNotification通知的解释 1. NSNotification 通知 @interface NSNotification : NSObject <NSCopying ...
- ABAP术语-Connection Type
Connection Type 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/17/1042479.html A connection ty ...
- openresty安装配置 Ubuntu下
1.进入openresty-1.11.2.4的压缩包木木,我这里是在“/usr/local/”下: 2.进入后执行[tar -xzvf openresty-1.11.2.4.tar.gz]进行解压 3 ...
- VMware虚拟化NSX-Manager命令行更改admin用户密码
1.1 登录到NSX-Manager命令行界面,输入用户名和密码登录到用户模式 Log in to the vSphere Client and select an NSX virtual ap ...
- BootStrap的动态模态框及静态模态框
1.要用bootStrap这个框架就必须要重载它的class类,也就是说class要一样 代码如下: 有疑问的可以在下面留言,欢迎大家一起交流 1.1动态模态框 <!DOCTYPE html&g ...
- [转]ThinkPHP5 隐藏index.php问题
ThinkPHP5 隐藏index.php问题 Apache,修改.htaccess文件 ----------------------------------------------------- R ...
- Chrome浏览器调试移动端网页 chrome://inspect/#devices
我使用的是魅族(魅蓝NOTE6 ),电脑是win 7系统,以下几步就可以轻松使用浏览器内置的功能调试移动端网页了: 注意:谷歌浏览器需要先FQ,不然调试页面会空白或者报404错误,(不会FQ的可以联系 ...