CF#305-B. Mike and Fun-暴力模拟水题
2015-10-12 16:16
295 查看
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;
}
题意:
给出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;
}
相关文章推荐
- 顺序表
- kafka官方测试工具
- css去掉iPhone、iPad默认按钮样式
- 每天一个linux命令(33):df 命令
- 图片预加载问题解决方案
- ant的讲解
- 第8周 项目2—建立链串算法库
- 第4周项目3 单链表的应用(2)
- leetcode 27:Remove Element
- 利用开源工具搭一套汉英翻译系统(四):解码器Moses
- xcode 7 错误:CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG
- [原]iOS--字符编码,NSString和UTF-8间的转换
- 第六周项目二-建立链栈算法库
- DeepCopy
- 浅谈 PHP 与手机 APP 开发(API 接口开发)
- Android Binder设计与实现 - 设计篇
- Mongod 芒果数据库初学.
- nginx切割日志
- Java设计模式 —— 工厂方法模式 (Factory Method)
- 005-Impala SQL操作内部表和外部表