Codeforces 486E. LIS of Sequence

本文探讨了如何确定序列中最长递增子序列(LIS)中各元素的归属情况,即判断每个元素是否属于所有、部分还是不属于最长递增子序列,并通过具体示例进行说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,我们计算出以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
1
4
output
3
input
4
1 3 2 5
output
3223
input
4
1 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];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值