Alyona and Spreadsheet
这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢?
【题目链接】Alyona and Spreadsheet
&题意:
给一n*m的表,之后给T个询问,每个询问是r1和r2,问从r1行到r2行(包括这2行)是否至少有一列是非递减的?
&题解:
可以把给的n*m表处理一下,处理成b[i][j]数组,存的是第j列递增开始的位置是第几行.
这样,只有b中第r2行最小的小于r1就行了,否则就是No
【时间复杂度】(O(max(q,n*m)))q是询问次数
&代码:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn= 1e3 +9,INF=0x3f3f3f3f;
int n,m;
vector<vector<int> > a,b;
vector<int> c;
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// freopen("C:\Users\Zmy\Desktop\in.txt", "r", stdin);
cin>>n>>m;
a.resize(n);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
int t;
cin>>t;
a[i].push_back(t);
}
b=a;
for(int i=0;i<m;i++) b[0][i]=0;
for(int i=0;i<m;i++)
for(int j=1;j<n;j++){
if(a[j][i]>=a[j-1][i]){
b[j][i]=b[j-1][i];
}
else{
b[j][i]=j;
}
}
c.resize(n);
for(int i=0;i<n;i++){
c[i]=INF;
for(int j=0;j<m;j++){
c[i]=min(c[i],b[i][j]);
}
}
int T;
cin>>T;
for(int i=0;i<T;i++){
int r1,r2;
cin>>r1>>r2;
if(c[r2-1]<=r1-1)
puts("Yes");
else
puts("No");
}
return 0;
}