This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
using namespace std;
#define minimize(a,b) a=min(a,b)
int64_t ans = 1e18;
int n, m, a, b;
int x;
int64_t ps[503][503];
int64_t s[503];
int64_t toQ(int64_t x){
// cout << x << '\n';
return abs(x-a) + abs(x-b);
}
signed main()
{
cin.tie(0) -> sync_with_stdio(0);
// if(fopen("DAUTU.inp", "r")){
// freopen("DAUTU.inp", "r",stdin);
// freopen("DAUTU.out", "w",stdout);
// }
cin >> n >> m >> a >> b; if(a>b)swap(a,b);
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j){
cin >> x;
ps[i][j] = ps[i][j-1]+x;
}
// cout << ps[3][1] << '\n';
for(int l=1; l<=n; ++l)
for(int r=l; r<=m; ++r){
for(int i=1; i<=n; ++i) s[i] = s[i-1]+ps[i][r] - ps[i][l-1];
// cout << l << ' ' << r << '\n';
for(int i=1; i<=n; ++i){
// s[i]-b<s[i]-a
int c = lower_bound(s+1,s+i,s[i]-b) - s,
d = upper_bound(s+1,s+i,s[i]-a) - s;
// cout << c << ' ' << d << ' ' << i << " ~ " << s[i] << ' ' << s[i]-a << ' ' << s[i]-b << '\n';
// cout << s[i] << "~~\n";
if(c!=d){
cout << b-a;
return 0;
}
minimize(ans, toQ(s[i]-s[c]));
minimize(ans, toQ(s[i]-s[c-1]));
// minimize(ans, toQ(s[i]-s[d]));
// minimize(ans, toQ(s[i]-s[d-1]));
// minimize(ans, toQ(s[i]-s[c+1]));
// minimize(ans, toQ(s[i]-s[d+1]));
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |