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 ...
随机推荐
- webservice 教程
https://ke.qq.com/webcourse/index.html#cid=28875&term_id=100182700&taid=800324205965515& ...
- IDE 设备(磁盘/CD-ROM)配置不正确。“ide1:1”上具有一个 IDE 从设备,但没有主设备。此配置在虚拟机中无法正常运行。请使用配置编辑器将磁盘/CD-ROM 从“ide1:1”移到“ide1:0”。
开启vmware报这个错: IDE 设备(磁盘/CD-ROM)配置不正确.“ide1:1”上具有一个 IDE 从设备,但没有主设备.此配置在虚拟机中无法正常运行.请使用配置编辑器将磁盘/CD-ROM ...
- flex布局-css
1.html <div id="parent"> <div id="child1"></div> <div id=& ...
- vue之表单输入绑定
- Educational Codeforces Round 30 D. Merge Sort
题意:给你n和k,n代表有多少个数,k代表几次操作,求一个1到n的序列,要k次mergesort操作才能还原 Examples Input 3 3 Output 2 1 3 Input 4 1 Out ...
- CDH5.15.1 hive 连接mongodb配置及增删改查
1. 下载 wget http://repo1.maven.org/maven2/org/mongodb/mongo-hadoop/mongo-hadoop-hive/2.0.2/mongo-hado ...
- Linux系统中无iptables文件的解决
在RHEL 7 / CentOS 7中,firewalld被引入来管理iptables,CentOS7开始,默认是没有iptables的,而是使用firewall防火墙.本文将屏蔽掉firewall, ...
- 既然还看不到未来之光,那就从骄阳开始的地方--IT携行
对于还没真正踏入IT的小白菜来说,哪有资格把刚学到的鸡毛蒜皮儿,三脚猫都不算的东西逮出来献丑,献丑都不算,还不如我们来谈谈人生,练练脸皮...... 我出发的起点肯定不同,多方限制,可能缺乏时光;那正 ...
- redis目前最好用的客户端推荐
- BAT机器学习面试1000题系列
https://blog.csdn.net/sinat_35512245/article/details/78796328