Codeforces 486E. LIS of Sequence

来源:互联网 发布:淘宝两钻买家号多少钱 编辑:程序博客网 时间:2024/05/17 03:44

首先,我们计算出以a[i]结束的前缀的LIS长度---f1[i]

   以a[i]开始的后缀的LIS长度---f2[i]

然后,如果f1[i] + f2[i] - 1 < LIS,显然a[i]不属于任何一个LIS。

否则,如果有重复的(f1[i],f2[i]),那么a[i]不属于每一个LIS。

否则,a[i]属于每一个LIS。

E. LIS of Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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, ..., aikwhere 1 ≤ 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 i belongs to.

Sample test(s)
input
14
output
3
input
41 3 2 5
output
3223
input
41 5 2 3
output
3133
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}.

const int N = 100010;int a[N], b[N];int g[N], gg[N];int f1[N], f2[N];int ans[N];int n;int main(){    cin>>n;    for(int i=0;i<n;i++)    {        scanf("%d", a+i);        b[n-1-i] = 1e5 - a[i];    }    memset(g, 63, sizeof g);    memset(gg, 63, sizeof gg);    int LIS = 0;    for(int i=0;i<n;i++)    {        int k = lower_bound(g+1, g+n+1, a[i]) - g;        f1[i] = k;        LIS = max(LIS, k);        g[k] = a[i];        k = lower_bound(gg+1, gg+n+1, b[i]) - gg;        f2[i] = k;        gg[k] = b[i];    }    for(int i=0;i<n;i++) cout<<f1[i]<<' ' ; cout<<endl;    for(int i=0;i<n/2;i++) swap(f2[i], f2[n-1-i]);    for(int i=0;i<n;i++) cout<<f2[i]<<' ' ; cout<<endl;    map<pair<int, int>, int> mp;    for(int i=0;i<n;i++)    {        if(f1[i]+f2[i]-1<LIS) ans[i] = 1;        else  {            ans[i] = -1;            mp[make_pair(f1[i], f2[i])]++;        }    }    for(int i=0;i<n;i++)    {        if(ans[i]==-1)        {            if(mp[make_pair(f1[i], f2[i])]==1) ans[i] = 3;            else ans[i] = 2;        }        cout<<ans[i];    }}


0 0
原创粉丝点击