本文主要是介绍Balanced Lineup (poj-3264),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
RMQ解决方案:
#include <iostream>
#include <math.h>
#define max(a,b) ((a>b)?a:b)
#define min(a,b) (a<b?a:b)using namespace std;const int maxn=50001;
int h[maxn];
int mx[maxn][16],mn[maxn][16];
int n,q;void rmq_init()
{int i,j,t;for(j=1;j<=n;j++) mx[j][0]=mn[j][0]=h[j];int m=floor(log((double)n)/log(2.0));for(i=1;i<=m;i++){for(j=1;j<=n;j++){t = j+(1<<(i-1));if(t<=n) mx[j][i]=max(mx[j][i-1],mx[t][i-1]);else mx[j][i]=mx[j][i-1];}}for(i=1;i<=m;i++){for(j=1;j<=n;j++){t = j+(1<<(i-1));if(t<=n) mn[j][i]=min(mn[j][i-1],mn[t][i-1]);else mn[j][i]=mn[j][i-1];}}
}int rmq(int l,int r)
{int m=floor(log((double)(r-l+1))/log(2.0));int a=max(mx[l][m],mx[r-(1<<m)+1][m]);int b=min(mn[l][m],mn[r-(1<<m)+1][m]);return a-b;
}int main()
{int i,l,r;scanf("%d%d",&n,&q);for(i=1;i<=n;i++) scanf("%d",&h[i]);rmq_init();for(i=0;i<q;i++){scanf("%d%d",&l,&r);printf("%d\n",rmq(l,r));}return 0;
}
这篇关于Balanced Lineup (poj-3264)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!