2019牛客暑期多校训练营(第三场)F Planting Trees 单调队列
F Planting Trees
题目链接
https://ac.nowcoder.com/acm/contest/883/F
题目描述
The semester is finally over and the summer holiday is coming. However, as part of your university's graduation requirement, you have to take part in some social service during the holiday. Eventually, you decided to join a volunteer group which will plant trees in a mountain.
To simplify the problem, let's represent the mountain where trees are to be planted with an \(N \times N\) grid. Let's number the rows \(\ 1\) to \(\ N\) from top to bottom, and number the columns \(\ 1\) to \(\ N\) from left to right. The elevation of the cell in the \(\ i\)-th row and \(\ j\)-th column is denoted by \(a_{i,j}\). Your leader decides that trees should be planted in a rectangular area within the mountain and that the maximum difference in elevation among the cells in that rectangle should not exceed M. In other words, if the coordinates of the top-left and the bottom-right corners of the rectangle are \((x_1,y_1)\) and \((x_2,y_2)\), then the condition $ |a_{i,j} - a_{k,l}| \le M$ must hold for \(x_1 \le i,k \le x_2, \ y_1 \le j,l \le y_2\). Please help your leader calculate the maximum possible number of cells in such a rectangle so that he'll know how many trees will be planted.
输入描述:
The input contains multiple cases. The first line of the input contains a single integer \(T\ (1 \le T \le 1000)\), the number of cases.
For each case, the first line of the input contains two integers $ N\ (1 \le N \le 500)$ and \(M\ (0 \le M \le 10^5)\). The following N lines each contain N integers, where the\(\ j-th\) integer in the i\ i i-th line denotes \(a_{i,j} \ (1 \le a_{i,j} \le 10^5)\).
It is guaranteed that the sum of \(N^3\) over all cases does not exceed \(25 \cdot 10^7\).
输出描述:
For each case, print a single integer, the maximum number of cells in a valid rectangle.
输入
2
2 0
1 2
2 1
3 1
1 3 2
2 3 1
3 2 1
输出
1
4
题意
给你一个\(N \times N\)的矩阵,求最大的自矩阵满足子矩阵中最大值和最小值只差小于m.
题解
一看体面最后一句话,寻思着就是\(n^3\)的算法,于是很自然地想到\(n^2\)枚举上届和下界,再用\(n\)来扫描一遍。
我们想想假如上下界确定了,我们再枚举左边界,右边界越长越好,那么左边界往右移时,右边界一定不会往左移(这里应该不难想,只要想肯定不难想到),这个好像就叫做尺取法。
剩下的就是要求区间的最小值和最大值了,我一开始直接二维倍增,结果超时了,改了两个小时还是超时,只能放弃了。之后用了单调队列来求最小值和最大值,这种做法感觉很经典,但是我竟然没看出来,还是做少了题目.
简要地讲讲单调队列:
我们搞两个队列,一个用来求最大值,另一个用来求最小值
第一步:去掉不合法的队首(更改左边界)
第二部:往后延伸右边界
第三部:更新答案
这么讲有点抽象,之后我去找找基础的题目,做完基础题目,这个自然也就会了。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 505
#define maxx(a,b) a<b?b:a
#define minn(a,b) a<b?a:b
int n,m,mx[N],mi[N],a[N][N];
struct Queue{int val,id;}Q1[N],Q2[N];
clock_t now;
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
inline void work()
{
now=clock();
int ans=1;
read(n); read(m);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
read(a[i][j]);
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
int ds1=1,dw1=0,ds2=1,dw2=0,r=0;
for(int k=1;k<=n;k++)
{
if (i==j)mx[k]=mi[k]=a[i][k];
else {mx[k]=maxx(mx[k],a[j][k]);mi[k]=minn(mi[k],a[j][k]);}
}
for(int k=1;k<=n;k++)
{
r=maxx(r,k-1);
while(ds1<=dw1&&Q1[ds1].id<k)ds1++;
while(ds2<=dw2&&Q2[ds2].id<k)ds2++;
if (mx[k]-mi[k]>m)continue;
int m1=maxx(mx[r+1],Q1[ds1].val);
int m2=minn(mi[r+1],Q2[ds2].val);
if (ds1>dw1)m1=mx[r+1],m2=mi[r+1];
while(r+1<=n&&(mx[r+1]-mi[r+1]<=m)&&(ds1>dw1||m1-m2<=m))
{
while(mx[r+1]>Q1[dw1].val&&ds1<=dw1)dw1--;
while(mi[r+1]<Q2[dw2].val&&ds2<=dw2)dw2--;
Q1[++dw1]={mx[r+1],r+1};
Q2[++dw2]={mi[r+1],r+1};
r++;
m1=maxx(m1,mx[r+1]);
m2=minn(m2,mi[r+1]);
}
ans=maxx(ans,(j-i+1)*(r-k+1));
}
}
printf("%d\n",ans);
//cout<<(double)(clock()-now)/CLOCKS_PER_SEC*1000<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int T;
read(T);
while(T--)work();
}
超时代码(仅供观赏)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 505
int n,m,mx[N][N][10][10],mi[N][N][10][10],mm[N],mv[10];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void build_ST()
{
for(int i=2;i<=n;i++)mm[i]=mm[i>>1]+1;
for(int i=0;i<=10;i++)mv[i]=1<<i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
mx[i][j][0][0]=mi[i][j][0][0];
for(int k=1;k<=8;k++)
for(int i=1;i<=n;i++)
for(int j=1;j+(1<<k)-1<=n;j++)
{
mx[i][j][0][k]=max(mx[i][j][0][k-1],mx[i][j+(1<<(k-1))][0][k-1]);
mi[i][j][0][k]=min(mi[i][j][0][k-1],mi[i][j+(1<<(k-1))][0][k-1]);
}
for(int k1=1;k1<=8;k1++)
for(int k2=0;k2<=8;k2++)
for(int i=1;i+(1<<k1)-1<=n;i++)
for(int j=1;j+(1<<k2)-1<=n;j++)
{
mx[i][j][k1][k2]=
max(mx[i][j][k1-1][k2],mx[i+(1<<(k1-1))][j][k1-1][k2]);
mi[i][j][k1][k2]=
min(mi[i][j][k1-1][k2],mi[i+(1<<(k1-1))][j][k1-1][k2]);
}
}
inline int get_mx(int x1,int y1,int x2,int y2)
{
int k1=mm[x2-x1+1],k2=mm[y2-y1+1];
x2=x2-mv[k1]+1;
y2=y2-mv[k2]+1;
int a1=max(mx[x1][y1][k1][k2],mx[x1][y2][k1][k2]);
int a2=max(mx[x2][y1][k1][k2],mx[x2][y2][k1][k2]);
return max(a1,a2);
}
inline int get_mi(int x1,int y1,int x2,int y2)
{
int k1=mm[x2-x1+1],k2=mm[y2-y1+1];
x2=x2-(1<<k1)+1;
y2=y2-(1<<k2)+1;
int a1=min(mi[x1][y1][k1][k2],mi[x1][y2][k1][k2]);
int a2=min(mi[x2][y1][k1][k2],mi[x2][y2][k1][k2]);
return min(a1,a2);
}
void work()
{
int ans=1;
read(n); read(m);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
read(mi[i][j][0][0]);
build_ST();
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
int l=1;
for(int k=1;k<=n;k++)
{
int mx=get_mx(i,l,j,k);
int mi=get_mi(i,l,j,k);
while(mx-mi>m&&l<=k)
{
l++;
if(l<=k)mx=get_mx(i,l,j,k);
if(l<=k)mi=get_mi(i,l,j,k);
}
ans=max(ans,(j-i+1)*(k-l+1));
}
}
printf("%d\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int T;
read(T);
while(T--)work();
}
2019牛客暑期多校训练营(第三场)F Planting Trees 单调队列的更多相关文章
- 2019牛客暑期多校训练营(第三场) F.Planting Trees(单调队列)
题意:给你一个n*n的高度矩阵 要你找到里面最大的矩阵且最大的高度差不能超过m 思路:我们首先枚举上下右边界,然后我们可以用单调队列维护一个最左的边界 然后计算最大值 时间复杂度为O(n*n*n) # ...
- 2019牛客暑期多校训练营(第三场)- F Planting Trees
题目链接:https://ac.nowcoder.com/acm/contest/883/F 题意:给定n×n的矩阵,求最大子矩阵使得子矩阵中最大值和最小值的差值<=M. 思路:先看数据大小,注 ...
- 2019牛客暑期多校训练营(第一场)-A (单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...
- 2019牛客暑期多校训练营(第三场)H题目
题意:给你一个N×N的矩阵,求最大的子矩阵 满足子矩阵中最大值和最小值之差小于等于m. 思路:这题是求满足条件的最大子矩阵,毫无疑问要遍历所有矩阵,并判断矩阵是某满足这个条件,那么我们大致只要解决两个 ...
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
随机推荐
- CONTINUE...? ZOJ - 4033
CONTINUE...? ZOJ - 4033 题解:先计算一下总数sum是否是偶数,如果不是则不能分出来,如果是则从后面开始分,先把人分到1.3组,分完sum / 2这些人,如果恰好能够分成零,那么 ...
- 顺序表应用7:最大子段和之分治递归法(SDUT 3664)
#include <bits/stdc++.h> using namespace std; const int maxn = 50005; int num = 0; struct node ...
- AE开发之shp转txt
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- BufferedWriter中write与close函数使用
BufferedWriter 是一个缓冲字符输出流,可以将要输出的内容先缓冲到一个字符数组中,等字符数组满了才一次性写到输出流内,默认的字符数组长度为8192.使用BufferedWriter 时需要 ...
- 问题MySQL Error (2013): Lost connection to MySQL server at waiting for initial communication packet
错误说明: SQL Error (2013): Lost connection to MySQL server at 'waiting for initial communication packet ...
- 《你不知道的JavaScript(上)》笔记——动态作用域
动态作用域让作用域作为一个在运行时就被动态确定的形式, 而不是在写代码时进行静态确定的形式.动态作用域并不关心函数和作用域是如何声明以及在何处声明的, 只关心它们从何处调用. 换句话说, 作用域链是基 ...
- kotlin使用中辍标记法调用函数
fun main(arg: Array<String>) { var str = "hello world" print(str div("l")) ...
- 超详细MySQL安装及基本使用教程
一.下载MySQL 首先,去数据库的官网http://www.mysql.com下载MySQL. 点击进入后的首页如下: 然后点击downloads,community,选择MySQL Commun ...
- PS改变图片像素大小(一寸照片变二寸)
1.打开Photoshop,点击“文件”菜单,选择“打开”,将要处理的图片加载进来 2.找到“图像”菜单,选择“图像大小”,我们在这里对图片进行处理. 3.根据需要对其分辨率或者尺寸进行设置,设置好后 ...
- HDFS数据定期清理
HDFS数据清理一些办法: datanode数据做reblance清理临时目录.日志目录文件全量分区表历史分区清理使用lzo,orc格式进行数据压缩清理或者归档历史冷数据增加datanode横向扩容附 ...