[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 ...
随机推荐
- 数据分析在web交互设计中的作用 页面跳出率 100% 原因分析
通过分析访问的路径,发现,访问者访问其他页面,直接跳出 页面跳出率 100% 说明: 连作者都发现的导航路径不清晰 对导航进行改版:清晰.明了
- windowActionModeOverlay
windowActionModeOverlay: android:windowActionModeOverlay=“true|false” : actionmode 弹出时覆盖部分布局 若 ...
- 读取Excel文件到DataTable中
private static string[] GetExcelSheetNames(OleDbConnection conn) { DataTable dtbSh ...
- 55. GridPanel中getSelectionModel详解
转自:https://blog.csdn.net/qq_29663071/article/details/50728429 本文导读:Ext.grid.GridPanel继承自Panel,其xtype ...
- 【转载】HashMap底层实现原理及面试问题
①HashMap的工作原理 HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算h ...
- P4046 [JSOI2010]快递服务
传送门 很容易想出\(O(n^3m)\)的方程,三维分别表示某个快递员现在在哪里,然后直接递推即可 然而这样会T,考虑怎么优化.我们发现每一天的时候都有一个快递员的位置是确定的,即在前一天要到的位置. ...
- 开源矿工README
点击加入 NTMiner官方QQ群: 863725136 开源矿工内置的所有内核均为原版,开源矿工永远不会额外增加矿工支出: 开源矿工永远开源: 开源矿工永远不会去破解国人开发的内核: 下载地址 阿里 ...
- HttpPostedFileBase 基类
public void uploadDocMentSave(string Type) { if (Request.Files.Count > 0) { Htt ...
- printf的字符型
参 数 说 明 %s 按实际宽度输出一个字符串 %ms m指定宽度(不足时左补空格,大于时按实际宽度输出) %-ms 左对齐,不足时右补空格 %m.ns 输出占m个字符位置,其中字符数最多n个,左 ...
- 题解报告:hdu 1213 How Many Tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...