Codeforces.226D.The table(构造)
\(Description\)
给定一个\(n\times m\)的矩阵\(A_{i,j}\),每次可以将一列或一行取负。求一个方案使得若干次操作后,每行每列的和都非负。
\(n,m\leq100,\ 元素绝对值|A_{i,j}|\leq100\)。
\(Solution\)
容易想到每次找和为负的一行或一列取负。这样做正确性及复杂度会有啥问题么?
注意到每次取负,所有数的和是单调递增的,所以一定会结束。且每次和至少会增加\(2\)(\(-1\to1\)),而所有数的和最小是\(-10^6\),最大是\(10^6\),所以最多操作\(10^6\)次,复杂度\(O(10^6n)\)。
自己写了写,写的真是麻烦。。不需要queue,每次\(O(nm)\)for一遍反转行列即可。
//62ms 0KB
#include <cstdio>
#include <cctype>
#define gc() getchar()
typedef long long LL;
const int N=105;
int A[N][N];
bool x[N],y[N];
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now*f;
}
void Print(bool *a,int n)
{
int t=0;
for(int i=1; i<=n; ++i) t+=a[i];
printf("%d ",t);
for(int i=1; i<=n; ++i) a[i]&&printf("%d ",i);
putchar('\n');
}
int main()
{
const int n=read(),m=read();
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j) A[i][j]=read();
for(; ; )
{
bool ok=1;
for(int i=1; i<=n; ++i)
{
int s=0;
for(int j=1; j<=m; ++j) s+=x[i]^y[j]?-A[i][j]:A[i][j];
if(s<0) x[i]^=1, ok=0;
}
for(int j=1; j<=m; ++j)
{
int s=0;
for(int i=1; i<=n; ++i) s+=x[i]^y[j]?-A[i][j]:A[i][j];
if(s<0) y[j]^=1, ok=0;
}
if(ok) break;
}
Print(x,n), Print(y,m);
return 0;
}
Codeforces.226D.The table(构造)的更多相关文章
- Codeforces 1098B. Nice table 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1098B.html 题解 首先,我们来证明一个结论: 合法的矩阵要么满足每列只有两种字符,要么满足每行只有两 ...
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
- Codeforces Round #140 (Div. 1) D. The table 构造
D. The table 题目连接: http://www.codeforces.com/contest/226/problem/D Description Harry Potter has a di ...
- CodeForces 1099E - Nice table - [好题]
题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...
- Lua中的table构造式(table constructor)
最简单的构造式就是一个空构造式{},用于创建一个空table. 构造式还可以用于初始化数组.例如,以下语句:days = {"Sunday", "Monday" ...
- Codeforces 1383D - Rearrange(构造)
Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
- Codeforces 549B. Looksery Party[构造]
B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
随机推荐
- hdu2460 e-DCC染色缩点+暴力LCA
/* 给定一个无向图,往里面加边,问加第i条边时图中的桥数 首先肯定要求初始状态下的桥,染色缩点 每次给定的边为(u,v), 那么u->lca(u,v)->v路上的所有边都不再是桥 求LC ...
- 纯CSS3超酷3D旋转立方体动画特效
简要教程 这是一款神奇的纯 CSS3 立方体动画特效插件.使用CSS3来制作动画效果已经成为WEB前端开发的一种时尚,从简单的颜色和尺寸动画,到复杂的旋转.翻转动画, CSS3 展现了它无穷的魅力.使 ...
- Vue router拦截 如果用户并未登录直接跳转到登录界面(最简单的cookie演示)
router.beforeEach(function(to,from,next){ console.log('路由拦截') console.log(to.name) console.log(from. ...
- tomcat中浏览器重新选择下.就解决该问题了
- cmake方式使用vlfeat
目录 environment statement compile vlfeat with cmake compile example project with cmake 1. make sure c ...
- C#正则Groups高级使用方法
正则表达式号称开发者得瑞士军刀,使用好正则表达式尤其重要. 拆分多个正则: public static string[] SplitByManyRegex(string text, string[] ...
- OpenJDK-study-001 windows上安装Mercurial 4.4.1 克隆OPENJDK版本库
下载安装 1.下载Mercurial 进入https://www.mercurial-scm.org/wiki/Mercurial下载,windows上傻瓜式安装的,安装好之后,命令行进入安装目录, ...
- 【Android】jar包Proguard混淆方法
本文章的前提条件是,读者已经掌握了正确导出jar包的技能. 1.拷贝Android项目中“proguard.cfg”文件到你指定的位置,并改名为“proguard.pro”,此步是为proguardg ...
- Js计算时间差,天数,小时数,余数
var begintime_ms = Date.parse(new Date(begintime.replace(/-/g, "/"))); //begintime 为开始时间 v ...
- js网页下载csv格式的表格
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...