【hautoj1306】company

传送门:点击打开链接
题目描述

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 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?

输入

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 max total benefit.

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.

样例输入
4
-1 -100 5 6
1 1 1 2
样例输出
51

题意:
n件物品,第二行分别的单体利润,第三行分别的件数,每天必须卖一件,直到不卖停止。求最大利润。(公式:∑第i天*单体利润)

思路:
从小到大排序,先找出单体利润为0的位置,算出此时的利润,然后向前进一位,再算利润然后比较,依次比较出最大利润。

代码:
解法一:
#include <bits/stdc++.h>
using namespace std;
typedef  long long   ll;
ll a[100010] , x[100010];
ll b[1010];
int main()
{
    int n;
    while (~scanf ("%d",&n))
    {
        for (int i=0; i<n; i++)  scanf("%lld",&b[i]);
        int k = 0;
        for (int i=0; i<n; i++)
        {
            int t;
            scanf ("%d",&t);
            while (t--)  a[k++] = b[i];
        }
        sort(a,a+k);

        ll ans = 0;
        x[0] = 0;
        for (int i=1; i<=k; i++)
            x[i] = x[i-1] + a[k-i];
        for (int i=1; i<=k; i++)
        {
            if (x[i] >= 0)  ans += x[i];
            else  break;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

解法二:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=1002;
int v[1005],t[N*N];
int main()
{
    int n,i,x,cnt=0,j,pos;
    cin>>n;
    for(i=1; i<=n; i++)
        scanf("%d",&v[i]);
    for(i=1; i<=n; i++)
    {
        scanf("%d",&x);
        for(j=1; j<=x; j++)
            t[++cnt]=v[i];
    }
    sort(t+1,t+1+cnt);
    LL sum=0;
    for(i=1; i<=cnt; i++)
    {
        if(t[i]>0)
        {
            pos=i;//记录单体利润为0的位置
            break;
        }
    }
    j=1;
    LL max1=0,sum_part=0;
    for(i=pos; i<=cnt; i++)
    {
        sum=sum+j*t[i];//计算此时总利润
        j++;//天数
        sum_part+=t[i];//不算天数的总利润(以便计算前一位时可以直接加上)
    }
    while(sum>max1&&pos>=1)
    {
        max1=sum;
        sum=sum+sum_part+t[--pos];//sum为向前一天的总利润
        sum_part=sum_part+t[pos];//不算天数的总利润也要更新
    }
    printf("%lld\n",max1);
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值