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; }
随机推荐
- 章节九、1-Selenium环境配置
一.Selenium环境安装配置,这里使用Selenium WebDriver 3.6.0 1.下载Selenium WebDriver (点击后网站响应比较慢,需要多等等) 2.打开该网址后点击“d ...
- gitbook 入门教程之使用 gitbook.com 在线开发电子书
gitbook 官网是官方提供的图书托管的在线平台,分为新版官网(需要FQ) https://www.gitbook.com/ 和旧版官网(无需FQ) https://legacy.gitbook.c ...
- JMeter中文返回乱码
JMeter中文返回乱码 结果树响应数据中文返回乱码 其实有几个方法: 在线程组->http请求的字符集里设置 在http 消息管理头中设置 3.如果以上方法还没有解决,请打开安装目录 ...
- linux文件行首行尾添加或替换
sed -i 's/\(^.*\)/http:\/\/www.blutmagie.de\/img\/flags\//g' cc.txt sed -i 's/\($\)/.gif/g' cc.txt
- git 初探
1,创建GIT代码仓库 git init 2,添加修改到缓存区 git add filename 3,提交缓存区的修改 git commit -m "任意文字(便于自己记忆)" 4 ...
- JavaScript数据类型之null和undeined
null null是JavaScrpt的关键字,表示一个特殊值,常用于描述"空值".对null执行typeof运算将返回字符串"object". undefin ...
- 登陆验证AuthorizeAttribute
自定义验证,验证失败后:Response.Redirect.
- phpstudy运行时出现没有安装VC库
系统默认的VC库是安装在C:\Program Files\Common Files\microsoft shared\VC的文件夹里,当运行PHP Study是出现如下的提示: 可以到下面的网站去下载 ...
- 使用springMVC时的web.xml配置文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...
- winserver的consul部署实践与.net core客户端使用(附demo源码)
winserver的consul部署实践与.net core客户端使用(附demo源码) 前言 随着微服务兴起,服务的管理显得极其重要.都知道微服务就是”拆“,把臃肿的单块应用,拆分成多个轻量级的 ...