//記得跳題
//#pragma GCC optimize("O4,unroll_loops")
//#pragma GCC target("avx2")
#include<iostream>
#include<array>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<tuple>
#include<random>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define pb push_back
#define INF (ll)(2e18)
#define F first
#define S second
#define endl "\n"
#define AC ios::sync_with_stdio(0);
using namespace std;
ll pre[505][505], x[505][505];
set<ll> st;
signed main(){
AC;
int r, s, a, b; cin >> r >> s >> a >> b;
if (a > b) swap(a, b);
FOR (i, 1, r + 1) FOR (j, 1, s + 1) cin >> x[i][j];
FOR (j, 1, s + 1) {
FOR (i, 1, r + 1) {
pre[i][j] = pre[i-1][j] + x[i][j];
}
}
ll ans = INF;
FOR (u, 1, r + 1) FOR (d, u, r + 1) {
st.clear();
ll sum = 0;
st.insert(sum);
FOR (j, 1, s + 1) {
sum += pre[d][j] - pre[u-1][j];
//cerr << "sum = " << sum << endl;
auto it = st.lower_bound(sum - b);
if (it != st.end() && sum - *it >= a && sum - *it <= b) {
cout << b - a << endl; return 0;
}
if (sum - *it < a) {
ans = min(ans, a - (sum - *it) + b - (sum - *it));
}
if (it != st.begin()) {
it = prev(it);
ans = min(ans, (sum - *it) - b + (sum - *it) - a);
}
st.insert(sum);
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
39 ms |
1108 KB |
Output is correct |
16 |
Correct |
39 ms |
1108 KB |
Output is correct |
17 |
Correct |
39 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
39 ms |
1108 KB |
Output is correct |
16 |
Correct |
39 ms |
1108 KB |
Output is correct |
17 |
Correct |
39 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
25 ms |
4236 KB |
Output is correct |
24 |
Correct |
25 ms |
4148 KB |
Output is correct |
25 |
Correct |
24 ms |
4264 KB |
Output is correct |
26 |
Correct |
25 ms |
4176 KB |
Output is correct |
27 |
Correct |
17 ms |
4172 KB |
Output is correct |
28 |
Correct |
24 ms |
4212 KB |
Output is correct |
29 |
Correct |
25 ms |
4220 KB |
Output is correct |
30 |
Correct |
24 ms |
4212 KB |
Output is correct |
31 |
Correct |
5 ms |
3796 KB |
Output is correct |
32 |
Execution timed out |
2060 ms |
4268 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |