1 solutions

  • 0
    @ 2025-11-5 15:30:05

    C++ :

    #include <iostream>
    #include <cstdio>
    #define MAX_NUM 105
    using namespace std;
    
    int main()
    {
      int high[MAX_NUM], left[MAX_NUM], right[MAX_NUM], n;
      scanf("%d", &n);
      for(int i = 0; i < n; i++)
        scanf("%d", &high[i]);
      for(int i = 0; i < n; i++)
      {
        left[i] = 1;
        for(int j = 0;j < i; j++)
        {
          if(high[j] <high[i] && left[j] > left[i] - 1)   // 找出左边最长递增子序列
            left[i] = left[j] + 1;
        }
      }
      for(int i = n-1; i >= 0; i--)
      {
        right[i] = 1;
        for(int j = n-1;j > i; j--)
        {
          if(high[j] < high[i] && right[j] > right[i] - 1)  // 找出右边最长递减子序列
            right[i] = right[j] + 1;
        }
      }
      int max = 0;
      for(int i = 0; i < n; i++)
      {
        if(max < left[i] + right[i] - 1)
          max = left[i] + right[i] - 1;  // 寻找最多合唱人数
      }
      printf("%d\n",n - max);  
      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.
    
    • 1

    Information

    ID
    16860
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By