이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 505;
ll n, m, a, b, mat[N][N];
ll f(ll i, ll j, ll x, ll y){
return mat[x][y] - mat[x][j - 1] - mat[i - 1][y] + mat[i - 1][j - 1];
}
int main(){
cin >> n >> m >> a >> b;
for (ll i = 1; i <= n; i ++){
for (ll j = 1; j <= m; j ++){
cin >> mat[i][j];
mat[i][j] += mat[i - 1][j];
mat[i][j] += mat[i][j - 1];
mat[i][j] -= mat[i - 1][j - 1];
}
}
if (b < a) swap(a, b);
ll ans = 1e18;
for (ll i = 1; i <= n; i ++){
for (ll j = 1; j <= m; j ++){
for (ll x = i; x <= n; x ++){
ll sm = f(i, j, x, j);
ans = min(ans, abs(a - sm) + abs(b - sm));
if (sm > b) break;
ll l = j;
ll r = m + 1;
while (r - l > 1){
ll mid = (l + r) / 2;
if (f(i, j, x, mid) > b)
r = mid;
else
l = mid;
}
sm = f(i, j, x, l);
ans = min(ans, abs(a - sm) + abs(b - sm));
if (l < m) sm = f(i, j, x, l + 1);
ans = min(ans, abs(a - sm) + abs(b - sm));
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |