CodeforcesGym101116 B Bulbs
Description
Greg has an \(m \times n\) grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Initially, some of the bulbs are on and some are off. Greg can toggle some bulbs by shooting his laser at them. When he shoots his laser at a bulb, it toggles that bulb between on and off. But, it also toggles every bulb directly below it,and every bulb directly to the left of it. What is the smallest number of times that Greg needs to shoot his laser to turn all the bulbs on?
Input
The first line of input contains a single integer \(T (1 \le T \le 10)\), the number of test cases. Each test case starts with a line containing two space-separated integers \(m\) and \(n\) \((1 \le m, n \le 400)\). The next \(m\) lines each consist of a string of length \(n\) of \(1\)s and \(0\)s. A \(1\) indicates a bulb which is on, and a \(0\) represents a bulb which is off.
Output
For each test case, output a single line containing the minimum number of times Greg has to shoot his laser to turn on all the bulbs.
Sample Input
2
3 4
0000
1110
1110
2 2
10
00
Sample Output
1
2
对于这题,有个朴素做法——高斯消元解异或方程组(每个灯泡为一个方程,每个能够影响此灯泡的为次方程未知元)。但这样明显过不去,经过仔细观察,可以发现这个方程可以\(O(N^{2})\)推出解,统计解为\(1\)的个数即为答案。
#include<cstring>
#include<bitset>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define maxn (410)
int ans,T,M,N,bulb[maxn][maxn],up[maxn];
inline int id(int x,int y) { return (x-1)*N+y-1; }
int main()
{
freopen("B.in","r",stdin);
freopen("B.out","w",stdout);
scanf("%d",&T);
while (T--)
{
scanf("%d %d",&M,&N); memset(up,0,sizeof(up)); ans = 0;
for (int i = 1,now = 0;i <= M;++i)
for (int j = 1;j <= N;++j,++now)
scanf("%1d",bulb[i]+j);
for (int i = 1;i <= M;++i)
for (int j = N,ri = 0;j;--j)
{
int res = (bulb[i][j]^1^ri^up[j]);
ri ^= res; up[j] ^= res; ans += res;
}
printf("%d\n",ans);
}
fclose(stdin); fclose(stdout);
return 0;
}
CodeforcesGym101116 B Bulbs的更多相关文章
- Codeforces Round #338 (Div. 2) A. Bulbs 水题
A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...
- HDU 5601 N*M bulbs 找规律
N*M bulbs 题目连接: http://codeforces.com/contest/510/problem/C Description NM个灯泡排成一片,也就是排成一个NM的矩形,有些开着, ...
- BestCoder Round #67 (div.2) N bulbs(hdu 5600)
N bulbs Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- N bulbs(规律)
N bulbs Accepts: 408 Submissions: 1224 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 655 ...
- Bulbs【暴力?】
问题 B: Bulbs 时间限制: 1 Sec 内存限制: 128 MB 提交: 216 解决: 118 [提交] [状态] [命题人:admin] 题目描述 Greg has an m × n ...
- zoj 2976 Light Bulbs(暴力枚举)
Light Bulbs Time Limit: 2 Seconds Memory Limit: 65536 KB Wildleopard had fallen in love with hi ...
- 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs
MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...
- upc组队赛5 Bulbs
Bulbs 题目描述 Greg has an m × n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. Ini ...
- 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)
题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...
随机推荐
- Android开发:Handler Runnable和Thread之间的区别和联系 应用--------------------看完本篇,从此一览无余!
http://blog.csdn.net/yanzi1225627/article/details/8582081 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnab ...
- FASTDFS 5X安装
FASTDFS 5X安装 http://www.wingdevops.com/?p=603 流行的开源分布式文件系统有很多,FastDFS 是国人在mogileFS的基础上进行改进的key-value ...
- NSURLSessionDownloadTask 断点下载
#import "ViewController.h" #import "ASIHTTPRequest.h" #import <AFNetworking/A ...
- go 初使用
hello.go package main import "fmt" func main(){ fmt.Println("hello world") 直接运行 ...
- 转:Myeclipse连接MySQL数据库经验分享
要使除 JDBC ODBC Bridge 之外的 Driver 生效,需要手动配置. 首先获得 MySQL Connector / J 的 jar : http://dev.mysql.com/dow ...
- IOS Remote Notification
1. 本地证书合成 rm *.pem echo "export cert..." openssl pkcs12 -clcerts -nokeys -out push_cert.pe ...
- 基于Lucene的文件检索Demo
通过Lucene实现了简单的文件检索功能的Demo.这个Demo支持基于文件内容的检索,支持中文分词和高亮显示. 下面简单的介绍下核心的类 1)索引相关的类 1.FileIndexBuilder -- ...
- 需要重新启动计算机.必须重新启动计算机才能安装 SQL Server
在开始运行中输入regedit找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager 在右边窗口找到PendingFi ...
- UI层调用WCF服务实例(源码)
WCF原理性的东西,暂时还没有深入研究,只是在公司的项目中使用到了,会调用,然后再多做了一些了解,现在将它抽出来了一个小实例,写了一个WCF的demo. 我写的这个WCF.Demo主要包括数据契约和服 ...
- Object-C内存管理基础
如果你通过手工alloc的方式分配内存实例化创建一个对象,之后你需要release这个对象,同理你也不能手工释放(release)一个能自动释放(autoreleased)的对象,因为这个样子会使你的 ...