题面:
画一个表格,一目了然
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
using namespace std;
#define eps (1e-6)
#define LL long long
#define pi acos(-1.0)
#define ALL(a) (a.begin(),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define IOS cin.tie(0),cout.sync_with_stdio(0)
#define PRINT(a,b) cout << "#" << (a) << " " << (b) << endl
//#define DEBUG(a,b) cout << "$" << (a) << " " << (b) << endl
#define lin cout << "\n--------------------\n"
const LL LLINF = 1e18+100;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MOD = 1000000000+7;
#define esp 1e-6
int H[MAXN] = {0};
int dp[MAXN][100];
int main()
{
int t;
int n,W;
cin >> t;
while(t--){
cin >> n >> W;
ZERO(H);
ZERO(dp);
for(int i=1; i<=n; ++i){
scanf("%d", &H[i]);
}
H[0] = H[1];
for(int i=H[1]; i<=100; ++i){
dp[1][i] = (i-H[1])*(i-H[1]);
}
for(int i=2; i<=n; ++i){
for(int j=1; j<=100; ++j){
if(j<H[i]) continue;
for(int k=1; k<=100; ++k){
if(k<H[i-1]) continue;
if(k==H[i-1]){
dp[i][j] = dp[i-1][k] + abs(j-k)*W; //
}
else{
dp[i][j] = min(dp[i][j], dp[i-1][k] + abs(j-k)*W); //
}
}
//cout << "#" << i << " " << j << ": " << dp[i][j] << endl;
dp[i][j] += (j-H[i])*(j-H[i]); //垫高的花费
//cout << "#" << i << " " << j << ": " << dp[i][j] << endl;
}
}
int ans = INF;
for(int i=H[n]; i<=100; ++i){
ans = min(dp[n][i], ans);
//cout << dp[n][i] << endl;
}
cout << ans << endl;
}
return 0;
}