日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

The table(CF-226D)

發布時間:2025/3/17 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 The table(CF-226D) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem Description

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

Input

4 1
-1
-1
-1
-1

Output

4 1 2 3 4?
0

Input

2 4
-1 -1 -1 2
1 1 1 1

Output

1 1?
1 4?

題意:給出一個 n*m 的矩陣,其元素絕對值均小于 100,現在可以讓某一行或一列的所有數取反,要求構造一個解,使得每一行每一列的和都是非負數

思路:

首先統計矩陣每一行每一列的和,然后去枚舉行列的和,當某一行的值小于 0,那么將這一行直接翻轉,再對行列和進行相應處理,直到沒有負值

由于矩陣元素最多為 100 個,元素值范圍為 -100~100,因此,整個矩陣和的范圍為 [-10000,10000],故而當某一行列的和為負時,將其進行翻轉后,和至少 +2,因此最多枚舉 100^4 即可令整個矩陣的行列和為正

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<bitset> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 5000+5; const int dx[] = {-1,1,0,0,-1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std;LL a[N][N]; LL sumRow[N],sumCol[N]; int row[N],col[N]; vector<int>resRow,resCol; int main() {int n,m;scanf("%d%d",&n,&m);for(int i=1; i<=n; i++) {for(int j=1; j<=m; j++) {scanf("%lld",&a[i][j]);sumRow[i]+=a[i][j];sumCol[j]+=a[i][j];}}while(true) {for(int i=1; i<=n; i++) {if(sumRow[i]<0) {sumRow[i]=0;for(int j=1; j<=m; j++) {a[i][j]=-a[i][j];sumRow[i]+=a[i][j];sumCol[j]=sumCol[j]-(-a[i][j])+a[i][j];}row[i]+=1;}}for(int j=1; j<=m; j++) {if(sumCol[j]<0) {sumCol[j]=0;for(int i=1; i<=n; i++) {a[i][j]=-a[i][j];sumCol[j]+=a[i][j];sumRow[i]=sumRow[i]-(-a[i][j])+a[i][j];}col[j]+=1;}}bool flag=false;for(int i=1; i<=n; i++) {if(sumRow[i]<0) {flag=true;}}for(int i=1; i<=m; i++) {if(sumCol[i]<0) {flag=true;}}if(!flag)break;}int num1=0,num2=0;for(int i=1; i<=n; i++) {if(row[i]%2==1) {resRow.push_back(i);num1++;}}for(int i=1; i<=m; i++) {if(col[i]%2==1) {resCol.push_back(i);num2++;}}printf("%d ",num1);for(int i=0; i<num1; i++)printf("%d ",resRow[i]);printf("\n");printf("%d ",num2);for(int i=0; i<num2; i++)printf("%d ",resCol[i]);printf("\n");return 0; }

總結

以上是生活随笔為你收集整理的The table(CF-226D)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。