company(思维 + 贪心)

Problem Description

There are n kinds of goods in the company, with each of them has a inventory of  and direct unit benefit . Now you find due to price changes, for any goods sold on day i, if its direct benefit is val, the total benefit would be ival.
Beginning from the first day, you can and must sell only one good per day until you can't or don't want to do so. If you are allowed to leave some goods unsold, what's the max total benefit you can get in the end?

Input

The first line contains an integers n(1≤n≤1000).
The second line contains n integers val1,val2,..,valn(−100≤.≤100).
The third line contains n integers cnt1,cnt2,..,cntn(1≤≤100).

Output

Output an integer in a single line, indicating the max total benefit.

Sample Input
4
-1 -100 5 6
1 1 1 2
Sample Output
51
Hint

sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51.

Source
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

题意:某公司有n种物品,每种物品的价值为vali,数量为cnti,在第k天卖物品i,公司将获益k*val i,并且必须天天买,如果有一天中断,将不再卖东西,求公司可以获得的最大收益

思路:可以用贪心的思想来解决此问题,val为正的物品肯定要卖出,收益才会增大,而且先卖val小的,后卖大的,

对于val为负数的物品,卖与不卖要考虑最后收益会不会增大

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const int N = 100005;
vector<LL>vec1,vec2;
LL arr[N];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	   scanf("%lld",&arr[i]);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		for(int j=0;j<x;j++){
			if(arr[i]>=0) vec1.push_back(arr[i]);
		    else vec2.push_back(arr[i]);
		}
	}
	sort(vec1.begin(),vec1.end());
	sort(vec2.begin(),vec2.end());
	LL ans=0,sum1=0;
	for(int i=0;i<vec1.size();i++){
		sum1+=vec1[i];
		ans+=vec1[i]*(i+1);
	}
//	printf("ans:  %d\n",ans);
	LL per[N]={0};
	LL sum[N]={0};
	for(int i=vec2.size()-1;i>=0;i--)
	   per[i]=per[i+1]+vec2[i];
	for(int i=vec2.size()-1;i>=0;i--)
	   sum[i]=sum[i+1]+per[i];
	LL maxt=0;
	int k=1;
	for(int i=vec2.size()-1;i>=0;i--){
		maxt=max(maxt,sum1*k+sum[i]);
		k++;
	}
	printf("%lld\n",ans+maxt);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值