1. 程式人生 > >山東省第八屆ACM省賽 J 題(company)

山東省第八屆ACM省賽 J 題(company)

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 i⋅val.
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.

題意:商店買東西,有n種商品,每種商品賣出去以後得到的價值是v,每種商品有c件,第i天賣出v價值的商品,得到的價值是i*v,時間是從第一天開始,每天只能賣一件,對於每件商品你可以選擇賣或者不賣;

思路:把每件商品都存到一個數組裡,排序,找到第一個不是負數的數,記錄位置,然後把這個位置之後的(包括它自己)按順序全賣,然後把這個位置往前移一位,再進行,記錄得到的最大的價值;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 100010
#define INF 0x3f3f3f3f
using namespace std;
int d[maxn];
int n,v[1010],c[1010];
long long sum,MAX;
int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&v[i]);
    }
    for(int i=0; i<n; i++)
    {
        scanf("%d",&c[i]);
    }
    int ii=1;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<c[i]; j++)
        {
            d[ii++]=v[i];
        }
    }
    sort(d+1,d+ii);
    d[0]=-1;  //這個地方是為了避免全是正數或者全是負數的情況;
    d[ii]=1;
    int xx=0;
    for(int i=1; i<ii; i++) //找的第一個不是負數的數;
    {
        if(d[i]>=0&&d[i-1]<0)
        {
            xx=i;
            break;
        }
    }
    MAX=-INF;
    for(int i=0; i<xx; i++) //挨個的往前取,並把最大的儲存;
    {
        int day=1;
        sum=0;
        for(int j=xx-i; j<ii; j++)
        {
            sum+=day*d[j];
            day++;
        }
        if(MAX<sum) MAX=sum;
    }
    cout<<MAX<<endl;
    return 0;
}