Submission #572588

# Submission time Handle Problem Language Result Execution time Memory
572588 2022-06-04T17:55:29 Z model_code Zemljište (COCI22_zemljiste) C++17
70 / 70
593 ms 3336 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF_int = 0x3f3f3f3f;
const ll INF = (ll)INF_int * INF_int;
const int N=505;

int n, m, A, B;
int mat[N][N];
ll red[N][N], pref[N];

ll f(ll x) {
    return abs(x-A) + abs(x-B);
}

void solve() {
    if (A > B) swap(A, B);

    ll sol = INF;
    for (int j1=1; j1<=m; ++j1) {
        for (int j2=j1; j2<=m; ++j2) {
            for (int i=1; i<=n; ++i) pref[i]=pref[i-1] + red[i][j2] - red[i][j1-1];
            pref[n+2] = pref[n+1] = pref[n];

            int l=0;
            for (int i=1; i<=n; ++i) {
                while (pref[i] - pref[l] > B) ++l;

                sol = min(sol, f(pref[i] - pref[l]));
                if (l) sol = min(sol, f(pref[i] - pref[l - 1]));
            }
        }
    }
    printf("%lld\n", sol);
}

void load() {
    scanf("%d %d %d %d", &n, &m, &A, &B);
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j) {
            scanf("%d", &mat[i][j]);
            red[i][j] = red[i][j-1] + mat[i][j];
        }
    }
}

int main() {
    load();
    solve();
    return 0;
}

Compilation message

Main.cpp: In function 'void load()':
Main.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     scanf("%d %d %d %d", &n, &m, &A, &B);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:44:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |             scanf("%d", &mat[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 424 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 424 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 8 ms 904 KB Output is correct
12 Correct 6 ms 852 KB Output is correct
13 Correct 6 ms 852 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 12 ms 852 KB Output is correct
16 Correct 7 ms 852 KB Output is correct
17 Correct 8 ms 852 KB Output is correct
18 Correct 4 ms 852 KB Output is correct
19 Correct 8 ms 908 KB Output is correct
20 Correct 4 ms 852 KB Output is correct
21 Correct 6 ms 852 KB Output is correct
22 Correct 6 ms 900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 424 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 8 ms 904 KB Output is correct
12 Correct 6 ms 852 KB Output is correct
13 Correct 6 ms 852 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 12 ms 852 KB Output is correct
16 Correct 7 ms 852 KB Output is correct
17 Correct 8 ms 852 KB Output is correct
18 Correct 4 ms 852 KB Output is correct
19 Correct 8 ms 908 KB Output is correct
20 Correct 4 ms 852 KB Output is correct
21 Correct 6 ms 852 KB Output is correct
22 Correct 6 ms 900 KB Output is correct
23 Correct 593 ms 3260 KB Output is correct
24 Correct 541 ms 3256 KB Output is correct
25 Correct 545 ms 3260 KB Output is correct
26 Correct 534 ms 3260 KB Output is correct
27 Correct 271 ms 3232 KB Output is correct
28 Correct 501 ms 3272 KB Output is correct
29 Correct 469 ms 3264 KB Output is correct
30 Correct 484 ms 3276 KB Output is correct
31 Correct 15 ms 2900 KB Output is correct
32 Correct 465 ms 3260 KB Output is correct
33 Correct 467 ms 3260 KB Output is correct
34 Correct 462 ms 3260 KB Output is correct
35 Correct 463 ms 3256 KB Output is correct
36 Correct 478 ms 3256 KB Output is correct
37 Correct 470 ms 3336 KB Output is correct
38 Correct 473 ms 3260 KB Output is correct
39 Correct 468 ms 3256 KB Output is correct
40 Correct 454 ms 3268 KB Output is correct
41 Correct 345 ms 3264 KB Output is correct
42 Correct 324 ms 3156 KB Output is correct
43 Correct 329 ms 3260 KB Output is correct
44 Correct 330 ms 3256 KB Output is correct