【题目链接】 点击打开链接
【题意】中文题目。
【解题方法】 容易想到一个非常朴素的DP。dp[i][j] 代表第i个儿子,身高为j的最低花费。 dp[i][j] = min(dp[i-1][k] + abs(j - k) * C + (x[i] - j) * (x[i] - j));
然后分为两种情况:
1:第i个儿子的身高,比i-1高时。 dp[i][j] = min(dp[i - 1][k] + j * c - k * c + X), (k <= j) X等于 (x[i] - j) * (x[i] - j);
2:第i个儿子的身高,比i-1矮时,dp[i][j] = min(dp[i - 1][k] - j * c + k * c + X), (k >= j) X等于 (x[i] - j) * (x[i] - j);
对于第一种情况,我们让f[i - 1][k] = dp[i - 1][k] - k * c; g[i][j] = j * c + x;
【AC代码】
//
//Created by BLUEBUFF 2016/1/8
//Copyright (c) 2016 BLUEBUFF.All Rights Reserved
//
#pragma comment(linker,"/STACK:102400000,102400000")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <time.h>
#include <cstdlib>
#include <cstring>
#include <sstream> //isstringstream
#include <iostream>
#include <algorithm>
using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define REP1(i, a, b) for(int i = a; i < b; i++)
#define REP2(i, a, b) for(int i = a; i <= b; i++)
#define REP3(i, a, b) for(int i = a; i >= b; i--)
#define CLR(a, b) memset(a, b, sizeof(a))
#define MP(x, y) make_pair(x,y)
const int maxn = 220;
const int maxm = 2e5;
const int maxs = 10;
const int maxp = 1e3 + 10;
const int INF = 1e9;
const int UNF = -1e9;
const int mod = 1e9 + 7;
int gcd(int x, int y) {return y == 0 ? x : gcd(y, x % y);}
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head
int dp[2][110], n, x, c, q[110], head, tail, cur;
int main()
{
while(scanf("%d%d", &n, &c) != EOF)
{
head = tail = cur = 0;
CLR(q, 0);
scanf("%d", &x);
REP2(i, 0, 100){
if(i < x) dp[cur][i] = INF;
else dp[cur][i] = (x - i) * (x - i);
}
REP1(i, 1, n){
scanf("%d", &x);
cur ^= 1;
head = tail = 0;
REP2(j, 0, 100){
int nowf = dp[cur ^ 1][j] - j * c;
while(head < tail && q[tail - 1] > nowf) tail--;
q[tail++] = nowf;
if(j < x) dp[cur][j] = INF;
else dp[cur][j] = q[head] + j * c + (j - x) * (j - x);
}
head = tail = 0;
REP3(j, 100, 0){
int nowf = dp[cur^1][j] + j * c;
while(head < tail && q[tail - 1] > nowf) tail--;
q[tail++] = nowf;
if(j >= x) dp[cur][j] = min(dp[cur][j], q[head] - j * c + (j - x) * (j - x));
else dp[cur][j] = INF;
}
}
int ans = INF;
REP2(i, 0, 100){
ans = min(ans, dp[cur][i]);
}
cout << ans << endl;
}
return 0;
}