2 条题解
-
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int n; int h[MAXN] = {0}; int dp[MAXN] = {0}; // dp[i]表示跳到第i块石头所需的最小费用 int cost(int i, int j) { // cost函数表示从第i块石头跳到第j块石头的花费 return abs(h[i] - h[j]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); dp[2] = cost(1, 2); // 初始化,2号石头只能从前一块(1号)石头跳来 // 注:不用初始化dp[1](它本来就是0) for (int i = 3; i <= n; i++) dp[i] = min(dp[i - 1] + cost(i, i - 1), dp[i - 2] + cost(i, i - 2)); printf("%d", dp[n]); return 0; } -
0
#include<bits/stdc++.h> using namespace std; const int MAXN=1e5+5; int n; int h[MAXN]={0}; int dp[MAXN]={0};//dp[i]表示跳到第i块石头所需的最小费用 int cost(int i,int j){//cost函数表示从第i块石头跳到第j块石头的花费 return abs(h[i]-h[j]); } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&h[i]); dp[2]=cost(1,2);//初始化,2号石头只能从前一块(1号)石头跳来 //注:不用初始化dp[1](它本来就是0) for(int i=3;i<=n;i++) dp[i]=min(dp[i-1]+cost(i,i-1),dp[i-2]+cost(i,i-2)); printf("%d",dp[n]); return 0; }
- 1
信息
- ID
- 875
- 时间
- 2000ms
- 内存
- 1024MiB
- 难度
- 5
- 标签
- 递交数
- 121
- 已通过
- 43
- 上传者