题号未知 , Array Queries 【RMQ】
2016-07-28 13:59
405 查看
Description
Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index I to J.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 105), q (1 ≤ q ≤ 50000). The next line contains N space separated integers forming the array. There integers
range in [0, 105].
The next q lines will contain a query which is in the form I J (1 ≤ I ≤ J ≤ N).
Output
For each test case, print the case number in a single line. Then for each query you have to print a line containing the minimum value between index I and J.
Sample Input
2
5 3
78 1 22 12 3
1 2
3 5
4 4
1 1
10
1 1
Sample Output
Case 1:
1
3
12
Case 2:
10
题目大意:区间找最小值, 这里多一个操作, 就是对k进行了定位;确定在哪层 ,然后得到最小值;
AC代码:
Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index I to J.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 105), q (1 ≤ q ≤ 50000). The next line contains N space separated integers forming the array. There integers
range in [0, 105].
The next q lines will contain a query which is in the form I J (1 ≤ I ≤ J ≤ N).
Output
For each test case, print the case number in a single line. Then for each query you have to print a line containing the minimum value between index I and J.
Sample Input
2
5 3
78 1 22 12 3
1 2
3 5
4 4
1 1
10
1 1
Sample Output
Case 1:
1
3
12
Case 2:
10
题目大意:区间找最小值, 这里多一个操作, 就是对k进行了定位;确定在哪层 ,然后得到最小值;
AC代码:
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define N 100000+10 int dp [20],a ; void initrmq(int n) { int i,j; for(i=1;i<=n;i++) dp[i][0]=a[i]; for(j=1;(1<<j)<=n;j++) for(i=1;i+(1<<j)-1<=n;i++) dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } int rmq(int l,int r) { int k=(int)(log(r-l+1)/log(2.0));//*一共20层,定位到那层直接进行查找最小值 return min(dp[l][k],dp[r-(1<<k)+1][k]); } int main() { int n,m,i,cas=1,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%d",&a[i]); initrmq(n); printf("Case %d:\n",cas++); while(m--) { int x , y ; scanf("%d%d",&x,&y); printf("%d\n",rmq(x,y)); } } return 0 ; }
相关文章推荐
- StringBuffer和StringBuilder
- Vue新手向 知乎日报 by Vue.js
- Android UI系列-----ImageView的scaleType属性
- QT Creator 使用 design 修改 ui界面编译后界面未更新问题的解决 From EricQu
- SPOJ 375 Query on a tree(树链剖分)
- 外观数列(Look-and-say sequence)
- BlockingQueue
- 安装GRID时跑root.sh脚本报错(ORA-27091: unable to queue I/O)
- HDUOJ 1216 - Assistance Required
- STL 之 priority_queue
- sql deque
- Hibernate中的Query cache(查询缓存)
- padding margin;gravity,layout_gravity
- 谈 UIView Animation 编程艺术
- JUEL中得到identifier的值
- ios基于UITabBarController实现tab页面切换
- moqui学习之初接触
- luigi学习6--parameters详解
- StringBuffer和StringBuilder
- 如何禁止UITableView的section停留(去除卡住的效果)