#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 505;
int n, m, a, b, i, j, x, y;
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 ++){
y = m;
for (x = i; x <= n; x ++){
while (y - 1 >= j and f(y) > b){
sm = f(y);
tmp = abs(a - sm) + abs(b - sm);
ans = min(ans, tmp);
y--;
}
sm = f(y);
tmp = abs(a - sm) + abs(b - sm);
ans = min(ans, tmp);
if (y + 1 <= m) sm = f(y + 1);
tmp = abs(a - sm) + abs(b - sm);
ans = min(ans, tmp);
}
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
504 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
504 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
12 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
17 |
Correct |
7 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
856 KB |
Output is correct |
20 |
Correct |
3 ms |
860 KB |
Output is correct |
21 |
Correct |
3 ms |
916 KB |
Output is correct |
22 |
Correct |
4 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
504 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
860 KB |
Output is correct |
12 |
Correct |
12 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
17 |
Correct |
7 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
856 KB |
Output is correct |
20 |
Correct |
3 ms |
860 KB |
Output is correct |
21 |
Correct |
3 ms |
916 KB |
Output is correct |
22 |
Correct |
4 ms |
856 KB |
Output is correct |
23 |
Correct |
790 ms |
4828 KB |
Output is correct |
24 |
Correct |
734 ms |
4828 KB |
Output is correct |
25 |
Correct |
726 ms |
4852 KB |
Output is correct |
26 |
Correct |
766 ms |
4832 KB |
Output is correct |
27 |
Correct |
434 ms |
4004 KB |
Output is correct |
28 |
Correct |
752 ms |
4696 KB |
Output is correct |
29 |
Correct |
788 ms |
4832 KB |
Output is correct |
30 |
Correct |
734 ms |
4700 KB |
Output is correct |
31 |
Correct |
73 ms |
2648 KB |
Output is correct |
32 |
Correct |
718 ms |
4688 KB |
Output is correct |
33 |
Correct |
736 ms |
4952 KB |
Output is correct |
34 |
Correct |
738 ms |
4832 KB |
Output is correct |
35 |
Correct |
762 ms |
4836 KB |
Output is correct |
36 |
Correct |
718 ms |
4832 KB |
Output is correct |
37 |
Correct |
649 ms |
4832 KB |
Output is correct |
38 |
Correct |
701 ms |
4836 KB |
Output is correct |
39 |
Correct |
697 ms |
4836 KB |
Output is correct |
40 |
Correct |
736 ms |
4700 KB |
Output is correct |
41 |
Correct |
290 ms |
3416 KB |
Output is correct |
42 |
Correct |
340 ms |
3712 KB |
Output is correct |
43 |
Correct |
333 ms |
3460 KB |
Output is correct |
44 |
Correct |
313 ms |
3580 KB |
Output is correct |