Codeforces Round #419 (Div. 1), problem: (A) Karen and Game Solution In C/C++

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int const N = 111;
int g[N][N], r[N], c[N], e[N], f[N];
int main() {
int n, m; scanf(“%d%d”, &n, &m);
rep(i, n) rep(j, m) scanf(“%d”, &g[i][j]);
int ans = inf, q;
rep(t, g[0][0] + 1) {
r[0] = t, q = 0;
rep(j, m) c[j] = g[0][j] – t, q += c[j];
rep(i, n) r[i] = g[i][0] – c[0], q += r[i];
bool fail = 0;
rep(i, n) rep(j, m) if (r[i] < 0 || c[j] < 0 || g[i][j] != r[i] + c[j]) fail = 1;
if (fail) continue;
if (q < ans) {
ans = q;
rep(i, n) e[i] = r[i];
rep(j, m) f[j] = c[j];
}
}
if (ans != inf) {
printf(“%d\n”, ans);
rep(i, n) rep(j, e[i]) printf(“row %d\n”, i + 1);
rep(i, m) rep(j, f[i]) printf(“col %d\n”, i + 1);
} else puts(“-1”);
return 0;
}

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int const N = 111;
int g[N][N], r[N], c[N], e[N], f[N];
int main() {
int n, m; scanf(“%d%d”, &n, &m);
rep(i, n) rep(j, m) scanf(“%d”, &g[i][j]);
int ans = inf, q;
rep(t, g[0][0] + 1) {
r[0] = t, q = 0;
rep(j, m) c[j] = g[0][j] – t, q += c[j];
rep(i, n) r[i] = g[i][0] – c[0], q += r[i];
bool fail = 0;
rep(i, n) rep(j, m) if (r[i] < 0 || c[j] < 0 || g[i][j] != r[i] + c[j]) fail = 1;
if (fail) continue;
if (q < ans) {
ans = q;
rep(i, n) e[i] = r[i];
rep(j, m) f[j] = c[j];
}
}
if (ans != inf) {
printf(“%d\n”, ans);
rep(i, n) rep(j, e[i]) printf(“row %d\n”, i + 1);
rep(i, m) rep(j, f[i]) printf(“col %d\n”, i + 1);
} else puts(“-1”);
return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Stop Buying Cheap Alternatives This Viral Kitchen Gadgets Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Do crabs foam at the mouth?

If you have ever spent time exploring coastal shores, tide pools, or beaches, you may have encountered a crab producing a thick cluster of...

The Real Truth About Viral Kitchen Gadgets Before You Buy

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!