# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
367824 | phathnv | Selotejp (COCI20_selotejp) | C++11 | 42 ms | 512 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |