题意:已知n种砖,每种砖有无穷多个,问这些砖最多能堆多高,要求堆放时,上面的砖摆放的长和宽必须都严格小于下面砖的长和宽。
分析:
1、将砖堆放时的堆放面上所有的长x和宽y统计出来,去重,且记录时保证x<y。每种砖最多能统计出三种长和宽。
2、如果之前的砖比当前砖长和宽都严格小,则可将之前的砖放在这个砖上面dp[i] = max(dp[i], dp[j] + num[i].h);
3、dp[i]---截止到第i块砖,能堆的最大高度。
4、因为不知道哪个砖放在最底下,所以扫一遍,ans = max(ans, dp[i]);
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define lowbit(x) (x & (-x)) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1000 + 10; const int MAXT = 10000 + 10; using namespace std; map<pair<int, int>, int> mp; struct Node{ int x, y, h; }num[110]; int dp[110]; int main(){ int N; int kase = 0; while(scanf("%d", &N) == 1){ if(!N) return 0; memset(dp, 0, sizeof dp); mp.clear(); int x, y, z; for(int i = 0; i < N; ++i){ scanf("%d%d%d", &x, &y, &z); vector<int> v; v.push_back(x); v.push_back(y); v.push_back(z); sort(v.begin(), v.end()); if(!mp.count(pair<int, int>(v[0], v[1]))){ mp[pair<int, int>(v[0], v[1])] = v[2]; } if(!mp.count(pair<int, int>(v[1], v[2]))){ mp[pair<int, int>(v[1], v[2])] = v[0]; } if(!mp.count(pair<int, int>(v[0], v[2]))){ mp[pair<int, int>(v[0], v[2])] = v[1]; } } int cnt = 0; for(map<pair<int, int>, int>::iterator it = mp.begin(); it != mp.end(); ++it){ num[cnt].x = (*it).first.first; num[cnt].y = (*it).first.second; num[cnt].h = (*it).second; ++cnt; } for(int i = 0; i < cnt; ++i){ dp[i] = num[i].h; for(int j = 0; j < i; ++j){ if(num[j].x < num[i].x && num[j].y < num[i].y){ dp[i] = max(dp[i], dp[j] + num[i].h); } } } int ans = 0; for(int i = 0; i < cnt; ++i){ ans = max(ans, dp[i]); } printf("Case %d: maximum height = %d ", ++kase, ans); } return 0; }