HDU 5532 almost Sorted Array (longest not descending or not ascending sub-sequence)

Source: Internet
Author: User

Topic Stamp This

title: give you a sequence that asks you, this sequence decreases only one element, decreases afterwards whether it is not ascending or not descending sequence.

train of thought: directly to the sequence to find the longest non-descending and non-ascending sub-sequence, if the length of the sub-sequence is n-1 or N, it will meet the problem conditions.

Longest non- descending subsequence and longest not ascending subsequence: Here the method of data structure is used to find the length of the longest non-ascending (descending) sub-sequence. is to construct an array f[], this array means: f [i] represents the minimum (maximum) of a subsequence of length I, because, the same length of the sub-sequence, the last number is the smallest or largest, more convenient to put the number behind. This time may say that even if my number is small enough or large enough, if the position is not correct, then the length of the sequence ending with him cannot be the longest. As for this question, in fact, we update is f[], every time we all sweep f[], update this array, there will be no problem.

1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <math.h>5#include <algorithm>6#include <vector>7#include <string>8#include <queue>9#include <map>Ten#include <stack> One#include <Set> A #definell Long Long - #defineMAXN 100010 - #definePI ACOs (-1.0)//Pi the Const intmod=1e9+7; - using namespacestd; - intNUM[MAXN]; - intF[MAXN]; + intN; - intMain () + { A     intT; atscanf"%d",&T); -      while(t--) -     { -Memset (F,0,sizeof(f)); -memset (NUM,0,sizeof(num)); -  inscanf"%d",&n); -          for(intI=0; i<n;i++) scanf ("%d",&num[i]); to  +         intlen=1; -f[0]=num[0]; the          for(intI=1; i<n;i++) *         { $             if(num[i]>=f[len-1]) f[len++]=Num[i];Panax Notoginseng             Else -             { the                 intCnt=upper_bound (F,f+len,num[i])-F; +f[cnt]=Num[i]; A             } the         } +  -         if(len==n-1|| len==N) $         { $printf"yes\n"); -             Continue; -         } the  -len=1;WuyiMemset (F,0,sizeof(f)); thef[0]=num[n-1]; -          for(inti=n-2; i>=0; i--) Wu         { -             if(num[i]>=f[len-1]) f[len++]=Num[i]; About             Else $             { -                 intCnt=upper_bound (F,f+len,num[i])-F; -f[cnt]=Num[i]; -             } A         } +  the         if(len==n-1|| Len==n) printf ("yes\n"); -         Elseprintf"no\n"); $     } the  the     return 0; the}
View Code

HDU 5532 almost Sorted Array (longest not descending or not ascending sub-sequence)

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.