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
1.一看到题目应该反映出来它是贪心,背包类型的问题。
2因为题目中给予我们提示:同一件物品,在不同的时间去售卖,它的价值是不一样的,而且停的时间越多也就是越放到后面卖,价值也就会越大。根据这个思路,我们可以把所有的物品按价值从高到低降序排序然后拆开,倒着来考虑:
(1) 题目中的样例也就可以拆成:6 6 5 -1 -100。
(2) 每过一天,价值都会增加一个单位的 val所以,我们用sum来记录:sum=sum+t[i].v;
(3) 然后用一个变量ans来记录总价值ans=ans+sum+t[i].v;
(4) 当且仅当当,前价值小于现在的价值的时候也就不再累加。
3. 再遇到此类题目的时候,一定考虑不同的变量来记录,因为价值每天在改变,需要记录。与此同时,总价值也要不断更新,而且这两者有着密切联系。ans=ans+sum+t[i].v; sum=sum+t[i].v;
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef struct thing {
int v;
int c;
}thing;
thing t[1050];
long long sum=0;
long long ans=0;
bool cmp(thing a,thing b)
{
return a.v<b.v;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
scanf("%d",&t[i].v);
for(int i=0;i<n;i++)
scanf("%d",&t[i].c);
sort(t,t+n,cmp);
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<t[i].c;j++)
{
if(ans+sum+t[i].v>=ans)//这里的ans才是记录的总利益,而sum只是记录当前第n天物品i的价值
{
ans=ans+sum+t[i].v;
sum=sum+t[i].v;
}
}
}
cout<<ans<<endl;
return 0;
}