1. 程式人生 > >SXCPC2018 nucoj2007 和Mengjiji一起攻克難關

SXCPC2018 nucoj2007 和Mengjiji一起攻克難關

ans AC names long name pac problem 分享 for

problem

技術分享圖片

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, a[2005];
ll dp[2005][2005];
int main(){
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d", &a[i]);
    dp[2][0] = abs(a[2]-a[1]);
    for(int i=3; i<=n; i++)
        for
(int j=0; j<i; j++){ if(j==i-1){ dp[i][j] = dp[i-1][0]; for(int k=1; k<=i-2; k++) dp[i][j] = min(dp[i][j], dp[j][k]+abs(a[k]-a[i])); } else dp[i][j] = dp[i-1][j] + abs(a[i] - a[i-1]); } ll ans=0x3f3f3f3f3f3f3f3f
; for(int i=0; i<n; i++) ans = min(ans, dp[n][i]); cout<<ans<<endl; return 0; }

SXCPC2018 nucoj2007 和Mengjiji一起攻克難關