HAUTOJ company

本文介绍了一种算法,用于解决在给定商品种类及其库存数量的情况下,如何通过合理安排销售顺序来实现公司收益最大化的问题。该算法涉及排序和前缀和等数据结构与算法知识。

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

问题 J: company

时间限制: 1 秒  内存限制: 64 MB
提交: 86  解决: 27
 
 
题目描述

There are n kinds of goods in the company, with each of them has a inventory of cnti and direct unit benefit vali. Now you find due to pricechanges, 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 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  6
1    1   1  2
样例输出
51
提示

 

题意:

第二行给定n个数,第三行给定对应每个数的个数。然后选出几个数按一定的顺序排好,最后每个数乘以所在的位置求和。输出最大和。


思路:

把所有数存入数组中,然后从大到小排序,求出最大的前缀和即为答案。


如此题样例:

排序后 6  6  5  -1  -100

一个数  6

两个数 6*2 + 6 = (6+6)+ 6

三个数 6*3 + 6*2 + 5 = (6+6+6) + (6+6) + 5

。。。。。。


此类递增相乘的题要想到前缀和


代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long s[100005];
int main()
{
    
    int n;
    
    int pos=0;
    scanf("%d",&n);
    int cnt=n;
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&s[i]);
        if(s[i]<0)pos++;
    }
    
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        if(a>1)
        {
            for(int j=2;j<=a;j++)
            {
                s[cnt]=s[i];
                cnt++;
                if(s[i]<0)
                    pos++;
            }
        }
    }
    sort(s,s+cnt);
    long long sum=0,ans=0;
    
    long long p=0;
    for(int i=cnt-1;i>=0;i--)
    {
        p+=s[i];
        sum+=p;
        ans=ans>sum?ans:sum;
    }
    printf("%lld\n",ans);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值