//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
int n;int m;
string tgr[1010];
int dp[1010][3010];
int gr[1010];
int col[3010];
int sme[3010][3010];
int bts[3010];
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>tgr[i];
}
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
if(tgr[i][j]=='#'){
gr[i]+=(1<<j);
}
}
}
for(int i=0;i<(1<<m);i++){
for(int j=0;j<m;j++){
if( ((i&(1<<j))!=0) ){
col[i]++;
if(j>0&&((i&(1<<(j-1)))!=0)){
col[i]--;
}
bts[i]++;
}
}
}
for(int a=0;a<(1<<m);a++){
for(int b=0;b<(1<<m);b++){
for(int j=0;j<m;j++){
if( ((a&(1<<j))!=0)&&((b&(1<<j))!=0) ){
sme[a][b]++;
}
}
}
}
for(int i=0;i<=n+1;i++){
for(int j=0;j<(1<<m)+10;j++){
dp[i][j]=1e9;
}
}
dp[0][0]=0;
for(int i=1;i<=n+1;i++){
for(int mask=0;mask<(1<<m);mask++){
if(((mask|gr[i])!=gr[i])){continue;}
for(int mskb=0;mskb<(1<<m);mskb++){
dp[i][mask]=min(dp[i][mask],dp[i-1][mskb]+bts[mask]-sme[mask][mskb]+col[gr[i]^mask]);
}
}
}
cout<<dp[n+1][0]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2004 KB |
Output is correct |
2 |
Correct |
50 ms |
23808 KB |
Output is correct |
3 |
Correct |
9 ms |
7508 KB |
Output is correct |
4 |
Correct |
20 ms |
12284 KB |
Output is correct |
5 |
Correct |
58 ms |
24576 KB |
Output is correct |
6 |
Correct |
72 ms |
24628 KB |
Output is correct |
7 |
Correct |
76 ms |
24324 KB |
Output is correct |
8 |
Correct |
35 ms |
23744 KB |
Output is correct |
9 |
Correct |
46 ms |
23848 KB |
Output is correct |
10 |
Correct |
752 ms |
24784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
12756 KB |
Output is correct |
2 |
Correct |
29 ms |
12724 KB |
Output is correct |
3 |
Correct |
27 ms |
12680 KB |
Output is correct |
4 |
Correct |
31 ms |
12696 KB |
Output is correct |
5 |
Correct |
25 ms |
12688 KB |
Output is correct |
6 |
Correct |
28 ms |
12776 KB |
Output is correct |
7 |
Correct |
4 ms |
1876 KB |
Output is correct |
8 |
Correct |
33 ms |
12680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2004 KB |
Output is correct |
2 |
Correct |
50 ms |
23808 KB |
Output is correct |
3 |
Correct |
9 ms |
7508 KB |
Output is correct |
4 |
Correct |
20 ms |
12284 KB |
Output is correct |
5 |
Correct |
58 ms |
24576 KB |
Output is correct |
6 |
Correct |
72 ms |
24628 KB |
Output is correct |
7 |
Correct |
76 ms |
24324 KB |
Output is correct |
8 |
Correct |
35 ms |
23744 KB |
Output is correct |
9 |
Correct |
46 ms |
23848 KB |
Output is correct |
10 |
Correct |
752 ms |
24784 KB |
Output is correct |
11 |
Correct |
27 ms |
12756 KB |
Output is correct |
12 |
Correct |
29 ms |
12724 KB |
Output is correct |
13 |
Correct |
27 ms |
12680 KB |
Output is correct |
14 |
Correct |
31 ms |
12696 KB |
Output is correct |
15 |
Correct |
25 ms |
12688 KB |
Output is correct |
16 |
Correct |
28 ms |
12776 KB |
Output is correct |
17 |
Correct |
4 ms |
1876 KB |
Output is correct |
18 |
Correct |
33 ms |
12680 KB |
Output is correct |
19 |
Correct |
3 ms |
4692 KB |
Output is correct |
20 |
Correct |
14 ms |
7964 KB |
Output is correct |
21 |
Correct |
35 ms |
12564 KB |
Output is correct |
22 |
Correct |
36 ms |
24472 KB |
Output is correct |
23 |
Correct |
37 ms |
24268 KB |
Output is correct |
24 |
Correct |
61 ms |
24168 KB |
Output is correct |
25 |
Correct |
105 ms |
24524 KB |
Output is correct |
26 |
Correct |
400 ms |
24048 KB |
Output is correct |
27 |
Correct |
851 ms |
24068 KB |
Output is correct |
28 |
Execution timed out |
1044 ms |
24396 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |