HDU1069——Monkey and Banana
2013-03-27 21:43
369 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069
题目说长方形是无限的,但是上一层的长方形的长和宽必须严格的小于下一层的长方形的长和宽。因为每组数据有x,y,z,所以考虑分别用xy,xz,yz作为长方形的长和宽,这样每一组数据输入都对应有三种可能。把题目完全抽象出来就是,把每一组数据的三种可能都存放到一个数组里面,再按面积大小进行排序,然后求出最长的递增子序列的对应的高的和就可以了。
参考代码:
题目说长方形是无限的,但是上一层的长方形的长和宽必须严格的小于下一层的长方形的长和宽。因为每组数据有x,y,z,所以考虑分别用xy,xz,yz作为长方形的长和宽,这样每一组数据输入都对应有三种可能。把题目完全抽象出来就是,把每一组数据的三种可能都存放到一个数组里面,再按面积大小进行排序,然后求出最长的递增子序列的对应的高的和就可以了。
参考代码:
#include<stdio.h> #include<stdlib.h> struct tu { int x; int y; int z; }s[105]; int compare1(const void *a, const void *b) { return *(int *)a - *(int *)b; } int compare2(const void *a, const void *b) { struct tu *t1 = (struct tu *)a; struct tu *t2 = (struct tu*)b; return (t1->x * t1->y) - (t2->x * t2->y); } inline int max(const int a,const int b) { return a > b ? a : b; } int dp[105]; void main() { int n; int i,j; int m; int t = 1; int Max; int a[3]; while(scanf("%d",&n)) { if(n == 0) break; j = 0; while(n--) { for(i = 0; i < 3; ++i) { scanf("%d",&a[i]); } qsort(a,3,sizeof(int),compare1); s[j].x = a[1]; s[j].y = a[2]; s[j++].z = a[0]; s[j].x = a[0]; s[j].y = a[2]; s[j++].z = a[1]; s[j].x = a[0]; s[j].y = a[1]; s[j++].z = a[2]; } m = j; qsort(s,m,sizeof(s[0]),compare2); Max = 0; for(i = 0; i < m; ++i) { dp[i] = s[i].z; for(j = 0; j < i; ++j) { if(s[j].x < s[i].x && s[j].y < s[i].y) dp[i] = max(dp[i],dp[j] + s[i].z); } Max = max(dp[i], Max); } printf("Case %d: maximum height = %d\n",t++, Max); } }
相关文章推荐
- hdu 1069 Monkey and Banana
- hdu1069 Monkey and Banana
- C - Monkey and Banana HDU - 1069——基础dp
- HDU 1069 Monkey and Banana(DP 长方体堆放问题)
- HDU-1069-Monkey and Banana-DP
- HDU 1069 Monkey and Banana(最长递减子序列)
- hdu 1069 Monkey and Banana LIS变形
- hdu 1069 Monkey and Banana(dp)
- HDU 1069 —— Monkey and Banana
- HDU 1069 Monkey and Banana
- hdu 1069 Monkey and Banana---dp(也可用 求最长路+floyd水过)
- hdu 1069 Monkey and Banana
- HDU 1069 Monkey and Banana(dp)
- HDU 1069 Monkey and Banana (DP)
- hdu_1069_Monkey and Banana hdu_1078_FatMouse and Cheese poj_1836_Alignment
- HDU 1069 Monkey and Banana
- HDU 1069 Monkey and Banana(最大的单调递减序列啊 dp)
- hdu 1069 Monkey and Banana
- hdu-1069-Monkey and Banana(动态规划)
- HDU 1069 Monkey and Banana (动态规划+LIS)