[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 ...
随机推荐
- vijos - P1302连续自然数和 (公式推导 + python)
P1302连续自然数和 Accepted 标签:[显示标签] 描写叙述 对一个给定的自然数M,求出所有的连续的自然数段(连续个数大于1).这些连续的自然数段中的所有数之和为M. 样例:1998+199 ...
- 【C语言】不使用大小于号,求出两数最大值
//不使用大小于号,求出两数最大值 #include <stdio.h> #include <math.h> double Max(double a, double b) { ...
- CSDN 厦门大学线下编程比赛第一题:求和(同余定理)
题目意思: 给定a和n,计算a+aa+aaa+aaaa+...+a...a(n个a) 的和. 输入描写叙述:測试数据有多组,以文件结尾.每行输入a,n(1<=a,n<=1000000). ...
- ios13--购物车优化
// // ViewController.m // 03-综合练习 // #import "ViewController.h" @interface ViewController ...
- Fragmen的onAttach方法
现在Android开发多使用一个Activity管理多个Fragment进行开发,不免需要两者相互传递数据,一般是给Fragment添加回调接口,让Activity继承并实现. 回调接口一般都写在Fr ...
- Hybrid 开发
主讲人:吴彬 要学习某个东西之前,我们首先要了解这个东西是什么?然后我们要了解这东西有什么用,有什么好处和弊端?最后我们要知道这东西怎么用? 简单点就是 ——是什么?有什么用?怎么用? 那么进入正题 ...
- 码位(code position/point)Unicode 编码与 Python 2/3 编码兼容性问题
Unicode HOWTO 0. 码位(code position/point) 一个码位由某个数值表示,全部码位共同构成其码值空间(code space). ASCII,0~7Fhex(128) 拓 ...
- JSP页面的跳转及传值
1.response.sendRedirect("跳转到页面的URL"); 该方法通过修改HTTP协议的HEADER部分,对浏览器下达重定向指令的,使浏览器显示重定向网页的内容. ...
- 41. extjs--combobox下拉列表的triggerAction
转自:https://icrwen.iteye.com/blog/939247 一般combobox的store先load加载数据,然后combobox的mode设置为local,则不会每次下拉列表都 ...
- 53. 部门信息显示 EXTJS 单击树节点
1. /** * @author sux * @time 2011-1-14 * @desc 部门信息显示 */ deptInfoGridPanel = Ext.extend(Ext.grid.Edi ...