【codeforces 548B】Mike and Fun
来源:互联网 发布:js获取cookie过期时间 编辑:程序博客网 时间:2024/05/21 13:58
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 n, m 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
【题目链接】:http://codeforces.com/contest/548/problem/B
【题解】
思路挺简单的;
就是先算出每一行的答案;存在ans[maxn]里面;
(然后把这maxn个答案存在set里面->结构体记录id);
然后每次更新只会影响这一行的答案。
所以更新一个点之后用O(N)更新这一行的答案.在set中把这一行原来的答案去掉,然后把新的答案加到set里面.
每次结束后输出最大值就好了。
复杂度就O(N*Q*lo2gn)左右吧;
【完整代码】
#include <bits/stdc++.h>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define mp make_pair#define pb push_back#define fi first#define se second#define rei(x) scanf("%d",&x)#define rel(x) scanf("%I64d",&x)typedef pair<int,int> pii;typedef pair<LL,LL> pll;const int MAXN = 500+10;const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};const double pi = acos(-1.0);struct abc{ int num,id; friend bool operator < (abc a,abc b) { if (a.num != b.num) return a.num > b.num; else return a.id < b.id; }};int n,m,q,ans[MAXN];int a[MAXN][MAXN];set <abc> myset;int main(){ //freopen("F:\\rush.txt","r",stdin); rei(n);rei(m);rei(q); rep1(i,1,n) rep1(j,1,m) rei(a[i][j]); rep1(i,1,n) { int le = 0; rep1(j,1,m) if (a[i][j]==1) { int r = j; while (r+1<=m && a[i][r+1]==1) r++; int temp = r-j+1; if (temp > le) le = temp; } ans[i] = le; myset.insert({le,i}); } rep1(i,1,q) { int x,y; rei(x);rei(y); a[x][y] = 1-a[x][y]; int le = 0; rep1(j,1,m) if (a[x][j]==1) { int r = j; while (r+1<=m && a[x][r+1]==1) r++; int temp = r-j+1; if (temp > le) le = temp; } myset.erase({ans[x],x}); ans[x] = le; myset.insert({ans[x],x}); printf("%d\n",myset.begin()->num); } return 0;}
- CodeForces 548B Mike and Fun
- CodeForces 548B Mike and Fun
- 【codeforces 548B】Mike and Fun
- CodeForces - 548B Mike and Fun (模拟)
- B. Mike and Fun
- B. Mike and Fun
- Codeforces Round #305 (Div. 2) B.Mike and Fun
- Codeforces Round #305 (Div. 2) B. Mike and Fun(水题)
- Codeforces Round #305 (Div. 2)B. Mike and Fun
- Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun
- codeforces B. Mike and strings
- Codeforces548B:Mike and Fun
- CF#305-B. Mike and Fun-暴力模拟水题
- Mike and Feet - CodeForces #547 B
- Codeforces 547B Mike and Feet
- CodeForces 689B - Mike and Shortcuts
- codeforces 689B Mike and Shortcuts (bfs)
- CodeForces 689B BFS-Mike and Shortcuts
- 链表常用操作
- 计算机语言递归原理及行列式递归求值
- Unity NGUI 重叠的黑边
- js数组去重
- 连连看——C++简单小游戏
- 【codeforces 548B】Mike and Fun
- 超级课程表点赞插件
- bzoj 4016: [FJOI2014]最短路径树问题
- mysql 发生系统错误1067的解决方法
- Mac利用PD虚拟机安装Centos7
- 161221
- 微信小程序开发之页面分享 onShareAppMessage 分享参数用处
- faster-rcnn 之 RPN网络的结构解析以及RPN代码详解
- Android:初级控件之(一)ViewPager(一)