Submission #44136

# Submission time Handle Problem Language Result Execution time Memory
44136 2018-03-30T08:08:29 Z wasyl Orchard (NOI14_orchard) C++11
25 / 25
183 ms 20316 KB
#include <bits/stdc++.h>
#ifndef dbg
#define dbg(...)
#endif
#define all(x) begin(x), end(x)
#define rsz(...) resize(__VA_ARGS__)
#define psh(...) push_back(__VA_ARGS__)
#define emp(...) emplace_back(__VA_ARGS__)
#define prt(...) print(cout, __VA_ARGS__)
#define dmp(...) print(cerr, #__VA_ARGS__, '=', __VA_ARGS__)
#define dprt(...) dbg(print(cerr,__VA_ARGS__))
#define ddmp(...) dbg(dmp(__VA_ARGS__))
using namespace std;using ll=long long;
template<typename t>using V=vector<t>;
template<typename t>void print(ostream& os, const t& a){os<<a<<'\n';}
template<typename t, typename... A>void print
(ostream& os, const t& a, A&&... b){os<<a<<' ';print(os, b...);}

int n, m;
V< int > ilo;
V< V< int > > tb;

inline int spr (V< int >& ilo)
{
    int sum = 0;
    int res = 0;    
        
    for (int i = 0; i < m; ++i)
    {
        sum += ilo[i];
        if (sum < 0)
            sum = 0;
        res = max(res, sum);    
    }
    
    return res;
}

inline int spr (int lo, int hi)
{
    for (int k = 0; k < m; ++k)
        ilo[k] = (tb[hi][k] - tb[lo - 1][k]);
    
dbg(
    dmp(lo, hi);
    for (int& i : ilo)
        cerr << i << ' ';
    cerr << '\n';
)
    
    return spr(ilo);    
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    ilo.rsz(m);
    tb.rsz(n + 1, V< int >(m));
    
    int sum = 0;
    
    for (int i = 1; i <= n; ++i)
    {
        for (int k = 0; k < m; ++k)
        {
            cin >> tb[i][k];
            sum += tb[i][k];
            tb[i][k] = tb[i][k] == 1? 1 : -1;
        }
    }
    
    ddmp(sum);
    
    for (int k = 0; k < m; ++k)
        for (int i = 1; i <= n; ++i)
            tb[i][k] += tb[i - 1][k];    
            
    int res = INT_MAX;
    for (int i = 1; i <= n; ++i)
        for (int k = i; k <= n; ++k)
            res = min(res, sum - spr(i, k));
    
    prt(res);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 2 ms 484 KB Output is correct
4 Correct 2 ms 536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 736 KB Output is correct
2 Correct 3 ms 796 KB Output is correct
3 Correct 3 ms 828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 16284 KB Output is correct
2 Correct 90 ms 18336 KB Output is correct
3 Correct 95 ms 20316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 20316 KB Output is correct
2 Correct 18 ms 20316 KB Output is correct
3 Correct 19 ms 20316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20316 KB Output is correct
2 Correct 7 ms 20316 KB Output is correct
3 Correct 7 ms 20316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 20316 KB Output is correct
2 Correct 183 ms 20316 KB Output is correct
3 Correct 183 ms 20316 KB Output is correct