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

来源:互联网 发布:2017年新出的网络剧 编辑:程序博客网 时间:2024/06/11 04:08

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 1int 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;}


0 0
原创粉丝点击