递推DP UVA 1291 Dance Dance Revolution
题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少
分析:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程:(a[i], k) <- min (a[i-1], k) + cost以及(a[i-1], a[i]) <- min (a[i-1], k) + cost, (k, a[i])和(a[i], a[i-1])情况类似,最后再去最小值就行了
收获:四个状态转移方向
代码:
/************************************************
* Author :Running_Time
* Created Time :2015-8-15 14:31:31
* File Name :UVA_1291.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int a[MAXN];
int dp[MAXN][5][5]; int cal(int x, int y) {
int ret;
if (x == y) ret = 1;
else {
if (y == 0) ret = 2;
else {
if (abs (x - y) == 2) {
ret = 4;
}
else ret = 3;
}
}
return ret;
} int main(void) { //UVA 1291 Dance Dance Revolution
int n = 0;
while (scanf ("%d", &a[++n]) == 1) {
if (a[1] == 0) break;
while (a[n] != 0) {
scanf ("%d", &a[++n]);
}
n--;
memset (dp, INF, sizeof (dp));
dp[1][a[1]][0] = (a[1] == 0 ? 1 : 2);
dp[1][0][a[1]] = (a[1] == 0 ? 1 : 2);
for (int i=2; i<=n; ++i) {
for (int j=0; j<=4; ++j) {
int c1 = cal (a[i], j);
int c2 = cal (a[i], a[i-1]);
int x = a[i], y = a[i-1];
dp[i][x][y] = min (dp[i][x][y], dp[i-1][j][y] + c1);
dp[i][j][x] = min (dp[i][j][x], dp[i-1][j][y] + c2); dp[i][y][x] = min (dp[i][y][x], dp[i-1][y][j] + c1);
dp[i][x][j] = min (dp[i][x][j], dp[i-1][y][j] + c2);
}
} int ans = INF;
for (int i=0; i<=4; ++i) {
ans = min (ans, min (dp[n][i][a[n]], dp[n][a[n]][i]));
}
printf ("%d\n", ans); n = 0;
} return 0;
}
递推DP UVA 1291 Dance Dance Revolution的更多相关文章
- 递推DP UVA 607 Scheduling Lectures
题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...
- 递推DP UVA 1366 Martian Mining
题目传送门 /* 题意:抽象一点就是给两个矩阵,重叠的(就是两者选择其一),两种铺路:从右到左和从下到上,中途不能转弯, 到达边界后把沿途路上的权值相加求和使最大 DP:这是道递推题,首先我题目看了老 ...
- 递推DP UVA 1424 Salesmen
题目传送门 /* 题意:给定包含n个点的无向图和一个长度为L的序列,修改尽量少的点使得相邻的数字相同或连通 DP:状态转移方程:dp[i][j] = min (dp[i][j], dp[i-1][k] ...
- 递推DP UVA 590 Always on the run
题目传送门 题意:题意难懂,就是一个小偷在m天内从城市1飞到城市n最小花费,输入的是每个城市飞到其他城市的航班. 分析:dp[i][j] 表示小偷第i天在城市j的最小花费.状态转移方程:dp[i][j ...
- 递推DP UVA 473 Raucous Rockers
题目传送门 题意:n首个按照给定顺序存在m张光盘里,每首歌有播放时间ti,并且只能完整的存在一张光盘里,问最多能存几首歌 分析:类似01背包和完全背包,每首歌可存可不存,存到下一张光盘的情况是当前存不 ...
- 递推DP URAL 1167 Bicolored Horses
题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...
- 递推DP URAL 1017 Staircases
题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
随机推荐
- SLF4J: Failed to load class的问题及解决
今天在做接口测试,一运行测试程序,就跳出这样一个大大的错误: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. SLF4 ...
- linux nginx service nginx restart [fail]
命令:nginx -t 查看失败原因: nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/sit ...
- Android开发文档翻译之-Services
Service是一种能长期在后台运行同一时候不须要与用户进行交互的应用组件.其它组件能够开启service,开启后service能够自行运行及时用户已经切换到其它的应用.此外,组件能够与service ...
- 原生js 平滑滚动到页面的某个位置
window.scrollTo() 语法1: window.scrollTo(x-coord,y-coord) x-coord 是文档中的横轴坐标. y-coord 是文档中的纵轴坐标. 例子: w ...
- 【iOS系列】-UIImageView帧动画相关属性介绍
UIImageView帧动画相关属性介绍 1:相关属性: //An array of UIImage objects to use for an animation.存放UIImage对象,会按顺序显 ...
- 2016/04/18 session cookie 对比 应用 <?php session_start() / setcookie()?>
①会话 huihua.php <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- 在ubuntu18.0下安装qt4.7以及qt-creator安装过程中遇到的坑
最近的嵌入式Linux系统上要做课程设计= =要用贼老贼老的qt4.7,配环境踩坑都费了我1天时间.....所以记录下来,希望能给和我遇到相同问题的朋友一点帮助 apt-get install g++ ...
- 使用PXE安装CentOS7
1.环境 本文使用VMware 虚拟机进行实验. 点击VMware--编辑--虚拟网络编辑器,新建VMnet15,选择仅主机模式,取消勾选DHCP服务(因为这里使用自己的DHCP服务).我这里配好后是 ...
- HDU1083 Courses —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-1083 Courses Time Limit: 20000/10000 MS (Java/Others) Memory ...
- Easier SQL with Cupboard
Overview Cupboard is a way to manage persistence in a sqlite instance for your app. It was written b ...