#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(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
13 ms |
908 KB |
Output is correct |
19 |
Correct |
13 ms |
908 KB |
Output is correct |
20 |
Correct |
13 ms |
860 KB |
Output is correct |
21 |
Correct |
14 ms |
860 KB |
Output is correct |
22 |
Correct |
13 ms |
904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
13 ms |
908 KB |
Output is correct |
19 |
Correct |
13 ms |
908 KB |
Output is correct |
20 |
Correct |
13 ms |
860 KB |
Output is correct |
21 |
Correct |
14 ms |
860 KB |
Output is correct |
22 |
Correct |
13 ms |
904 KB |
Output is correct |
23 |
Correct |
27 ms |
4700 KB |
Output is correct |
24 |
Correct |
34 ms |
4540 KB |
Output is correct |
25 |
Correct |
29 ms |
4696 KB |
Output is correct |
26 |
Correct |
42 ms |
4696 KB |
Output is correct |
27 |
Correct |
22 ms |
3928 KB |
Output is correct |
28 |
Correct |
31 ms |
4532 KB |
Output is correct |
29 |
Correct |
28 ms |
4548 KB |
Output is correct |
30 |
Correct |
31 ms |
4696 KB |
Output is correct |
31 |
Correct |
4 ms |
2396 KB |
Output is correct |
32 |
Correct |
29 ms |
4508 KB |
Output is correct |
33 |
Correct |
31 ms |
4700 KB |
Output is correct |
34 |
Correct |
31 ms |
4700 KB |
Output is correct |
35 |
Correct |
30 ms |
4696 KB |
Output is correct |
36 |
Correct |
36 ms |
4548 KB |
Output is correct |
37 |
Correct |
36 ms |
4696 KB |
Output is correct |
38 |
Correct |
34 ms |
4692 KB |
Output is correct |
39 |
Correct |
33 ms |
4700 KB |
Output is correct |
40 |
Correct |
40 ms |
4696 KB |
Output is correct |
41 |
Correct |
1901 ms |
3508 KB |
Output is correct |
42 |
Execution timed out |
2047 ms |
3420 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |