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。
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:
- group of all i such that ai belongs to no longest increasing subsequences.
- group of all i such that ai belongs to at least one but not every longest increasing subsequence.
- 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.
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).
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.
14
3
41 3 2 5
3223
41 5 2 3
3133
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]; }}
- Codeforces 486E. LIS of Sequence
- CodeForces 486E LIS of Sequence
- CodeForces - 486E LIS of Sequence
- Codeforces 486E LIS of Sequence
- Codeforces 486E. LIS of Sequence
- Codeforces 486E LIS of Sequence(线段树+LIS)
- [Codeforces 486E] LIS of Sequence (LIS套路)
- [Codeforces 486E] LIS of Sequence (LIS套路)
- 486E - LIS of Sequence(LIS)
- cf 486E LIS of Sequence
- Codeforces Round #277 (Div. 2) E. LIS of Sequence
- Codeforces Round #277 (Div. 2) E. LIS of Sequence
- CF 277 Div2 E LIS of Sequence
- E. LIS of Sequence(好题 LIS )
- LIS of Sequence
- [CF486E] LIS of Sequence && DP
- CodeForces 490E Restoring Increasing Sequence
- CodeForces 490E Restoring Increasing Sequence
- 《用GNS3 路由器 模拟 PC 实现访问外网》
- windows driver 枚举串口
- 第十二周项目3-用递归方法求解(2)求1*3*...*n的递归式
- 整理音乐
- windows driver 映射小文件
- Codeforces 486E. LIS of Sequence
- windows driver 映射大文件
- 不同域的SqlServer 如何用hostname访问服务器
- 【SQL Server】 表值参数(Table-Valued Parameter)的使用
- 进程与线程的区别(转)
- 二维高斯函数
- 胡思乱想
- SQL优化
- Android 高仿微信6.0主界面 带你玩转切换图标变色