#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define lg long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 505;
lg v[N][N];
lg get_sum(lg a, lg b, lg c, lg d)
{
if(c > d) swap(c, d);
if(a > b) swap(a, b);
return v[b][d]-v[a-1][d]-v[b][c-1]+v[a-1][c-1];
}
int main()
{
fastio;
lg n, m, a, b;
cin >> n >> m >> a >> b;
if(b < a) swap(a, b);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> v[i][j];
v[i][j] += v[i-1][j]+v[i][j-1]-v[i-1][j-1];
}
}
lg ans = 2e18;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
lg ptr1 = m, ptr2 = 1;
for(int k = 1; k <= m; k++)
{
while(ptr2 < k && get_sum(j, i, k, ptr2+1) >= a) ptr2++;
lg y = get_sum(j, i, k, ptr2);
ans = min(ans, abs(y-a)+abs(y-b));
}
for(int k = m; k; k--)
{
while(ptr1 > k && get_sum(j, i, k, ptr1) > b) ptr1--;
lg y = get_sum(j, i, k, ptr1);
ans = min(ans, abs(y-a)+abs(y-b));
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
860 KB |
Output is correct |
12 |
Correct |
5 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
4 ms |
1012 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
868 KB |
Output is correct |
19 |
Correct |
4 ms |
856 KB |
Output is correct |
20 |
Correct |
4 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
860 KB |
Output is correct |
12 |
Correct |
5 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
4 ms |
1012 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
868 KB |
Output is correct |
19 |
Correct |
4 ms |
856 KB |
Output is correct |
20 |
Correct |
4 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
860 KB |
Output is correct |
23 |
Correct |
439 ms |
2412 KB |
Output is correct |
24 |
Correct |
406 ms |
4840 KB |
Output is correct |
25 |
Correct |
412 ms |
4848 KB |
Output is correct |
26 |
Correct |
412 ms |
4852 KB |
Output is correct |
27 |
Correct |
266 ms |
4016 KB |
Output is correct |
28 |
Correct |
405 ms |
4840 KB |
Output is correct |
29 |
Correct |
444 ms |
4700 KB |
Output is correct |
30 |
Correct |
405 ms |
4748 KB |
Output is correct |
31 |
Correct |
54 ms |
2396 KB |
Output is correct |
32 |
Correct |
411 ms |
4840 KB |
Output is correct |
33 |
Correct |
406 ms |
4696 KB |
Output is correct |
34 |
Correct |
404 ms |
4840 KB |
Output is correct |
35 |
Correct |
457 ms |
4844 KB |
Output is correct |
36 |
Correct |
400 ms |
4844 KB |
Output is correct |
37 |
Correct |
414 ms |
4948 KB |
Output is correct |
38 |
Correct |
409 ms |
4700 KB |
Output is correct |
39 |
Correct |
408 ms |
4908 KB |
Output is correct |
40 |
Correct |
417 ms |
4840 KB |
Output is correct |
41 |
Correct |
384 ms |
3576 KB |
Output is correct |
42 |
Correct |
400 ms |
3420 KB |
Output is correct |
43 |
Correct |
390 ms |
3676 KB |
Output is correct |
44 |
Correct |
380 ms |
3420 KB |
Output is correct |