"Dynamic Programming DP" RMQ problem (ST algorithm)

Source: Internet
Author: User

"RMQ" range minimum/maximum Query range max problem

"St Algorithm"

An effective algorithm for solving RMQ problems

  

  pretreatment pre-treatment to construct D, preprocessing time complexity O (NLOGN)

Using the idea of dynamic programming D (I, j) indicates that the minimum value of the range I ~ i + 2j-1 has a state transition equation

  D (i, j) = min {d (i, j-1), D (i + 2j-1, j-1)}

  

Set the original data stored in the array a[], the initial state

D (i, 0) = a[i]

  Querying for the maximum time complexity of a given interval O (1)

Set the query interval to [L, R]

Interval length = r-l + 1 < = 2k (where k is the largest integer that satisfies the condition)

The query results are min {d (L, K), D (r-2k + 1, k)}

  

Template

#include <iostream>#include<vector>#include<algorithm>using namespacestd;/*rmq-st algorithm interval min template interval maximum need to change min () to Max () by Chsobin*/Const intMAXN = -;intDP[MAXN][MAXN];voidRmq_init (Constvector<int>&A) {    intn =a.size ();  for(intI=0; i<n;++i) dp[i][0] =A[i];  for(intj=1; (1&LT;&LT;J) <=n; J + +){         for(intI=0; i+ (1&LT;&LT;J)-1<n; i++) {Dp[i][j]= Min (dp[i][j-1], Dp[i + (1<< (J-1))][j-1]); }    } }intRMQ (intLintR) {    intK =0;  while( (1<< (k +1)) <= r-l+1) k++; returnMin (Dp[l][k], dp[r-(1&LT;&LT;K) +1][k]);}intMain () {inta[Ten] = {1,5,8,7,2,6,1,9,3,4}; Vector<int> A (A, A +Ten);    Rmq_init (A);  for(intI=0; I<a.size (); + +i) {cout<< A[i] <<" "; } cout<<Endl;  while(1){        intL, R; cout<<"[L, R]"; CIN>> L >>R; cout<< RMQ (L, R) <<Endl; }    return 0;} 

Topic

hdu3183

/*problem-solving ideas: The use of RMQ, set the original number is n, then after the removal of m number of n-m numbers left. (1) Because there are n-m numbers, then the smallest number in the 1 to m+1 position must be the first number in the result, the smallest number in the number that is recorded as POS (2) and then from the next position of the position pos+1 to the m+2 position must be the second number in the result, Then go back and look backwards. (3) In order to ensure the smallest number, so to ensure that the highest minimum, but also to ensure that the digital length to meet the conditions*/#include<iostream>#include<cstring>using namespacestd;Const intMAXN =1010;CharA[MAXN];intdp[maxn][ One];CharANS[MAXN];intN, M;//Custom Comparison RulesintMIN (intIintj) {    returnA[i] <= a[j]?i:j;}//pretreatmentvoidinit () { for(intI=0; i<n;++i) {dp[i][0] =i; }     for(intj=1;(1&LT;&LT;J) <=n;++j) {         for(intI=0;(i + (1&LT;&LT;J)-1) < n;++i) {Dp[i][j]= MIN (dp[i][j-1], dp[i+ (1<< (J-1))][j-1]); }    }}//EnquiryintQueryintLintR) {    intk=0; inttemp = r-l+1; intsum =1;  while(Sum <=temp) {        ++K; Sum*=2; } k--; returnMIN (Dp[l][k], dp[r-(1&LT;&LT;K) +1][k]); }intMain () {intL =0; intR =0;  while(SCANF ("%s%d", A, &m) = =2){        intindex =0; L=0; R=m; N=strlen (a); M= N-m;                Init (); ////for (int i=0;i<n;++i) {//for (int j=0; (i + (1<<j)-1) < n; ++J) {//printf ("%c", A[dp[i][j]]);//            }//printf ("\ n");//        }                     while(m--){//cout << "L =" << l << "r =" << r << Endl;L =query (L, R); Ans[index++] =A[l]; L++; R++; if(r==n) R = n1;//ensure R does not cross} Ans[index]=' /'; intK =0;  for(; k<index;++k) {//Remove leading 0            if(Ans[k]! ='0') Break; }        if(K==index) printf ("0\n"); Elseprintf"%s\n", ans+k); }    return 0;} 
AC 0ms

Reference

Petition P197

"Dynamic Programming DP" RMQ problem (ST algorithm)

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.