Codeforces 340D D. Bubble Sort Graph (dp+ segment tree)

Source: Internet
Author: User

Topic Links:

Codeforces 340D

Main topic:

Given a program, is the bubble sort, each time if found adjacent to two number before one is greater than the next one, after the swap position to build the edge, ask the last to get the largest independent set in this figure, this maximum independent set defined as all the points are not adjacent to the largest point of the scale of the collection.

Topic Analysis:
  • First we can know that for a[i], it will only and must be smaller than the back side of the building, so we just need to fix the first point, and then find the longest ascending subsequence. (This ensures that no adjacent points are available)
  • Defines the state Dp[i] is the length of the longest ascending subsequence ending with the I term.
  • The transfer equation is as follows: d P[I]=max{d P[J]+1},(1≤J≤I?1,a[J]<a[I])
  • The maximum length of the interval can be recorded by the segment tree, and the maximum size is smaller than a[i], and the final optimized complexity is O(N?Lo g n 2 )
AC Code:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define MAX 100007using namespace STD;intN,a[max];structtree{intL,R,MAXN;} tree[max<<2];voidPUSH_UP (intu) {TREE[U].MAXN = max (tree[u<<1].MAXN, tree[u<<1|1].MAXN);}voidBuild (intU,intLintr) {tree[u].l = l; TREE[U].R = R;intMID = L+r>>1; TREE[U].MAXN =0;if(L = = r)return; Build (u<<1, L, mid); Build (u<<1|1, mid+1, r);}voidUpdate (intU,intXintV) {intL = TREE[U].L;intR = TREE[U].R;intMID = L+r>>1;if(L = = r) {tree[u].maxn = v;return; }if(x > Mid) Update (u<<1|1, x, v);ElseUpdate (u<<1, x, v); PUSH_UP (u);}intQuery (intU,intLeft,intright) {intL = TREE[U].L;intR = TREE[U].R;intMID = L+r>>1;if(left <= l && R <= right)returnTREE[U].MAXN;intRET =0;if(Left <= mid && right >= l) ret = max (ret, query (u<<1, left, right));if(left <= R && right > mid) ret = MAX (ret, query (u<<1|1, left, right));returnRET;}intMain () { while( ~scanf("%d", &n)) { for(inti =1; I <= N; i++)scanf("%d", &a[i]); Build (1,1, n);intx,ans=1; Update (1, a[1] ,1); for(inti =2; I <= N; i++) {x = query (1,1, A[i]); ans = max (ans, x+1); Update (1, A[i], x+1); }printf("%d\n", ans); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 340D D. Bubble Sort Graph (dp+ segment tree)

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.