ACM 第八届山东省赛 J company SDUT 3902

本文介绍了一种基于贪心算法的编程题解决方案,通过倒序处理商品库存与收益,实现利益最大化。问题背景为一家公司拥有多种商品,在价格波动下如何安排销售顺序以获得最大总收益。

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

贪心算法 ,只能用一个for循环 并且 用long long int  数据量会非常大

倒着 从后面考虑  叠楼梯

 层数代表天数 有前到后依次增加 


代码: 

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdio.h>
#include <string>

typedef long long ll;
using namespace std;
ll a[1000000];
ll dp[1000000];
int main()
{
    int k,i,j,n;
    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0,j=0;i<n;i++)
        {
            cin>>k;
            while(k>1)
            {
                a[n+j]=a[i];
                j++;
                k--;
            }
        }

        sort(a,a+n+j);
        int len=n+j;
        ll ans=0;
        ll dsum=0;
        //dp[len-1]=a[len-1];
        for(i=len-1;i>=0;i--)
        {
            dsum+=a[i];
            if(dsum<0)
                break;
            dp[i]=dp[i+1]+dsum;
            if(ans<dp[i])
                ans=dp[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
5
-1 -1 -1 -1 1
1 1 1 1 1

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值