其實,dp我一直是拒絕的。這方面我一直十分薄弱。但是難關還是一定要攻克的= =如是我終於開始磕dp專題了。那就先從比較簡單的問題開始吧。
題目大意是有一些不同規格立方塊無限個,把它們疊起來。要求下方的在橫截面上長寬嚴格大於上面的,問疊起來最高的高度。
分析:
考慮到立方塊可以交換長寬高,所以我把一個立方塊變成六個不同的立方塊來思考。將變換長寬高後的立方塊按照關鍵詞先後先a從小到大,再b從大到小,最後c從小到大的順序排序並且去重。這樣,我們能夠簡化考慮的維數。因爲a已經按照從小到大的順序排好了,我們只需要考慮b的大小就可以了。不難想到一個遞推式子
代碼:
/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define DEBUG freopen("#.txt", "r", stdin)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&-x)
const int INF = 0x3f3f3f3f;
const ll INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const ll mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
inline ll bits(ll x) {
return !x ? x : bits(x - lowbit(x)) + 1;
}
/*****************************************************/
const int maxn = 35;
struct Node {
int a, b, c;
bool operator <(const Node &rhs) const {
if (a == rhs.a && b == rhs.b) return c < rhs.c;
if (a == rhs.a) return b > rhs.b;
return a < rhs.a;
}
bool operator ==(const Node &rhs) const {
return a == rhs.a && b == rhs.b && c == rhs.c;
}
}node[maxn * 6];
int N;
int lis[maxn * 3];
int tree[(maxn * 6) << 2];
int dp[maxn * 6];
void PushUp(int v) {
tree[v] = max(tree[slch], tree[srch]);
}
void update(int x, int val, int l, int r, int v) {
if (l == r) tree[v] = max(tree[v], val);
else {
int m = (l + r) >> 1;
if (x <= m) update(x, val, lson);
else update(x, val, rson);
PushUp(v);
}
}
int query(int L, int R, int l, int r, int v) {
if (L > R) return 0;
if (L <= l && r <= R) return tree[v];
int m = (l + r) >> 1;
int ans = -1;
if (L <= m) ans = max(ans, query(L, R, lson));
if (R > m) ans = max(ans, query(L, R, rson));
return ans;
}
int main(int argc, char const *argv[]) {
int kase = 1;
while (~scanf("%d", &N) && N) {
memset(tree, 0, sizeof(tree));
int tot = 0, cnt = 0;
for (int i = 0; i < N; i ++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
node[tot ++] = (Node){a, b, c};
node[tot ++] = (Node){b, a, c};
node[tot ++] = (Node){a, c, b};
node[tot ++] = (Node){c, a, b};
node[tot ++] = (Node){b, c, a};
node[tot ++] = (Node){c, b, a};
lis[cnt ++] = a;
lis[cnt ++] = b;
lis[cnt ++] = c;
}
sort(node, node + tot);
sort(lis, lis + cnt);
int len = unique(node, node + tot) - node;
int num = unique(lis, lis + cnt) - lis;
for (int i = 0; i < len; i ++) {
int p = lower_bound(lis, lis + num, node[i].b) - lis;
int d = query(0, p - 1, 0, num, 1);
dp[i] = d + node[i].c;
update(p, dp[i], 0, num, 1);
}
printf("Case %d: maximum height = %d\n", kase ++, *max_element(dp, dp + len));
}
return 0;
}