此问题与求上升序列最大和类似,可以作为DAG模型计算。将每一快砖分解为3块,将所有砖块按照底排序,注意sort排序中涉及到底的两个参数x,y,这时候一定要有优先排,比如先排x再排y,不能同时排x和y,下面排序写法是错误的:
bool operator<(Rec a){ return x<a.x&&y<a.y; }
/*----UVa437 --首先将每一个长方体按照三个方向,分解为3个长方体 --用dp[i]表示以第i个长方体为底所得到的最大高度 --问题其实和hdu1087一样 */ #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<vector> #include<string.h> #include<algorithm> using namespace std; const int MAXN = 100; struct Rec{ int x, y,h; Rec(int a=0,int b=0,int c=0) :x(a), y(b), h(c){} bool operator<( Rec a){ //一定要有优先比较顺序,先按照x再按照y if (x == a.x) return y < a.y; return x < a.x; } }; Rec rec[MAXN]; int dp[MAXN]; int n; /*vector<int>vec[MAXN]; int dfs(int i){ int &ans = dp[i]; if (ans >= 0) return ans; ans = rec[i].h; for (int j = 0; j < (int)vec[i].size(); j++) ans = max(ans, dfs(vec[i][j]) + rec[i].h); return ans; }*/ int main(){ int i,j,iCase=1; int a[3]; while (scanf("%d", &n) && n){ int cnt =1; for (i = 0; i < n; i++){ scanf("%d%d%d", &a[0], &a[1], &a[2]); sort(a, a + 3); rec[cnt++] = Rec(a[0],a[1],a[2]); rec[cnt++] = Rec(a[1],a[2],a[0]); rec[cnt++] = Rec(a[0],a[2],a[1]); } n = cnt; sort(rec, rec + n); int ans = 0; for (i = 1; i < n; i++){ dp[i] = rec[i].h; for (j = 1; j < i; j++){ if (rec[j].x < rec[i].x&&rec[j].y < rec[i].y) dp[i] = max(dp[i], dp[j] + rec[i].h); } ans = max(ans, dp[i]); } /*for (i = 0; i < n; i++){ vec[i].clear(); for (j = 0; j < n; j++){ if (rec[j] < rec[i]) vec[i].push_back(j); } } int ans = 0; memset(dp, -1, sizeof(dp)); for (i = 1; i <n; i++){ dp[i] = dfs(i); ans = max(ans, dp[i]); }*/ printf("Case %d: maximum height = %d\n",iCase++,ans); } return 0; }
时间: 2024-10-09 09:45:04