#include "bits/stdc++.h"
#define st first
#define nd second
using lint = int64_t;
constexpr int MOD = int(1e9) + 7;
constexpr int INF = 0x3f3f3f3f;
constexpr int NINF = 0xcfcfcfcf;
constexpr lint LINF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;
int r, s;
lint a, b;
vector<vector<lint>> prefix;
lint getSum(int l, int r, int sr, int er){
if(r < l || er < sr) return 0;
lint ans = prefix[r][er];
ans -= prefix[l-1][er] + prefix[r][sr-1];
ans += prefix[l-1][sr-1];
return ans;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>r>>s>>a>>b;
if(a > b) swap(a, b);
lint ans = LINF;
prefix = vector<vector<lint>>(r+2, vector<lint>(s+2));
for(int i=1; i<=r; i++)
for(int j=1; j<=s; j++){
cin>>prefix[i][j];
prefix[i][j] += prefix[i-1][j] + prefix[i][j-1];
prefix[i][j] -= prefix[i-1][j-1];
}
//cout<<"\ta "<<a<<" b "<<b<<"\n"; return 0;
for(int sr=1; sr<=s; sr++)
for(int er=sr; er<=s; er++){
int left=1;
lint cur = 0;
for(int right = 1; right <= r; right++){
cur = getSum(left,right,sr,er);
ans = min(ans, abs(a-cur) + abs(b-cur));
while(cur > b){
left++;
cur = getSum(left,right,sr,er);
ans = min(ans, abs(a-cur) + abs(b-cur));
}
cur = getSum(left,right,sr,er);
ans = min(ans, abs(a-cur) + abs(b-cur));
}
}
cout<<ans<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
5 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
468 KB |
Output is correct |
16 |
Correct |
5 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
508 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
456 KB |
Output is correct |
21 |
Correct |
4 ms |
328 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
468 KB |
Output is correct |
13 |
Correct |
5 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
468 KB |
Output is correct |
16 |
Correct |
5 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
508 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
456 KB |
Output is correct |
21 |
Correct |
4 ms |
328 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
408 ms |
4712 KB |
Output is correct |
24 |
Correct |
417 ms |
4716 KB |
Output is correct |
25 |
Correct |
412 ms |
4708 KB |
Output is correct |
26 |
Correct |
413 ms |
4812 KB |
Output is correct |
27 |
Correct |
189 ms |
3248 KB |
Output is correct |
28 |
Correct |
437 ms |
4712 KB |
Output is correct |
29 |
Correct |
414 ms |
4684 KB |
Output is correct |
30 |
Correct |
420 ms |
4708 KB |
Output is correct |
31 |
Correct |
13 ms |
840 KB |
Output is correct |
32 |
Correct |
415 ms |
4716 KB |
Output is correct |
33 |
Correct |
422 ms |
4596 KB |
Output is correct |
34 |
Correct |
414 ms |
4716 KB |
Output is correct |
35 |
Correct |
425 ms |
4712 KB |
Output is correct |
36 |
Correct |
428 ms |
4708 KB |
Output is correct |
37 |
Correct |
478 ms |
4724 KB |
Output is correct |
38 |
Correct |
438 ms |
4708 KB |
Output is correct |
39 |
Correct |
411 ms |
4716 KB |
Output is correct |
40 |
Correct |
417 ms |
4712 KB |
Output is correct |
41 |
Correct |
305 ms |
3452 KB |
Output is correct |
42 |
Correct |
307 ms |
3452 KB |
Output is correct |
43 |
Correct |
344 ms |
3452 KB |
Output is correct |
44 |
Correct |
349 ms |
3452 KB |
Output is correct |