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的更多相关文章

  1. H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛

    题面见 https://ac.nowcoder.com/acm/contest/700#question 题目大意是有n个单词,有k条替换规则(单向替换),每个单词会有一个元音度(单词里元音的个数)和 ...

  2. Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D Grid(简单构造)

    链接:https://ac.nowcoder.com/acm/contest/560/D来源:牛客网 题目描述 Give you a rectangular gird which is h cells ...

  3. Minieye杯第十五届华中科技大学程序设计邀请赛网络赛 部分题目

    链接:https://pan.baidu.com/s/12gSzPHEgSNbT5Dl2QqDNpA 提取码:fw39 复制这段内容后打开百度网盘手机App,操作更方便哦 D    Grid #inc ...

  4. 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees

    A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...

  5. 第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Beautiful Land 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1 ...

  6. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  7. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  8. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  9. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

随机推荐

  1. Redis读超时排查

    现象:STORM UI中发现bolt中有failed,异常日志抛异常read time out 业务场景:storm读写redis. 查看redis日志发现,正常情况下 每隔2小时都会有超时的异常抛出 ...

  2. FreeMarker案例

  3. C# Excel导入数据

    表 表的创建脚本 CREATE TABLE [dbo].[TB_PROJECTS_New1]( , ) NOT NULL, ) NULL, ) NULL, , ) NULL, , ) NULL, , ...

  4. 图论.DP

    见题: 看一眼,就知道是个依赖性背包,于是乎就草草的打了树上DP,一交发现才20,仔细检查也没错呀,忍不住点了题解,只喵一眼看到了强联通缩点等的字样,又重新审了一遍题,发现这句话理解有偏差:软件i只有 ...

  5. 图解Windows 10下Visual Studio Code的下载和安装

    1. 百度搜索“Visual Studio Code”,如下图所示: 2. 点击第一个搜索结果项,进入官方网站,然后点击“Download for Windows”,如下图所示: 3. 进入提示下载页 ...

  6. 使用Log4J收集日志

    一:搭建好Selenium+testNG+Maven环境,可参考 http://www.cnblogs.com/hc1020/p/8602863.html 二:百度搜索Maven仓库,然后搜索Log4 ...

  7. 自己电脑组一个Wifi热点

    ① 在cmd窗口 ssid 是wifi名称.key是密码 netsh wlan set hostednetwork mode=allow ssid=yb key=15564130 ② ③ 运行脚本 新 ...

  8. 9. maps

    C++有vertor,java有HashMap,C语言想使用则需要自行封装,不同的类型还需要再封装,特别麻烦. 看看Go语言的map的使用方法:var member map[string]int,创建 ...

  9. 【c】多级指针

    一.一级指针 1.int *p,*p2; p是变量名,*表明是指针,指针指向地址. 在定义时初始化,如int *p_2 = &b; //定义一个指针,指针指向一个地址 先定义再初始化,如int ...

  10. Cannot attach medium 'D:\program\VirtualBox\VBoxGuestAdditions.iso' {}: medium is already associated with the current state of machine uuid {}返回 代码: VBOX_E_OBJECT_IN_USE (0x80BB000C)

    详细的错误信息如下: Cannot attach medium 'D:\program\VirtualBox\VBoxGuestAdditions.iso' {83b35b10-8fa2-4b81-8 ...