Cornfields(poj2019)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6798 | Accepted: 3315 |
Description
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
* 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
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
思路:单调栈;
因为正方形的大小是固定的,然后我们先每列每个元素用单调队列维护最大最小值,然后在用维护的矩阵,在每行每个元素维护最大最小值
这样ama[i][j]就是以(i-b+1,j-b+1)为左上角,所有元素的最大值;同理ami[i][j]为最小。复杂度O(n*n);
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<deque>
7 #include<stack>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 int ma[300][300];
12 int maxx[300][300];
13 int minn[300][300];
14 int que[300*2];
15 int ama[300][300];
16 int ami[300][300];
17 void get_maxx(int n,int k);
18 void get_minn(int n,int k);
19 int main(void)
20 {
21 int n,b,k;
22 int i,j;
23 while(scanf("%d %d %d",&n,&b,&k)!=EOF)
24 {
25 for(i = 1; i <= n; i++)
26 {
27 for(j = 1; j <= n; j++)
28 {
29 scanf("%d",&ma[i][j]);
30 }
31 }
32 get_minn(n,b);
33 get_maxx(n,b);
34 while(k--)
35 {
36 int x;
37 int y;
38 scanf("%d %d",&x,&y);
39 x+=b-1;
40 y+=b-1;
41 printf("%d\n",ama[x][y]-ami[x][y]);
42 }
43 }
44 return 0;
45 }
46 void get_maxx(int n,int k)
47 {
48 int i,j;
49 for(j = 1; j <= n; j++)
50 {
51 int head = 1;
52 int rail = 0;
53 for(i = 1; i <= n; i++)
54 {
55 if(head > rail)
56 {
57 que[++rail] = i;
58 }
59 else
60 {
61 int id = que[rail];
62 while(ma[id][j] <= ma[i][j])
63 {
64 rail--;
65 if(rail < head)
66 break;
67 id = que[rail];
68 }
69 que[++rail] = i;
70 }
71 int ic = que[head];
72 while(ic < max(0,i-k)+1)
73 {
74 head++;
75 ic = que[head];
76 }
77 maxx[i][j] = ma[que[head]][j];
78 }
79 }
80 for(i = 1; i <= n; i++)
81 {
82 int head = 1;
83 int rail = 0;
84 for(j = 1; j <= n; j++)
85 {
86 if(head > rail)
87 {
88 que[++rail] = j;
89 }
90 else
91 {
92 int id = que[rail];
93 while(maxx[i][id] <= maxx[i][j])
94 {
95 rail--;
96 if(rail < head)
97 break;
98 id = que[rail];
99 }
100 que[++rail] = j;
101 }
102 int ic = que[head];
103 while(ic < max(0,j-k)+1)
104 {
105 head++;
106 ic = que[head];
107 }
108 ama[i][j] = maxx[i][que[head]];
109 }
110 }
111 }
112 void get_minn(int n,int k)
113 {
114 int i,j;
115 for(j = 1; j <= n; j++)
116 {
117 int head = 1;
118 int rail = 0;
119 for(i = 1; i <= n; i++)
120 {
121 if(head > rail)
122 {
123 que[++rail] = i;
124 }
125 else
126 {
127 int id = que[rail];
128 while(ma[id][j] >= ma[i][j])
129 {
130 rail--;
131 if(rail < head)
132 break;
133 id = que[rail];
134 }
135 que[++rail] = i;
136 }
137 int ic = que[head];
138 while(ic < max(0,i-k)+1)
139 {
140 head++;
141 ic = que[head];
142 }
143 minn[i][j] = ma[que[head]][j];
144 }
145 }
146 for(i = 1; i <= n; i++)
147 {
148 int head = 1;
149 int rail = 0;
150 for(j = 1; j <= n; j++)
151 {
152 if(head > rail)
153 {
154 que[++rail] = j;
155 }
156 else
157 {
158 int id = que[rail];
159 while(minn[i][id] >= minn[i][j])
160 {
161 rail--;
162 if(rail < head)
163 break;
164 id = que[rail];
165 }
166 que[++rail] = j;
167 }
168 int ic = que[head];
169 while(ic < max(0,j-k)+1)
170 {
171 head++;
172 ic = que[head];
173 }
174 ami[i][j] = minn[i][que[head]];
175 }
176 }
177 }
Cornfields(poj2019)的更多相关文章
- Cornfields poj2019 二维RMQ
Cornfields Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit S ...
- [poj2019]Cornfields(二维RMQ)
题意:给你一个n*n的矩阵,让你从中圈定一个小矩阵,其大小为b*b,有q个询问,每次询问告诉你小矩阵的左上角,求小矩阵内的最大值和最小值的差. 解题关键:二维st表模板题. 预处理复杂度:$O({n^ ...
- [POJ 2019] Cornfields
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5516 Accepted: 2714 Descri ...
- POJ 2019 Cornfields [二维RMQ]
题目传送门 Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7963 Accepted: 3822 ...
- POJ 2019 Cornfields (二维RMQ)
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4911 Accepted: 2392 Descri ...
- poj2019 二维RMQ裸题
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions:8623 Accepted: 4100 Descrip ...
- POJ 2019 Cornfields(二维RMQ)
相比以前的RMQ不同的是,这是一个二维的ST算法 #include<iostream> #include<cstring> #include<cstdio> #in ...
- poj2019 二维RMQ模板题
和hdu2888基本上一样的,也是求一个矩阵内的极值 #include<iostream> #include<cstring> #include<cstdio> # ...
- Cornfields POJ - 2019(二维RMQ板题)
就是求子矩阵中最大值与最小值的差... 板子都套不对的人.... #include <iostream> #include <cstdio> #include <sstr ...
随机推荐
- git的使用理解(分支合并的使用理解,多人编程的解决方案)
本文主要记录了对git日常使用的一些理解,主要是对git分支的一些感悟. git强大的版本控制系统,之前也使用过SVN,感觉上git对于多人开发的版本控制更加强大,特别是最近对git分支的使用,更是深 ...
- php导出pdf,dompdf中文字体乱码解决办法(特别是代码迁移引起的乱码)
dompdf\lib\fonts\dompdf_font_family_cache.php记住这个文件里面存放的是字体生成的缓存,迁移时如果覆盖了这个文件会导致乱码而且很难找到出错的地方,相信我... ...
- 大数据学习----day27----hive02------1. 分桶表以及分桶抽样查询 2. 导出数据 3.Hive数据类型 4 逐行运算查询基本语法(group by用法,原理补充) 5.case when(练习题,多表关联)6 排序
1. 分桶表以及分桶抽样查询 1.1 分桶表 对Hive(Inceptor)表分桶可以将表中记录按分桶键(某个字段对应的的值)的哈希值分散进多个文件中,这些小文件称为桶. 如要按照name属性分为3个 ...
- 2016广东工业大学新生杯决赛 A-pigofzhou的巧克力棒
题目:GDUTOJ | pigofzhou的巧克力棒 (gdutcode.cn) 之前看了大佬博客的题解,一直没懂(我太菜了),后来听了朋友@77的讲解,终于懂了. 和拆分出2的n次方不一样,这是一种 ...
- 使用mybatis更新数据时 时间字段的值自动更新
1.debug打印出来执行的sql语句发现并没有修改时间的字段,最后发现是设计表时勾选了根据当前时间戳更新..... 去掉该字段的根据当前时间戳更新语句: alter table tableName ...
- Linux上Zookeeper集群搭建
一.官网 https://zookeeper.apache.org/ 二.下载安装 (1)下载 复制链接地址 http://mirror.bit.edu.cn/apache/zookeeper/zo ...
- 1.Vuejs-第一个实例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 使用MyBatis框架时发现的一些小bug
在大配置MyBatis.xml中: 不能有空节点属性 ,否则启动服务器后点击登录没有反应. 异常问题: ause: java.sql.SQLException: Value '0000-00-00 ...
- Python pyecharts绘制水球图
一.水球图Liquid.add()方法简介 Liquid.add()方法签名add(name, data, shape='circle', liquid_color=None, is_liquid_a ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...