company 买商品 dp 贪心

J - company

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

use MathJax to parse formulas

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.

 


题意:商店买东西,有n种商品,每种商品卖出去以后得到的价值是v,每种商品有c件,第i天卖出v价值的商品,时间是从第一天开始,每天只能卖一件,对于每件商品你可以选择卖或者不卖;

第i天卖出v价值的商品,得到的价值是i*v,


思路:把商品价值从小到大排列 ,k个价格相同的商品也加入数组,得到所有价格排序的a[n+j] 然后从后向前扫 sum 记录已经扫过的商品的总价值(val)  最后ans 记录最后的dp[i],终止状态

#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--;  //每种价值k件商品 
            }  
        } //所有的商品都放入a数组 
  
        sort(a,a+n+j);  //所有的商品都进行排序  
        int len=n+j;  
        ll ans=0;  
        ll sum=0;  
       
        for(i=len-1;i>=0;i--)  
        {  
            sum+=a[i];// 
			//从大到小买,如果sum<0 接下来的也是负数肯定会亏,所以跳出循环,输出此时的盈利,不继续买了  
            if(sum<0)  
                break;  
            dp[i]=max(dp[i+1],dp[i+1]+sum) ;
           // dp[i]=dp[i+1]+sum; //dp[i+1]其实是前一个状态 
           //if(ans<dp[i])  
               ans=dp[i];  
        }  
        cout<<ans<<endl; //不能用dp[0] 因为不一定能搜到0,中途可能跳出 
    }  
    return 0;  
}  

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值