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>
using namespace std;
int m,n,res=1e9,c0[1000005],c1[1000005],s[2][155][5005],s1[2][3][1000005];
int Sum(int t,int u1,int v1,int u2,int v2)
{
return s[t][u2][v2]-s[t][u1-1][v2]-s[t][u2][v1-1]+s[t][u1-1][v1-1];
}
void Sub1()
{
int x;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
cin>>x;
s[0][i][j]+=s[0][i-1][j]+s[0][i][j-1]-s[0][i-1][j-1]+(x==0);
s[1][i][j]+=s[1][i-1][j]+s[1][i][j-1]-s[1][i-1][j-1]+(x==1);
}
for(int i=1;i<=m;i++)
for(int j=i;j<=m;j++)
{
int add=Sum(1,1,1,i-1,n)+Sum(1,j+1,1,m,n);
for(int t=1;t<=n;t++)
{
c0[t]=c0[t-1]+Sum(0,i,t,j,t);
c1[t]=c1[t-1]+Sum(1,i,t,j,t);
}
int minn=1e9;
for(int t=n;t>=1;t--)
{
minn=min(minn,c0[t]-c1[t]);
res=min(res,c1[t-1]+c1[n]-c0[t-1]+minn+add);
}
}
cout<<res;
}
int Sum1(int t,int u1,int v1,int u2,int v2)
{
return s1[t][u2][v2]-s1[t][u1-1][v2]-s1[t][u2][v1-1]+s1[t][u1-1][v1-1];
}
void Sub2()
{
int x;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
cin>>x;
s1[0][i][j]+=s1[0][i-1][j]+s1[0][i][j-1]-s1[0][i-1][j-1]+(x==0);
s1[1][i][j]+=s1[1][i-1][j]+s1[1][i][j-1]-s1[1][i-1][j-1]+(x==1);
}
for(int i=1;i<=m;i++)
for(int j=i;j<=m;j++)
{
int add=Sum1(1,1,1,i-1,n)+Sum1(1,j+1,1,m,n);
for(int t=1;t<=n;t++)
{
c0[t]=c0[t-1]+Sum1(0,i,t,j,t);
c1[t]=c1[t-1]+Sum1(1,i,t,j,t);
}
int minn=1e9;
for(int t=n;t>=1;t--)
{
minn=min(minn,c0[t]-c1[t]);
res=min(res,c1[t-1]+c1[n]-c0[t-1]+minn+add);
}
}
cout<<res;
}
int main()
{
ios_base::sync_with_stdio(false);
//freopen("TEST.INP","r",stdin);
cin>>m>>n;
if(m<=150&&n<=5000)
Sub1();
else
Sub2();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |