486E-LIS of Sequence (LIS)

Source: Internet
Author: User

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
      
       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.