您的位置:首页 > 其它

Codeforces Round #419 (Div. 2) C.Karen and Game 思维

2017-06-18 18:46 603 查看
传送门

题意:

给你一个矩阵 问你是不是能化为0矩阵。 每次可以使列或者行减少1。 最后输出操作次数并输出过程。

做法:

取列行最小的一个数。 整个列行都减这个数就好啦。判断一下减了多少次((s != cc * n + rr * m))

具体看代码就知道啦。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>
using namespace std;

const int N = 105;
int n, m;
int A

;
int r
, c
;
int doRow()
{
int ans = 0;
for (int i = 0; i < n; ++i)
{
int miv = 1000;
for (int j = 0; j < m; ++j)
miv = std::min(miv, A[i][j]);
r[i]=miv;
ans += r[i];
// cout<<ans<<endl;
for (int j = 0; j < m; ++j)
A[i][j] -= r[i];
}
return ans;
}
int doCol()
{
int ans = 0;
for (int j = 0; j < m; ++j)
{
int miv = 1000;
for (int i = 0; i < n; ++i)
miv = std::min(miv, A[i][j]);
ans += (c[j] = miv);
for (int i = 0; i < n; ++i)
A[i][j] -= c[j];
}
return ans;
}
int main()
{
scanf("%d%d", &n, &m);
int s = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
{
scanf("%d", &A[i][j]);
s += A[i][j];
}
int cc, rr;
if (n > m)
{
cc = doCol();
rr = doRow();
}
else
{
rr = doRow();
cc = doCol();
}
// cc=doCol();
// rr=doRow();
if (s != cc * n + rr * m) return puts("-1"), 0;
printf("%d\n", cc + rr);
for (int i = 0; i < n; ++i)
while (r[i]--) printf("row %d\n", i + 1);
for (int j = 0; j < m; ++j)
while (c[j]--) printf("col %d\n", j + 1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: