CF#305-B. Mike and Fun-暴力模拟水题

本文解析了CodeForces上的一道题目,该题要求在给定的01矩阵上进行翻转操作,并求每行最大连续1的数量。通过遍历更新的方法实现了O(q*n)的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/contest/548/problem/B

题意:

给出n*m的01矩阵,给一个q,表示q次操作

操作 给出【i,j】,表示对 矩阵的i行k列的元素取反

每次操作后 给出 n行中, 最连续1的个数。


用一个ans【n】数组维护每一行的最多连续的1就好

n只有500,每次取反操作后,直接遍历把当前行更新后的最多连续的1记录到ans[i]

然后for(i=1:n)  遍历取一个最大的ans[i] 

复杂度  q*o(n)


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

int tm[505][505];
int ans[505];       //i-th row's max continous 1
int main()
{
    
    int n,m,q;
    int i,j;
    scanf("%d%d%d",&n,&m,&q);
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
        {
            scanf("%d",&tm[i][j]);
        }
    }
    int tmp,maxx;
    for (i=1;i<=n;i++)      //pre_
    { 
        tmp=0;
        maxx=0;
        for (j=1;j<=m;j++)
        {
            if (tmp>maxx)
                maxx=tmp;
            if (j==1)
                tmp=tm[i][j];
            else
                if (tm[i][j]==1)
                {
                    
                    if (tm[i][j-1]==1)
                        tmp++;
                    else
                        tmp=1;
                }
                else
                    tmp=0;
                
        }
            if (tmp>maxx)
                maxx=tmp;
        ans[i]=maxx;
    }
    int a,b;
    for (i=1;i<=q;i++)
    {
        scanf("%d%d",&a,&b);
        tm[a][b]=!tm[a][b];
        ///*************update 
        tmp=0;
        maxx=0;
         
        for (j=1;j<=m;j++)
        {
            if (tmp>maxx)
                maxx=tmp;
            if (j==1)
                tmp=tm[a][j];
            else
                if (tm[a][j]==1)
                {
                    
                    if (tm[a][j-1]==1)
                        tmp++;
                    else
                        tmp=1;
                }
                else
                    tmp=0;
                
        }
            if (tmp>maxx)
                maxx=tmp;
        ans[a]=maxx;
        maxx=0;
        for (j=1;j<=n;j++)
        {
            if (ans[j]>maxx)
                maxx=ans[j];
        }
        printf("%d\n",maxx);
        //****************
    }
 



return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值