题目链接

题意

给出一个\(n\times m\)的矩阵,可以把某些行和某些列上面的数字变为相反数。问修改那些行和哪些列可以使得所有行和所有列之和都为非负数。

思路

每次将负数的行或者列变为相反数。因为矩阵上面的数字绝对值不超过\(100\),而每改变一次,最少使得整个矩阵和\(+2\),所以最多操作\(n\times m \times 100 = 10^6\)次。

代码

  1. /*
  2. * @Author: wxyww
  3. * @Date: 2019-03-02 18:19:07
  4. * @Last Modified time: 2019-03-02 18:54:53
  5. */
  6. #include<cstdio>
  7. #include<iostream>
  8. #include<cstdlib>
  9. #include<cmath>
  10. #include<ctime>
  11. #include<bitset>
  12. #include<cstring>
  13. #include<algorithm>
  14. #include<string>
  15. #include<vector>
  16. #include<queue>
  17. #include<vector>
  18. using namespace std;
  19. typedef long long ll;
  20. const int N = 1100;
  21. ll read() {
  22. ll x=0,f=1;char c=getchar();
  23. while(c<'0'||c>'9') {
  24. if(c=='-') f=-1;
  25. c=getchar();
  26. }
  27. while(c>='0'&&c<='9') {
  28. x=x*10+c-'0';
  29. c=getchar();
  30. }
  31. return x*f;
  32. }
  33. int n,m,a[N][N],sum[2][N],ans[2][N];
  34. void work1(int x) {
  35. sum[0][x] = -sum[0][x];
  36. for(int i = 1;i <= m;++i) {
  37. sum[1][i] -= a[x][i];
  38. sum[1][i] += -a[x][i];
  39. a[x][i] = -a[x][i];
  40. }
  41. }
  42. void work2(int x) {
  43. sum[1][x] = -sum[1][x];
  44. for(int i = 1;i <= n;++i) {
  45. sum[0][i] -= a[i][x];
  46. sum[0][i] += -a[i][x];
  47. a[i][x] = -a[i][x];
  48. }
  49. }
  50. int main() {
  51. n = read(),m = read();
  52. for(int i = 1;i <= n;++i) {
  53. for(int j = 1;j <= m;++j) {
  54. a[i][j] = read();
  55. sum[0][i] += a[i][j];
  56. sum[1][j] += a[i][j];
  57. }
  58. }
  59. while(1) {
  60. int bz = 0;
  61. for(int i = 1;i <= n;++i)
  62. if(sum[0][i] < 0) work1(i),bz = 1,ans[0][i] ^= 1;
  63. for(int i = 1;i <= m;++i)
  64. if(sum[1][i] < 0) work2(i),bz = 1,ans[1][i] ^= 1;
  65. if(!bz) break;
  66. }
  67. int js = 0;
  68. for(int i = 1;i <= n;++i) js += ans[0][i];
  69. printf("%d ",js);
  70. for(int i = 1;i <= n;++i) if(ans[0][i]) printf("%d ",i);
  71. js = 0;
  72. for(int i = 1;i <= m;++i) js += ans[1][i];
  73. puts("");
  74. printf("%d ",js);
  75. for(int i = 1;i <= m;++i) if(ans[1][i]) printf("%d ",i);
  76. return 0;
  77. }
  78. /*
  79. 5 10
  80. -2 -7 -10 -9 5 -9 -3 8 -8 5
  81. 3 0 9 8 -4 -3 -8 1 8 1
  82. 2 3 7 5 -8 -3 0 -9 -7 -2
  83. -6 -7 0 0 6 9 -8 6 -8 3
  84. 7 9 -4 -5 -9 -3 8 6 -5 6
  85. */

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. 章节九、1-Selenium环境配置

    一.Selenium环境安装配置,这里使用Selenium WebDriver 3.6.0 1.下载Selenium WebDriver (点击后网站响应比较慢,需要多等等) 2.打开该网址后点击“d ...

  2. gitbook 入门教程之使用 gitbook.com 在线开发电子书

    gitbook 官网是官方提供的图书托管的在线平台,分为新版官网(需要FQ) https://www.gitbook.com/ 和旧版官网(无需FQ) https://legacy.gitbook.c ...

  3. JMeter中文返回乱码

    JMeter中文返回乱码 结果树响应数据中文返回乱码 其实有几个方法: 在线程组->http请求的字符集里设置 ​ 在http 消息管理头中设置 ​ 3.如果以上方法还没有解决,请打开安装目录 ...

  4. linux文件行首行尾添加或替换

    sed -i 's/\(^.*\)/http:\/\/www.blutmagie.de\/img\/flags\//g' cc.txt sed -i 's/\($\)/.gif/g' cc.txt

  5. git 初探

    1,创建GIT代码仓库 git init 2,添加修改到缓存区 git add filename 3,提交缓存区的修改 git commit -m "任意文字(便于自己记忆)" 4 ...

  6. JavaScript数据类型之null和undeined

    null null是JavaScrpt的关键字,表示一个特殊值,常用于描述"空值".对null执行typeof运算将返回字符串"object". undefin ...

  7. 登陆验证AuthorizeAttribute

    自定义验证,验证失败后:Response.Redirect.

  8. phpstudy运行时出现没有安装VC库

    系统默认的VC库是安装在C:\Program Files\Common Files\microsoft shared\VC的文件夹里,当运行PHP Study是出现如下的提示: 可以到下面的网站去下载 ...

  9. 使用springMVC时的web.xml配置文件

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...

  10. winserver的consul部署实践与.net core客户端使用(附demo源码)

    winserver的consul部署实践与.net core客户端使用(附demo源码)   前言 随着微服务兴起,服务的管理显得极其重要.都知道微服务就是”拆“,把臃肿的单块应用,拆分成多个轻量级的 ...