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;
}
代码

 

posted @ 2015-11-08 23:57  meekyan  阅读(282)  评论(0编辑  收藏  举报