2017年ACM第八届省赛J题:Company - SDUT3902

本文介绍了一种通过合理安排商品销售顺序来最大化总收益的方法。在考虑商品价值随时间递增的特点下,采取一种类似贪心算法的方式,优先出售单位价值较高的商品,直到某天出售商品不再增加总收益。

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

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大学生程序设计竞赛(感谢青岛科技大学)

由题干可知,benefit=val*i; 也就是说这件物品每放一天就会增值一倍,在这个前提下我们当然要先买价格高的(有一丢丢贪心的思想),只要这天盈利(物品的价值+增值>0),就可以购买。

//贪心
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
#define maxn 100010
typedef struct Node{
    int val,cnt;
}Node;
Node mes[maxn];
long long sum[maxn];

bool cmp(Node A,Node B)
{
   if(A.val!=B.val)
        return A.val>B.val;
    return A.cnt>B.cnt;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>mes[i].val;
    for(int i=0;i<n;i++)
        cin>>mes[i].cnt;
    sort(mes,mes+n,cmp);
    memset(sum,0,sizeof(sum));
    bool flag=true;
    int i=1;
    for(int j=0;j<n;j++)
    {
        if(!flag)
            break;
        while(mes[j].cnt!=0){
            if(sum[i-1]+mes[j].val>0)  //增值的+这一天要买的
            {
                //cout<<"****"<<mes[j].val<<endl;
                sum[i]=sum[i-1]+mes[j].val;  
                i++;
                mes[j].cnt--;
            }else{
                flag=false;
                break;
            }

        }
    }
    long long res=0;
    for(int j=0;j<i;j++)
        res+=sum[j];
    cout<<res<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值