[NOIP2004] chorus formation (dp)


1065: [Video] Getting Started with dynamic programming (one-dimensional side push 3: Chorus formation)

Time limit: 1 Sec   Memory Limit: 128 MB
submission: 1480   Resolution: 745
[ submit ] [ state ] [ Discussion Board ] [proposition man: ADMIN ]

Title Description

[Title] is intended to
    N bits students stand in a row, the music teacher to please them (NK) bit the students out of the line so that the rest of the students lined up K-bit chorus formation.
    Chorus formation refers to a formation: students set K-bit from left to right are numbered 1,2 ..., K,
    their height are T1, T2, ..., TK, then their height satisfies T1 < T2 ... <Ti> Ti + 1 > ...> TK (1 <= i <= K).
    Your task is known to all students of the N-bit height to calculate a minimum of several students out of the line, you can make the rest of the students lined up chorus formation.
[Input format
    of the first line is an integer N (2 <= N <= 100), represents the total number of students.
    Down n integers, separated by spaces, the integer i Ti (130 <= Ti <= 230) is the bit i students height (cm).
[] Output format
    output line, an integer, i.e., requires a minimum number of students in a column.
[Sample input]
. 8
186 186 150 200,160,130,197,220
[output] Sample
4
#include<bits/stdc++.h>
using namespace std;
const int N =1005;
int a[N],b[N],c[N],n,ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++){
     b[i]=1;c[i]=1;
    } 
    for(int i=1;i<=n;i++){   
        for(int j=i-1;j>=b[i];j--){
            if(a[j]<a[i]){
            b[i]=max(b[i],b[j]+1);
            }    
        }
    }
    for(int i=n-1;i>=1;i--){
        for(int j=i+1;j<=n;j++){
            if(a[i]>a[j]){
              c[i]=max(c[i],c[j]+1);    
            }    
        }
    }
    for(int i=1;i<=n;i++){
    ans=max(ans,b[i]+c[i]-1);
    }
    
    printf("%d",n-ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/phemiku/p/11420962.html