• POJ1836——DP(LCS+LDS)——Alignment


    Description

    In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity. 

    Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line. 

    Input

    On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k <= n). 

    There are some restrictions: 
    • 2 <= n <= 1000 
    • the height are floating numbers from the interval [0.5, 2.5] 

    Output

    The only line of output will contain the number of the soldiers who have to get out of the line.

    Sample Input

    8
    1.86 1.86 1.30621 2 1.4 1 1.97 2.2
    

    Sample Output

    4

    Source

    大意:找到最长的LCS+LDS,用N减去就是答案,有了二分模板就是好用,不过要考虑全是LCS全是LDS的时候,害我WA了一次,只要移动分界点就行
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAX = 1100;
    double a[MAX];
    double b[MAX],c[MAX];
    double k1[MAX],k2[MAX];
    int main()
    {
        int n, l, r, mid;
        scanf("%d",&n);
        for(int i = 1; i <= n;i++)
            scanf("%lf",&a[i]);
        int t1 ,t2 ;
        int max1 = 0;
        int p;
        for( p = 0; p <= n;p++){
                memset(b,0,sizeof(b));
                memset(c,0,sizeof(c));
                t1 = t2 = 1;
            if( p == 0){
                    for(int j = 1; j <= n;j++)
                     c[t2++] = a[j];
            }
            else if(p == n){
                    for(int j = 1; j <= n ;j++)
                    b[t1++] = a[j];
            }
            else {
              for(int j = 1; j <= p ;j++)
                b[t1++] = a[j];
              for(int j = p + 1 ; j <= n ;j++)
                c[t2++] = a[j];
            }
    
              //LCS
             k1[1] = b[1];
             int i, k;
             for(i = 2, k = 1; i < t1; i++){
                  if(b[i] > k1[k]) k1[++k] = b[i];
             else {
                 l = 1, r = k;
                while( l <= r){
                    mid = (l + r) /2;
                   if(k1[mid] < b[i]) l = mid + 1;
                   else if(k1[mid] > b[i]) r = mid - 1;
                   else break;
                }
                k1[l] = a[i];
               }
             }
             if(p == 0) k = 0;
             int temp1 = k;
             //LDS
         //    for(int q = 1; q < t2; q++)
           //     printf("%.2lf ",c[q]);
             k2[1] =c[1];
             for(i = 2, k = 1; i < t2; i++){
                 if(c[i] < k2[k]) k2[++k] = c[i];
             else {
                l = 1, r = k;
             while( l <= r){
                   mid = (l + r) /2;
                  if(k2[mid] > c[i]) l = mid + 1;
                  else if(k2[mid] < c[i]) r= mid - 1;
                  else break;
             }
             k2[l] = c[i];
               }
             }
             if(p == n) k = 0;
             int temp2 = k;
            int x = temp1 + temp2;
           // printf("%d %d
    ",temp1,temp2);
    
            max1 = max(max1,x);
        }
        printf("%d",n - max1);
       return 0;
    }
    View Code
  • 相关阅读:
    四十一.redis主从复制 RDB/AOF持久化 数据类型
    四十.创建Redis集群 管理集群
    三十九.NoSQL概述 部署Redis服务 、 部署LNMP+Redis
    三十八. 分库分表概述 配置mycat
    520D
    442C
    LA4788
    LA3276
    LA4122
    zoj3478
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4365832.html
Copyright © 2020-2023  润新知