Codeforces486 E. LIS of Sequence(LIS唯一性判断)
Posted live4m
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces486 E. LIS of Sequence(LIS唯一性判断)相关的知识,希望对你有一定的参考价值。
题意:
解法:
lc[i]表示以a[i]为结尾的LIS长度.
rc[i]表示以a[i]为开头的LIS长度.
设序列的LIS为ma,
如果lc[i]+rc[i]-1==ma,那么说明a[i]可以作为某个LIS的数.
那么如何判断LIS是否一定需要a[i]呢?
mp[x]表示可以作为LIS的数中,lc[i]=x的数个数.
预处理mp[],对于可以在LIS的数a[i],如果mp[lc[i]]==1,那么说明a[i]必选.
code:
#include<bits/stdc++.h>
// #define SYNC_OFF
typedef std::vector<int> VE;
typedef std::pair<int,int> PI;
#define int long long
#define ll long long
#define ull unsigned long long
//fast-coding
#define ST(x) x.begin()
#define ED(x) x.end()
#define RST(x) x.rbegin()
#define RED(x) x.end()
#define CL(x) x.clear();
#define all(a,n) a+1,a+1+n
#define ff(i,n) for(ll i=1;i<=n;i++)
#define rff(i,n) for(ll i=n;i>=1;i--)
#define fff(i,n) for(ll i=0;i<n;i++)
#define rfff(i,n) for(ll i=n-1;i>=0;i--)
#define SC(x) scanf("%s",x)
#define SL(x) strlen(x)
#define pss(a) push_back(a)
#define ps(a) push(a)
#define SZ(x) (int)x.size()
#define pee puts("");
#define eee putchar(' ');
#define re readdd()
#define pr(a) printtt(a)
int readdd(){int x=0,f=1;char c=getchar();//
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c))x=x*10+c-'0',c=getchar();
return f*x;}
void printtt(int x){if(x<0)putchar('-'),x=-x;//
if(x>=10)printtt(x/10);putchar(x%10+'0');}
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}//
int ppow(int a,int b,int mod){a%=mod;//
int ans=1%mod;while(b){if(b&1)ans=(long long)ans*a%mod;
a=(long long)a*a%mod;b>>=1;}return ans;}
bool addd(int a,int b){return a>b;}
int lowbit(int x){return x&-x;}
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
bool isdigit(char c){return c>='0'&&c<='9';}
bool Isprime(int x){
for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
return 1;
}
void ac(int x){if(x)puts("YES");else puts("NO");}
//
using namespace std;
// const int mod=998244353;
const int mod=1e9+7;
const int maxm=2e6+5;
const int BIT_maxm=2e6+5;
int lc[maxm],rc[maxm];
int ans[maxm];
int a[maxm];
int n;
void solve(){
n=re;
ff(i,n)a[i]=re;
VE temp;
//lc[i]表示以a[i]结尾的最大长度
//求最长上升子序列
ff(i,n){
int p=lower_bound(ST(temp),ED(temp),a[i])-ST(temp);
if(p==SZ(temp))temp.pss(a[i]);
else temp[p]=a[i];
lc[i]=p+1;
}
int ma=SZ(temp);
//rc[i]表示以a[i]开头的最大长度
//反转后求最长下降子序列
//取反变为求最长上升子序列
reverse(all(a,n));
ff(i,n)a[i]=-a[i];
CL(temp);
ff(i,n){
int p=lower_bound(ST(temp),ED(temp),a[i])-ST(temp);
if(p==SZ(temp))temp.pss(a[i]);
else temp[p]=a[i];
rc[i]=p+1;
}
reverse(all(rc,n));//记得翻转回去
//
ff(i,n)ans[i]=1;
map<int,int>mp;
ff(i,n){
if(lc[i]+rc[i]-1==ma){
ans[i]=2;
mp[lc[i]]++;
}
}
ff(i,n){
if(lc[i]+rc[i]-1==ma&&mp[lc[i]]==1){
ans[i]=3;
}
}
//
ff(i,n){
pr(ans[i]);
}
}
void Main(){
// #define MULTI_CASE
#ifdef MULTI_CASE
int T;cin>>T;while(T--)
#endif
solve();
}
void Init(){
#ifdef SYNC_OFF
ios::sync_with_stdio(0);cin.tie(0);
#endif
#ifndef ONLINE_JUDGE
freopen("../in.txt","r",stdin);
freopen("../out.txt","w",stdout);
#endif
}
signed main(){
Init();
Main();
return 0;
}
以上是关于Codeforces486 E. LIS of Sequence(LIS唯一性判断)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 486E LIS of Sequence 题解
Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) D. Points and Powers of Two
Codeforces1156 E. Special Segments of Permutation(单调栈,复杂度分析)