1 /* 2 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少 3 DP:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程:(a[i], k) <- min (a[i-1], k) + cost 4 以及(a[i-1], a[i]) <- min (a[i-1], k) + cost, (k, a[i])和(a[i], a[i-1])情况类似,最后再去最小值就行了 5 */ 6 /************************************************ 7 * Author :Running_Time 8 * Created Time :2015-8-15 14:31:31 9 * File Name :UVA_1291.cpp 10 ************************************************/ 11 12 #include <cstdio> 13 #include <algorithm> 14 #include <iostream> 15 #include <sstream> 16 #include <cstring> 17 #include <cmath> 18 #include <string> 19 #include <vector> 20 #include <queue> 21 #include <deque> 22 #include <stack> 23 #include <list> 24 #include <map> 25 #include <set> 26 #include <bitset> 27 #include <cstdlib> 28 #include <ctime> 29 using namespace std; 30 31 #define lson l, mid, rt << 1 32 #define rson mid + 1, r, rt << 1 | 1 33 typedef long long ll; 34 const int MAXN = 1e4 + 10; 35 const int INF = 0x3f3f3f3f; 36 const int MOD = 1e9 + 7; 37 int a[MAXN]; 38 int dp[MAXN][5][5]; 39 40 int cal(int x, int y) { 41 int ret; 42 if (x == y) ret = 1; 43 else { 44 if (y == 0) ret = 2; 45 else { 46 if (abs (x - y) == 2) { 47 ret = 4; 48 } 49 else ret = 3; 50 } 51 } 52 return ret; 53 } 54 55 int main(void) { //UVA 1291 Dance Dance Revolution 56 int n = 0; 57 while (scanf ("%d", &a[++n]) == 1) { 58 if (a[1] == 0) break; 59 while (a[n] != 0) { 60 scanf ("%d", &a[++n]); 61 } 62 n--; 63 memset (dp, INF, sizeof (dp)); 64 dp[1][a[1]][0] = (a[1] == 0 ? 1 : 2); 65 dp[1][0][a[1]] = (a[1] == 0 ? 1 : 2); 66 for (int i=2; i<=n; ++i) { 67 for (int j=0; j<=4; ++j) { 68 int c1 = cal (a[i], j); 69 int c2 = cal (a[i], a[i-1]); 70 int x = a[i], y = a[i-1]; 71 dp[i][x][y] = min (dp[i][x][y], dp[i-1][j][y] + c1); 72 dp[i][j][x] = min (dp[i][j][x], dp[i-1][j][y] + c2); 73 74 dp[i][y][x] = min (dp[i][y][x], dp[i-1][y][j] + c1); 75 dp[i][x][j] = min (dp[i][x][j], dp[i-1][y][j] + c2); 76 } 77 } 78 79 int ans = INF; 80 for (int i=0; i<=4; ++i) { 81 ans = min (ans, min (dp[n][i][a[n]], dp[n][a[n]][i])); 82 } 83 printf ("%d\n", ans); n = 0; 84 } 85 86 return 0; 87 }
时间: 2024-10-11 00:17:27