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.
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
题意:有n种商品,每种商品有多种,每种商品的价值是v,每种商品有c件,第day天只能卖一件商品,最后价值为 day*v
最后要求其最大价值
思路:我们把每种商品都购买,价值越大的商品,在天数越大时购买,其价值就会越大(day*v[i]),而且有的商品有多件,这就要把商品都放开,件数多的商品也都 一 一 放在d数组中,这样利于判断,然后将d数组从小到大排序,寻找第一个价值为>0的货物,从它为第一天开始卖会出来一个总的价值,然后从它的前一天开始卖会出来一个总的价值,然后依次往前推移,在过程中就可以把最大价值寻找出来(用 if判断语句)
-100 | -1 | 5 | 6 | 6 | value |
1 | 2 | 3 | 35 | ||
1 | 2 | 3 | 4 | 51 | |
1 | 2 | 3 | 4 | 5 | -33(赔钱了) |
注:每一件商品都要卖,只不过我们要找的是卖货物的最大价值
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
#define maxn 100010
#define INF 0x3f3f3f3f
int d[maxn];
int v[2000];
int c[2000];
long long int sum,ma; //sum和ma必须要定义成long long 不然就会Runtime Error 将类型换为long long之后 就Accepted
int main(){
int n,i,j,k;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&v[i]);
}
for(i=0; i<n; i++)
{
scanf("%d",&c[i]);
}
int x=1;
for(i=0;i<n;i++){
for(j=0;j<c[i];j++){
d[x++]=v[i];
}
}
sort(d+1,d+x);
d[0]=-1;//将d数组的前后位置设置好,放止这个数组里的数据全部都是负数
d[x]=1;
for(i=1;i<x;i++){
if(d[i]>=0 && d[i-1]<0){
k=i;
break;
}
}
ma=-INF;
for(i=0;i<k;i++){
int day=1;
sum=0;
for(j=k-i;j<x;j++){
sum+=day*d[j];
day++;
}
if(ma<sum)//找出最大价值
ma=sum;
}
printf("%lld\n",ma);
return 0;
}