杭电 3730—— Chain(贪心)

Chain

链接:杭电——3730 Chain(贪心)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1062 Accepted Submission(s): 334

Problem Description
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.

Input

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.

Output

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

Sample Input

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

Sample Output

5
8

思路:当时没想出来写了结构体排了序,后面就不知道咋整了。思路就是最后会有一种数量最多的珠子留下,再去判断。

正确代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node{
	long long num;
	long long k;
};
bool cmp(node a,node b){
	return a.num<b.num;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	long long n;
	while(cin>>n){
		node s[n+5];
		long long ans=0;
		for(int i=0;i<n;i++){
			cin>>s[i].num; 
		}
		for(int i=0;i<n;i++){
			cin>>s[i].k; 
		}
		long long p=n-1;
		sort(s,s+n,cmp);
		for(int i=0;i<p;i++){
			ans+=s[i].num;
		}
		if((ans+1)*s[p].k>=s[p].num){
			ans+=(s[p].num);
		}
		else{
			ans+=((ans+1)*s[p].k);
		}
		cout<<ans<<endl;
		//cout<<ans<<endl;
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值