传送门:点击打开链接
题目描述
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 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?
输入
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
#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;
}