Submission #367824

#TimeUsernameProblemLanguageResultExecution timeMemory
367824phathnvSelotejp (COCI20_selotejp)C++11
110 / 110
42 ms512 KiB
#include <bits/stdc++.h> #define mp make_pair #define X first #define Y second #define taskname "Selotejp" using namespace std; typedef long long ll; typedef pair <int, int> ii; const int M = 1001; const int N = 10; const int INF = 1e9; void minimize(int &x, int y){ if (x > y) x = y; } int m, n, closedInRow[M], dp[2][1 << N], f[1 << N], cost[1 << N]; void readInput(){ cin >> m >> n; for(int i = 1; i <= m; i++){ string s; cin >> s; for(int j = 0; j < n; j++) if (s[j] == '#') closedInRow[i] |= (1 << j); } } void solve(){ for(int mask = 0; mask < (1 << n); mask++){ cost[mask] = 0; for(int i = 0; i < n; i++){ cost[mask] += (mask >> i) & 1; if (i > 0) cost[mask] -= (mask >> (i - 1)) & (mask >> i) & 1; } } for(int id = 0; id < 2; id++) for(int mask = 0; mask < (1 << n); mask++) dp[id][mask] = INF; dp[0][0] = 0; for(int i = 1; i <= m; i++){ int id = i & 1; for(int mask = 0; mask < (1 << n); mask++) f[mask] = dp[id][mask] = INF; for(int mask = 0; mask < (1 << n); mask++) minimize(f[mask & closedInRow[i]], dp[id ^ 1][mask]); for(int mask = 0; mask < (1 << n); mask++) for(int j = 0; j < n; j++) minimize(f[mask | (1 << j)], f[mask] + 1); for(int mask = (1 << n) - 1; mask >= 0; mask--) for(int j = 0; j < n; j++) minimize(f[mask & (~(1 << j))], f[mask]); for(int mask = 0; mask < (1 << n); mask++){ if ((closedInRow[i] & mask) != mask) continue; dp[id][mask] = f[mask] + cost[closedInRow[i] ^ mask]; } } int res = INF; for(int mask = 0; mask < (1 << n); mask++) res = min(res, dp[m & 1][mask]); cout << res; } int main(){ if (fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); freopen(taskname".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(nullptr); readInput(); solve(); return 0; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   78 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   79 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...