[CodeForces] CF226D The table
Harry Potter has a difficult homework. Given a rectangular table, consisting of n × m cells. Each cell of the table contains the integer. Harry knows how to use two spells: the first spell change the sign of the integers in the selected row, the second — in the selected column. Harry's task is to make non-negative the sum of the numbers in each row and each column using these spells.
Alone, the boy can not cope. Help the young magician!
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of rows and the number of columns.
Next n lines follow, each contains m integers: j-th integer in the i-th line is ai, j (|ai, j| ≤ 100), the number in the i-th row and j-th column of the table.
The rows of the table numbered from 1 to n. The columns of the table numbered from 1 to m.
Output
In the first line print the number a — the number of required applications of the first spell. Next print a space-separated integers — the row numbers, you want to apply a spell. These row numbers must be distinct!
In the second line print the number b — the number of required applications of the second spell. Next print b space-separated integers — the column numbers, you want to apply a spell. These column numbers must be distinct!
If there are several solutions are allowed to print any of them.
Examples
4 1
-1
-1
-1
-1
4 1 2 3 4
0
2 4
-1 -1 -1 2
1 1 1 1
1 1
1 4
题目解析
输出有点玄学。
大概意思就是先输出行、列最小交换几次,然后输出交换了哪一行
我的做法是贪心,反复judge每一行、列的和,发现负的就翻转次数++,不停地翻转小于0的行列,总会让每行每列都变得大于0的。
最后,因为翻转两次=不翻转,所以最后要输出次数&1。挺好玩的,神题。
Code
#include<iostream>
#include<cstdio>
using namespace std; int n,m;
int a[][];
int sumx[],sumy[];
int flagx[],flagy[];
int ans1,ans2; inline int judge_x() {
for(int i = ;i <= n;i++) {
if(sumx[i] < ) return i;
}
return ;
} inline int judge_y() {
for(int i = ;i <= m;i++) {
if(sumy[i] < ) return i;
}
return ;
} int main() {
scanf("%d%d",&n,&m);
for(int i = ;i <= n;i++) {
for(int j = ;j <= m;j++) {
scanf("%d",&a[i][j]);
sumx[i] += a[i][j];
sumy[j] += a[i][j];
}
}
while() {
int tmpx = judge_x();
int tmpy = judge_y();
if(!tmpx && !tmpy) break;
if(tmpx) {
flagx[tmpx]++;
sumx[tmpx] = ;
for(int i = ;i <= m;i++) {
int k = -a[tmpx][i];
sumy[i] = sumy[i] - a[tmpx][i] + k;
a[tmpx][i] = k;
sumx[tmpx] += k;
}
} else {
flagy[tmpy]++;
sumy[tmpy] = ;
for(int i = ;i <= n;i++) {
int k = -a[i][tmpy];
sumx[i] = sumx[i] - a[i][tmpy] + k;
a[i][tmpy] = k;
sumy[tmpy] += k;
}
}
}
for(int i = ;i <= n;i++) if(flagx[i] & ) ans1++;
for(int i = ;i <= m;i++) if(flagy[i] & ) ans2++;
printf("%d ",ans1);
for(int i = ;i <= n;i++) {
if(flagx[i] & ) printf("%d ",i);
}
printf("\n%d ",ans2);
for(int i = ;i <= m;i++) {
if(flagy[i] & ) printf("%d ",i);
}
printf("\n");
return ;
}
[CodeForces] CF226D The table的更多相关文章
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
- CodeForces 1099E - Nice table - [好题]
题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- codeforces D. Multiplication Table
http://codeforces.com/contest/448/problem/D 题意:一个n×m的矩阵,a[i][j]=i*j; 然后把a数组排序,找出第k个数. 思路:1-n×m二分枚举,然 ...
- Codeforces 22B Bargaining Table
http://www.codeforces.com/problemset/problem/22/B 题意:求出n*m的方格图中全是0的矩阵的最大周长 思路:枚举 #include<cstdio& ...
- Codeforces #662C Binary Table
听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...
- Codeforces 40E Number Table - 组合数学
题目传送门 传送门I 传送门II 题目大意 给定一个$n\times m$的网格,每个格子上要么填$1$,要么填$-1$,有$k$个位置上的数是已经填好的,其他位置都是空的.问有多少种填法使得任意一行 ...
- Codeforces 233 D - Table
D - Table 思路:dp 首先,第i列的个数肯定和第i - n列个数一样,假设[i - n + 1, i - 1] 之间的个数之和为x,那么第i列和第i-n列的个数应该是n - x 那么我们可以 ...
- 【CODEFORCES】 C. Table Decorations
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- ORA-00904:"T1"."AREA_ID" :标识符无效
1.错误描写叙述 ORA-00904:"T1"."AREA_ID" :标识符无效 00904 . 00000 - "%s:invalid identi ...
- 【Swift】学习笔记(八)——类和结构体
1.类和结构体的定义 <pre name="code" class="html">struct Resolution { var width = 0 ...
- PHP正则表达式-从文本中提取URL
1.从文本中提取URL的正则表达式 '/https?:\/\/[\w-.%#?\/\\\]+/i'
- CA certificate
1 什么是CA certificate CA证书本质上是一把公钥. 2 为什么需要CA证书 是为了避免黑客冒充服务器,服务器通过CA证书证明自己是真的服务器,而不是黑客. 就是说,一旦客户端有了一个服 ...
- IE浏览器 多版本之间切换
由于Win7系统最低支持IE8的版本,不能通过安装IE7版本来达到要求. 有一个替代方案,通过IE自带Emulation来实现,实现步骤如下: 打开现有的IE浏览器 按下F12键 切换到“Emulat ...
- sql server中数据约束相关的查询
根据表名查找数据约束 SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'CMS_EventLog'; SEL ...
- windows2003下svn的安装
Windows2003下svn平台搭建 编辑:dnawo 日期:2010-08-03 转自http://www.mzwu.com/article.asp?id=2557 字体大小: 小 中 大 ...
- [Swift通天遁地]二、表格表单-(4)使用系统自带的下拉刷新控件,制作表格的下拉刷新效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Akka源码分析-故障恢复
Actor故障恢复是akka中非常重要的内容,在之前的博客中虽然有介绍,但都是杂糅在其他知识点的细节中,本博客将单独介绍这一部分的故障恢复.为了简化分析的单独,本文只研究用户的actor故障恢复的步骤 ...
- BZOJ 4085 丧心病狂的毒瘤题目 线段树+矩乘
思路: 一眼矩阵快速幂 再用线段树维护一下矩阵就完了... 我hhhhh 哎我还是too young,too simple 入了这个大坑 线段树维护9个值 以上 如果A+1 转移矩阵是这个样 ...