1 条题解

  • 0
    @ 2021-6-14 23:29:46

    C++ :

    #include<iostream>
    using namespace std;
    int a[101]={0},b[101]={0},c[101]={0};
    int main()
    {
    	int n,i,j,max;
    	cin>>n;
    	for(i=1;i<=n;i++)
    	cin>>a[i];
    	for(i=1;i<=n;i++)
    	{
    		b[i]=1;
    		for(j=1;j<=i-1;j++)
    		  if(a[i]>a[j]&&b[j]+1>b[i])
    		    b[i]=b[j]+1;
    	}
    	for(i=n;i>=1;i--)
    	{
    		c[i]=1;
    		for(j=i+1;j<=n;j++)
    		  if(a[j]<a[i]&&c[j]+1>c[i])
    		    c[i]=c[j]+1; 
    	}
    	max=0;
    	for(i=1;i<=n;i++)
    	  if(b[i]+c[i]>max)
    	    max=b[i]+c[i];
    	cout<<n-max+1;
    	return 0;
    }
    

    Pascal :

    var n,i,j,x:longint;p2,p1,a:array[1..1000]of longint;
    begin
      readln(n);
      for i:=1 to n do read(a[i]);
      p1[1]:=1;
      for i:=2 to n do
        begin
          x:=0;
          for j:=1 to i-1 do
            if (a[j]<a[i])and(p1[j]>x) then x:=p1[j];
          p1[i]:=x+1;
        end;
      p2[n]:=1;
      for i:=n-1 downto 1 do
        begin
          x:=0;
          for j:=i+1 to n do
            if (a[j]<a[i])and(p2[j]>x) then x:=p2[j];
          p2[i]:=x+1;
        end;
      x:=0;
      for i:=1 to n do
        if p1[i]+p2[i]-1>x then x:=p1[i]+p2[i]-1;
      writeln(n-x);
    end.
    

    Java :

    import java.util.Scanner;
    
    public class Main {
        public static int MAX_N = 105;
        public static int a[] = new int[MAX_N];
        public static int dp1[] = new int[MAX_N];
        public static int dp2[] = new int[MAX_N];
        public static void main(String args[]) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            for(int i=1;i<=n;i++){
                a[i] = in.nextInt();
            }
            dp1[1] = 0;
            for(int i=1;i<=n;i++)
                for(int j=1;j<i;j++)
                    if(a[i] > a[j])
                        dp1[i] = max(dp1[i],dp1[j]+1);
            for(int i=n;i>=1;i--)
                for(int j=n;j>i;j--)
                    if(a[i] > a[j])
                        dp2[i] = max(dp2[i],dp2[j]+1);
            int mmax = 0;
            for(int i=1;i<=n;i++)
                mmax = max(mmax,dp1[i]+dp2[i]);
            System.out.println(n-mmax-1);
        }
    
        public static int max(int a,int b){
            return a>b?a:b;
        }
    }
    
    • 1

    信息

    ID
    236
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者