#include<bits/stdc++.h>
#define int long long
#define forn(i,n) for(int i = 0;i < n;i++)
#define Forn(i,n) for(int i = 1;i <= n;i++)
#define vi vector<int>
#define pb push_back
#define pii pair<int,int>
#define all(p) p.begin(),p.end()
#define F first
#define S second
using namespace std;
void debug() {cerr << endl;}
template <class T, class ...U> void debug(T a, U ... b) { cerr << a << " "; debug(b...);}
const int INF = 1e13 + 10;
const int MOD = (int) 998244353;
const int N = 509;
int n,m,a,b;
int c[N][N];
void build(){
Forn(i,n) Forn(j,m){
c[i][j] += c[i-1][j] + c[i][j-1] - c[i-1][j-1];
}
}
int query(int sx,int sy,int ex,int ey){
return c[ex][ey] - c[ex][sy-1] - c[sx-1][ey] + c[sx-1][sy-1];
}
void solve(){
// pancake ( ^-^)_
cin >> n >> m >> a >> b;
if(a > b) swap(a,b);
Forn(i,n) Forn(j,m) cin >> c[i][j];
build();
int ans = INF;
Forn(i,n){
for(int j = i;j <= n;j++){
int lb = m,cur;
for(int k = m;k >= 1;k--){
cur = query(i,0,j,k);
while(lb-1 >= 0 && cur - query(i,0,j,lb-1) < a){
lb--;
}
assert(a > cur - query(i,0,j,lb));
ans = min(ans,a + b - 2*(cur - query(i,0,j,lb)));
}
//lb = 0;
//debug(ans);
lb = 0;
Forn(k,m){
cur = query(i,0,j,k);
while(lb+1 < k && cur - query(i,0,j,lb+1) > b){
lb++;
}
if(lb+1 <= m){
if(cur - query(i,0,j,lb+1) >= a && cur - query(i,0,j,lb+1) <= b){
ans = b - a;
}
}
}
lb = 0;
Forn(k,m){
cur = query(i,0,j,k);
while(lb+1 < k && cur - query(i,0,j,lb+1) > b){
lb++;
}
if(cur - query(i,0,j,lb) > b){
ans = min(ans,2*(cur - query(i,0,j,lb)) - a - b);
}
}
}
}
cout << ans << '\n';
}
signed main(){
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(0);
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:26:31: warning: array subscript -1 is below array bounds of 'long long int [509]' [-Warray-bounds]
26 | return c[ex][ey] - c[ex][sy-1] - c[sx-1][ey] + c[sx-1][sy-1];
| ~~~~~~~~~~^
Main.cpp:26:61: warning: array subscript -1 is below array bounds of 'long long int [509]' [-Warray-bounds]
26 | return c[ex][ey] - c[ex][sy-1] - c[sx-1][ey] + c[sx-1][sy-1];
| ~~~~~~~~~~~~^
Main.cpp:26:31: warning: array subscript -1 is below array bounds of 'long long int [509]' [-Warray-bounds]
26 | return c[ex][ey] - c[ex][sy-1] - c[sx-1][ey] + c[sx-1][sy-1];
| ~~~~~~~~~~^
Main.cpp:26:61: warning: array subscript -1 is below array bounds of 'long long int [509]' [-Warray-bounds]
26 | return c[ex][ey] - c[ex][sy-1] - c[sx-1][ey] + c[sx-1][sy-1];
| ~~~~~~~~~~~~^
Main.cpp:26:31: warning: array subscript -1 is below array bounds of 'long long int [509]' [-Warray-bounds]
26 | return c[ex][ey] - c[ex][sy-1] - c[sx-1][ey] + c[sx-1][sy-1];
| ~~~~~~~~~~^
Main.cpp:26:61: warning: array subscript -1 is below array bounds of 'long long int [509]' [-Warray-bounds]
26 | return c[ex][ey] - c[ex][sy-1] - c[sx-1][ey] + c[sx-1][sy-1];
| ~~~~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
596 KB |
Output is correct |
12 |
Correct |
10 ms |
724 KB |
Output is correct |
13 |
Correct |
6 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
6 ms |
724 KB |
Output is correct |
16 |
Correct |
6 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
720 KB |
Output is correct |
19 |
Correct |
5 ms |
724 KB |
Output is correct |
20 |
Correct |
5 ms |
596 KB |
Output is correct |
21 |
Correct |
5 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
596 KB |
Output is correct |
12 |
Correct |
10 ms |
724 KB |
Output is correct |
13 |
Correct |
6 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
6 ms |
724 KB |
Output is correct |
16 |
Correct |
6 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
720 KB |
Output is correct |
19 |
Correct |
5 ms |
724 KB |
Output is correct |
20 |
Correct |
5 ms |
596 KB |
Output is correct |
21 |
Correct |
5 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
596 KB |
Output is correct |
23 |
Correct |
599 ms |
2300 KB |
Output is correct |
24 |
Correct |
635 ms |
2584 KB |
Output is correct |
25 |
Correct |
574 ms |
2592 KB |
Output is correct |
26 |
Correct |
583 ms |
2636 KB |
Output is correct |
27 |
Correct |
391 ms |
2544 KB |
Output is correct |
28 |
Correct |
596 ms |
2628 KB |
Output is correct |
29 |
Correct |
561 ms |
2584 KB |
Output is correct |
30 |
Correct |
560 ms |
2660 KB |
Output is correct |
31 |
Correct |
78 ms |
2264 KB |
Output is correct |
32 |
Correct |
551 ms |
2588 KB |
Output is correct |
33 |
Correct |
571 ms |
2692 KB |
Output is correct |
34 |
Correct |
572 ms |
2796 KB |
Output is correct |
35 |
Correct |
566 ms |
2764 KB |
Output is correct |
36 |
Correct |
572 ms |
2788 KB |
Output is correct |
37 |
Correct |
574 ms |
2776 KB |
Output is correct |
38 |
Correct |
569 ms |
2696 KB |
Output is correct |
39 |
Correct |
583 ms |
2632 KB |
Output is correct |
40 |
Correct |
577 ms |
2712 KB |
Output is correct |
41 |
Correct |
447 ms |
2592 KB |
Output is correct |
42 |
Correct |
433 ms |
2736 KB |
Output is correct |
43 |
Correct |
461 ms |
2716 KB |
Output is correct |
44 |
Correct |
436 ms |
2692 KB |
Output is correct |