CF226D The table
题意
给出一个\(n\times m\)的矩阵,可以把某些行和某些列上面的数字变为相反数。问修改那些行和哪些列可以使得所有行和所有列之和都为非负数。
思路
每次将负数的行或者列变为相反数。因为矩阵上面的数字绝对值不超过\(100\),而每改变一次,最少使得整个矩阵和\(+2\),所以最多操作\(n\times m \times 100 = 10^6\)次。
代码
/*
* @Author: wxyww
* @Date: 2019-03-02 18:19:07
* @Last Modified time: 2019-03-02 18:54:53
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<bitset>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 1100;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int n,m,a[N][N],sum[2][N],ans[2][N];
void work1(int x) {
sum[0][x] = -sum[0][x];
for(int i = 1;i <= m;++i) {
sum[1][i] -= a[x][i];
sum[1][i] += -a[x][i];
a[x][i] = -a[x][i];
}
}
void work2(int x) {
sum[1][x] = -sum[1][x];
for(int i = 1;i <= n;++i) {
sum[0][i] -= a[i][x];
sum[0][i] += -a[i][x];
a[i][x] = -a[i][x];
}
}
int main() {
n = read(),m = read();
for(int i = 1;i <= n;++i) {
for(int j = 1;j <= m;++j) {
a[i][j] = read();
sum[0][i] += a[i][j];
sum[1][j] += a[i][j];
}
}
while(1) {
int bz = 0;
for(int i = 1;i <= n;++i)
if(sum[0][i] < 0) work1(i),bz = 1,ans[0][i] ^= 1;
for(int i = 1;i <= m;++i)
if(sum[1][i] < 0) work2(i),bz = 1,ans[1][i] ^= 1;
if(!bz) break;
}
int js = 0;
for(int i = 1;i <= n;++i) js += ans[0][i];
printf("%d ",js);
for(int i = 1;i <= n;++i) if(ans[0][i]) printf("%d ",i);
js = 0;
for(int i = 1;i <= m;++i) js += ans[1][i];
puts("");
printf("%d ",js);
for(int i = 1;i <= m;++i) if(ans[1][i]) printf("%d ",i);
return 0;
}
/*
5 10
-2 -7 -10 -9 5 -9 -3 8 -8 5
3 0 9 8 -4 -3 -8 1 8 1
2 3 7 5 -8 -3 0 -9 -7 -2
-6 -7 0 0 6 9 -8 6 -8 3
7 9 -4 -5 -9 -3 8 6 -5 6
*/
CF226D The table的更多相关文章
- [CodeForces] CF226D The table
Harry Potter has a difficult homework. Given a rectangular table, consisting of n × m cells. Each ce ...
- 散列表(hash table)——算法导论(13)
1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...
- React使用antd Table生成层级多选组件
一.需求 用户对不同的应用需要有不同的权限,用户一般和角色关联在一起,新建角色的时候会选择该角色对应的应用,然后对应用分配权限.于是写了一种实现的方式.首先应用是一个二级树,一级表示的是应用分组,二级 ...
- 创建几个常用table展示方式插件
这次和大家分享的是自己写的一个table常用几种展示格式的js插件取名为(table-shenniu),样式使用的是bootstrap.min.css,还需要引用jquery.min.js包,这个插件 ...
- html中table边框属性
1.向右(横向)合并: <td colspan="5"><span>后台管理系统</span></td> 2.向下(纵向)合并: & ...
- MySQL中You can't specify target table for update in FROM clause一场
mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值 ...
- 打印Lua的Table对象
小伙伴们再也不用为打印lua的Table对象而苦恼了, 本人曾也苦恼过,哈哈 不过今天刚完成了这个东西, 以前在网上搜过打印table的脚本,但是都感觉很不理想,于是,自己造轮子了~ 打印的效果,自己 ...
- React中使用Ant Table组件
一.Ant Design of React http://ant.design/docs/react/introduce 二.建立webpack工程 webpack+react demo下载 项目的启 ...
- css设置table表格tr分离
table { border-collapse:separate; border-spacing:10px 50px; }
随机推荐
- SqlServer主键和自增长设置
SqlServer主键和自增长设置 Intro 有时候有些 sql 语句有些不太记得了,谨以此文备忘. 设置主键以及自增长可分两种情况: 新创建表 表已创建但是没有设置主键和自增长 新创建表 创建表 ...
- postgresql 日志报错could not write to log file: No space left on device,could not write lock file "postmaster.pid": No space left on device
今天遇到了一个特别奇怪的问题,我在用docker容器的时候,发现我的postgresql怎么也启动不起来 尝试了N多种办法,最后看了看postgresql的日志发现 postgresql 日志中报错 ...
- JHipster技术栈定制 - JHipster Registry配置信息加密
本文说明了如何开启和使用JHipster-Registry的加解密功能. 1 整体规划 1.1 名词说明 名词 说明 备注 对称加密 最快速.最简单的一种加密方式,加密(encryption)与解密( ...
- linux 大小写转化
(1)sed: cat file | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' (2)tr: cat file | ...
- Java 环境部署之Idea :解决Idea 中gradle编译 war 包编码不对
IDE进行Gradle操作,那么还需要设置IDE的参数.例如在IDEA中,需要打开File->Other Settings->Default Settings->Gradle,在Gr ...
- Win10解除网速限制让网速飞起来
Win10隐藏了20%的网速,下面叫你怎么释放它: 1.按Win+R调出运行,输入gpedit.msc点击确定; 2.点击windows设置,右击基于策略Qos,选择高级Qos设置: 3.勾选如图方框 ...
- LeetCode算法题-Maximum Product of Three Numbers(Java实现)
这是悦乐书的第275次更新,第291篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第143题(顺位题号是628).给定一个整数数组,从其中找出三个数,使得乘积最大.例如: ...
- LeetCode算法题-Minimum Moves to Equal Array Elements(Java实现)
这是悦乐书的第233次更新,第246篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第100题(顺位题号是453).给定大小为n的非空整数数组,找到使所有数组元素相等所需的 ...
- hmac_检验客户端是否合法
老师博客:http://www.cnblogs.com/Eva-J/articles/8244551.html#_label6 server端 import socket import os impo ...
- PHP跨域jsonp方式
<?php header('Access-Control-Allow-Origin:*');//注意!跨域要加这个头 上面那个没有 $arr = array ('a'=>1,'b'=> ...