// - Art -
#include <bits/stdc++.h>
#define el cout << '\n'
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define REP(i, c) for (int i = 0, _c = (c); i < _c; ++i)
const int N = 5e2 + 7;
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
using namespace std;
int a[N][N];
long long sum[N], pre[N];
int main() {
#define name "art"
if (fopen(name".inp", "r")) {
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int m, n, A, B;
cin >> m >> n >> A >> B;
if (A > B) {
swap(A, B);
}
long long res = 1e18;
FOR (i, 1, m) FOR (j, 1, n) {
cin >> a[i][j];
}
FOR (x1, 1, m) {
FOR (i, 1, n) {
sum[i] = 0;
pre[i] = 0;
}
FOR (x2, x1, m) {
int idx1 = 0, idx2 = 0;
FOR (i, 1, n) {
sum[i] += a[x2][i];
pre[i] = pre[i - 1] + sum[i];
while (pre[i] - pre[idx1] >= A + B) {
long long c = pre[i] - pre[idx1];
minimize(res, abs(c - A) + abs(c - B));
++idx1;
}
if ((A + B) >= (pre[i] - pre[idx1])) {
long long c = pre[i] - pre[idx1];
minimize(res, abs(c - A) + abs(c - B));
}
while (pre[i] - pre[idx2] >= A) {
if (pre[i] - pre[idx2] <= B) {
minimize(res, B - A);
}
++idx2;
}
}
}
}
cout << res, el;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:33:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |