CSU 1453: Balance sequence Learning line tree must be done

Source: Internet
Author: User

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1453,

Title: Given an array of size 100000, the maximum number is 100000. now I ask you to find a sequence so that the absolute value of any two-digit difference cannot exceed K.

In fact, the crux of the problem is the range of numbers, not more than 100000, so that, you can use the whole paragraph segment tree coverage. Dp[i] For the end of this number, the longest Lis, the beginning of the dp[i]=0, with a line tree to cover him. Each time you insert a number a[i], look for the DP maximum in this interval [a[i]-k,a[i]+k] and modify dp[a[i] = Find () +1.

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN =100000+ -;structdata{intL,R,MX;//for each node, an interval [L,r] is recorded. And the sum of the recording intervals .    intMid () {return(L + R)/2;}} SEGTREE[MAXN<<2];//move right two bits.voidBuilt (intRootintBeginintend) {Segtree[root]. L= begin; Segtree[root]. R = end;//Coverage Interval    if(Begin = =end) {segtree[root].mx=0;return ; } built (Root<<1, Begin,segtree[root].mid ()); Built (Root<<1|1, Segtree[root].mid () +1, end); Segtree[root].mx= Max (segtree[root<<1].mx,segtree[root<<1|1].mx); return ;}voidAdd (intRootintPosintval) {    if(Segtree[root]. L = = Pos && pos = =Segtree[root]. R) {segtree[root].mx= Val;return ; }    if(Pos <= Segtree[root].mid ()) Add (root<<1, Pos,val); Else if(Pos >= segtree[root].mid () +1) Add (root<<1|1, Pos,val); Segtree[root].mx= Max (segtree[root<<1].mx,segtree[root<<1|1].mx); return ;}//[Begin,end] is the interval to query, if the desired interval contains segment tree coverage interval, you can returnintFind (intRootintBeginintEnd//interval Query{    //query [1,7], the left sub-tree range covers the [1,6], you can return directly, the left subtree max.    if(Begin <= Segtree[root]. L && end >= Segtree[root]. Rreturnsegtree[root].mx;//cover the    if(End <= Segtree[root].mid ())//completely on the left sub-number        returnFind (root<<1, Begin,end); Else if(Begin >= Segtree[root].mid () +1)//completely in the right sub-tree        returnFind (root<<1|1, Begin,end); Else    {        intLmax = Find (root<<1, Begin,end); intRmax = Find (root<<1|1, Begin,end); returnMax (Lmax,rmax); }}voidWork () {Built (1,0, maxn- -); intN; intK; scanf ("%d%d",&n,&k);  for(intI=1; i<=n;++i) {intx; scanf ("%d",&x); intA = max (0, X-k); intb = min (maxn- -, x+k); intt = Find (1, A, b); Add (1, x,t+1); } printf ("%d\n", Find (1,0, maxn- -)); return ;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endif    intT; scanf ("%d",&t);  while(t--) work (); return 0;}
View Code

Thinking: This complexity is NLOGN, then we have found a nlogn algorithm for Lis?

No, the key to the problem is the size of the numbers. not more than 100000, in order to use the line tree so cover, otherwise. It's not going to work.

The number of the array in the LIS is indeterminate.

100000

CSU 1453: Balance sequence Learning line tree must be done

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.