HDU-1069 Monkey and Banana (dynamic programming)

Link to the original question; http://acm.hdu.edu.cn/showproblem.php?pid=1069

Insert picture description here
Test sample

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

Question: Give you nnn blocks, each block has its length, width and height (blocks can be placed arbitrarily), the number of blocks is unlimited. Now you have to build a tall tower. The length and width of the upper square must be strictly smaller than the length and width of the lower square. Ask the maximum height you can stack.

Problem-solving idea: Blow up this DP question, it is so good, it was not done when I did it before. Now it can finally be solved. OK, we are on the subject, we found that the square can actually be rotated and placed arbitrarily, so a square actually has 6 6With 6 placement methods, we can think of it as having 6 squares.Then the key to this question is actually to find an increasing sequence of squares. Maximize the height. So we need to sort the squares. According to the specified conditions, we can customize the sorting with length as the main factor and width as the secondary factor.(Here you can overload your own operator or write a cmp function). Then we are going to find the state, we can use dp [i] dp [i]d p [ i ] to indicate that the bottom one is theiiThe maximum height of a tower of i blocks. Then this state must be from the previousi − 1 i-1iDetermined by 1 square. (In other words, from the previousi − 1 i-1i1 cube piled up. ) During the entire transfer process, we have no final state, so we need to use amaxx maxxm A X X variable maximum real memory. Traverse all the squaresmaxx maxxm A X X is the result of this last. Look at the code specifically.

AC code

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#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;
}


Guess you like

Origin blog.csdn.net/hzf0701/article/details/108987515