SDUT 3902 company【思维】

本文探讨了一种最优销售策略问题,给出N种物品及其价值和数量,通过合理安排销售顺序以最大化总收益。文章提供了详细的解题思路及高效算法实现。

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

company

Time Limit: 1000MS  Memory Limit: 65536KB
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.

Example Input
4
-1 -100 5 6
1 1 1 2
Example 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.

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

题目大意:

给你N种物品,每种物品已知两个元素,价值和个数。

每天可以卖出一个物品,第一天卖出的价值是val.第二天卖出的价值是val*2,第三天卖出的价值是val*3.............依次类推。

现在可以舍弃一些物品不去卖,问最大收益。


思路:


首先肯定是要按照物品价值从小到大排序的。

而且我们已知如果商品的价值>0就一定不要舍弃掉,所以我们不舍弃掉的价值<0的部分就是为了垫高天数的。

所以哦们按照物品价值从小到大排序之后呢,我们只要枚举一个物品作为第一天卖出的物品然后向后扫维护最大值即可。

那么时间复杂度O(n^2);

直接这么做肯定要TLE.

所以我们倒序维护即可,当前物品选定之后,之前选定的物品就都要多加一遍,所以维护一个后缀和即可。

过程维护一个最大值。时间复杂度O(n);


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long int
struct node
{
    ll val,cnt;
}a[1540];
ll b[180000];
ll sum[180000];
int main()
{
    ll n;
    while(~scanf("%lld",&n))
    {
        for(ll i=0;i<n;i++)scanf("%lld",&a[i].val);
        for(ll i=0;i<n;i++)scanf("%lld",&a[i].cnt);
        ll tot=0;
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<a[i].cnt;j++)
            {
                b[tot++]=a[i].val;
            }
        }
        sort(b,b+tot);
        ll ans=0;
        ll output=0;
        ll sum=0;
        for(ll i=tot-1;i>=0;i--)
        {
            sum+=b[i];
            output+=sum;
            ans=max(ans,output);
        }
        printf("%lld\n",ans);
    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值