P2484 [SDOI2011]打地鼠
差分
代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 1999999999
int n,m,sum,a[200][200],b[200][200],c[200][200];
int main(){
int minn=INF;
cin>>n>>m;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
cin>>a[i][j],sum+=a[i][j];
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (sum%(i*j)==0)
{
memset(b,0,sizeof(b)); memset(c,0,sizeof(c));
int num=0;
for (int i1=1;i1<=n;i1++)
for (int j1=1;j1<=m;j1++)
{
c[i1][j1]=c[i1-1][j1]+c[i1][j1-1]-c[i1-1][j1-1]+b[i1][j1];
if (a[i1][j1]<c[i1][j1])
{
goto ll;
} else
if (a[i1][j1]==c[i1][j1])
{
continue;
}
{
if (i+i1-1>n || j+j1-1>m)
{
goto ll;
}
int tmp=a[i1][j1]-c[i1][j1];
b[i1+i][j1]-=tmp;
b[i1][j1+j]-=tmp;
b[i1+i][j1+j]+=tmp;
c[i1][j1]+=tmp;
num+=tmp;
}
}
minn=min(minn,num);
ll:;
}
cout<<minn;
}