Submission #124753

# Submission time Handle Problem Language Result Execution time Memory
124753 2019-07-03T21:09:21 Z johutha Orchard (NOI14_orchard) C++14
25 / 25
405 ms 33632 KB
#include <iostream>
#include <vector>
#include <algorithm>

#define int int64_t

using namespace std;

struct orchard
{
    vector<vector<int>> orchard;

    vector<vector<int>> table;

    int n, m;


    void printtable()
    {
        for (auto t : table)
        {
            for (auto i : t)
            {
                cout << i << " ";
            }
            cout << "\n";
        }
        cout << "\n";
    }

    int prefsum(int x1, int y1, int x2, int y2)
    {
        return table[x1][y1] + table[x2][y2] - table[x1][y2] - table[x2][y1];
    }

    int calc(int y1, int y2)
    {
        vector<int> arr(m + 1);
        for (int i = 0; i <= m; i++)
        {
            arr[i] = 2*prefsum(y1, i, y2, 0) - i*(y1 - y2);
        }
        int mmin = 0;
        int mp = 0;
        int maxdiff = 0;
        pair<int,int> msp = {0, 0};
        for (int i = 1; i <= m; i++)
        {
            if (arr[i] < mmin)
            {
                mmin = arr[i];
                mp = i;
            }
            if (arr[i] - mmin > maxdiff)
            {
                maxdiff = arr[i] - mmin;
                msp = make_pair(mp, i);
            }
        }
        
        int in = prefsum(y1, msp.second, y2, msp.first);
        int wrongin = (msp.second - msp.first) * (y1 - y2) - in;
        int wrongout = prefsum(n, m, 0, 0) - in;
        return wrongin + wrongout;
    }

    int createtable()
    {
        table.resize(n + 1, vector<int>(m + 1));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                table[i + 1][j + 1] = table[i + 1][j] + table[i][j + 1] - table[i][j] + orchard[i][j];
            }
        }
        int mmin = 100000000000;
        for (int y1 = 0; y1 <= n; y1++)
        {
            for (int y2 = 0; y2 <= y1; y2++)
            {
                mmin = min(mmin, calc(y1, y2));
            }
        }
        return mmin;
    }
};

signed main()
{
    int n, m;
    cin >> n >> m;
    orchard orch;
    orch.n = n;
    orch.m = m;
    orch.orchard.resize(n, vector<int>(m));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> orch.orchard[i][j];
        }
    }
    cout << orch.createtable() << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 760 KB Output is correct
2 Correct 6 ms 888 KB Output is correct
3 Correct 6 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 307 ms 33524 KB Output is correct
2 Correct 304 ms 33632 KB Output is correct
3 Correct 303 ms 33604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 5352 KB Output is correct
2 Correct 59 ms 5476 KB Output is correct
3 Correct 58 ms 5348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 504 KB Output is correct
2 Correct 16 ms 760 KB Output is correct
3 Correct 16 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 395 ms 13688 KB Output is correct
2 Correct 403 ms 13688 KB Output is correct
3 Correct 405 ms 13560 KB Output is correct