#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define INF (ll)1e18
#define mod 998244353
#define maxn 1010
#define maxm 11
#define maxb 1034
ll i, i1, j, k, k1, t, n, m, res, flag[10], a, b;
ll mk0, mk1, mk2, mka, dp[maxn][maxb], x;
char mt[maxn][maxm];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
cin >> n >> m;
for (i = 1; i <= n; i++) {
for (j = 0; j <= m - 1; j++) {
cin >> mt[i][j];
}
}
for (i = 0; i <= n; i++) {
for (mk1 = 0; mk1 < (1 << m); mk1++) {
dp[i][mk1] = INF;
}
}
dp[0][0] = 0;
for (i = 1; i <= n; i++) {
mk0 = 0;
for (j = 0; j <= m - 1; j++) {
if (mt[i][j] == '#') mk0 += (1 << j);
}
for (mk1 = mk0;; mk1 = (mk1 - 1) & mk0) {
mka = (mk0 & (~mk1)); k = 0;
for (j = 0; j <= m - 1; j++) {
if (((mka >> j) & 1) == 0) continue;
if (j == 0 || ((mka >> (j - 1)) & 1) == 0) k++;
}
for (mk2 = mk1;; mk2 = (mk2 - 1) & mk1) {
x = __builtin_popcountll(mk1) - __builtin_popcountll(mk1 & mk2);
x += dp[i - 1][mk2];
dp[i][mk1] = min(dp[i][mk1], k + x);
if (mk2 == 0) break;
}
if (mk1 == 0) break;
}
for (mk1 = mk0;; mk1 = (mk1 - 1) & mk0) {
for (mk2 = mk1;; mk2 = (mk2 - 1) & mk1) {
dp[i][mk2] = min(dp[i][mk2], dp[i][mk1]);
if (mk2 == 0) break;
}
if (mk1 == 0) break;
}
}
/* for (i = 0; i <= n; i++) {
for (mk1 = 0; mk1 < (1 << m); mk1++) {
cout << dp[i][mk1] << ' ';
}
cout << nl;
} */
cout << dp[n][0] << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
6 ms |
7756 KB |
Output is correct |
3 |
Correct |
4 ms |
5836 KB |
Output is correct |
4 |
Correct |
6 ms |
8108 KB |
Output is correct |
5 |
Correct |
7 ms |
8308 KB |
Output is correct |
6 |
Correct |
8 ms |
8340 KB |
Output is correct |
7 |
Correct |
7 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
7756 KB |
Output is correct |
9 |
Correct |
6 ms |
7756 KB |
Output is correct |
10 |
Correct |
203 ms |
8420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
6 ms |
7756 KB |
Output is correct |
3 |
Correct |
4 ms |
5836 KB |
Output is correct |
4 |
Correct |
6 ms |
8108 KB |
Output is correct |
5 |
Correct |
7 ms |
8308 KB |
Output is correct |
6 |
Correct |
8 ms |
8340 KB |
Output is correct |
7 |
Correct |
7 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
7756 KB |
Output is correct |
9 |
Correct |
6 ms |
7756 KB |
Output is correct |
10 |
Correct |
203 ms |
8420 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
4428 KB |
Output is correct |
20 |
Correct |
6 ms |
6220 KB |
Output is correct |
21 |
Correct |
10 ms |
8244 KB |
Output is correct |
22 |
Correct |
5 ms |
8136 KB |
Output is correct |
23 |
Correct |
5 ms |
8140 KB |
Output is correct |
24 |
Correct |
6 ms |
8012 KB |
Output is correct |
25 |
Correct |
14 ms |
8268 KB |
Output is correct |
26 |
Correct |
72 ms |
7932 KB |
Output is correct |
27 |
Correct |
196 ms |
7948 KB |
Output is correct |
28 |
Correct |
375 ms |
8212 KB |
Output is correct |