标签:style blog io color os ar java for sp
6 7
4 2 6 2 5 6
27
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN 11000 #define UP 0 #define DN 1 #define sqr(x) ((x)*(x)) #define INFL 0x3f3f3f3f3f3f3f3f typedef long long qword; inline void deal(qword &x,qword y) { if (x>y)x=y; } qword dp[MAXN][2][2][2]; qword h[MAXN]; int main() { freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int i,j,k,k2,n; qword m; scanf("%d%lld",&n,&m); for (i=1;i<=n;i++) scanf("%lld",h+i); memset(dp,0x3f,sizeof(dp)); dp[1][UP][UP][0]=(h[n]<h[1]?(h[1]-h[n]):sqr(h[1]-h[n])); dp[1][DN][DN][0]=(h[n]>h[1]?(h[n]-h[1]):sqr(h[n]-h[1])); dp[1][UP][DN][1]=(h[n]<h[1]?(h[1]-h[n]):sqr(h[1]-h[n])) + m; dp[1][DN][UP][1]=(h[n]>h[1]?(h[n]-h[1]):sqr(h[n]-h[1])) + m; for (i=2;i<=n;i++) { for (k=0;k<2;k++) { for (k2=0;k2<2;k2++) { deal(dp[i][UP][k][k2],dp[i-1][UP][k][k2]+((h[i]>h[i-1])?(h[i]-h[i-1]):sqr(h[i]-h[i-1])) ); deal(dp[i][UP][k][k2|1],dp[i-1][DN][k][k2]+((h[i]>h[i-1])?(h[i]-h[i-1]):sqr(h[i]-h[i-1])) +m); deal(dp[i][DN][k][k2],dp[i-1][DN][k][k2]+((h[i]<h[i-1])?(h[i-1]-h[i]):sqr(h[i-1]-h[i])) ); deal(dp[i][DN][k][k2|1],dp[i-1][UP][k][k2]+((h[i]<h[i-1])?(h[i-1]-h[i]):sqr(h[i-1]-h[i])) +m); } } } qword ans=INFL; deal(ans,dp[n][DN][DN][0]); deal(ans,dp[n][UP][UP][0]); deal(ans,dp[n][DN][DN][1]-m); deal(ans,dp[n][UP][UP][1]-m); printf("%lld\n",ans); }
标签:style blog io color os ar java for sp
原文地址:http://www.cnblogs.com/mhy12345/p/4060466.html