#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define all(x) x.begin(),x.end()
const ll mxn=505;
ll pre[mxn][mxn];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n,m;
ll a,b;
cin>>n>>m>>a>>b;
if(a>b) swap(a,b);
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
cin>>pre[i][j];
pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
}
}
ll ans=1e18;
for(ll i=1;i<=n;i++){
for(ll j=i;j<=n;j++){
vector<ll> vec;
for(ll k=1;k<=m;k++){
vec.push_back(pre[j][k]-pre[i-1][k]);
}
for(ll k=0;k<m;k++){
ll sum=pre[j][k]-pre[i-1][k];
ll aa=sum+a;
ll bb=sum+b;
auto it1=lower_bound(all(vec),aa);
auto it2=upper_bound(all(vec),bb);
it2--;
if(it1<=it2){
cout<<b-a;
return 0;
}
if(it1-vec.begin()>=0 and it1-vec.begin()<n){
ans=min(ans,abs((*it1)-a)+abs((*it1)-b));
}
if(it2-vec.begin()>=0 and it2-vec.begin()<n){
ans=min(ans,abs((*it2)-a)+abs((*it2)-b));
}
}
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |