Longest ascending subsequence nlogn

Source: Internet
Author: User

In the longest ascending subsequence, for the number of IPTS [I], traverse forward, when the number of IPTS [J] is smaller than the value of IPTS [I], IPTS [J] can be used as the first digit in the ascending sequence.

DP [I] = max {DP [J] + 1 | j <I & IPT [J] <IPT [I]}


If two statuses A and B meet the requirements of DP [a] = DP [B] and

It is better for the following status DP [a], because if you set the stage to "I"> "DP [B", you must set the stage to "IPT [I]>" DP [a] ". if you do not use the following command,

Therefore, if you only save status A, the optimal solution will not be lost.

For the same Dp value, you only need to keep one of the smallest

Then, the error message returned when the error message returned ).....

This sequence is ordered.

For example, 1 6 2 3 7 5

DP 1 2 2 3 4 4

When 2 is calculated, 6 can be replaced with 2 if the value of "2" is 6.

Similarly, 1 (6)2 3 (7)5

DP 1 2 3 4

In this way, you can maintain a sequence during computing.


//#pragma comment(linker, "/STACK:102400000,102400000")//HEAD#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <algorithm>#include <queue>#include <string>#include <set>#include <stack>#include <map>#include <cmath>#include <cstdlib>using namespace std;//LOOP#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define FED(i, b, a) for(int i = (b); i>= (a); --i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(A,value) memset(A,value,sizeof(A))//STL#define PB push_back//INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)#define RS(s) scanf("%s", s)typedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 1010;#define FF(i, a, b) for(int i = (a); i < (b); ++i)#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)#define CPY(a, b) memcpy(a, b, sizeof(a))#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)#define ALL(c) (c).begin(), (c).end()#define SZ(V) (int)V.size()#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)#define WI(n) printf("%d\n", n)#define WS(s) printf("%s\n", s)#define sqr(x) (x) * (x)typedef vector <int> VI;typedef unsigned long long ULL;const double eps = 1e-10;const LL MOD = 1e9 + 7;int ipt[1010], dp[1010], a[1010];int main(){    int n;    while (~RI(n))    {        REP(i, n)            RI(ipt[i]);        REP(i, n)   dp[i] = 1;        int ans = 0;        CLR(a, INF);        REP(i, n)        {            int x = lower_bound(a, a + ans, ipt[i]) - a;            dp[i] =  x + 1;            ans = max(ans, dp[i]);            a[x] = ipt[i];        }        cout << ans << endl;    }    return 0;}



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.