Nyoj 214-Monotone increment subsequence (two) (algorithm, PS: Normal dynamic programming to timeout)

Source: Internet
Author: User
Tags cmath

214-Monotone Increment subsequence (ii)


Memory limit: 64MB time limit: 1000ms special Judge:no
accepted:11 submit:35

Title Description:

Given an integer sequence {a1,a2...,an} (0<n<=100000), find out the monotone increment of the eldest son and determine its length.

For example: 1 9 10 5 11 2 13 The longest monotonically incrementing subsequence is 1 9 10 11 13 with a length of 5.

Input Description:
There are multiple sets of test data (<=7) The first line of each set of test data is an integer n indicating that the sequence has n integers, followed by n integers in the next row, representing all the elements in the sequence. The middle of each shaping number is separated by a space (0<n<=100000). The data ends with EOF. The input data is guaranteed to be legal (all int integers)!
Output Description:
The length of the longest increment subsequence for each set of test data output shaping sequences, one row per output.
Sample input:Copy
71 9 10 5 11 2 1322-1
Sample output:
51

Analysis:
1, if the string itself is ascending, it is added directly in the temp[] string
2, otherwise we will find the first position greater than or equal to the value, and change the value of the position (so that the final composition of the temp[] string Ascⅱ The sum of the smallest)

Core code:

1  while(M--)2 {3scanf"%d", &v);4     if(Temp[cnt] <v)5     {6TEMP[++CNT] =v;7         Continue;8     }9      for(inti =0; I <= CNT; ++i)Ten     { One         if(Temp[i] >=v) A         { -Temp[i] =v; -              Break; the         } -     } -}

C + + code implementation (AC):

1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <cmath>6#include <stack>7#include <map>8#include <queue>9#include <Set>Ten  One using namespacestd; A Const intMAXN =100010; -  - intMain () the { -  -     intT, A[MAXN], TEMP[MAXN], CNT; -      while(~SCANF ("%d", &t)) +     { -CNT =0; +memset (A,0,sizeof(A)); Amemset (temp,0,sizeof(temp)); atscanf"%d", &a[0]); -TEMP[CNT] = a[0]; -          for(inti =1; I < T; ++i) -         { -scanf"%d", &a[i]); -             if(Temp[cnt] <A[i]) in             { -TEMP[++CNT] =A[i]; to                 Continue; +             } -              for(intj =0; J <= CNT; ++j) the                 if(A[i] <=Temp[j]) *                 { $TEMP[J] =A[i];Panax Notoginseng                      Break; -                 } the  +         } Aprintf"%d\n", CNT +1); the     } +     return 0; -}

C + + code (TLE) < dynamic planning:

1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <cmath>6#include <stack>7#include <map>8#include <queue>9#include <Set>Ten  One using namespacestd; A Const intMAXN =100010; -  - intMain () the { -  -     intT, A[MAXN], DP[MAXN], CNT; -      while(~SCANF ("%d", &t)) +     { -CNT =0; +memset (A,0,sizeof(A)); AMemset (DP,0,sizeof(DP)); at          for(inti =0; I < T; ++i) -         { -scanf"%d", &a[i]); -Dp[i] =1; -              for(intj =0; J < I; ++j) -                 if(A[i] >A[j]) inDp[i] = max (Dp[i], dp[j] +1); -CNT =Max (CNT, dp[i]); to         } +printf"%d\n", CNT); -     } the     return 0; *}

Nyoj 214-Monotone increment subsequence (two) (algorithm, PS: Normal dynamic programming to timeout)

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.