zoukankan      html  css  js  c++  java
  • Codeforces Round #277 (Div. 2) E. LIS of Sequence DP

    E. LIS of Sequence
     

    The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.

    Nam created a sequence a consisting of n (1 ≤ n ≤ 105) elements a1, a2, ..., an (1 ≤ ai ≤ 105). A subsequence ai1, ai2, ..., aik where1 ≤ i1 < i2 < ... < ik ≤ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.

    Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n), into three groups:

    1. group of all i such that ai belongs to no longest increasing subsequences.
    2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
    3. group of all i such that ai belongs to every longest increasing subsequence.

    Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.

    Input

    The first line contains the single integer n (1 ≤ n ≤ 105) denoting the number of elements of sequence a.

    The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105).

    Output

    Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index ibelongs to.

    Sample test(s)
    input
    1
    4
    output
    3
     
    Note

    In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.

    In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.

    题意:对于一个n序列中的LIS进行一个分类

    题解:我们用nlogn二分的方法求LIS,l[i]表示正向包含a[i]的LIS,r[i]表示反向不包含a[i]的LIS

             如果l[i]+r[i]==LIS 则为可以判断其必处于某一LIS之中,否则必然不在任意一个LIS之中,可判该点为1

              在对于多个相同l[i] 可以判断为2

             其余为3

    ///1085422276
    #include<bits/stdc++.h>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    #define meminf(a) memset(a,127,sizeof(a));
    
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-')f=-1;ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            x=x*10+ch-'0';ch=getchar();
        }return x*f;
    }
    //****************************************
    #define maxn 100000+50
    #define mod 1000000007
    #define inf 1000000007
    
    int a[maxn],b[maxn],dp[maxn],l[maxn],r[maxn],ans[maxn],D[maxn];
    int main(){
      int n=read();
      for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[n-i+1]=-a[i];
      }
       fill(dp+1,dp+n+1,inf);
       for(int i=1;i<=n;i++){
          int tmp=lower_bound(dp+1,dp+n+1,a[i])-dp;
          l[i]=tmp;
          dp[tmp]=a[i];
       }
       int L=lower_bound(dp+1,dp+n+1,inf)-dp;
       L--;
       fill(dp+1,dp+n+1,inf);
       for(int i=1;i<=n;i++){
         int tmp=lower_bound(dp+1,dp+n+1,b[i])-dp;
         r[n-i+1]=tmp-1;
         dp[tmp]=b[i];
       }mem(D);mem(ans);
       for(int i=1;i<=n;i++){
           if(r[i]+l[i]==L){
              D[l[i]]++;
           }
           else {
             ans[i]=1;
           }
       }
       for(int i=1;i<=n;i++){
          if(ans[i]==1)cout<<1;
          else if(D[l[i]]>1)cout<<2;
          else cout<<3;
       }
       cout<<endl;
      return 0;
    }
    代码
  • 相关阅读:
    jqgard改变单元格后重新定值(事件和弹窗)
    js多个input框赋相同值
    查看PHP已安装拓展的指令
    PHP重新安装zlib拓展,处理PHP Startup: Invalid library (maybe not a PHP library) 'zlib.so' in Unknown
    php拓展安装报错:PHP Startup: Invalid library (maybe not a PHP library) 'zlib.so' in Unknown
    Composer提示:Installation Failed, Reverting ./Composer.Json To Its Original Content.错误的解决办法
    SQL Server序列号的获取
    一步步开发Windows服务(Windows Service)[转]
    HTML+CSS+JS实现的贪吃球小游戏【转】
    自制一个滚动条
  • 原文地址:https://www.cnblogs.com/zxhl/p/4948777.html
Copyright © 2011-2022 走看看