Codeforces Round #277 (Div. 2) E. LIS of Sequence
2014-11-15 21:44
363 查看
Let F1i be
the length of LIS ending exactly at ai of
sequence {a1, a2, ..., ai}.
Let F2i be
the length of LIS beginning exactly at ai of
sequence {ai, ai + 1, ..., an}.
l = length of LIS of {a1, a2, ..., an} = max{F1i} = max{F2j}.
Let Fi be
the length of LIS of sequence {a1, a2, ..., ai - 1, ai + 1, ..., an}
(i.e the length of LIS of initial sequence a after removing element ai).
Index i must in group:
1) if F1i + F2i - 1 < l,
2) if Fi = l
3) if Fi = l - 1
How to caculate Fi?
We have: Fi = max{F1u + F2v} among 1 ≤ u < i < v ≤ n such
that au < av.
From this formula, we can use Segment tree to calculate Fi.
(Tutorial)
the length of LIS ending exactly at ai of
sequence {a1, a2, ..., ai}.
Let F2i be
the length of LIS beginning exactly at ai of
sequence {ai, ai + 1, ..., an}.
l = length of LIS of {a1, a2, ..., an} = max{F1i} = max{F2j}.
Let Fi be
the length of LIS of sequence {a1, a2, ..., ai - 1, ai + 1, ..., an}
(i.e the length of LIS of initial sequence a after removing element ai).
Index i must in group:
1) if F1i + F2i - 1 < l,
2) if Fi = l
3) if Fi = l - 1
How to caculate Fi?
We have: Fi = max{F1u + F2v} among 1 ≤ u < i < v ≤ n such
that au < av.
From this formula, we can use Segment tree to calculate Fi.
(Tutorial)
#include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #include <complex> #include <fstream> #include <cassert> #include <cstdio> #include <bitset> #include <vector> #include <deque> #include <queue> #include <stack> #include <ctime> #include <set> #include <map> #include <cmath> #define CLR(x,y) memset(x,y,sizeof(x)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define eps 1e-9 #define INF 0x3f3f3f3f #define LLINF 1LL<<62 using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef complex<ld> point; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef vector<int> vi; template<class T> inline bool read(T &n) { T x = 0, tmp = 1; char c = getchar(); while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if(c == EOF) return false; if(c == '-') c = getchar(), tmp = -1; while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar(); n = x*tmp; return true; } template <class T> inline void write(T n) { if(n < 0) { putchar('-'); n = -n; } int len = 0,data[20]; while(n) { data[len++] = n%10; n /= 10; } if(!len) data[len++] = 0; while(len--) putchar(data[len]+48); } //----------------------------------- #define lowbit(x) (x&(-x)) const int MAXN=100010; int ince[MAXN],dece[MAXN],a[MAXN],num[MAXN+1],c[MAXN+1],n,L; int get(int i) { int res=0; for(; i; i-=lowbit(i)) res=max(c[i],res); return res; } void update(int i,int w) { for(; i<=MAXN; i+=lowbit(i)) c[i]=max(c[i],w); } int main() { read(n); for(int i=0; i<n; i++) read(a[i]); for(int i=0; i<n; i++) { ince[i]=get(a[i]-1)+1; num[ince[i]]++; update(a[i],ince[i]); L=max(L,ince[i]); } CLR(c,0); for(int i=n-1; i>=0; i--) { dece[i]=get(MAXN-a[i])+1; update(MAXN-a[i]+1,dece[i]); L=max(L,dece[i]); } for(int i=0; i<n; i++) if(ince[i]+dece[i]-1!=L) num[ince[i]]--; // 该前段长度不能构成LIS,故减一 for(int i=0; i<n; i++) { if(ince[i]+dece[i]-1!=L) putchar('1'); else if(num[ince[i]]!=1) putchar('2'); else putchar('3'); } return 0; }
相关文章推荐
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #277.5 (Div. 2) D. Unbearable Controversy of Being
- Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits
- Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- Codeforces Round #164 (Div. 2) C. Beautiful Sets of Points
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
- Codeforces Round #377 (Div. 2) Cormen --- The Best Friend Of a Man
- Codeforces Round #350 (Div. 2) E - Correct Bracket Sequence Editor(并查集)
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
- Codeforces Round #428 (Div. 2) E - Mother of Dragons
- 【Codeforces Round 354 (Div 2)B】【简单模拟】Pyramid of Glasses
- Codeforces Round #277 (Div. 2)(B)构造
- Codeforces Round #105 (Div. 2) D. Bag of mice
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #399 (Div. 1 + Div. 2, combined) (博弈,Nim游戏)E.Game of Stones
- Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
- Codeforces Round #250 (Div. 1) D. The Child and Sequence