# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
799290 | MercubytheFirst | Zemljište (COCI22_zemljiste) | C++17 | 2044 ms | 6348 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
*/
#include<iostream>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<map>
#include<set>
#include<climits>
#include<numeric>
#include<queue>
#include<unordered_map>
#include<string>
#include<climits>
#include<unordered_set>
#include<bitset>
#include<cstring>
#include<cmath>
#include<stack>
#if 1
#define pb push_back
#define st first
#define nd second
#define endl '\n'
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define N ((int)(2e5+5))
#define int long long int
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vi> vivi;
typedef pair<char,int> pci;
const int inf = 1e18 + 5;
#endif
void fileio(string s){
#ifndef ONLINE_JUDGE
freopen((s+string(".in")).c_str(),"r",stdin);
freopen((s+string(".out")).c_str(),"w",stdout);
#endif
}
/*
1. CHECK IF THE ANSWER IS MODULO M
2. Edge cases
3. Check for overflow (maybe before edge cases idk)
23. Use the number 23 (it automatically makes everything turn to ac)
*/
bool valid(string& a){
stack<char> s;
for(char c : a){
if(c == ')'){
if(s.empty()){
return false;
}
s.pop();
}
else{
s.push(c);
}
}
if(s.size())return false;
return true;
}
vivi pref(501, vi(501)), g(501, vi(501));
int r, s, a, b;
int get(int x, int y, int xx, int yy){
int sum = pref[xx][yy];
sum -= pref[xx][y];
sum -= pref[x][yy];
sum += pref[x][y];
return sum;
}
void build(){
for(int i = 1; i <= r; ++i){
for(int j = 1; j <= s; ++j){
pref[i][j] = pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1] + g[i][j];
}
}
}
void solve(){
cin >> r >> s >> a >> b;
for(int i = 1; i <= r; ++i){
for(int j = 1; j <= s; ++j){
cin >> g[i][j];
}
}
build();
int ans = inf;
for(int li = 1; li <= r; ++li){
for(int ri = li; ri <= r; ++ri){
for(int lj = 1; lj <= s; ++lj){
for(int rj = lj; rj <= s; ++rj){
int cur = get(li - 1, lj - 1, ri, rj);
ans = min(ans, abs(cur - a) + abs(cur - b));
//cout << li << ' ' << lj << ' ' << ri << ' ' << rj << ' ' << cur << endl;
}
}
}
}
cout << ans << endl;
}
signed main(){
//fileio("test"); // Don't forget to comment out
fio;
//int t;cin >> t;while(t--)
solve();
}
/*
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |