第八届山东省赛题 J company 【贪心】

本文介绍了一个关于商品库存收益最大化的问题。通过将商品按价值排序并逐日出售,旨在找到最优出售策略,以实现总收益的最大化。文章提供了一段C++代码实现,通过排序和动态调整策略来求解该问题。

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

2018: J company

时间限制: 1 Sec  内存限制: 128 MB
提交: 4  解决: 4
[ 提交][ 状态][ 讨论版]

题目描述

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 pricechanges, for any goods sold on dayi, 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 untilyou can't or don't want to do so. If you are allowed to leave some goodsunsold, what's the max total benefit you can get in the end?

输入

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

输出

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

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

样例输入

4-1 -100 5 61 1 1 2

样例输出

51


列出每种物品的的价值以及数量,要求每天只能卖一个商品。

我们先把所有商品的价格存在数组里,从小到大排序。然后在算买全部商品得到的钱数ans

再从数组的最左边开始枚举,逐个删去,在过程中取ans的最大值


删的过程中,会改变其他商品卖的天数,。。。。


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

typedef long long LL;
const int MAX=100000+5;

struct ss
{
    int cnt,val;
};
int re[MAX];
int sum[MAX];
int main()
{
    int n;

    while(cin>>n)
    {
        int flag=0;
        ss num[1005];

        memset(re,0,sizeof(re));
        memset(sum,0,sizeof(sum));

        for(int i=0; i<n; i++)
        {
            scanf("%d",&num[i].val);
        }
        for(int i=0; i<n; i++)
        {
            scanf("%d",&num[i].cnt);
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<num[i].cnt; j++)
            {
                re[flag++]=num[i].val;///把所有商品的所有价格存到数组里
            }
        }

        sort(re,re+flag);

        sum[0]=re[0];///sum数组记录商品价格和
        LL ans=re[0];///ans为最终答案

        for(int i=1; i<flag; i++)
        {
            sum[i]=re[i]+sum[i-1];
            ans+=(i+1)*re[i];
        }

        for(int i=0; i<flag; i++)///从数组最左边开始枚举,逐一删去
        {
            LL ls=ans-re[i]-sum[flag-1]+sum[i];///改变的价格
            if(ls>ans)///取过程中的最大值
                ans=ls;
            else
                break;
        }
        printf("%lld\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值