이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |