#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll cnt(ll x, ll h, ll w, ll c){
return x - h - w + c;
}
ll dis(ll a, ll b, ll c){
return abs(a - c) + abs(b - c);
}
int main(){
int r, s; ll a, b; cin >> r >> s >> a >> b;
ll c[r + 1][s + 1] = {{0}}; for(int i = 1; i <= r; i++) for(int j = 1; j <= s; j++) cin >> c[i][j];
for(int i = 1; i <= r; i++){
for(int j = 1; j <= s; j++){
c[i][j] += c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1];
}
}
ll ans = LLONG_MAX;
for(int i = 1; i <= r; i++){
for(int j = 1; j <= s; j++){
for(int h = 0; h < i; h++){
int l = 0, r = j - 1;
while(l <= r){
int md = (l + r) / 2;
if(cnt(c[i][j], c[h][j], c[i][md], c[h][md]) < a){
r = md - 1;
}else{
l = md + 1;
}
ans = min(ans, dis(a, b, cnt(c[i][j], c[h][j], c[i][md], c[h][md])));
}
// cout << i << ' ' << j << ": " << ans << endl;
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |