递推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] ...
随机推荐
- HDU 5280 Senior's Array
Senior's Array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- c++多线程编程:常见面试题
题目:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码 子线程与主线程必有一个满足条件(flag == ...
- 跟着9张思维导图学JavaScript
思维导图小tips 思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又极其有效,是一种革命性的思维工具.思维导图运用图文并重的技巧,把各级主题的关系用相互隶属与相关的层级图表现出来 ...
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- HTML的DIV如何实现水平居中
内部的DIV必须有下面两行代码即可 text-align:center; margin:0 auto; 在IE6中同样可以
- eclipse Alt+/ 不能提示
普通情况下alt+/有代码提示作用,还有代码提示的快捷代码也不是alt+/,因此要恢复代码提示用alt+/.须要做两件事. 在 Window - Preferences - General - Ke ...
- Android 四大组件学习之Service五
本节学习IntentService, 可能就有人问了. 什么是IntentService, IntentService有什么作用? 不是已经有了Service,那为什么还要引入IntentServic ...
- 1 Angular 2 简介与 AngularJS 1.x 历史对比
Angular 2 是一款JavaScript的开源框架,用于协助单一页面应用程序运行.Angular 2 是 AngularJS 1.x 的升级版本,应Web的进化和前端开发的变革还有从Angula ...
- linux 【目录】
[第一篇]linux[目录] [第五篇]特殊权限及定时任务 [第六篇]用户和用户管理及定时任务复习
- 最简单的基于FFmpeg的移动端样例:IOS 视频转码器
===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...