Cow sort--rmq

Source: Internet
Author: User

"Problem description"
The cows were lined up under the leadership of Aunt Bear.
Obviously, different cows are not necessarily of the same height ...
Now, cows want to know, if you find some continuous cows, ask the leftmost cow A is the shortest, right
The b of the side is the highest, and B is higher than a cow, and if there is a cow in the middle, the height cannot be the same as a and B cows.
Ask some of these cows how many heads they will have.
Give the cow's height from left to right, tell them the maximum number of cows that match the condition (the answer could be 0 or two,
But it won't be a one).
"Input Format"
The first row is a number N (2<=n<=100000), which indicates the number of cows in the head.
Next N number, one number per line, top to bottom for the cow's height from left to right (1<= height
<=maxlongint).
"Output Format"
A row that represents the maximum number of cows.
"Input Sample" tahort.in
5
1
2
3
4
1
"Output Sample" Tahort.out
4
"Sample Parsing"
Take 1th to 4th cows, meet the conditions and the most.

The idea of this problem is as follows: We can first find the position of the highest bull in the whole range and the shortest cow. In this case, the answer interval will not be able to cross the two cows, then the interval is separated by recursive solution. If the high bull (MAXN) is in front of the Dwarf Bull (Minn), then the recursive search

(L,MAXN), (maxn+1,minn-1), (minn,r), if the dwarf cow in front of the high cattle, then we directly update the answer with MAXN-MINN+1, then recursive search (l,minn-1), (Maxn+1,r) can.

In this case, the problem is transformed into the position of how to quickly find the maximum and minimum value in any interval. This is a typical RMQ problem, we choose to use St table to solve. Since we are asking for the maximum minimum value, we can not use ST table-stored values, which is not only good writing but also short time. The specific practice is that we customize the function to compare the number, each time after the end of the size of the current interval to record the maximum (small) value of the position. So we can even do O (1) Every time we have a recursive access.

This problem has a lot of places or very pit, for example, you write a very correct rmq in lemon or this machine run will lead to re, it is said that may be because of excessive number of recursive layer and explosion stack, this oneself debugging also not come out, you will only see the program after all the data input error. You can use the Vijos, but my program submits all the tle several times before, and the result is an optimization that returns without a search if the interval length of the recursion is less than the current answer. But it's still not working. The last discovery is that because of the global variable, the local variable is turned over ... Konjac Konjac still do not understand why.

Finally, go to the code.

#include <cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<cstdlib>#defineRep (i,a,n) for (int i = a;i <= n;i++)#definePer (i,n,a) for (int i = n;i >= a;i--)#defineEnter Putchar (' \ n ')using namespacestd;Const intN =100005, M = -; typedefLong Longll;intn,h[n],max1[n][m],min1[n][m],ans1;intRead () {intAns =0; CharCh,last =' '; CH=GetChar ();  while(Ch <'0'|| CH >'9')    {        if(ch = ='-') ch =Last ; CH=GetChar (); }     while(Ch >='0'&& CH <='9') {ans*=Ten; Ans+ = CH-'0'; CH=GetChar (); }    if(Last = ='-') ans =-ans; returnans;}intFmaxintCintD//custom Compare numbering functions{    if(H[c] > H[d] | | (H[c] = = H[d] && C < D))returnC; Else returnD; } intFmin (intCintd) {    if(H[c] < h[d] | | (H[c] = = H[d] && C > D))returnC; Else returnD;}voidAskintLintR) {    if(l >= R | | ans1 >= r-l+1)return;//This optimization can save a lot of time    intK = log2 (r-l+1);//attention, open cstdlib here .    intg =1<<K; intMAXN = Fmax (max1[l][k],max1[r-g+1][k]); intMinn = Fmin (min1[l][k],min1[r-g+1][k]);//This is the two lines of the pit countless times!!!     if(MAXN = = Minn)return; if(Maxn >Minn) {ans1= Max (ans1,maxn-minn+1);//Update and recursionAsk (l,minn-1); Ask (Maxn+1, R); return; }    Else{Ask (L,MAXN); Ask (Maxn+1, minn-1);        Ask (Minn,r); return; }}intMain () {n=read (); Rep (I,1, N) {H[i]=read (); max1[i][0] =i; min1[i][0] =i; }    intQ =log2 (n); Rep (J,1, Q) {         for(inti =1; i+ (1&LT;&LT;J)-1<=n;i++) {Max1[i][j]= Fmax (max1[i][j-1],max1[i+ (1<< (J-1))][j-1]); MIN1[I][J]= Fmin (min1[i][j-1],min1[i+ (1<< (J-1))][j-1]); }//St table to find the maximum minimum value} ask (1, N); printf ("%d\n", ans1); return 0;}

Cow sort--rmq

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.