1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <cstdio> #include <algorithm> #include <vector> using std::vector;
const int N = 5 + 2; const int M = 100 + 5;
int n, m, K; struct Node { int r, c; } a[M], tmp[M];
int ub[N], db[N], lb[N], rb[N];
int p[N], sum[N];
int ans = 0; void dfs(int id, int ret) { if(id == K + 1) { ans = std::max(ans, ret); return; } if(ret + sum[id] <= ans) return; for(int i = 0; i < id; i++) for(int ki = 0; ki <= 1; ki++) { if(ki == 0) ub[id] = db[i] + 1; else ub[id] = ub[i] - a[id].r; db[id] = ub[id] + a[id].r - 1; if(ub[id] < 1 || db[id] > n) continue; for(int j = 0; j < id; j++) for(int kj = 0; kj <= 1; kj++) { if(kj == 0) lb[id] = rb[j] + 1; else lb[id] = lb[j] - a[id].c; rb[id] = lb[id] + a[id].c - 1; if(lb[id] < 1 || rb[id] > m) continue; int oldret = ret, U = (1 << (id - 1)) - 1; for(int s = 0; s <= U; s++) { int l = lb[id], r = rb[id], u = ub[id], d = db[id]; for(int o = 1; o < id; o++) if(s >> (o - 1) & 1) l = std::max(l, lb[o]), r = std::min(r, rb[o]), u = std::max(u, ub[o]), d = std::min(d, db[o]); ret += (__builtin_popcount(s) & 1 ? -1 : 1) * std::max(r - l + 1, 0) * std::max(d - u + 1, 0); } dfs(id + 1, ret); ret = oldret; } } }
class Posters { public: int maxCover(int width, int height, vector<int> pWidth, vector<int> pHeight) { n = height, m = width, K = pWidth.size(); for(int i = 1; i <= K; i++) tmp[i].r = pHeight[i - 1], tmp[i].c = pWidth[i - 1]; if(K == 0) return 0; if(K == 1) return tmp[1].r * tmp[1].c; ub[0] = n + 1, db[0] = 0, lb[0] = m + 1, rb[0] = 0; for(int i = 1; i <= K; i++) p[i] = i; do { if(p[1] > p[2]) continue; for(int i = 1; i <= K; i++) a[i] = tmp[p[i]]; for(int i = K; i >= 1; i--) sum[i] = sum[i + 1] + a[i].r * a[i].c; ub[1] = 1, db[1] = a[1].r, lb[1] = 1, rb[1] = a[1].c; ub[2] = n - a[2].r + 1, db[2] = n, lb[2] = m - a[2].c + 1, rb[2] = m; dfs(3, a[1].r * a[1].c + a[2].r * a[2].c - std::max(db[1] - ub[2] + 1, 0) * std::max(rb[1] - lb[2] + 1, 0)); } while(std::next_permutation(p + 1, p + K + 1)); return ans; } };
|