Submission #516348

# Submission time Handle Problem Language Result Execution time Memory
516348 2022-01-21T07:38:12 Z Jomnoi Orchard (NOI14_orchard) C++17
25 / 25
332 ms 49172 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int INF = 1e9 + 7;

vector <vector <int>> a;
vector <int> dp;

int get(int x1, int x2, int y1, int y2) {
    return a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1];
}

int main() {
    int n, m;
    scanf(" %d %d", &n, &m);
    a.resize(n + 10, vector <int> (m + 10));
    dp.resize(m + 10);

    int sum = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            scanf(" %d", &a[i][j]);

            if(a[i][j] == 0) {
                a[i][j] = 1;
            }
            else {
                sum++;
                a[i][j] = -1;
            }
            a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
        }
    }

    int ans = INF;
    dp[m + 1] = INF;
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= n; j++) {
            for(int k = m; k >= 1; k--) {
                dp[k] = min(dp[k + 1], get(i, j, 1, k));
            }

            if(DEBUG) {
                cout << "Debugging : " << i << " " << j << endl;
                for(int k = 1; k <= m; k++) {
                    cout << dp[k] << " ";
                }
                cout << endl;
            }

            for(int k = 1; k <= m; k++) {
                ans = min(ans, sum + dp[k] - get(i, j, 1, k - 1));
            }
        }
    }
    cout << ans;
    return 0;
}

Compilation message

orchard.cpp: In function 'int main()':
orchard.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     scanf(" %d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
orchard.cpp:23:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |             scanf(" %d", &a[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 972 KB Output is correct
2 Correct 2 ms 948 KB Output is correct
3 Correct 3 ms 936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 47404 KB Output is correct
2 Correct 109 ms 49172 KB Output is correct
3 Correct 109 ms 49172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 5404 KB Output is correct
2 Correct 22 ms 5772 KB Output is correct
3 Correct 21 ms 5788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 10 ms 400 KB Output is correct
3 Correct 10 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 321 ms 3432 KB Output is correct
2 Correct 332 ms 3428 KB Output is correct
3 Correct 314 ms 3424 KB Output is correct