Submission #1272924

#TimeUsernameProblemLanguageResultExecution timeMemory
1272924ken7236Zemljište (COCI22_zemljiste)C++20
0 / 70
1 ms568 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() 
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int r, s, pp, qq;
    cin>>r>>s>>pp>>qq;
    int p = min(pp,qq);
    int q = max(pp,qq);
    vector<vector<int> > arr(r+1, vector<int>(s+1));
    for(int a=1; a<=r; a++)
    {
        for(int b=1; b<=s; b++)
        {
            cin>>arr[a][b];
        }
    }
    int ans = INT_MAX;
    vector<vector<int> > pref(r+1, vector<int>(s+1, 0));
    for(int a=1; a<=s; a++)
    {
        for(int b=1; b<=r; b++)
        {
            pref[b][a] = pref[b-1][a] + arr[b][a];
        }
    }
    for(int a=1; a<=r; a++)
    {
        for(int b=1; b<=s; b++)
        {
            //cout<<pref[a][b]<<" ";
        }
        //cout<<endl;
    }
    for(int a=1; a<=r; a++)
    {
        for(int b=a; b<=r; b++)
        {
            //cout<<a<<" "<<b<<endl;
            vector<int> temp(s+1, 0);
            for(int c=1; c<=s; c++)
            {
                temp[c] = pref[b][c] - pref[a-1][c];
                temp[c] = temp[c-1] + temp[c];
                //cout<<temp[c]<<" ";
            }
            //cout<<endl;
            int l = 1;
            for(int rr = 1; rr<=s; rr++)
            {
                int sv = temp[rr] - temp[l-1];
                while(sv > q)
                {
                    l++;
                    sv = temp[rr] - temp[l-1];
                    if(l == s)
                        break;
                }
                int res = abs(sv - p) + abs(sv-q);
                //cout<<"l = "<<l<<", r = "<<rr<<", temprr = "<<temp[rr]<<", templ = "<<temp[l]<<", sv = "<<sv<<", res = "<<res<<endl;
                ans = min(ans, res);
            }
        }
    }
    cout<<ans<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...