LOJ Chen 6030. "Yali training 2017 Day1" matrix (greedy construction)

meaning of the title link Sol I don't know how to make the series It is not difficult to observe several properties: The best strategy must be to...
meaning of the title
Sol

meaning of the title

link

Sol

I don't know how to make the series

It is not difficult to observe several properties:

  1. The best strategy must be to black a row first, and then use this row to cover columns that are not all black
  2. No solution if and only if there is no black. Otherwise, the row \ (i \) where the first black is located can first make a black in column \ (i \), and then the black in column \ (i \) can make all the rows \ (i \) black.

Then calculate the steps of blackening each line directly and take a min ute.

There are comments in the code.

#include<bits/stdc++.h> #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) #define fi first #define se second #define LL long long #define ull unsigned long long #define Fin(x) #define Fout(x) using namespace std; const int MAXN = 1001, INF = 1e9 + 1, mod = 1e9 + 7; const double eps = 1e-9, pi = acos(-1); template <typename A, typename B> inline bool chmin(A &a, B b) return 0;} template <typename A, typename B> inline bool chmax(A &a, B b) return 0;} template <typename A, typename B> inline LL add(A x, B y) template <typename A, typename B> inline void add2(A &x, B y) template <typename A, typename B> inline LL mul(A x, B y) template <typename A, typename B> inline void mul2(A &x, B y) inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N; char s[MAXN][MAXN]; int FullRow[MAXN], HaveRow[MAXN], FullCol[MAXN], HaveCol[MAXN];//full: whether each row and column is all 1 / have: whether there is at least one 1 signed main() { N = read(); for(int i = 1; i <= N; i++) scanf("%s", s[i] + 1); bool flag = 0; fill(FullRow + 1, FullRow + N + 1, 1); fill(FullCol + 1, FullCol + N + 1, 1); for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) if(s[i][j] == '#') flag = 1, HaveRow[i] = 1, HaveCol[j] = 1; else FullRow[i] = 0, FullCol[j] = 0; if(!flag) return puts("-1"), 0; int ans = INF; for(int i = 1; i <= N; i++) { int now = 0; if(FullRow[i]) { for(int j = 1; j <= N; j++) now += (!FullCol[j]);//If line i is all black, add the number of columns that are not black to the answer chmin(ans, now); continue; } if(!HaveCol[i]) { if(!HaveRow[i]) continue; else now++;//Use a black of the row to color the corresponding column } for(int j = 1; j <= N; j++) { if(FullCol[j]) continue; if(s[i][j] == '.') now += 2; else now++; } chmin(ans, now); } cout << ans; return 0; }

3 December 2019, 14:02 | Views: 3054

Add new comment

For adding a comment, please log in
or create account

0 comments