zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    hdu 1496 equations(哈希)
    为什么要微服务化
    什么是分布式系统中的幂等性
    不积跬步无以至千里
    服务治理与微服务
    使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码 (jvm性能调优)
    Spring MVC重定向和转发
    两个实体复制
    IntelliJ IDEA类头注释和方法注释
    Linux下单机安装部署kafka及代码实现
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/10752614.html
Copyright © 2011-2022 走看看