#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e3,M=10;
const int INF=1e9+7;
int dp[N+10][(1<<M)+10];
int cons(int msk)
{
int ans=0;
while(msk>0)
{
ans++;
while(msk%2==0)
msk/=2;
while(msk%2==1)
msk/=2;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
dp[0][0]=0;
for(int j=1;j<(1<<m);j++)
dp[0][j]=INF;
for(int i=1;i<=n;i++)
{
int row=0;
for(int j=1;j<=m;j++)
{
char c;
cin>>c;
if(c=='#')
row^=(1<<(m-j));
}
for(int j=0;j<(1<<m);j++)
dp[i][j]=dp[i-1][j];
for(int b=0;b<m;b++)
{
for(int j=0;j<(1<<m);j++)
{
if(j&(1<<b))
dp[i][j^(1<<b)]=min(dp[i][j^(1<<b)],dp[i][j]);
}
}
for(int b=0;b<m;b++)
{
for(int j=0;j<(1<<m);j++)
{
if(j&(1<<b))
dp[i][j]=min(dp[i][j],dp[i][j^(1<<b)]+1);
}
}
for(int j=0;j<(1<<m);j++)
{
if((j&row)!=j)
dp[i][j]=INF;
else
dp[i][j]+=cons(row^j);
}
}
int ans=INF;
for(int j=0;j<(1<<m);j++)
ans=min(ans,dp[n][j]);
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
31 ms |
3948 KB |
Output is correct |
3 |
Correct |
9 ms |
4028 KB |
Output is correct |
4 |
Correct |
21 ms |
4164 KB |
Output is correct |
5 |
Correct |
32 ms |
4264 KB |
Output is correct |
6 |
Correct |
33 ms |
4244 KB |
Output is correct |
7 |
Correct |
32 ms |
4188 KB |
Output is correct |
8 |
Correct |
31 ms |
3920 KB |
Output is correct |
9 |
Correct |
31 ms |
3984 KB |
Output is correct |
10 |
Correct |
42 ms |
4324 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 |
332 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 |
316 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 |
31 ms |
3948 KB |
Output is correct |
3 |
Correct |
9 ms |
4028 KB |
Output is correct |
4 |
Correct |
21 ms |
4164 KB |
Output is correct |
5 |
Correct |
32 ms |
4264 KB |
Output is correct |
6 |
Correct |
33 ms |
4244 KB |
Output is correct |
7 |
Correct |
32 ms |
4188 KB |
Output is correct |
8 |
Correct |
31 ms |
3920 KB |
Output is correct |
9 |
Correct |
31 ms |
3984 KB |
Output is correct |
10 |
Correct |
42 ms |
4324 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 |
332 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 |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
4172 KB |
Output is correct |
20 |
Correct |
10 ms |
4300 KB |
Output is correct |
21 |
Correct |
17 ms |
4256 KB |
Output is correct |
22 |
Correct |
32 ms |
4180 KB |
Output is correct |
23 |
Correct |
32 ms |
4104 KB |
Output is correct |
24 |
Correct |
39 ms |
4168 KB |
Output is correct |
25 |
Correct |
41 ms |
4164 KB |
Output is correct |
26 |
Correct |
39 ms |
4016 KB |
Output is correct |
27 |
Correct |
45 ms |
4108 KB |
Output is correct |
28 |
Correct |
50 ms |
4168 KB |
Output is correct |