题目链接

题意

给出一个\(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的更多相关文章

  1. [CodeForces] CF226D The table

    Harry Potter has a difficult homework. Given a rectangular table, consisting of n × m cells. Each ce ...

  2. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  3. React使用antd Table生成层级多选组件

    一.需求 用户对不同的应用需要有不同的权限,用户一般和角色关联在一起,新建角色的时候会选择该角色对应的应用,然后对应用分配权限.于是写了一种实现的方式.首先应用是一个二级树,一级表示的是应用分组,二级 ...

  4. 创建几个常用table展示方式插件

    这次和大家分享的是自己写的一个table常用几种展示格式的js插件取名为(table-shenniu),样式使用的是bootstrap.min.css,还需要引用jquery.min.js包,这个插件 ...

  5. html中table边框属性

    1.向右(横向)合并: <td colspan="5"><span>后台管理系统</span></td> 2.向下(纵向)合并: & ...

  6. 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出同一表中的某些值 ...

  7. 打印Lua的Table对象

    小伙伴们再也不用为打印lua的Table对象而苦恼了, 本人曾也苦恼过,哈哈 不过今天刚完成了这个东西, 以前在网上搜过打印table的脚本,但是都感觉很不理想,于是,自己造轮子了~ 打印的效果,自己 ...

  8. React中使用Ant Table组件

    一.Ant Design of React http://ant.design/docs/react/introduce 二.建立webpack工程 webpack+react demo下载 项目的启 ...

  9. css设置table表格tr分离

    table { border-collapse:separate; border-spacing:10px 50px; }

随机推荐

  1. 播放器的书签--推荐使用Potplayer

    VLC Player https://www.vlchelp.com/skipping-and-playing-audio-and-video-portions-in-vlc/ PotPlayer  ...

  2. windows 为qt5.7.1 安装openssl

    本人使用qt5.7.1+msvc2015写一个https的客户端程序,但是用到解析https协议时,报出如下错误 qt.network.ssl: QSslSocket: cannot call unr ...

  3. 中科曙光I620-G15服务器登录密码破解

    服务器型号:中科曙光I620-G15服务器 系统:windowsserver2008R2 单位:保密 服务器登录密码忘记了,进不去桌面,后来在我们云修网工程师的指导下,顺利绕过密码登录系统,然后修改系 ...

  4. 4.机器学习——统计学习三要素与最大似然估计、最大后验概率估计及L1、L2正则化

    1.前言 之前我一直对于“最大似然估计”犯迷糊,今天在看了陶轻松.忆臻.nebulaf91等人的博客以及李航老师的<统计学习方法>后,豁然开朗,于是在此记下一些心得体会. “最大似然估计” ...

  5. docker其他参考资料

    https://yeasy.gitbooks.io/docker_practice/image/build.html https://blog.csdn.net/weixin_42596342/art ...

  6. 在Linux命令行中以图形化窗口打开文件夹

    Linux 系统中也有类似的命令.Ubuntu 发行版的命令行中,我们可以使用 nautilus 命令来打开指定目录的图形化窗口界面.类似下面命令这样使用: nautilus /home/testPr ...

  7. 四 Struts2 反射实现

    package com.myreflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import ...

  8. esp8266 免费wifi强推广告神器(4) 发现当前WIFI下的用户数目,IP,MAC请求http信息 在用户请求跳转后跳转

    需求: 1 获取当前连接客户端的HTTP请求各种信息 方法 get  http 请求路径  例如  /index.html   /    /pic.jpg 请求版本   HTTP/1.0     HT ...

  9. 微信小程序测试

    1.连接真机,微信已经登录过了 2.代码: 3.appium自带的识别工具 4.设置工具连接设备的方式 参考资料: https://www.cnblogs.com/yoyoketang/p/91449 ...

  10. python小白——进阶之路——day2天-———变量的缓存机制+自动类型转换

    # ###同一文件,变量的缓存机制 ''' -->Number 部分 1.对于整型而言,-5~正无穷范围内的相同值 id一致 2.对于浮点数而言,非负数范围内的相同值 id一致 3.布尔值而言, ...