Language: Java

/*
　　　∫ ∫ ∫
　　　ノヽ
　　（＿　 ）
　（＿　　　 ）
（＿＿＿＿＿＿ ）
　ヽ(´･ω･)ﾉ　
　　 |　 /
　　 UU
*/
#pragma region macro
#include <bits/stdc++.h>
typedef long long int64;
using namespace std;
using P = pair<int64, int64>;
typedef vector<int> vi;
const int MOD = (int)1e9 + 7;
const int64 INF = 1LL << 62;
const int inf = 1<<30;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
#define bn '\n'
template <typename T>
ostream& operator<<(ostream& os, const vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, pair<T,S> const&P){
    os << "(";
    os << P.first;
    os << " , ";
    os << P.second;
    os << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
     os<<endl;
    return os;
}
vector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};
#pragma endregion
//fixed<<setprecision(10)<<ans<<endl;

template <typename T>
struct SegmentTree{
    /*
    ~~~~1-indexで実装~~~~
            1
        2       3
      4   5   6    7

    ~~~~親・兄弟・子へのアクセスの仕方~~~~
                i<<1
            i           i^1
    i<<1|0   i<<1|1
    */


    using F = function<T(T,T)>;
    int N;
    F func; //関数(minとか)
    T identity; //単位元
    vector<T> data; //上から添字 2*Nくらいのノード
    SegmentTree(){}
    SegmentTree(F f,T identity):func(f),identity(identity){}

    void init(int n_){
        N=1;
        while(N<n_) N<<=1; //完全二分木がいいので2^k個にする
        data.assign(N<<1, identity); //2N個のノード
    }

    //木を構成(vectorを元に各ノードの値を計算)
    void build(const vector<T>& v){
        int n_ = v.size();
        init(n_);
        for(int i=0; i<n_; i++){
            data[N+i] = v[i]; //葉
        }
        for(int i=N-1; i; i--){
            data[i] = func(data[i<<1], data[(i<<1)+1] ); //子をみて親を更新
        }
    }

    //k番目の値をxに変える
    void set_val(int k, T x){
        k+=N; //indexをセグ木でのindexに変換
        data[k] = x; //値の書き換え(葉)
        while(k>>=1){ //右シフトして0にならない間
            data[k] = func(data[k<<1], data[(k<<1)+1] );
        }
    }

    //https://hcpc-hokudai.github.io/archive/structure_segtree_001.pdf
    //閉開区間[left,right)で
    T query(int left,int right){
        T left_val=identity, right_val=identity; 
        for(int l=left+N,r=right+N; l<r; l>>=1,r>>=1){
            if(l&1) left_val = func(left_val, data[l++] );
            if(r&1) right_val = func(data[--r], right_val);
        }
        return func(left_val, right_val);
    }

    //葉の値を取得する
    T get_val(int idx){
        return data[idx+N];
    }
};
template <typename T>
ostream& operator<<(ostream& os, SegmentTree<T> &S){
    int N = S.data.size();
    int cnt = 0;
    int up = 1;
    for(int i=1;i<N;i++){
        if(S.data[i]!=S.identity){
            os << S.data[i];
        }else os << "単";
        os << " ";
        cnt++;
        if (cnt == up){
            os << "\n\n";
            up<<=1;
            cnt=0;
        }else if (cnt%2==0) os<<",";
    }
    os << "\n";
    return os;
};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H,W,K;
    cin >> H >> W >> K;
    vector<string> grid(H);
    REP(i,H) cin >> grid[i];
    
    vector<vector<int>> cnt(H,vector<int> (W,0));
    REP(h,H){
        REP(w,W){
            cnt[h][w] += grid[h][w]-'0';
            if(w) cnt[h][w] += cnt[h][w-1];
        }
    }

    int bit_max = 1<<(H-1);
    int ans = inf;
    REP(mask, bit_max){
        vector<int> div;
        div.emplace_back(0);
        REP(i,H-1){
            if(mask>>i&1){
                div.emplace_back(i+1);
            }
        }
        div.emplace_back(H);
        
        vector<int> DP(W+1,inf);
        // vector<SegmentTree<int>> Seg(div.size(),SegmentTree<int> (
        //     [](int a,int b)->int{
        //         return min(a,b);
        //     }, INT_MAX
        // ));
        // REP(i,div.size()) Seg[i].init(W+1);
        // Seg[i].set_val(0,0);
        
        SegmentTree<int> Seg(
            [](int a,int b)->int{
                return min(a,b);
            }, INT_MAX
        );
        Seg.init(W+1);
        Seg.set_val(0,0);
        
        DP[0] = 0;
        int l_bound = 0;
        REP(r,W+1){ if(r==0) r++;
            debug(r)
            while(true){
                bool ok = true;
                REP(i,div.size()-1){
                    int sum_ = 0;
                    int up=div[i], bot=div[i+1];
                    debug(mp(up,bot))
                    for(int j=up;j<bot;j++){
                        debug(mp(j,r))
                        sum_ += cnt[j][r-1];
                        if(l_bound) sum_ -= cnt[j][l_bound-1];
                    }
                    debug(mp(i,sum_))
                    if(sum_>K) ok = false;
                }
                if(ok) break;
                l_bound++;
            }
            debug(mp(l_bound,r))
            debug(Seg)

            DP[r] = Seg.query(l_bound,r) + 1;
            Seg.set_val(r,DP[r]);
            debug("A")
        }
        debug(div)
        debug(DP)
        int res = DP.back() - 1;
        res += (__builtin_popcount(mask));
        chmin(ans, res);
        debug(res)
        cerr << bn;
    }



    cout << ans << endl;
}

Compiler output:
Main.java:11: error: illegal character: '#'
#pragma region macro
^
Main.java:12: error: illegal character: '#'
#include <bits/stdc++.h>
^
Main.java:14: error: class, interface, enum, or record expected
using namespace std;
^
Main.java:15: error: unnamed classes are a preview feature and are disabled by default.
using P = pair<int64, int64>;
^
  (use --enable-preview to enable unnamed classes)
Main.java:15: error: ';' expected
using P = pair<int64, int64>;
                           ^
Main.java:16: error: class, interface, enum, or record expected
typedef vector<int> vi;
^
Main.java:17: error: class, interface, enum, or record expected
const int MOD = (int)1e9 + 7;
^
Main.java:18: error: class, interface, enum, or record expected
const int64 INF = 1LL << 62;
^
Main.java:19: error: class, interface, enum, or record expected
const int inf = 1<<30;
^
Main.java:20: error: class, interface, enum, or record expected
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
^
Main.java:20: error: '{' expected
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
                ^
Main.java:20: error: illegal start of type
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
                                                ^
Main.java:20: error: > or ',' expected
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
                                                       ^
Main.java:20: error: illegal start of type
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
                                                                            ^
Main.java:21: error: class, interface, enum, or record expected
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
^
Main.java:21: error: '{' expected
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
                ^
Main.java:21: error: illegal start of type
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
                                                ^
Main.java:21: error: > or ',' expected
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
                                                       ^
Main.java:21: error: illegal start of type
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
                                                                            ^
Main.java:22: error: illegal character: '#'
#define REP(i, n) for (int i = 0; i < (n); i++)
^
Main.java:22: error: class, interface, enum, or record expected
#define REP(i, n) for (int i = 0; i < (n); i++)
        ^
Main.java:22: error: class, interface, enum, or record expected
#define REP(i, n) for (int i = 0; i < (n); i++)
                                  ^
Main.java:22: error: class, interface, enum, or record expected
#define REP(i, n) for (int i = 0; i < (n); i++)
                                           ^
Main.java:23: error: illegal character: '#'
#define FOR(i,s,n) for (int i = s; i < (n); i++)
^
Main.java:23: error: class, interface, enum, or record expected
#define FOR(i,s,n) for (int i = s; i < (n); i++)
                                   ^
Main.java:23: error: class, interface, enum, or record expected
#define FOR(i,s,n) for (int i = s; i < (n); i++)
                                            ^
Main.java:24: error: illegal character: '#'
#define ALL(obj) (obj).begin(), (obj).end() //?????????????!!
^
Main.java:25: error: illegal character: '#'
#define debug(x) cerr << #x << ": " << x << "\n";
^
Main.java:25: error: illegal character: '#'
#define debug(x) cerr << #x << ": " << x << "\n";
                         ^
Main.java:26: error: illegal character: '#'
#define mp make_pair
^
Main.java:27: error: illegal character: '#'
#define bn '\n'
^
Main.java:31: error: class, interface, enum, or record expected
    REP(i,N){
    ^
Main.java:33: error: class, interface, enum, or record expected
        if (i!=N-1) os << " ";
        ^
Main.java:34: error: class, interface, enum, or record expected
    }
    ^
Main.java:36: error: class, interface, enum, or record expected
    return os;
    ^
Main.java:37: error: class, interface, enum, or record expected
}
^
Main.java:41: error: class, interface, enum, or record expected
    os << P.first;
    ^
Main.java:42: error: class, interface, enum, or record expected
    os << " , ";
    ^
Main.java:43: error: class, interface, enum, or record expected
    os << P.second;
    ^
Main.java:44: error: class, interface, enum, or record expected
    os << ")";
    ^
Main.java:45: error: class, interface, enum, or record expected
    return os;
    ^
Main.java:46: error: class, interface, enum, or record expected
}
^
Main.java:50: error: class, interface, enum, or record expected
    while(it!=S.end()){
    ^
Main.java:52: error: class, interface, enum, or record expected
        os << " ";
        ^
Main.java:53: error: class, interface, enum, or record expected
        it++;
        ^
Main.java:54: error: class, interface, enum, or record expected
    }
    ^
Main.java:56: error: class, interface, enum, or record expected
    return os;
    ^
Main.java:57: error: class, interface, enum, or record expected
}
^
Main.java:60: error: class, interface, enum, or record expected
    for(auto it=q.begin();it<q.end();it++){
                          ^
Main.java:60: error: class, interface, enum, or record expected
    for(auto it=q.begin();it<q.end();it++){
                                     ^
Main.java:62: error: class, interface, enum, or record expected
        os<<" ";
        ^
Main.java:63: error: class, interface, enum, or record expected
    }
    ^
Main.java:65: error: class, interface, enum, or record expected
    return os;
    ^
Main.java:66: error: class, interface, enum, or record expected
}
^
Main.java:68: error: illegal character: '#'
#pragma endregion
^
Main.java:68: error: class, interface, enum, or record expected
#pragma endregion
        ^
Main.java:91: error: class, interface, enum, or record expected
    SegmentTree(){}
    ^
Main.java:96: error: class, interface, enum, or record expected
        while(N<n_) N<<=1; //??????????2^k????
        ^
Main.java:97: error: class, interface, enum, or record expected
        data.assign(N<<1, identity); //2N?????
        ^
Main.java:98: error: class, interface, enum, or record expected
    }
    ^
Main.java:103: error: class, interface, enum, or record expected
        init(n_);
        ^
Main.java:104: error: class, interface, enum, or record expected
        for(int i=0; i<n_; i++){
        ^
Main.java:104: error: class, interface, enum, or record expected
        for(int i=0; i<n_; i++){
                     ^
Main.java:104: error: class, interface, enum, or record expected
        for(int i=0; i<n_; i++){
                           ^
Main.java:106: error: class, interface, enum, or record expected
        }
        ^
Main.java:107: error: class, interface, enum, or record expected
        for(int i=N-1; i; i--){
                       ^
Main.java:107: error: class, interface, enum, or record expected
        for(int i=N-1; i; i--){
                          ^
Main.java:109: error: class, interface, enum, or record expected
        }
        ^
Main.java:115: error: class, interface, enum, or record expected
        data[k] = x; //??????(?)
        ^
Main.java:116: error: class, interface, enum, or record expected
        while(k>>=1){ //??????0??????
        ^
Main.java:118: error: class, interface, enum, or record expected
        }
        ^
Main.java:125: error: class, interface, enum, or record expected
        for(int l=left+N,r=right+N; l<r; l>>=1,r>>=1){
        ^
Main.java:125: error: class, interface, enum, or record expected
        for(int l=left+N,r=right+N; l<r; l>>=1,r>>=1){
                                    ^
Main.java:125: error: class, interface, enum, or record expected
        for(int l=left+N,r=right+N; l<r; l>>=1,r>>=1){
                                         ^
Main.java:127: error: class, interface, enum, or record expected
            if(r&1) right_val = func(data[--r], right_val);
            ^
Main.java:128: error: class, interface, enum, or record expected
        }
        ^
Main.java:130: error: class, interface, enum, or record expected
    }
    ^
Main.java:135: error: class, interface, enum, or record expected
    }
    ^
Main.java:137: error: class, interface, enum, or record expected
template <typename T>
^
Main.java:142: error: class, interface, enum, or record expected
    for(int i=1;i<N;i++){
    ^
Main.java:142: error: class, interface, enum, or record expected
    for(int i=1;i<N;i++){
                ^
Main.java:142: error: class, interface, enum, or record expected
    for(int i=1;i<N;i++){
                    ^
Main.java:145: error: class, interface, enum, or record expected
        }else os << "?";
        ^
Main.java:146: error: class, interface, enum, or record expected
        os << " ";
        ^
Main.java:147: error: class, interface, enum, or record expected
        cnt++;
        ^
Main.java:148: error: class, interface, enum, or record expected
        if (cnt == up){
        ^
Main.java:150: error: class, interface, enum, or record expected
            up<<=1;
            ^
Main.java:151: error: class, interface, enum, or record expected
            cnt=0;
            ^
Main.java:152: error: class, interface, enum, or record expected
        }else if (cnt%2==0) os<<",";
        ^
Main.java:153: error: class, interface, enum, or record expected
    }
    ^
Main.java:155: error: class, interface, enum, or record expected
    return os;
    ^
Main.java:156: error: class, interface, enum, or record expected
};
^
Main.java:161: error: not a statement
    ios::sync_with_stdio(false);
    ^
Main.java:163: error: not a statement
    cin >> H >> W >> K;
                  ^
Main.java:164: error: not a statement
    vector<string> grid(H);
                        ^
Main.java:165: error: not a statement
    REP(i,H) cin >> grid[i];
                 ^
Mai