0
点赞
收藏
分享

微信扫一扫

HDU——1069 Monkey and Banana(动态规划)

香小蕉 2022-06-24 阅读 78

原题链接; ​​http://acm.hdu.edu.cn/showproblem.php?pid=1069​​

HDU——1069 Monkey and Banana(动态规划)_c代码
测试样例

Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

题意: 给你HDU——1069 Monkey and Banana(动态规划)_ios_02个方块,其中每个方块具有它的长宽高(方块可以任意旋转放置),方块数量不限。现在你要堆一个高塔,上面方块的长和宽必须严格小于下面方块的长和宽。问你能堆起来的最大高度。

解题思路: 吹爆这道DP题,出得太好了,以前做的时候没做出来。现在终于可以解决了。OK,我们步入正题,我们发现方块其实是可以任意旋转放置的,所以一个方块其实是有HDU——1069 Monkey and Banana(动态规划)_ios_03种放置方法,我们可以认为是有6个方块。那么这道题的关键其实就是寻找一个递增方块序列。使得高度最大。所以我们需要对方块进行排序。根据指定条件,所以我们可以以长为主要因素,宽为次要因素进行自定义排序。 (这里自己运算符重载或者写一个cmp函数都行)。那么我们就要寻找状态了,我们可以用HDU——1069 Monkey and Banana(动态规划)_c代码_04来表示最下面一个为第HDU——1069 Monkey and Banana(动态规划)_#define_05个方块的高塔的最大高度。那么这个状态肯定是由前HDU——1069 Monkey and Banana(动态规划)_#define_06个方块决定的。(换句话说是由前HDU——1069 Monkey and Banana(动态规划)_#define_06个方块堆起来的。)在整个转移过程中,我们并没有最终状态,所以我们需要用一个HDU——1069 Monkey and Banana(动态规划)_c代码_08变量实时存储最大值。遍历完这所有的方块HDU——1069 Monkey and Banana(动态规划)_c代码_08则是这最后结果。具体看代码。

AC代码

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n;
struct node{
int l,w,h;
bool operator <(const node &a){
if(l==a.l)return w<a.w;
return l<a.l;
}
};
int dp[300];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
int cnt=1;
while(cin>>n&&n){
int x,y,z;
vector<node> blocks;
rep(i,0,n-1){
cin>>x>>y>>z;
blocks.push_back({x,y,z});
blocks.push_back({x,z,y});
blocks.push_back({y,x,z});
blocks.push_back({y,z,x});
blocks.push_back({z,x,y});
blocks.push_back({z,y,x});
}
sort(blocks.begin(),blocks.end());
int len=blocks.size();
int maxx=0;
rep(i,0,len-1){
dp[i]=blocks[i].h;
rep(j,0,i-1){
if(blocks[i].l>blocks[j].l&&blocks[i].w>blocks[j].w){
dp[i]=max(dp[i],dp[j]+blocks[i].h);
}
}
maxx=max(dp[i],maxx);//保存最大值。
}
cout<<"Case "<<cnt++<<": maximum height = "<<maxx<<endl;
}
return 0;
}


举报

相关推荐

0 条评论