zoukankan      html  css  js  c++  java
  • CF 548B Mike and Fun

    B. Mike and Fun
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in ann × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number iby (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 and1 ≤ 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.

    Sample test(s)
    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
    #include <algorithm>
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <map>
    
    using namespace std;
    const int N=5E2+5;
    int a[N][N];
    int fans;
    int n,m,q,x,y,cur,ans[N];
    int main()
    {
        cin>>n>>m>>q;
        for (int i = 1 ; i <= n;i++ )
        {
            for (int j = 1; j <= m ; j++ )
                    scanf("%d",&a[i][j]);
            cur = 0;
            for (int j = 1; j <=m ;j++ )
                if (a[i][j]==1)
                {
                    cur++;
                    ans[i]=max(cur,ans[i]);
                }
                else
                {
                   cur = 0;
                }
        }
        for ( int i = 1 ; i <= q; i++ )
        {
            scanf("%d %d",&x,&y);
            a[x][y]=a[x][y]^1;
           // if (i==3) cout<<a[x][y]<<"sadsadasd"<<endl;
            cur = 0;
            ans[x]=0;
            for (int j = 1; j <=m ;j++ )
                if (a[x][j]==1)
                {
                    cur++;
                    ans[x]=max(cur,ans[x]);
                }
                else
                {
                   cur = 0;
                }
            fans=-1;
            for (int j = 1;j <= n ; j++ )
                if (ans[j]>fans)
                {
                    fans=ans[j];
                }
            cout<<fans<<endl;
        }
    
    
        return 0;
    }

    3
    4

    比赛的时候不懂为什么就没做出来....
    其实很容易想到一个o(q*(n+m))的做法...
    就是每次更新,要同时更新当前更新行的最大连续和....O(m)可以完成...然后在O(n)扫一遍,找到所有行中的最大值。
    然后需要注意的是,在第一次更改之前就要把每个行的最大值处理出来l..
    然后cf机器真是够快,O(n*m*q)的1.2S过。。。。



  • 相关阅读:
    newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解
    str&repr的使用&format模板的自定义
    内置函数的补充与getattrebuit & item系列
    python几种常用模块
    面向对象的反射&动态导入模块
    面向对象的封装&定制数据类型
    面向对象的多态
    面向对象的继承
    面向对象的属性与方法
    面向对象的属性及类的增删改查
  • 原文地址:https://www.cnblogs.com/111qqz/p/4534661.html
Copyright © 2011-2022 走看看