[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 ...
随机推荐
- LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- SPOJ XMAX - XOR Maximization
XMAX - XOR Maximization Given a set of integers S = { a1, a2, a3, ... a|S| }, we define a function X ...
- 【Hnoi2010】Bzoj2002 Bounce & Codevs2333 弹飞绵羊
Position: http://www.lydsy.com/JudgeOnline/problem.php?id=3143 http://codevs.cn/problem/2333/ Descri ...
- 使用WCF进行跨平台开发之一(WCF的实现、控制台托管与.net平台的调用)
WCF是Windows Communication Foundation的缩写,是微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,是WinFx的三个重要开发类库之一,其它两个是WP ...
- 洛谷P2059 [JLOI2013]卡牌游戏
题目描述 N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字为X,则庄家首先把 ...
- JAVA基础(多线程Thread和Runnable的使用区别(转载)
转自:http://jinguo.iteye.com/blog/286772 Runnable是Thread的接口,在大多数情况下“推荐用接口的方式”生成线程,因为接口可以实现多继承,况且Runnab ...
- bzoj3224 普通平衡树(splay 模板)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11427 Solved: 4878[Submit][St ...
- xfs文件备份恢复篇一vm中linux新增磁盘
XFS提供了 xfsdump 和 xfsrestore 工具协助备份XFS文件系统中的数据.xfsdump 按inode顺序备份一个XFS文件系统.centos7选择xfs格式作为默认文件系统,而且不 ...
- easyui DatagrId 的实例讲解
下面是代码实现 @{ ViewBag.Title = "人员查找"; ViewBag.LeftWidth = "200px"; ViewBag ...
- ACM_夏天到了,又到了出游的季节
夏天到了,又到了出游的季节 Time Limit: 2000/1000ms (Java/Others) Problem Description: QWER最近无心打代码,于是带着n套衣服出去浪.但是每 ...