CodeForces 486B OR in Matrix

Posted zaq19970105

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 486B OR in Matrix相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/contest/486/problem/B

题目大意:

  有两个矩阵A和B,矩阵B的元素Bij的值是所有A的第i行元素和第j列元素或运算后的值。现在给出B矩阵,求A矩阵。

分析:

  首先,如果B[i][j] == 1,那么B矩阵或者第i行全为1,或者第j列全为1;如果B[i][j] == 0,那么A矩阵第i行全为0且第j列也全为0。
  其次,如果A[i][j] == 1,那么B矩阵第i行全为1且第j列也全为1。以此可以推出B矩阵全为1的行和全为1的列要么全有,要么全没有。
  于是可以把B矩阵全为1的行和全为1的列找出来一一配对,交点位置对应的A矩阵位置置1。

代码如下:

技术图片
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define rep(i,n) for (int i = 0; i < (n); ++i)
  5 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  6 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  7 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  8 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  9  
 10 #define pr(x) cout << #x << " = " << x << "  "
 11 #define prln(x) cout << #x << " = " << x << endl
 12  
 13 #define LOWBIT(x) ((x)&(-x))
 14  
 15 #define ALL(x) x.begin(),x.end()
 16 #define INS(x) inserter(x,x.begin())
 17  
 18 #define ms0(a) memset(a,0,sizeof(a))
 19 #define msI(a) memset(a,inf,sizeof(a))
 20 #define msM(a) memset(a,-1,sizeof(a))
 21  
 22 #define pii pair<int,int> 
 23 #define piii pair<pair<int,int>,int> 
 24 #define mp make_pair
 25 #define pb push_back
 26 #define fi first
 27 #define se second
 28  
 29 inline int gc(){
 30     static const int BUF = 1e7;
 31     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 32      
 33     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 34     return *bg++;
 35 } 
 36  
 37 inline int ri(){
 38     int x = 0, f = 1, c = gc();
 39     for(; c<48||c>57; f = c==-?-1:f, c=gc());
 40     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 41     return x*f;
 42 }
 43  
 44 typedef long long LL;
 45 typedef unsigned long long uLL;
 46 const int inf = 1e9 + 9;
 47 const LL mod = 1e9 + 7;
 48 const int maxN = 1e2 + 7;
 49  
 50 int n, m;
 51 int A[maxN][maxN];
 52 // Brow[i]表示第i行所有数与运算的值 
 53 // Bcol[i]表示第i列所有数与运算的值 
 54 // AllBrow表示所有Brow[i]或运算的值 
 55 // AllBcol表示所有Bcol[i]或运算的值 
 56 int B[maxN][maxN], Brow[maxN], AllBrow, Bcol[maxN], AllBcol;
 57 bool ans;
 58  
 59 int main(){
 60     while(cin >> n >> m) {
 61         ms0(A);
 62         rep(i, n) rep(j, m) cin >> B[i][j];
 63          
 64         AllBrow = AllBcol = 0; 
 65         rep(i, n) {
 66             Brow[i] = 1;
 67             rep(j, m) Brow[i] &= B[i][j];
 68             AllBrow |= Brow[i];
 69         }
 70         rep(i, m) {
 71             Bcol[i] = 1;
 72             rep(j, n) Bcol[i] &= B[j][i];
 73             AllBcol |= Bcol[i];
 74         }
 75          
 76         ans = !(AllBrow ^ AllBcol);
 77          
 78         rep(i, n) rep(j, m) {
 79             if(B[i][j] == 1 && Brow[i] == 0 && Bcol[j] == 0) {
 80                 ans = false;
 81                 i = n;
 82                 break;
 83             }
 84         }
 85          
 86         if(ans) {
 87             rep(i, n) rep(j, m) {
 88                 if(Brow[i] & Bcol[j]) {
 89                     A[i][j] = 1;
 90                 }
 91             }
 92              
 93             cout << "YES" << endl;
 94             rep(i, n) {
 95                 rep(j, m) {
 96                     cout << A[i][j] << " ";
 97                 }
 98                 cout << endl;
 99             }
100         }
101         else cout << "NO" << endl;
102     }
103     return 0;
104 }
View Code

 

以上是关于CodeForces 486B OR in Matrix的主要内容,如果未能解决你的问题,请参考以下文章

错误:(-215:断言失败)_step >= minstep in function 'cv::Mat::Mat'

codeforces 877E Danil and a Part-time Job

在 OpenCV 中合并频道

opencv 报错:Error: Assertion failed (data) in cv::Mat::at, file ... mat.inl.hpp, line 897(访问了不存在矩阵的像素)

无法使用 OpenCV.js 创建 Mat

error:Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::at