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; }
随机推荐
- Redis进阶学习笔记
Redis是什么这里不用再说了吧?下面是官方的解释 Redis is an open source (BSD licensed), in-memory data structure store, us ...
- MySQL中Identifier Case Sensitivity
在MySQL当中,有可能遇到表名大小写敏感的问题.其实这个跟平台(操作系统)有关,也跟系统变量lower_case_table_names有关系.下面总结一下,有兴趣可以查看官方文档"Ide ...
- 注册表修改computer name
修改windows server的机器名的时候,发现change按钮是disable的. 手动修改不了,用注册表regedit来修改. HKEY_LOCAL_MACHINE\SYSTEM\Curren ...
- Centos7安装搭建NTP服务器和NTP客户端同步时间
NTP简介: NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议. 在计算机的世界里,时间非常地重要 例如:对于火箭发射这种科研活动,对时间的 ...
- 合并两个有序链表的golang实现
将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 输入:->->, ->-> 输出:->->->->-> ...
- Vue项目分环境打包的实现步骤
转:https://blog.csdn.net/xinzi11243094/article/details/80521878 方法一:亲测真的有效 在项目开发中,我们的项目一般分为开发版.测试版.Pr ...
- 阿里云RDS for MySQL 快速入门——笔记
1初始化配置 1.1设置白名单 创建RDS实例后,需要设置RDS实例的白名单,以允许外部设备访问该RDS实例.默认的白名单只包含默认IP地址127.0.0.1,表示任何设备均无法访问该RDS实例. 设 ...
- python3 pickle模块
import pickle '''将对象转化为硬盘能识别的bytes的过程被称为序列号将bytes转化为对象的过程被称为反序列化'''lst = ["苹果", "橘子&q ...
- Python编码、集合set、深浅拷贝
编码 : a.encode(' ') windows 默认编码GBK ASCII : 最早的编码. ⾥⾯有英⽂⼤写字⺟, ⼩写字⺟, 数字, ⼀些特殊字符.没有中⽂, 8个01代码, 8个bi ...
- uiautomator2 使用Python测试 Android应用
GitHub地址:https://github.com/openatx/uiautomator2 介绍 uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库.其底 ...