#include <bits/stdc++.h>
using namespace std;
#define int long long
int val[505][505], pr[505][505], pc[505][505];
int calc(int x, int y, int length, int depth)
{
return pc[x+length-1][y+depth-1]-pc[x-1][y+depth-1]-pc[x+length-1][y-1]+pc[x-1][y-1];
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, a, b; cin>>n>>m>>a>>b;
int ans=2e18;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
cin>>val[i][j];
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
pr[i][j]=pr[i][j-1]+val[i][j];
}
}
for(int j=1; j<=m; j++)
{
for(int i=1; i<=n; i++)
{
pc[i][j]+=pc[i-1][j]+pr[i][j];
}
}
for(int i=1; i<=n; i++)
{
for(int l=1; l<=m; l++)
{
for(int length=1; length+l-1<=m; length++)
{
int s=0;
for(int j=8; j>=0; j--)
{
int tmp=s+(1<<j);
if(tmp+i-1>n) continue;
if(calc(i, l, length, tmp)<=b) s=tmp;
}
int d=calc(i, l, length, s);
ans=min(ans, abs(b-d)+abs(a-d));
s=n-i+1;
for(int j=8; j>=0; j--)
{
int tmp=s-(1<<j);
if(tmp<1) continue;
if(calc(i, l, length, tmp)>=a) s=tmp;
}
d=calc(i, l, length, s);
ans=min(ans, abs(b-d)+abs(a-d));
}
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |