d[i][0]表示到达第i层,且在第i层没有使用魔法的最少时间
d[i][1]表示到达第i层,且在第i层使用魔法通过一层
d[i][2]表示到达第i层,且在第i层使用魔法通过两层
状态转移方程:
d[i][0] = h[i] + min(d[i-1][1], d[i - 1][0]);if(i > 2) d[i][0] = min(d[i][0], d[i - 2][2] + h[i]); d[i][1] = min(d[i - 1][2], d[i - 1][0]);d[i][2] = d[i - 1][0];AC代码:#include<cstdio>#include<algorithm>using namespace std;const int maxn = 1e4 + 5;int d[maxn][3], h[maxn];int solve(int n){ d[1][0] = h[1]; d[1][1] = d[1][2] = 0; for(int i = 2; i <= n; ++i){ d[i][0] = h[i] + min(d[i-1][1], d[i - 1][0]); if(i > 2) d[i][0] = min(d[i][0], d[i - 2][2] + h[i]); d[i][1] = min(d[i - 1][2], d[i - 1][0]); d[i][2] = d[i - 1][0]; } int ans = min(d[n][0], d[n][1]); return min(ans, d[n][2]);}int main(){ int n; while(scanf("%d", &n) == 1){ for(int i = 1; i <= n; ++i) scanf("%d", &h[i]); PRintf("%d/n", solve(n)); } return 0;}如有不当之处欢迎指出!
新闻热点
疑难解答