CodeForces - 799C Fountains

本文介绍了一种通过购买两种类型的喷泉以最大化其美化值的算法。该算法使用贪心策略,首先对喷泉按美化值排序,然后考虑所有可能的组合方式,包括全用硬币(CC)、硬币和钻石(CD)以及全用钻石(DD)购买喷泉的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

用c个硬币或者d个钻石购买两个喷泉,使得构造到的喷泉的美化值最大并求出美化值。

贪心,对喷泉的美化值排序,判断CC,CD,DD三种情况即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1e5+10;

struct spring{
	int beauty;
	int cost;
	char type;
};
spring f[N];

bool cmp1(spring a,spring b){
	if(a.beauty==b.beauty)
		return a.cost<b.cost;
	return a.beauty>b.beauty;
}

int main(){
	int n,c,d;
	while(~scanf("%d%d%d",&n,&c,&d)){
		for(int i=0;i<n;i++)
			scanf("%d %d %c",&f[i].beauty,&f[i].cost,&f[i].type);
		sort(f,f+n,cmp1);
		
		int ans=0,sumb;
		for(int i=0;i<n;i++){
			sumb=0;
			int costc=c,costd=d;
			if(f[i].type=='C'&&f[i].cost>costc) continue;
			if(f[i].type=='D'&&f[i].cost>costd) continue;
			
			if(f[i].type=='C'&&f[i].cost<=costc){
				costc-=f[i].cost;
				sumb+=f[i].beauty;
			}
			if(f[i].type=='D'&&f[i].cost<=costd){
				costd-=f[i].cost;
				sumb+=f[i].beauty;
			}
			
			for(int j=i+1;j<n;j++){
				if(f[j].type=='C'&&f[j].cost<=costc){
					sumb+=f[j].beauty;
					ans=max(ans,sumb);
					break;
				}
				if(f[j].type=='D'&&f[j].cost<=costd){
					sumb+=f[j].beauty;
					ans=max(ans,sumb);
					break;
				}	
			}	
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值