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 ...
随机推荐
- UICollectionView didSelectItemAtIndexPath实现方法
didSelectItemAtIndexPath是通过UIResponder的四个touch方法实现的(touchBegan, touchMove, touchEnd, touchCancel),因此 ...
- EasyUI 1.3.2 中 Combobox自动检索 键盘上下选择Bug问题
EasyUI 自带的Combobox控件,提供了下拉列值自动检索功能. 在用到的EasyUI 1.3.2版本中还是有点问题,在键盘上下键移动选择过程中只能定位在第一个,不能正常向下移动 问题解决方式: ...
- UE4C++定义属性修饰符总结
1.BlueprintAssignable 暴露该属性来在蓝图中进行赋值,用于绑定多播委托 2.BlueprintCallable 用于从蓝图中调用C++原生函数 3.BlueprintReadO ...
- 在create-react-app里使用ant design
使用create-react-app创建的项目,要使用ant design. 1.首先进入项目根目录,yarn add antd. 2.在App.css引入 样式文件,@import '~antd/d ...
- js 金额用逗号隔开
function money(s, n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s + "") ...
- k8s之调度约束
k8s调度约束有两种:第一种,直接指定某台node主机:这种形势将直接跳过调度器.如下: 第二种:先给各node指定标签,然后在通过标签的形势来关联node,这种形势人就会调用到调度器.如下: yao ...
- hadoop常见问题
Q1.什么是 Hadoop? Hadoop 是一个开源软件框架,用于存储大量数据,并发处理/查询在具有多个商用硬件(即低成本硬件)节点的集群上的那些数据.总之,Hadoop 包括以下内容: HDFS( ...
- Excel VBA 连接各种数据库(一) VBA连接MySQL数据库
本文参考[东围居士]的cnblog博文 Excel.VBA与MySQL交互 在自己机器上调试成功,把调试中遇到的问题一并写出了. 本文主要涉及: VBA中的MySQL环境配置 VBA连接MySQL ...
- 理解java的三大特性之封装
参考大神的理解,详情见https://blog.csdn.net/chenssy/article/details/12757911
- vue环境项目启动后因为eslint语法限制报错
报错太多,截取了一部分. 解决方法找到项目根目录的build 找到webpack.base.conf.js 打开js文件找到下图的位置 再重新启动项目就好了