Skip to content

Instantly share code, notes, and snippets.

@yurahuna
Created November 4, 2016 13:03
Show Gist options
  • Save yurahuna/6dd9125d2be522b9fb4680242827c230 to your computer and use it in GitHub Desktop.
Save yurahuna/6dd9125d2be522b9fb4680242827c230 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(_v) for(auto _x:_v){cout<<_x<<" ";}cout<<endl
#define printVS(_vs) for(auto _x : _vs){cout << _x << endl;}
#define printVV(_vv) for(auto _v:_vv){for(auto _x:_v){cout<<_x<<" ";}cout<<endl;}
#define printP(_p) cout << _p.first << " " << _p.second << endl
#define printVP(_vp) for(auto _p : _vp) printP(_p);
typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;
int H, W, S, U;
int a[35][35];
Pii dp[35][35][35][35];
Pii merge(Pii p, Pii q) {
return Pii(p.first + q.first, min(p.second, q.second));
}
int calcSum(int x1, int y1, int x2, int y2) {
return a[x1][y1] - a[x2][y1] - a[x1][y2] + a[x2][y2];
}
Pii dfs(int x1, int y1, int x2, int y2) {
Pii& ret = dp[x1][y1][x2][y2]; // (#group, mergin)
if (ret != Pii(0, inf)) return ret;
if (calcSum(x1, y1, x2, y2) >= U) {
ret = Pii(1, calcSum(x1, y1, x2, y2) - U);
} else {
return ret;
}
rep2(c, y1 + 1, y2) {
ret = max(ret, merge(dfs(x1, y1, x2, c), dfs(x1, c, x2, y2)));
}
rep2(r, x1 + 1, x2) {
ret = max(ret, merge(dfs(x1, y1, r, y2), dfs(r, y1, x2, y2)));
}
return ret;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
while (cin >> H >> W >> S, H) {
rep(i, H + 1) rep(j, W + 1) a[i][j] = 0;
rep(i, H) rep(j, W) cin >> a[i][j];
rep(i, H) {
rrep(j, W) {
a[i][j] += a[i][j + 1];
}
}
rep(j, W) {
rrep(i, H) {
a[i][j] += a[i + 1][j];
}
}
U = calcSum(0, 0, H, W) - S;
assert(U > 0);
rep(i, H + 1) rep(j, W + 1) rep(k, H + 1) rep(l, W + 1) dp[i][j][k][l] = Pii(0, inf);
printP(dfs(0, 0, H, W));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment