CodeForces - 777C Alyona and Spreadsheet(思维题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37129433/article/details/82052127

Alyona and Spreadsheet
题 意: 给你一个n*m的矩阵,举证种填入数字ai,j,给出q个询问,每次输入l,r问从第l行到第r行是否为不递减序列。
数据范围:
1<=n*m<=100000
1<=ai,j<=1e9
1<=q<=1e5
1<=li<=ri<=n

输入样例:

5 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5

输出样例:

Yes
No
Yes
Yes
Yes
No

思 路:这题维护当前行 r 最小的一行l 使得l到r为 存在一个非递减序列。其实是非常好维护的。current[j]当前列的最后一个值,b[j]当前列的最小的那个l,ans[i]当前行 最小的那个行。

收 获:这题在训练的时候没有写出来,思维有点僵硬啊。要训练思维。这题总的来说对思维的提升还是很有帮助的。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int current[maxn],b[maxn],ans[maxn];
int n,m,q;
int main(){
    scanf("%d %d",&n,&m);
    int temp;
    for(int i=1;i<=n;i++){
        ans[i] = i;
        for(int j=1;j<=m;j++){
            scanf("%d",&temp);
            if(temp<current[j]){
                b[j] = i;
            }
            current[j] = temp;
            if(ans[i] > b[j])ans[i] = b[j];
        }
    }
    scanf("%d",&q);
    int l,r;
    while(q--){
        scanf("%d %d",&l,&r);
        if(ans[r]<=l){
            printf("Yes\n");
        }else{
            printf("No\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37129433/article/details/82052127