#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e5+5,mod=1e9+7,Inf=10000;
int t,n,a[1050][15],dp[1005][1050],f[N],cost[N],r;
main(){
cin>>n>>r;
for(int i=1;i<=n;i++){
for(int j=0;j<r;j++) {
char c;
cin>>c;
if(c == '#') a[i][j] = 1;
}
}
for(int j=0;j<=n;j++)
for(int i=0;i<(1<<r);i++){
dp[j][i] = Inf;
}
dp[0][0] = 0;
int ans = Inf;
for(int i=1;i<=n;i++) {
int mask = 0;
for(int j=0;j<r;j++) {
mask +=(1<<j)*a[i][j];
}
f[0] = 1;
for(int m = mask;;m=mask&(m-1)) {
f[m] = 1; cost[m] = 0;
for(int k=0;k<r;k++){
int j = k;
if((1<<k)&m) {
cost[m]++;
while((1<<(j+1))&m) j++;
k = j;
}
}
if(!m) break;
}
for(int j=0;j<(1<<r);j++) {
if(!f[j]) continue;
for(int m=j;;m=(m-1)&j) {
dp[i][j] = min(dp[i][j],dp[i-1][m] + cost[mask^j] + __builtin_popcount(j^m));
if(i == n) ans = min(ans,dp[i][j]);
if(!m) break;
}
f[j] = 0;
}
for(int j=0;j<(1<<r);j++) {
for(int m = j;;m=j&(m-1)) {
dp[i][m] = min(dp[i][j],dp[i][m]);
if(!m) break;
}
}
}
cout<<ans;
}
Compilation message
Main.cpp:7:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
7 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
92 ms |
4152 KB |
Output is correct |
3 |
Correct |
14 ms |
4172 KB |
Output is correct |
4 |
Correct |
34 ms |
4272 KB |
Output is correct |
5 |
Correct |
95 ms |
4300 KB |
Output is correct |
6 |
Correct |
101 ms |
4428 KB |
Output is correct |
7 |
Correct |
95 ms |
4340 KB |
Output is correct |
8 |
Correct |
87 ms |
4172 KB |
Output is correct |
9 |
Correct |
88 ms |
4180 KB |
Output is correct |
10 |
Correct |
230 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
296 KB |
Output is correct |
3 |
Correct |
2 ms |
304 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
368 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
92 ms |
4152 KB |
Output is correct |
3 |
Correct |
14 ms |
4172 KB |
Output is correct |
4 |
Correct |
34 ms |
4272 KB |
Output is correct |
5 |
Correct |
95 ms |
4300 KB |
Output is correct |
6 |
Correct |
101 ms |
4428 KB |
Output is correct |
7 |
Correct |
95 ms |
4340 KB |
Output is correct |
8 |
Correct |
87 ms |
4172 KB |
Output is correct |
9 |
Correct |
88 ms |
4180 KB |
Output is correct |
10 |
Correct |
230 ms |
4480 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
296 KB |
Output is correct |
13 |
Correct |
2 ms |
304 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
368 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
4 ms |
4300 KB |
Output is correct |
20 |
Correct |
18 ms |
4428 KB |
Output is correct |
21 |
Correct |
39 ms |
4488 KB |
Output is correct |
22 |
Correct |
94 ms |
4300 KB |
Output is correct |
23 |
Correct |
92 ms |
4300 KB |
Output is correct |
24 |
Correct |
91 ms |
4300 KB |
Output is correct |
25 |
Correct |
98 ms |
4300 KB |
Output is correct |
26 |
Correct |
137 ms |
4236 KB |
Output is correct |
27 |
Correct |
224 ms |
4232 KB |
Output is correct |
28 |
Correct |
343 ms |
4300 KB |
Output is correct |