P2893 [USACO08FEB] road

Straight into the subject. Farmer John want to transform one way, the original of each segment elevation road is Ai, Bi after repair is spent | A_i-B_i |. We ask repaired road is not monotonous monotone does not rise or drop. Seek minimum cost.

Data range: n <= 2000,0≤ Ai ≤ 1,000,000,000

(Really, after a lapse of several months, I found that the problem is actually quite simple)

Most start, hit a greedy.

#include <bits / STDC ++ H.> // I really like the first universal title playing 
the using  namespace STD;
 const  int MAXN = 2010 ;
 Long  Long   n-, ANS1, ANS2;
 Long  Long A [MAXN]; 


int main () 
{ 
    Scanf ( " % LLD " , & n-);
     for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        Scanf ( " % LLD " , & A [I]); 
    } 
    for ( int I = . 1 ; I <n-; i ++)
    {
        if(a[i]>a[i+1])
        ans1+=abs(a[i]-a[i+1]);
        //printf("%d\n",ans1);
    }
    for(int i=n;i>1;i--)
    {
        if(a[i]>a[i-1])
        ans2+=abs(a[i-1]-a[i]);
        //printf("%d\n",ans2);
    }
    printf("%d",min(ans1,ans2));
    return 0;
}

 

This greedy train of thought is easy to find, if you want a minimum of words, sunken clods will be filled up, and this will definitely fill the height of the clods and sides of the lowest relative level (for example, the third sample earth, and will fill up as high as two) thus considered to run twice, by a single, a single down.

Results: 40 points

。。。

Readily a group of hack data:

9 5 5 5 1 1 1 5 5 5;

I did not update the height, so that there will be after-effect. So every time I finished cut will update once it highly. But we found that there was still a aftereffect of, hack data or cancer.

I finally thought dp(I hate dp)

Next consider the transfer equation considering the i-th clods, only the height of the front i-clods, as well as a high degree of local data blocks that affect them, so: we dp [i] [j] will change before i segment when the minimum cost for not drop sequence, and highly paragraph j way for j

 

However, j enumeration will time bomb heaven! ! ! !

By the previous conclusion, we found that the minimum fill will appear in the target height, so we opened another array, the height of the deposit to the inside, sort, find a suitable Ai from the inside, eliminating the need to traverse 1-100000000 time

Continue equation. Write like this, before the i-th column minimum (this is not monotonically decreasing)

This question, too Luo Gu data so that the water can only consider single by before.

Then put the code (only put a single growing)

#include<bits/stdc++.h>
using namespace std;
const int inf=0x7fffffff;
const int maxn=2010;
int n;
int a[maxn],b[maxn];
int dp[maxn][maxn];
int main()
{
    int ans=inf;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]),b[i]=a[i];
    sort(b+ . 1 , B + . 1 + n-);
     for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        for ( int J = . 1 ; J <= n-; J ++ ) 
        { 
            IF (J == . 1 ) // because the loop border, so when we want j == 1 out of Japanese sentence, to take a minimum 
                DP [I] [J] DP = [I- 1 ] [J] + ABS (A [I] - B [J]) ;
             the else 
                DP [I] [J] = min (DP [I] [J- . 1 ], DP [I- . 1 ] [J] + ABS (A [I] - B [J])); // Sign soil the 
            IF (i ==  n-)
                ANS = min (dp [i] [j] years); 
        } 
    } 
    Printf ( " % d \ n " , year);
    return  0 ; 
}

 (Worship the gods look next door dp)

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11427387.html