[关闭]
@xunuo 2017-01-18T20:07:57.000000Z 字数 4529 阅读 1051

Cornfields--(二维RMQ模板,模板...)


Time Limit: 1000MS      Memory Limit: 30000K

数据结构

知识点:二维RMQ

来源:
poj:poj 2019 Cornfields
vjudge:N-Cornfields


Description

FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find. 

FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it. 

FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield. 

Input

* Line 1: Three space-separated integers: N, B, and K. 

* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc. 

* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1. 

Output

* Lines 1..K: A single integer per line representing the difference between the max and the min in each query. 

Sample Input

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

Sample Output

5

题意:

有一个N*N的矩阵,找出矩阵的一个b*b的子矩阵的最大值与最小值,求他们的差;
输入第一行包括三个数:n,b,k;
分别表示一个n*n的矩阵,找出一个b*b的子矩阵,以及有k组查询
接下来输入矩阵,输入完毕后输入它的起点坐标,都是从(1,1)开始

解题思路·:

可以暴力过,但是顺便学习一下二维RMQ,存个模板

完整代码:

  1. ///*这个是照着思寒的写的*///
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<algorithm>
  6. using namespace std;
  7. int a[250][205];
  8. ///*2^8=256!如果起点从(0,0)开始;
  9. ///二维RMQ----dp[x][y][i][j]---表示以(x,y)为起点,到右下角点为(x+2^i-1,y+2^j-1)的最值*///
  10. int dpmax[255][255][8][8];
  11. int dpmin[255][255][8][8];
  12. ///n*m的矩阵;
  13. void rmq(int n,int m)
  14. {
  15. for(int i=0;(1<<i)<=n;i++)
  16. for(int j=0;(1<<j)<=m;j++)
  17. {
  18. if(i==0&&j==0)
  19. continue;
  20. for(int r=1;r+(1<<i)-1<=n;r++)
  21. for(int c=1;c+(1<<j)-1<=m;c++)
  22. {
  23. if(j==0)
  24. {
  25. dpmax[r][c][i][j]=max(dpmax[r][c][i-1][j],dpmax[r+(1<<(i-1))][c][i-1][j]);
  26. dpmin[r][c][i][j]=min(dpmin[r][c][i-1][j],dpmin[r+(1<<(i-1))][c][i-1][j]);
  27. }
  28. else
  29. {
  30. dpmax[r][c][i][j]=max(dpmax[r][c][i][j-1],dpmax[r][c+(1<<(j-1))][i][j-1]);
  31. dpmin[r][c][i][j]=min(dpmin[r][c][i][j-1],dpmin[r][c+(1<<(j-1))][i][j-1]);
  32. }
  33. }
  34. }
  35. }
  36. int querymax(int x1,int y1,int x2,int y2)
  37. {
  38. int xi=0,yi=0;
  39. while(x1+(1<<(xi+1))-1<=x2)
  40. xi++;
  41. while(y1+(1<<(yi+1))-1<=y2)
  42. yi++;
  43. return max(max(dpmax[x1][y1][xi][yi],dpmax[x2-(1<<xi)+1][y1][xi][yi]),max(dpmax[x1][y2-(1<<yi)+1][xi][yi],dpmax[x2-(1<<xi)+1][y2-(1<<yi)+1][xi][yi]));
  44. }
  45. int querymin(int x1,int y1,int x2,int y2)
  46. {
  47. int xi=0,yi=0;
  48. while(x1+(1<<(xi+1))-1<=x2)
  49. xi++;
  50. while(y1+(1<<(yi+1))-1<=y2)
  51. yi++;
  52. return min(min(dpmin[x1][y1][xi][yi],dpmin[x2-(1<<xi)+1][y1][xi][yi]),min(dpmin[x1][y2-(1<<yi)+1][xi][yi],dpmin[x2-(1<<xi)+1][y2-(1<<yi)+1][xi][yi]));
  53. }
  54. int main()
  55. {
  56. int n,b,k;
  57. while(scanf("%d%d%d",&n,&b,&k)!=EOF)
  58. {
  59. for(int i=1;i<=n;i++)
  60. for(int j=1;j<=n;j++)
  61. {
  62. scanf("%d",&a[i][j]);
  63. dpmax[i][j][0][0]=a[i][j];
  64. dpmin[i][j][0][0]=a[i][j];
  65. }
  66. rmq(n,n);
  67. for(int i=0;i<k;i++)
  68. {
  69. int x,y;
  70. scanf("%d%d",&x,&y);
  71. int ans=querymax(x,y,x+b-1,y+b-1)-querymin(x,y,x+b-1,y+b-1);
  72. printf("%d\n",ans);
  73. }
  74. }
  75. return 0;
  76. }

这个是邝斌博客的----直接粘过来的,万一哪天有用呢@_@

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. int val[255][255];
  7. int mm[255];
  8. int dpmin[255][255][8][8];//最小值
  9. int dpmax[255][255][8][8];//最大值
  10. void initRMQ(int n,int m)
  11. {
  12. for(int i = 1;i <= n;i++)
  13. for(int j = 1;j <= m;j++)
  14. dpmin[i][j][0][0] = dpmax[i][j][0][0] = val[i][j];
  15. for(int ii = 0; ii <= mm[n]; ii++)
  16. for(int jj = 0; jj <= mm[m]; jj++)
  17. if(ii+jj)
  18. for(int i = 1; i + (1<<ii) - 1 <= n; i++)
  19. for(int j = 1; j + (1<<jj) - 1 <= m; j++)
  20. {
  21. if(ii)
  22. {
  23. dpmin[i][j][ii][jj] = min(dpmin[i][j][ii-1][jj],dpmin[i+(1<<(ii-1))][j][ii-1][jj]);
  24. dpmax[i][j][ii][jj] = max(dpmax[i][j][ii-1][jj],dpmax[i+(1<<(ii-1))][j][ii-1][jj]);
  25. }
  26. else
  27. {
  28. dpmin[i][j][ii][jj] = min(dpmin[i][j][ii][jj-1],dpmin[i][j+(1<<(jj-1))][ii][jj-1]);
  29. dpmax[i][j][ii][jj] = max(dpmax[i][j][ii][jj-1],dpmax[i][j+(1<<(jj-1))][ii][jj-1]);
  30. }
  31. }
  32. }
  33. //查询矩形的最大值
  34. int rmq1(int x1,int y1,int x2,int y2)
  35. {
  36. int k1 = mm[x2-x1+1];
  37. int k2 = mm[y2-y1+1];
  38. x2 = x2 - (1<<k1) + 1;
  39. y2 = y2 - (1<<k2) + 1;
  40. return max(max(dpmax[x1][y1][k1][k2],dpmax[x1][y2][k1][k2]),max(dpmax[x2][y1][k1][k2],dpmax[x2][y2][k1][k2]));
  41. }
  42. //查询矩形的最小值
  43. int rmq2(int x1,int y1,int x2,int y2)
  44. {
  45. int k1 = mm[x2-x1+1];
  46. int k2 = mm[y2-y1+1];
  47. x2 = x2 - (1<<k1) + 1;
  48. y2 = y2 - (1<<k2) + 1;
  49. return min(min(dpmin[x1][y1][k1][k2],dpmin[x1][y2][k1][k2]),min(dpmin[x2][y1][k1][k2],dpmin[x2][y2][k1][k2]));
  50. }
  51. int main()
  52. {
  53. mm[0] = -1;
  54. for(int i = 1;i <= 500;i++)
  55. mm[i] = ((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
  56. int N,B,K;
  57. while(scanf("%d%d%d",&N,&B,&K)==3)
  58. {
  59. for(int i = 1;i <= N;i++)
  60. for(int j = 1;j <= N;j++)
  61. scanf("%d",&val[i][j]);
  62. initRMQ(N,N);
  63. int x,y;
  64. while(K--)
  65. {
  66. scanf("%d%d",&x,&y);
  67. printf("%d\n",rmq1(x,y,x+B-1,y+B-1)-rmq2(x,y,x+B-1,y+B-1));
  68. }
  69. }
  70. return 0;
  71. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注