题目传送门

题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少
分析: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的更多相关文章

  1. 递推DP UVA 607 Scheduling Lectures

    题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...

  2. 递推DP UVA 1366 Martian Mining

    题目传送门 /* 题意:抽象一点就是给两个矩阵,重叠的(就是两者选择其一),两种铺路:从右到左和从下到上,中途不能转弯, 到达边界后把沿途路上的权值相加求和使最大 DP:这是道递推题,首先我题目看了老 ...

  3. 递推DP UVA 1424 Salesmen

    题目传送门 /* 题意:给定包含n个点的无向图和一个长度为L的序列,修改尽量少的点使得相邻的数字相同或连通 DP:状态转移方程:dp[i][j] = min (dp[i][j], dp[i-1][k] ...

  4. 递推DP UVA 590 Always on the run

    题目传送门 题意:题意难懂,就是一个小偷在m天内从城市1飞到城市n最小花费,输入的是每个城市飞到其他城市的航班. 分析:dp[i][j] 表示小偷第i天在城市j的最小花费.状态转移方程:dp[i][j ...

  5. 递推DP UVA 473 Raucous Rockers

    题目传送门 题意:n首个按照给定顺序存在m张光盘里,每首歌有播放时间ti,并且只能完整的存在一张光盘里,问最多能存几首歌 分析:类似01背包和完全背包,每首歌可存可不存,存到下一张光盘的情况是当前存不 ...

  6. 递推DP URAL 1167 Bicolored Horses

    题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...

  7. 递推DP URAL 1017 Staircases

    题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...

  8. 递推DP URAL 1260 Nudnik Photographer

    题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...

  9. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

随机推荐

  1. Visual Studio VS如何卸载Visual assistant

    1 点击工具-扩展管理器   2 选中Visual Assist X,点击卸载即可.                            

  2. 微信小程序实战之 goods(订餐页)

    项目目录: 模拟数据: utils / data.js function getSData() { var data = [ { "name": "热销榜", ...

  3. iOS 获取手机 唯一标识

    存贮在keychainQuery 可以统计用户使用情况 -(void)gatherMessage{ //采集用户设备信息 NSUserDefaults *userDefaults=[NSUserDef ...

  4. 【Mongodb教程 第三课 】MongoDB 删除数据库

    dropDatabase() 方法 MongoDB db.dropDatabase() 命令是用来删除一个现有的数据库. 语法: dropDatabase() 命令的基本语法如下: db.dropDa ...

  5. hdoj-1242-Rescue【广搜+优先队列】

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  6. Kemans算法及其Python 实现

    算法优缺点: 优点:容易实现缺点:可能收敛到局部最小值,在大规模数据集上收敛较慢使用数据类型:数值型数据 算法思想 k-means算法实际上就是通过计算不同样本间的距离来判断他们的相近关系的,相近的就 ...

  7. 【iOS系列】-文件管理

    OC中操作文件,需要使用NSFileManager: 需要使用NSFileManager的创建方式: //单例模式创建对象 NSFileManager * f2 = [NSFileManager de ...

  8. Tomcat 安装错误

    安装tomcat时,遇到"failed to install tomcat6 service check your settings and permissions"的问题 安装时 ...

  9. netstat --numeric-ports -a -t -p 排查hadoop主从节点是否建立通信

    tcp  通信 [root@hadoop2 logs]# netstat --numeric-ports -a -tActive Internet connections (servers and e ...

  10. u-boot支持LCD显示(基于TQ2440)【转】

    本文转载自:http://www.cnblogs.com/pengdonglin137/p/4633877.html u-boot支持LCD显示(基于TQ2440)   阅读目录(Content) 平 ...