#include <stdio.h>
#define N 500
#define M 500
#define INF 0x3f3f3f3f3f3f3f3fLL
long long abs_(long long a) { return a > 0 ? a : -a; }
long long min(long long a, long long b) { return a < b ? a : b; }
int main() {
static long long aa[N + 1][M + 1];
int n, m, l, r, i, j, i1, i2, j1, j2;
long long a, ans;
scanf("%d%d%d%d", &n, &m, &l, &r);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf("%lld", &aa[i][j]);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
aa[i][j] += aa[i][j - 1];
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
aa[i][j] += aa[i - 1][j];
ans = INF;
for (i1 = 0; i1 < n; i1++)
for (i2 = i1 + 1; i2 <= n; i2++)
for (j1 = 0, j2 = 0; j1 < m; j1++) {
while (j2 <= m && aa[i2][j2] - aa[i2][j1] - aa[i1][j2] + aa[i1][j1] < l)
j2++;
if (j2 <= m) {
a = aa[i2][j2] - aa[i2][j1] - aa[i1][j2] + aa[i1][j1];
ans = min(ans, abs_(a - l) + abs_(a - r));
}
if (j2 - j1 > 1) {
a = aa[i2][j2 - 1] - aa[i2][j1] - aa[i1][j2 - 1] + aa[i1][j1];
ans = min(ans, abs_(a - l) + abs_(a - r));
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:15:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d%d%d%d", &n, &m, &l, &r);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:18:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%lld", &aa[i][j]);
| ^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
4 ms |
688 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
724 KB |
Output is correct |
21 |
Correct |
4 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
4 ms |
688 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
724 KB |
Output is correct |
21 |
Correct |
4 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
692 KB |
Output is correct |
23 |
Correct |
273 ms |
4236 KB |
Output is correct |
24 |
Correct |
293 ms |
4568 KB |
Output is correct |
25 |
Correct |
284 ms |
4596 KB |
Output is correct |
26 |
Correct |
287 ms |
4744 KB |
Output is correct |
27 |
Correct |
261 ms |
3656 KB |
Output is correct |
28 |
Correct |
316 ms |
4472 KB |
Output is correct |
29 |
Correct |
283 ms |
4552 KB |
Output is correct |
30 |
Correct |
288 ms |
4508 KB |
Output is correct |
31 |
Correct |
40 ms |
2336 KB |
Output is correct |
32 |
Correct |
282 ms |
4576 KB |
Output is correct |
33 |
Correct |
283 ms |
4600 KB |
Output is correct |
34 |
Correct |
333 ms |
4556 KB |
Output is correct |
35 |
Correct |
279 ms |
4564 KB |
Output is correct |
36 |
Correct |
296 ms |
4592 KB |
Output is correct |
37 |
Correct |
284 ms |
4432 KB |
Output is correct |
38 |
Correct |
289 ms |
4520 KB |
Output is correct |
39 |
Correct |
279 ms |
4548 KB |
Output is correct |
40 |
Correct |
300 ms |
4616 KB |
Output is correct |
41 |
Correct |
263 ms |
3368 KB |
Output is correct |
42 |
Correct |
307 ms |
3344 KB |
Output is correct |
43 |
Correct |
293 ms |
3444 KB |
Output is correct |
44 |
Correct |
265 ms |
3364 KB |
Output is correct |