Input
Output对于每组测试数据,输出最大高度。格式:Case 第几组数据: maximum height = 最大高度Sample Input
Sample Output
这个题有连个限制条件,你要先按照一个拍好序
bool cmp(node a,node b){ if (a.x==b.x) return a.y>b.y; return a.x>b.x; }
然后你就枚举一个i,向前遍历符合条件就跟新结果就行
for(int i=1;i<=cnt;i++){ dp[i]=a[i].h; for(int j=i-1;j>=1;j--){ if(a[i].x<a[j].x&&a[i].y<a[j].y){ dp[i]=max(dp[i],dp[j] + a[i].h); } } ans=max(ans,dp[i]); }
还有一点,就是这个长方形的长宽高可以换的
void PushUp(int t1,int t2,int t3) { a[ant].h = t3; a[ant].x = t1; a[ant].y = t2; ant++; a[ant].h = t3; a[ant].x = t2; a[ant].y = t1; ant++; a[ant].h = t2; a[ant].x = t1; a[ant].y = t3; ant++; a[ant].h = t2; a[ant].x = t3; a[ant].y = t1; ant++; a[ant].h = t1; a[ant].x = t2; a[ant].y = t3; ant++; a[ant].h = t1; a[ant].x = t3; a[ant].y = t2; ant++; }
#include <stdio.h> #include <cstring> #include <algorithm> /* Monkey and Banana */ using namespace std; #define CLR(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define LL long long struct node { int x,y,h; //让x >= y }a[200]; int ant; bool cmp(node a,node b) { if (a.x == b.x) return a.y > b.y; return a.x > b.x; } void PushUp(int t1,int t2,int t3) { a[ant].h = t3; a[ant].x = t1; a[ant].y = t2; ant++; a[ant].h = t3; a[ant].x = t2; a[ant].y = t1; ant++; a[ant].h = t2; a[ant].x = t1; a[ant].y = t3; ant++; a[ant].h = t2; a[ant].x = t3; a[ant].y = t1; ant++; a[ant].h = t1; a[ant].x = t2; a[ant].y = t3; ant++; a[ant].h = t1; a[ant].x = t3; a[ant].y = t2; ant++; } int main() { int n; int ans; int dp[200]; int Case = 1; while (~scanf ("%d",&n) && n) { ant = 1; for (int i = 1 ; i <= n ; i++) { int t1,t2,t3; scanf ("%d %d %d",&t1,&t2,&t3); PushUp(t1,t2,t3); } ant--; printf ("Case %d: maximum height = ",Case++); sort (a+1 , a+1+ant , cmp); ans = 0; for (int i = 1 ; i <= ant ; i++) { dp[i] = a[i].h; for (int j = i - 1 ; j >= 1 ; j--) { if (a[i].x < a[j].x && a[i].y < a[j].y) dp[i] = max (dp[i] , dp[j] + a[i].h); } ans = max (ans , dp[i]); } printf ("%d\n",ans); } return 0; }
LDU中的题:
#include<iostream> #include<algorithm> using namespace std; const int maxn=1e5+100; struct node{ int x,y,h; }a[maxn]; int dp[maxn]; int cnt=0; bool cmp(node a,node b){ if (a.x==b.x) return a.y>b.y; return a.x>b.x; } void push(int x1,int y1,int h1){ a[++cnt].h=x1; a[cnt].x=y1; a[cnt].y=h1; a[++cnt].h=x1; a[cnt].x=h1; a[cnt].y=y1; a[++cnt].h=y1; a[cnt].x=x1; a[cnt].y=h1; a[++cnt].h=y1; a[cnt].x=h1; a[cnt].y=x1; a[++cnt].h=h1; a[cnt].x=x1; a[cnt].y=y1; a[++cnt].h=h1; a[cnt].x=y1; a[cnt].y=x1; } int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ int x1,y1,h1; cin>>x1>>y1>>h1; push(x1,y1,h1); } sort(a+1,a+cnt+1,cmp); int ans=0; for(int i=1;i<=cnt;i++){ dp[i]=a[i].h; for(int j=i-1;j>=1;j--){ if(a[i].x<a[j].x&&a[i].y<a[j].y){ dp[i]=max(dp[i],dp[j] + a[i].h); } } ans=max(ans,dp[i]); } cout<<ans<<endl; }
原文:https://www.cnblogs.com/lipu123/p/14338700.html