486E-LIS of Sequence (LIS)
Question: Give a sequence with a length of n and ask each number about the role of LIS (longest increasing subsequence) in the sequence. There are three types:
1. This number does not appear in any LIS.
2. This number appears in at least one but not all LIS.
3. This number appears in all LIS
Solution: The LIS algorithm of nlgn can obtain the LIS length up [I] ending at each I position. The number of LIS is actually a dag. Just find the number of unique values in a layer. After the LIS algorithm, the system scans forward from the back and maintains the maximum length. In the middle, it can be judged that a length has several values. If some lengths have multiple positions, they all belong to 2, if a length has only one position, it belongs to 3, and the rest are non-LIS elements. When judging multiple numbers, a certain number of num [I] can relax its length position. Make sure that it is smaller than the number of help [up [I + 1] at this time, this ensures that he is in LIS (the last element of LIS is specific ).
Code:
#include
#include
#include #include
#define inf 10000000using namespace std;const int Max=100010;int num[Max];int up[Max];int down[Max];int D[Max];void getLIS(int num[],int n){ int last=0; for(int i=0; i
last) D[last = up[i]]=num[i]; D[up[i]-1]=num[i]; }}int help[Max];int ne[Max];int ans[Max];int main(){ int n; while(cin>>n) { for(int i=0; i
=0; i--) ma=max(ma,up[i]); for(int i=n-1; i>=0; i--) { if(up[i]==ma) { if(help[up[i]]==0) ans[i]=3,ne[ma]=i; else { ans[i]=2; ans[ne[ma]]=2; ne[ma]=i; } help[up[i]]=num[i]; } else { if(num[i]>=help[up[i]+1]) ans[i]=1; else { if(help[up[i]]==0) ans[i]=3,ne[up[i]]=i; else { ans[i]=2; ans[ne[up[i]]]=2; ne[up[i]]=i; } help[up[i]]=num[i]; } } } for(int i=0; i