This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//記得跳題
//#pragma GCC optimize("O4,unroll_loops")
//#pragma GCC target("avx2")
#include<iostream>
#include<array>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<tuple>
#include<random>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define pb push_back
#define INF (ll)(2e18)
#define F first
#define S second
#define endl "\n"
#define AC ios::sync_with_stdio(0);
using namespace std;
ll pre[505][505], x[505][505], p[505];
set<ll> st;
signed main(){
AC;
int r, s, a, b; cin >> r >> s >> a >> b;
if (a > b) swap(a, b);
FOR (i, 1, r + 1) FOR (j, 1, s + 1) cin >> x[i][j];
FOR (j, 1, s + 1) {
FOR (i, 1, r + 1) {
pre[i][j] = pre[i-1][j] + x[i][j];
}
}
ll ans = INF;
FOR (u, 1, r + 1) FOR (d, u, r + 1) {
st.clear();
ll sum = 0;
int l=0;
FOR (j, 1, s + 1) {
sum += pre[d][j] - pre[u-1][j];
p[j] = sum;
while (sum - p[l] >= b) {
ans = min(ans, sum - p[l] - b + sum - p[l] - a);
l++;
}
ans = min(ans, abs((sum - p[l]) - a) + abs((sum - p[l]) - b) );
//cerr << "sum = " << sum << endl;
/*auto it = st.lower_bound(sum - b);
if (it != st.end() && sum - *it >= a) {
cout << b - a << endl; return 0;
}
if (sum - *it < a) {
ans = min(ans, a - (sum - *it) + b - (sum - *it));
}
if (it != st.begin()) {
it = prev(it);
ans = min(ans, (sum - *it) - b + (sum - *it) - a);
}
st.insert(sum);*/
}
}
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... |