#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 505;
const ll LLINF = 2e18;
ll a[N][N], sum[N], col[N][N];
int main(void)
{ IO
ll n, i, m, L, R, ans = LLINF;
cin >> n >> m >> L >> R;
if (L > R) swap(L, R);
for (i=1; i <= n; ++i)
for (int j=1; j <= m; ++j)
cin >> a[i][j], col[i][j] = col[i-1][j] + a[i][j];
for (int lb=0; lb < n; ++lb)
{
for (int ub=lb+1; ub <= n; ++ub)
{
ll it = 0, it2 = 0;
for (i=1; i <= m; ++i)
{
sum[i] = sum[i-1] + col[ub][i]-col[lb][i];
while (it <= i && sum[i]-sum[it] >= L) ++it;
while (it2 <= i && sum[i]-sum[it2] > R) ++it2;
if (it != it2)
{
ans = R - L;
} else {
ll x = sum[i] - sum[it];
ans = min(ans, abs(L-x) + abs(R-x));
if (it != 0)
{
x = sum[i] - sum[it-1];
ans = min(ans, abs(L-x) + abs(R-x));
}
}
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2400 KB |
Output is correct |
5 |
Correct |
1 ms |
2400 KB |
Output is correct |
6 |
Correct |
1 ms |
2400 KB |
Output is correct |
7 |
Correct |
1 ms |
2404 KB |
Output is correct |
8 |
Correct |
1 ms |
2404 KB |
Output is correct |
9 |
Correct |
1 ms |
2404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2400 KB |
Output is correct |
5 |
Correct |
1 ms |
2400 KB |
Output is correct |
6 |
Correct |
1 ms |
2400 KB |
Output is correct |
7 |
Correct |
1 ms |
2404 KB |
Output is correct |
8 |
Correct |
1 ms |
2404 KB |
Output is correct |
9 |
Correct |
1 ms |
2404 KB |
Output is correct |
10 |
Correct |
2 ms |
2660 KB |
Output is correct |
11 |
Correct |
4 ms |
2916 KB |
Output is correct |
12 |
Correct |
4 ms |
2916 KB |
Output is correct |
13 |
Correct |
4 ms |
2968 KB |
Output is correct |
14 |
Correct |
1 ms |
2404 KB |
Output is correct |
15 |
Correct |
5 ms |
2916 KB |
Output is correct |
16 |
Correct |
4 ms |
2916 KB |
Output is correct |
17 |
Correct |
4 ms |
2788 KB |
Output is correct |
18 |
Correct |
3 ms |
2704 KB |
Output is correct |
19 |
Correct |
3 ms |
2916 KB |
Output is correct |
20 |
Correct |
4 ms |
2916 KB |
Output is correct |
21 |
Correct |
3 ms |
2660 KB |
Output is correct |
22 |
Correct |
3 ms |
2916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2400 KB |
Output is correct |
5 |
Correct |
1 ms |
2400 KB |
Output is correct |
6 |
Correct |
1 ms |
2400 KB |
Output is correct |
7 |
Correct |
1 ms |
2404 KB |
Output is correct |
8 |
Correct |
1 ms |
2404 KB |
Output is correct |
9 |
Correct |
1 ms |
2404 KB |
Output is correct |
10 |
Correct |
2 ms |
2660 KB |
Output is correct |
11 |
Correct |
4 ms |
2916 KB |
Output is correct |
12 |
Correct |
4 ms |
2916 KB |
Output is correct |
13 |
Correct |
4 ms |
2968 KB |
Output is correct |
14 |
Correct |
1 ms |
2404 KB |
Output is correct |
15 |
Correct |
5 ms |
2916 KB |
Output is correct |
16 |
Correct |
4 ms |
2916 KB |
Output is correct |
17 |
Correct |
4 ms |
2788 KB |
Output is correct |
18 |
Correct |
3 ms |
2704 KB |
Output is correct |
19 |
Correct |
3 ms |
2916 KB |
Output is correct |
20 |
Correct |
4 ms |
2916 KB |
Output is correct |
21 |
Correct |
3 ms |
2660 KB |
Output is correct |
22 |
Correct |
3 ms |
2916 KB |
Output is correct |
23 |
Correct |
346 ms |
5960 KB |
Output is correct |
24 |
Correct |
347 ms |
6748 KB |
Output is correct |
25 |
Correct |
337 ms |
6748 KB |
Output is correct |
26 |
Correct |
347 ms |
6840 KB |
Output is correct |
27 |
Correct |
232 ms |
6004 KB |
Output is correct |
28 |
Correct |
341 ms |
6708 KB |
Output is correct |
29 |
Correct |
357 ms |
6840 KB |
Output is correct |
30 |
Correct |
360 ms |
6740 KB |
Output is correct |
31 |
Correct |
51 ms |
4700 KB |
Output is correct |
32 |
Correct |
350 ms |
6840 KB |
Output is correct |
33 |
Correct |
379 ms |
6996 KB |
Output is correct |
34 |
Correct |
338 ms |
6840 KB |
Output is correct |
35 |
Correct |
336 ms |
6784 KB |
Output is correct |
36 |
Correct |
339 ms |
6840 KB |
Output is correct |
37 |
Correct |
351 ms |
6840 KB |
Output is correct |
38 |
Correct |
386 ms |
6844 KB |
Output is correct |
39 |
Correct |
340 ms |
6996 KB |
Output is correct |
40 |
Correct |
338 ms |
6740 KB |
Output is correct |
41 |
Correct |
209 ms |
5572 KB |
Output is correct |
42 |
Correct |
219 ms |
5572 KB |
Output is correct |
43 |
Correct |
209 ms |
5468 KB |
Output is correct |
44 |
Correct |
210 ms |
5576 KB |
Output is correct |