题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1204

思路:dp[i]从dp[i-1]转移,这个转移就枚举了不使用魔法的过程,这个思路挺巧妙的,还有就是刚开始的特判也挺巧妙的

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int dp[10010];
int h[10010];
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
scanf("%d",&h[i]);
if(n < 3)
{
printf("0\n");
continue;
}
if(n == 3)
{
sort(h+1,h+n+1);
printf("%d\n",h[1]);
continue;
}
if(n > 3)
{
dp[1] = h[1];
dp[2] = h[2];
h[n+1] = 0;
for(int i=3; i<=n+1; i++)
dp[i] = min(dp[i-1],min(dp[i-2],dp[i-3])) + h[i];
printf("%d\n",dp[n+1]);
}
}
return 0;
}