#include <iostream>
#include <algorithm>
using namespace std;
#define int int64_t
#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<=m; ++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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
2 ms |
848 KB |
Output is correct |
13 |
Correct |
2 ms |
1020 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
9 ms |
848 KB |
Output is correct |
16 |
Correct |
8 ms |
848 KB |
Output is correct |
17 |
Correct |
9 ms |
848 KB |
Output is correct |
18 |
Correct |
2 ms |
848 KB |
Output is correct |
19 |
Correct |
2 ms |
848 KB |
Output is correct |
20 |
Correct |
2 ms |
848 KB |
Output is correct |
21 |
Correct |
2 ms |
848 KB |
Output is correct |
22 |
Correct |
2 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
848 KB |
Output is correct |
12 |
Correct |
2 ms |
848 KB |
Output is correct |
13 |
Correct |
2 ms |
1020 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
9 ms |
848 KB |
Output is correct |
16 |
Correct |
8 ms |
848 KB |
Output is correct |
17 |
Correct |
9 ms |
848 KB |
Output is correct |
18 |
Correct |
2 ms |
848 KB |
Output is correct |
19 |
Correct |
2 ms |
848 KB |
Output is correct |
20 |
Correct |
2 ms |
848 KB |
Output is correct |
21 |
Correct |
2 ms |
848 KB |
Output is correct |
22 |
Correct |
2 ms |
848 KB |
Output is correct |
23 |
Correct |
18 ms |
2384 KB |
Output is correct |
24 |
Correct |
19 ms |
2424 KB |
Output is correct |
25 |
Correct |
28 ms |
2308 KB |
Output is correct |
26 |
Correct |
22 ms |
2384 KB |
Output is correct |
27 |
Correct |
14 ms |
2384 KB |
Output is correct |
28 |
Correct |
19 ms |
2252 KB |
Output is correct |
29 |
Correct |
18 ms |
2384 KB |
Output is correct |
30 |
Correct |
18 ms |
2208 KB |
Output is correct |
31 |
Correct |
7 ms |
2128 KB |
Output is correct |
32 |
Correct |
1142 ms |
2424 KB |
Output is correct |
33 |
Correct |
1085 ms |
2384 KB |
Output is correct |
34 |
Correct |
1050 ms |
2284 KB |
Output is correct |
35 |
Correct |
1056 ms |
2424 KB |
Output is correct |
36 |
Correct |
1051 ms |
2424 KB |
Output is correct |
37 |
Correct |
1084 ms |
2420 KB |
Output is correct |
38 |
Correct |
1065 ms |
2384 KB |
Output is correct |
39 |
Correct |
1119 ms |
2424 KB |
Output is correct |
40 |
Correct |
1110 ms |
2420 KB |
Output is correct |
41 |
Correct |
19 ms |
2384 KB |
Output is correct |
42 |
Correct |
16 ms |
2384 KB |
Output is correct |
43 |
Correct |
18 ms |
2384 KB |
Output is correct |
44 |
Correct |
17 ms |
2476 KB |
Output is correct |