果然暴力出奇迹。。 O(n^2m^2)=1e8 536ms能过。
枚举锤子的长和宽,再验证是否可以满足条件并更新答案。
我们先从左上角为(1,1)的先锤,显然锤的次数是a[1][1]. 锤(i,j)的时候呢,算一下右下角为(i,j)的锤数组的矩形面积,然后更新(i,j)的值。
用二维前缀和可以做到O(1).
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi 3.1415926535 # define eps 1e-9 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())==‘-‘) flag=1; else if(ch>=‘0‘&&ch<=‘9‘) res=ch-‘0‘; while((ch=getchar())>=‘0‘&&ch<=‘9‘) res=res*10+(ch-‘0‘); return flag?-res:res; } void Out(int a) { if(a<0) {putchar(‘-‘); a=-a;} if(a>=10) Out(a/10); putchar(a%10+‘0‘); } const int N=105; //Code begin... int a[N][N], sum[N][N]; int get_sum(int x, int y, int n, int m){ return sum[x][y]-(x>=n?sum[x-n][y]:0)-(y>=m?sum[x][y-m]:0)+(x>=n&&y>=m?sum[x-n][y-m]:0); } int main () { int n, m, ans=INF, flag; scanf("%d%d",&n,&m); FOR(i,1,n) FOR(j,1,m) scanf("%d",&a[i][j]); FOR(l,1,n) FOR(r,1,m) { flag=0; FOR(i,1,n) FOR(j,1,m) { int d=get_sum(i,j-1,l,r-1)+get_sum(i-1,j,l-1,r)-get_sum(i-1,j-1,l-1,r-1); if (d>a[i][j]) {flag=-1; break;} if ((i>n-l+1||j>m-r+1)&&d!=a[i][j]) {flag=-1; break;} flag+=(a[i][j]-d); sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(a[i][j]-d); } if (flag!=-1) ans=min(ans,flag); } printf("%d\n",ans); return 0; }
时间: 2024-10-08 18:29:20