A - The Water Problem
In Land waterless,water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,an representing the size of the water source. Given a set of queries each containing 2 integers l and r,please find out the biggest water source between al and ar. InputFirst you are given an integer T(T≤10) indicating the number of test cases. For each test case,there is a number n(0≤n≤1000) on a line representing the number of water sources. n integers follow,respectively a1,an,and each integer is in {1,106}. On the next line,there is a number q(0≤q≤1000) representing the number of queries. After that,there will be q lines with two integers l and r(1≤l≤r≤n) indicating the range of which you should find out the biggest water source. OutputFor each query,output an integer representing the size of the biggest water source. Sample Input3 Sample Output100 求区间内最大的水源,之前区间dp没法做,会T,用rmq#include<iostream> #include<stdio.h> #include<stdlib.h> #include <iomanip> #include<cmath> #include<float.h> #include<string.h> #include<algorithm> #define sf scanf #define pf printf #define scf(x) scanf("%d",&x) #define scff(x,y) scanf("%d%d",&x,&y) #define prf(x) printf("%dn",x) #define mm(x,b) memset((x),(b),sizeof(x)) #include<vector> #include<queue> //#include<map> #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,n) for (int i=a;i>=n;i--) typedef long long ll; const ll mod=1e9+7; const double eps=1e-8; const int inf=0x3f3f3f3f; using namespace std; const double pi=acos(-1.0); const int N=1e3+10; int a[N],MAX[N][20]; void first(int n) { for(int j=1;(1<<j)<=n;j++) { for(int i=1;i+(1<<j)-1<=n;i++) { MAX[i][j]=max(MAX[i][j-1],MAX[i+(1<<(j-1))][j-1]); //MIN[i][j]=min(MIN[i][j-1],MIN[i+(1<<(j-1))][j-1]; } } } int solve(int l,int r) { int x=0; while(l-1+(1<<x)<=r) x++; x--; return max(MAX[l][x],MAX[r-(1<<x)+1][x]); } int main() { int re,n,tot,l,r;scf(re); while(re--) { mm(a,0); mm(MAX,0); scf(n); rep(i,1,n+1) { scf(a[i]); MAX[i][0]=a[i]; } first(n); scf(tot); while(tot--) { scff(l,r); prf(solve(l,r)); } } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |