Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
https://ac.nowcoder.com/acm/contest/700/I
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
KK is the engineer responsible for sensor sensing in MINIEYE. KK recently need to use radar to sense the vehicles ahead, and KK thought about it day and night, and finally found a perfect solution.
You are now an intern at KK. To give you an understanding of the principles of radar sensing, KK gives you a simplified version of this problem.
Given a matrix of N×M, each element is an integer. If there is a submatrix of L × L, and the difference between the maximum and the minimum of this submatrix is less than or equal to G, then this submatrix is considered as a car matrix. Now you need to find the largest L.
输入描述:
The first line contains three integers N, M, G(0 < N, M ≤ 500, 0 ≤ G ≤ 200).
Each of the following N lines contains M integers, representing the matrix. It is guaranteed that every integer in the matrix is in the range [-100, 100].
输出描述:
Output the maximum L in a single line.
示例1
输入
3 3 4
0 1 2
3 4 5
6 7 8
输出
2
分析
题目大意就是从一个N*M的矩阵里选取一个L*L的矩阵,使得这个矩阵中的最大值和最小值的差小于等于G,求最大的L。
首先很显然跟RMQ问题,求最大最小值嘛,但是这题是矩阵,那就是二维的RMQ,用ST表实现的,模板++;
在处理完最大最小之后,如果要遍历所有的矩阵时间复杂度过大是不行的,考虑二分,,,然后二分写自闭了,最后还是队友改二分改正确了。
在我打这句话的时候,队内其他大佬用暴力过了这题,,,,,瞬间不想继续写这题的题解了,,,,撤了。
AC代码:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <list>
#include <string.h>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define LL long long using namespace std; int val[][];
int mm[];
char dpmin[][][][];//最小值
char dpmax[][][][];//最大值 void initRMQ(int n,int m) {
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
dpmin[i][j][][] = dpmax[i][j][][] = val[i][j];
for(int ii = ; ii <= mm[n]; ii++)
for(int jj = ; jj <= mm[m]; jj++)
if(ii+jj)
for(int i = ; i + (<<ii) - <= n; i++)
for(int j = ; j + (<<jj) - <= m; j++) {
if(ii) {
dpmin[i][j][ii][jj] = min(dpmin[i][j][ii-][jj],dpmin[i+(<<(ii-))][j][ii-][jj]);
dpmax[i][j][ii][jj] = max(dpmax[i][j][ii-][jj],dpmax[i+(<<(ii-))][j][ii-][jj]);
} else {
dpmin[i][j][ii][jj] = min(dpmin[i][j][ii][jj-],dpmin[i][j+(<<(jj-))][ii][jj-]);
dpmax[i][j][ii][jj] = max(dpmax[i][j][ii][jj-],dpmax[i][j+(<<(jj-))][ii][jj-]);
}
}
}
int rmq1(int x1,int y1,int x2,int y2) {
int k1 = mm[x2-x1+];
int k2 = mm[y2-y1+];
x2 = x2 - (<<k1) + ;
y2 = y2 - (<<k2) + ;
return max(max(dpmax[x1][y1][k1][k2],dpmax[x1][y2][k1][k2]),max(dpmax[x2][y1][k1][k2],dpmax[x2][y2][k1][k2]));
}
//查询矩形的最小值
int rmq2(int x1,int y1,int x2,int y2) {
int k1 = mm[x2-x1+];
int k2 = mm[y2-y1+];
x2 = x2 - (<<k1) + ;
y2 = y2 - (<<k2) + ;
return min(min(dpmin[x1][y1][k1][k2],dpmin[x1][y2][k1][k2]),min(dpmin[x2][y1][k1][k2],dpmin[x2][y2][k1][k2]));
} int main() {
mm[] = -;
for(int i = ; i <= ; i++)
mm[i] = ((i&(i-))==)?mm[i-]+:mm[i-];
int N,M,g;
scanf("%d%d%d",&N,&M,&g);
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
scanf("%d",&val[i][j]);
initRMQ(N,M);
//
int x,y;
int maxi = -;
for(int i = ; i <= N; i++) {
for(int j = ; j <= M; j++) {
int l = ,r = min(N-i+,M-j+),mid;
int ans;
while(l <= r) {
mid = (l+r) >> ;
if(rmq1(i,j,i+mid-,j+mid-)-rmq2(i,j,i+mid-,j+mid-) > g) {
r = mid-;
//ans = mid;
} else {
l = mid+;
}
}
if(r > maxi) {
maxi = r;
}
}
}
p(maxi) pn return ;
}
其他大佬代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,g;
int a[][];
int b[][];//最大值
int c[][];//最小值
int b1[][];
int c1[][]; int l;
int main()
{
scanf("%d%d%d",&n,&m,&g);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&a[i][j]);
if(i>=&&j>=){
b[i-][j-] = max(a[i][j], max(a[i-][j], max(a[i][j-],a[i-][j-]) ) );
c[i-][j-] = min(a[i][j], min(a[i-][j], min(a[i][j-],a[i-][j-]) ) );
}
}
} int x = n-;
int y = m-;
l = min(n,m);
int ans = ;
for(int i = ; i <= l;i++)
{ for(int j = ;j <= x;j++)
{
for(int k = ;k <= y;k++)
{
if(b[j][k] - c[j][k] <= g){
ans = i;
}
}
} for(int j = ;j <= x;j++)
{
for(int k = ;k <= y;k++)
{
b1[j][k] = max(b[j][k] , max(b[j-][k],max(b[j][k-],b[j-][k-])));
b[j-][k-] = b1[j][k];
c1[j][k] = min(c[j][k] , min(c[j-][k],min(c[j][k-],c[j-][k-])));
c[j-][k-] = c1[j][k];
}
}
x--;
y--;
}
cout<<ans<<endl; return ;
}
Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again的更多相关文章
- H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛
题面见 https://ac.nowcoder.com/acm/contest/700#question 题目大意是有n个单词,有k条替换规则(单向替换),每个单词会有一个元音度(单词里元音的个数)和 ...
- Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D Grid(简单构造)
链接:https://ac.nowcoder.com/acm/contest/560/D来源:牛客网 题目描述 Give you a rectangular gird which is h cells ...
- Minieye杯第十五届华中科技大学程序设计邀请赛网络赛 部分题目
链接:https://pan.baidu.com/s/12gSzPHEgSNbT5Dl2QqDNpA 提取码:fw39 复制这段内容后打开百度网盘手机App,操作更方便哦 D Grid #inc ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees
A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)
链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】
链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
随机推荐
- 分享一个微信自动跳转外部浏览器下载app的api接口!
现在微信渠道可以说是拉新最快的渠道,因为微信具备强裂变性.但是目前微信对第三方下载链接的拦截是越来越严格了,那么想要在微信内肆无忌惮地推广链接就需要用到微信跳转浏览器的接口,那如何获取该接口呢? ...
- 可视化n次贝塞尔曲线及过程动画演示--大宝剑
起因 研究css中提供了2次.3次bezier,但是没有对n次bezier实现.对n次的实现有很大兴趣,所以就用js的canvas搞一下,顺便把过程动画模拟了一下. 投入真实生产之中,偏少. n次be ...
- jquery中val属性操作
- ARTS打卡计划第二周-Review
本周review的文章是:https://medium.com/@hakibenita/optimizing-django-admin-paginator-53c4eb6bfca3 改篇文章的题目是: ...
- 宝塔面板安装在根目录www下
不能重装,重装防火墙要重新关闭. 安装时要主要更改文件权限.
- 今天遇到一个怪异的问题,maven生成项目war包中有一个Jar包不是我指定的版本,运行时会找不到符号,o(╥﹏╥)o
我要求的jar包: 这是我parent项目中pom文件的依赖管理 这是我要生成war包那个工程最后依赖的jar包,这个时候它们的版本号还是一致的 最后项目生成的: 下图是Dmaven.test.ski ...
- pandas 中有关isin()函数的介绍,python中del解释
- js实现接口隔离
昨天公司培训了接口隔离,简单说一下 接口隔离:类间的依赖关系应该建立在最小的接口上.接口隔离原则将非常庞大.臃肿的接口拆分成更小具体的接口,这样客户讲会只需要知道他们感兴趣的方法. 接口隔离原则的目的 ...
- tiny4412 --Uboot移植(6) SD卡驱动,启动内核
开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ...
- windows批量停止服务
此代码适合有一定windows操作系统基础的人使用 @echo off for %%i in ( mysql OracleDBConsoleleak OracleMTSRecoveryService ...