题目链接: http://codeforces.com/problemset/problem/815/A
题目描述: 一个初始都为0的n * m 的格子, 你做的操作只有两种, 给一行+1, 给一列加一, 问给出你最终状态, 输出最少步数和步骤
解题思路: 简单贪心即可, 自己一开始没有讨论n, m大小关系, WA了一发
代码:
#include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator> #include <cmath> #include <algorithm> #include <stack> #include <deque> #include <map> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) typedef long long ll; using namespace std; const int INF = 0x3fffffff; const int maxn = 510; int g[maxn][maxn]; int row[maxn]; int t1[maxn]; int col[maxn]; int t2[maxn]; int main() { int n, m; int sum = 0; mem0(g); mem0(row); mem0(col); mem0(t1); mem0(t2); scanf( "%d%d", &n, &m ); for( int i = 1; i <= n; i++ ) { for( int j = 1; j <= m; j++ ) { scanf( "%d", &g[i][j] ); } } int cnt = 0; int cnt2 = 0; if( n > m ) { for( int j = 1; j <= m; j++ ) { int minum = INF; for( int i = 1; i <= n; i++ ) { minum = min( minum, g[i][j] ); } sum += minum; if( minum > 0 ) { col[cnt2] = j; t2[cnt2++] = minum; } for( int i = 1; i <= n; i++ ) { g[i][j] -= minum; } } for( int i = 1; i <= n; i++ ) { int minum = INF; for( int j = 1; j <= m; j++ ) { minum = min( minum, g[i][j] ); } sum += minum; if( minum > 0 ) { row[cnt] = i; t1[cnt++] = minum; } for( int j = 1; j <= m; j++ ) { g[i][j] -= minum; } } } else { for( int i = 1; i <= n; i++ ) { int minum = INF; for( int j = 1; j <= m; j++ ) { minum = min( minum, g[i][j] ); } sum += minum; if( minum > 0 ) { row[cnt] = i; t1[cnt++] = minum; } for( int j = 1; j <= m; j++ ) { g[i][j] -= minum; } } for( int j = 1; j <= m; j++ ) { int minum = INF; for( int i = 1; i <= n; i++ ) { minum = min( minum, g[i][j] ); } sum += minum; if( minum > 0 ) { col[cnt2] = j; t2[cnt2++] = minum; } for( int i = 1; i <= n; i++ ) { g[i][j] -= minum; } } } for( int i = 1; i <= n; i++ ) { for( int j = 1; j <= m; j++ ) { if( g[i][j] != 0 ) { printf( "-1 " ); return 0; } } } printf( "%d ", sum ); for( int i = 0; i < cnt; i++ ) { while( t1[i]-- ) { printf( "row %d ", row[i] ); } } for( int i = 0; i < cnt2; i++ ) { while( t2[i]-- ) { printf( "col %d ", col[i] ); } } return 0; }
思考: 这是一场比赛, 我只想出来了A题, B题一直想出来, 一会儿补