zoukankan      html  css  js  c++  java
  • 【codeforces 816C】Karen and Game

    【题目链接】:http://codeforces.com/contest/816/problem/C

    【题意】

    给你一个n*m的矩阵;
    一开始所有数字都是0;
    每次操作,你能把某一行,或某一列的数字全部加上1;
    问你到达目标矩阵最少需要进行多少次操作;

    【题解】

    从目标矩阵开始减;
    直接枚举每一列需要减多少次;
    每一行需要减多少次即可;
    但有技巧;
    比如以下矩阵
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    应该一列一列地减比较快;

    1 1 1 1
    1 1 1 1
    一行一行地删比较快;
    所以对n和m的大小关系分类讨论一下;
    看是先删行还是先删列;

    【Number Of WA

    4

    【反思】

    一开始不知道在想什么,光想着快点写完了,结果写了个没任何把握的贪心,一直瞎试。
    没想到多试几个输入hack一下自己;太着急了;
    列的英文是colum/xk

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 110;
    
    int n,m,ans = 0;
    int g[N][N],srow[N],sclu[N];
    vector <int> row,col;
    
    void subrow(){
        rep1(i,1,n){
            int mi = 1000;
            rep1(j,1,m){
                mi = min(mi,g[i][j]);
            }
            rep1(j,1,m)
                g[i][j]-=mi;
            srow[i] = mi;
            ans+=mi;
        }
    }
    
    void subclu(){
        rep1(j,1,m){
            int mi = 1000;
            rep1(i,1,n){
                mi = min(mi,g[i][j]);
            }
            ans+=mi;
            rep1(i,1,n)
                g[i][j]-=mi;
            sclu[j] = mi;
        }
    }
    
    int main(){
        //Open();
        Close();
        cin >> n >> m;
        rep1(i,1,n){
            rep1(j,1,m)
                cin >> g[i][j];
        }
        if (n < m){
            subrow();
            subclu();
        }else {
            subclu();
            subrow();
        }
        rep1(i,1,n)
            rep1(j,1,m)
                if (g[i][j])
                    return cout <<-1<<endl,0;
        cout << ans << endl;
        rep1(i,1,n){
            while (srow[i]--){
                cout <<"row "<<i<<endl;
            }
        }
        rep1(j,1,m){
            while (sclu[j]--){
                cout <<"col "<<j<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    华为实习日记——第二十三天
    华为实习日记——第二十二天
    华为实习日记——第二十一天
    华为实习日记——第二十天
    HDU 5102 The K-th Distance(模拟)
    HDU 4113 Construct the Great Wall(插头dp)
    UVALive 4849 String Phone(2-sat、01染色)
    HDU 4859 海岸线(最大流最小割)
    HDU 3879 Base Station(最大权闭合子图)
    POJ 3155 Hard Life(最大密度子图)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626245.html
Copyright © 2011-2022 走看看