【想一想发现是贪心】HDU3730-Chain

本文介绍了一道关于制作珍珠项链的算法题目,通过分析不同颜色珍珠的数量限制和连续使用相同颜色珍珠的最大数量,给出了一种解决方案。该方案首先对珍珠数量进行排序,并确保除了最多数量的珍珠类型外,其余类型的珍珠均可完全利用。对于最多的珍珠类型,则通过贪心策略来判断是否可以插入到项链中。

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

水题联赛唯一做出的一道题大哭

问题描述

Li Lei has many pearls of N different color. The number of each color is limited. Han Mei is Li Lei’s girl friend.Since Han Mei’s birthday is coming, Li Lei wants to give Han Mei his present for her birthday. Finally, Li Lei decides to make a chain for his lovely girl friend with his beautiful pearls. He wants to make the chain more beautiful, so the number of the continuous pearls with same color is limited.Now, Li Lei wants to know how long his chain will be. He will always make the chain as long as possible. Pay attention that the shape of a chain is a line but not a circle here.

输入说明

The first line is an integer N. It means that Li Lei has many pearls of N different color.The next line contains N integers A(0),A(1)……A(N-1). It means that Li Lei has A(i) pearls of the i-th color.The third line contains N integers B(0),B(1)……B(N-1). It means that in the chain, there will be no more than B(i) continuous pearls of the i-th color.You can assume that A(i) is not smaller than B(i).All integers in the input is positive and no larger than 100000.

输出说明

There is one integer in a line for each test case, representing the length of the chain.

输入样例

3
1 1 100
1 1 1
3
1 1 100
1 1 2

输出样例

5
8

开始没思路,后来有人提供了一个思路,除了最多的,剩下的珍珠品种,我们肯定都能用上全部的数量。然后判断最多的那个能否插空放上珠子,分两种情况讨论,算是一个贪心的问题。数据很大,用__int64。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

const int INF=1000005;

class Zhenzhu
{
	public:	
	__int64 num;
	__int64 zhi;
};

Zhenzhu zhenzhu[INF];

bool cmp(Zhenzhu x,Zhenzhu y)
{
	return x.num<y.num;
}

int main()
{
	int n;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%I64d",&zhenzhu[i].num);
		}		
		for(int i=0;i<n;i++)
		{
			scanf("%I64d",&zhenzhu[i].zhi);
		}
		sort(zhenzhu,zhenzhu+n,cmp);
		__int64 num=n-1;
		__int64 total=0;
		for(int i=0;i<num;i++)
		{
			total+=zhenzhu[i].num;
		}
		
		if((total+1)*zhenzhu[num].zhi>=zhenzhu[num].num)
		{
			total+=zhenzhu[num].num;
		}
		
        else 
        {
			total+=(total+1)*zhenzhu[num].zhi;
        }
		cout<<total<<endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值