O (NLOGN) algorithm, longest ascending subsequence, non-motion gauge

Source: Internet
Author: User

//The fastest ascending sub-sequence of the fastest algorithm, non-dynamic programming, the use of two points of thought, and the idea of the stack,//with each number to the stack top elements compared, if larger than the top of the stack, then into the stack, or use a binary search, find out the first number larger than this number to replace#include <iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cmath>using namespacestd;intMain () {inttemp, tail; intN; intstack[1001]; scanf ("%d", &N); Tail=0; stack[0] = -1;  for(inti =1; I <= N; i++) {scanf ("%d", &temp); if(Temp >Stack[tail]) stack[++tail] = temp;//into the stack        Else {            intL =1, r =tail; intmid;  while(L <= R)//two points{mid= (L + r)/2; if(Temp >Stack[mid]) L= Mid +1; ElseR = Mid-1; } Stack[l]=temp; }} printf ("%d", tail); return 0;}

O (NLOGN) algorithm, longest ascending subsequence, non-motion gauge

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.