#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 505;
int n, m, a, b, i, j, x, l, r, mid;
ll mat[N][N], sm, tmp, ans;
ll f(int y){
return mat[x][y] - mat[x][j - 1] - mat[i - 1][y] + mat[i - 1][j - 1];
}
int main(){
cin >> n >> m >> a >> b;
for (i = 1; i <= n; i ++){
for (j = 1; j <= m; j ++){
cin >> mat[i][j];
mat[i][j] += mat[i - 1][j];
mat[i][j] += mat[i][j - 1];
mat[i][j] -= mat[i - 1][j - 1];
}
}
if (b < a) swap(a, b);
ans = 1e18;
for (i = 1; i <= n; i ++){
for (j = 1; j <= m; j ++){
for (x = i; x <= n; x ++){
sm = f(j);
tmp = abs(a - sm) + abs(b - sm);
ans = min(ans, tmp);
if (sm > b) break;
l = j;
r = m + 1;
while (r - l > 1){
mid = (l + r) / 2;
if (f(mid) > b)
r = mid;
else
l = mid;
}
sm = f(l);
ans = min(ans, abs(a - sm) + abs(b - sm));
if (l < m) sm = f(l + 1);
ans = min(ans, abs(a - sm) + abs(b - sm));
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
864 KB |
Output is correct |
12 |
Correct |
4 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
4 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
14 ms |
860 KB |
Output is correct |
19 |
Correct |
19 ms |
856 KB |
Output is correct |
20 |
Correct |
26 ms |
856 KB |
Output is correct |
21 |
Correct |
21 ms |
884 KB |
Output is correct |
22 |
Correct |
14 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
864 KB |
Output is correct |
12 |
Correct |
4 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
4 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
14 ms |
860 KB |
Output is correct |
19 |
Correct |
19 ms |
856 KB |
Output is correct |
20 |
Correct |
26 ms |
856 KB |
Output is correct |
21 |
Correct |
21 ms |
884 KB |
Output is correct |
22 |
Correct |
14 ms |
860 KB |
Output is correct |
23 |
Correct |
96 ms |
4692 KB |
Output is correct |
24 |
Correct |
70 ms |
4748 KB |
Output is correct |
25 |
Correct |
67 ms |
4676 KB |
Output is correct |
26 |
Correct |
81 ms |
4692 KB |
Output is correct |
27 |
Correct |
48 ms |
3784 KB |
Output is correct |
28 |
Correct |
74 ms |
4688 KB |
Output is correct |
29 |
Correct |
67 ms |
4688 KB |
Output is correct |
30 |
Correct |
69 ms |
4688 KB |
Output is correct |
31 |
Correct |
12 ms |
2396 KB |
Output is correct |
32 |
Correct |
66 ms |
4692 KB |
Output is correct |
33 |
Correct |
64 ms |
4744 KB |
Output is correct |
34 |
Correct |
69 ms |
4688 KB |
Output is correct |
35 |
Correct |
66 ms |
4812 KB |
Output is correct |
36 |
Correct |
81 ms |
4788 KB |
Output is correct |
37 |
Correct |
71 ms |
4732 KB |
Output is correct |
38 |
Correct |
70 ms |
4688 KB |
Output is correct |
39 |
Correct |
69 ms |
4692 KB |
Output is correct |
40 |
Correct |
73 ms |
4692 KB |
Output is correct |
41 |
Execution timed out |
2041 ms |
3412 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |