算法与设计作业三

题目1:古卡萨人为了建造一个高塔,先采集了大量不同类型的石块。每种类型的石块的高度为hi数量为ci。他们在修建高塔之前,先通过占卜确定每种类型的石块能够摆放的最大高度,然后再将他们一块一块垒起来,最终完成了高塔的建造。

    请你根据现有石块的情况计算出他们最高能够建造出的高塔的高度。

输入要求:输入第1行为整数n,表示石块的类型。其后有n行,每一行包含三个整数hi (1 <= hi <= 100) ,ai(1 <= ai <= 40000),ci(1 <= ci <= 10),分别表示该类型的石块的高度,该类型石块能够摆放在塔上的最大高度以及该类型石块的数量。

输出要求:输出1行,包含一个整数,也就是利用这些石块能够建造的高塔的最大高度。

样例输入:

3

7 40 3

5 23 8

2 52 6

样例输出:

48

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector> 

using namespace std ;

typedef pair<int,int> PII ;
const int N = 1e5 + 10  , INF = 0x3f3f3f;
int n , best_h , now_h , now_max , limit_h = INF;
vector<PII>path ;
vector<vector<PII>> res ;

struct Stone {
	int h ;
	int higher ;
	int num ;
	bool operator< (const Stone& W)const {        
        if (higher != W.higher) return higher < W.higher ;              
		return h < W.h ;                      
    } 
}stone[N];

bool check (int index , int num )
{
	if ( stone[index].higher >= stone[index].h * num) {
		if (stone[index].higher + now_h < limit_h) limit_h = now_h + stone[index].higher ;
		return true ;
	}
	
	return false ;
}

void dfs (int index , int flag)
{
	if (index < 1) {
		if (best_h < now_h) {          //更新最大高度 
			res.push_back (path) ;
			best_h = now_h ;
		}
		return ; 
	}
	// 不选择第index个石块 
	dfs (index -  1 , flag) ;
	// 选择第index个石块 
	if (flag == 1 ) limit_h = stone[index].higher ;
	int temp = limit_h ;
	for ( int i = 1 ; i <= stone[index].num ; i++){
		int h = stone[index].h * i ;
		if ( h + now_h <= limit_h && check (index , i)) {
			now_h += h ;
			path.push_back ({index,  i}) ;
			dfs (index - 1 , flag + 1) ;
			now_h -= h ;
			path.pop_back () ;
			limit_h = temp ;
		}
	}	
}

int main ()
{
//	cout <<"请输入石块的数量 : " ;
	cin >> n ; 
	for (int i = 1 ; i <= n ; i++) {
		int h , a , c ;
		scanf ("%d%d%d" , &h , &a , &c) ;
		stone[i] = { h , a , c } ;
	}
	
	sort (stone + 1 , stone + 1 + n) ;

	int flag = 1 ; 
	dfs (n , flag) ;
	
//	int len = res.size() - 1 ;
//	for (int i = 0 ; i < res[len].size() ; i++) cout <<res[len][i].first << "  " << res[len][i].second << endl ;
	cout << best_h  ;
	
	return 0 ;
}

代码已经用原题测试用例试过了涅,没有问题涅

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值