1607. Nested Shrubbery Boxes

1607. Nested Shrubbery Boxes

单点时限: 2.0 sec

内存限制: 256 MB

After each commission to install a shrubbery, Roger the Shrubber has to transport many empty planting boxes with a drawn cart. In this instance, a planting box is a wooden box with one open side.

Given a set of n planting boxes, compute the largest number of boxes that can be nested. Specifically, report the number of the largest subset of boxes which may be nested such that the smallest box of the subset fits within the second smallest, the second smallest of the subset fits within the third smallest, the third smallest of the subset fits within the fourth smallest, and so forth.

A box i (bi) fits into box j (bj) if there exists some rotation of bi such that each dimension of bi is less than the corresponding dimension of bj. Any box can be rotated to nest inside another box.

输入格式

The input will consist of an unspecified number of box sets. Each set will begin with a line containing n, 0 <= n <= 500, the number of boxes in the set. Each box will be described on its own line by three positive integers representing length, width and height (Each value will not exceed 1000). The first two numbers of each box description will be followed by a space, the letter ‘x’, and a space. The end of input occurs when n = -1.

输出格式

For each set of boxes, print a line containing the largest number of boxes that can be selected from the original set to form a fully nesting subset of boxes.

样例

input

5
145 x 472 x 812
827 x 133 x 549
381 x 371 x 900
271 x 389 x 128
718 x 217 x 491
4
432 x 123 x 139
942 x 844 x 783
481 x 487 x 577
677 x 581 x 701
-1

output

2
4

心得: 动态规划。一开始以为必须开口朝上嵌套,或者嵌套成没有开口朝上时不能继续嵌套,后面想通了,从小的开始嵌套,无论哪个方向一定可以嵌套成功。

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

void swapInt(int &a,int &b){
	int t=a;
	a=b;
	b=t;
}

int main(){
	int n;
	while(scanf("%d",&n)&&n!=-1){
		vector<vector<int>> boxs;
		for(int i=0;i<n;i++){
			vector<int> tmp(3);
			scanf("%d x %d x %d",&tmp[0],&tmp[1],&tmp[2]);
			if(tmp[0]>tmp[1])swapInt(tmp[0],tmp[1]);
			if(tmp[1]>tmp[2])swapInt(tmp[1],tmp[2]);
			if(tmp[0]>tmp[1])swapInt(tmp[0],tmp[1]);
			boxs.push_back(tmp);
		}
		
		sort(boxs.begin(),boxs.end(),[](auto& a,auto& b){
			return a[2]<b[2];
		});
		
		vector<int> dp(n,1);
		for(int i=1;i<n;i++){
			for(int j=0;j<i;j++){
				if(boxs[i][0]>boxs[j][0]&&boxs[i][1]>boxs[j][1]){
					dp[i]=max(dp[i],dp[j]+1);
				}
			}
		}
		cout<<dp[n-1]<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值