• codeforces548B


    Mike and Fun

     CodeForces - 548B 

    Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes.

    They play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.

    Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.

    Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round.

    Input

    The first line of input contains three integers nm and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000).

    The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes).

    The next q lines contain the information about the rounds. Each of them contains two integers i and j (1 ≤ i ≤ n and 1 ≤ j ≤ m), the row number and the column number of the bear changing his state.

    Output

    After each round, print the current score of the bears.

    Examples

    Input
    5 4 5
    0 1 1 0
    1 0 0 1
    0 1 1 0
    1 0 0 1
    0 0 0 0
    1 1
    1 4
    1 1
    4 2
    4 3
    Output
    3
    4
    3
    3
    4

    sol:直接暴力模拟就可以了,每次修改一个点,只会影响一行的答案,所以只要修改一行就可以了,O(Q*m)水过。。。
    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0');    return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=505;
    int n,m,Q,a[N][N];
    int ans[N];
    int main()
    {
        int i,j;
        R(n); R(m); R(Q);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++) R(a[i][j]);
        }
        for(i=1;i<=n;i++)
        {
            int tmp=0;
            for(j=1;j<=m;j++)
            {
                if(a[i][j]) tmp++;
                else tmp=0;
                ans[i]=max(ans[i],tmp);
            }
        }
        while(Q--)
        {
            int x,y,Max=0;
            R(x); R(y);
            a[x][y]^=1;
            int tmp=0; ans[x]=0;
            for(i=1;i<=m;i++)
            {
                if(a[x][i]) tmp++;
                else tmp=0;
                ans[x]=max(ans[x],tmp);
            }
            for(i=1;i<=n;i++) Max=max(Max,ans[i]);
            Wl(Max);
        }
        return 0;
    }
    /*
    input
    5 4 5
    0 1 1 0
    1 0 0 1
    0 1 1 0
    1 0 0 1
    0 0 0 0
    1 1
    1 4
    1 1
    4 2
    4 3
    output
    3
    4
    3
    3
    4
    */
    View Code
     
  • 相关阅读:
    spring初始化bean时执行某些方法完成特定的初始化操作
    flask与数据库连接相关操作
    解决flask中文乱码的问题
    flask(1)
    First Unique Character in a String (找到一个字符串中第一个不重复的字符)
    Java hashCode() 方法
    Java 类和对象
    Java 一维数组的定义和初始化
    Maven 在运行部署的时候是如何确定推送到 releases 还是 snapshots 仓库的
    Samples for Parallel Programming with the .NET Framework
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10624050.html
Copyright © 2020-2023  润新知