【POJ】【2019】
2015-07-23 20:27
393 查看
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
Sample Output
Source
USACO 2003 March Green
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5805 | Accepted: 2868 |
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
Source
USACO 2003 March Green
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <assert.h> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; int N,B,K; const int maxn = 250*250+10; int arr[maxn]; int LOG[maxn]; int dp1[maxn][20]; int dp2[maxn][20]; void initRMQ(int n) { LOG[0] = -1; for(int i=1;i<=n;i++) { LOG[i] = ((i & (i-1) ) == 0) ? LOG[i-1] + 1 : LOG[i-1]; } memset(dp1,0,sizeof(dp1)); memset(dp2,0,sizeof(dp2)); for(int i=1;i<=n;i++) { dp1[i][0] = dp2[i][0] = arr[i]; } for(int j=1;j<=LOG ;j++) { for(int i=1;i + (1<<j) -1 <=n;i++) { dp1[i][j] = max(dp1[i][j-1],dp1[i+(1 << (j-1))][j-1]); dp2[i][j] = min(dp2[i][j-1],dp2[i+(1 << (j-1))][j-1]); } } } int calcmin(int l,int r) { int k = LOG[r - l + 1]; int mins = min(dp2[l][k],dp2[r - (1 << k) + 1][k]); return mins; } int calcmax(int l,int r) { int k = LOG[r - l + 1]; int maxs = max(dp1[l][k],dp1[r - (1 << k) + 1][k]); return maxs; } int main() { while(scanf("%d%d%d",&N,&B,&K) != EOF) { for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { scanf("%d",&arr[i*N+j+1]); } } initRMQ(N*N); int ans = 0; while(K --) { int x,y; ans = 0; int mins = 0x3f3f3f3f; int maxs = -1; scanf("%d%d",&x,&y); x --; y --; for(int i=0;i<B;i++) { int l = (x + i) * N + y + 1; int tempmin = calcmin(l,l+B-1); if(tempmin < mins) mins = tempmin; int tempmax = calcmax(l,l+B-1); if(tempmax > maxs) maxs = tempmax; } printf("%d\n",maxs-mins); } } }
相关文章推荐
- 动态规划 钢条切割问题 两种方法 自底而上 自上而下的方法
- Android测试提升效率批处理脚本(二)
- C - 链表,推荐
- 【Struts2框架】第二节Result-向结果传参数
- iOS前期OC训练OC_06字典
- shell学习
- 数据库分组函数
- Fedora 22 设置顶部 Top Bar 透明
- 深度神经网络DNN的多GPU数据并行框架 及其在语音识别的应用
- Codeforces Round #313 (Div. 2) C. Gerald's Hexagon
- caffe源码分析--SyncedMemory类代码研究
- 简学Oracle
- VS2010+Qt+OpenCv(显示图像)
- linux下mysql安装、目录结构、配置
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
- hbase
- 用Pandas作图
- 【数据库】分组函数
- DNS 知识点
- 【Struts2框架】第二节Result-动态结果集