#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<=r; j++){
cin>>prefix[i][j];
prefix[i][j] += prefix[i-1][j] + prefix[i][j-1];
prefix[i][j] -= prefix[i-1][j-1];
}
for(int sr=1; sr<=r; sr++)
for(int er=1; er<=r; er++){
int left=1, cur = 0;
for(int right = 1; right <= s; right++){
if(cur > b) left++;
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?)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |