宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~dalao们点个关注呗~~
都说卖东西需要商业头脑,嗯看来我还是比较有商业头脑的。上题~
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.
题目解析:
1.第一行的数字n代表有几种东西要卖。
2.第二行就输入n个数,代表这n种物品分别的利润,注意不是成本也不是卖的价格,就直接是卖这样东西本身的利润,就不需要考虑成本什么的了。
3.第三行也是输入n个数,代表与第二个行对应位置上的物品的数量。
假设某物品是在第i天卖的,则该物品的总利润=天数(i) 该物品的利润,样例中的51是由-1×1+5×2+6×3+6×4=51得来的,即第一天卖利润为-1的,第二天卖利润为5的,第三天卖利润为6的,第四天还卖利润为6的(利润为6的有两个),-100的直接扔掉不卖,因为亏本太大了(果然有商业头脑)。
注意:只卖赚的不卖赔的不一定利润最大,比如样例中,如果不卖利润为-1的物品,总利润就变成了5×1+6×2+6×3=35元,而35<51。对于每一件物品你只可以选择卖或者不卖,以怎样的顺序卖,只要还打算卖东西,每天就都要连续的卖。
思路:因为第二行和第三行对应位置表示的是同一个物品的利润和数量,所以我用了结构体, 先按照利润快排出来,记录一下第一个为非负数的下标,之前即为利润为负的也就都是赔的,之后就都是赚的,先计算出从第一个非负作为第一天卖的总利润,再依次向前推。
-----------------------------------------------------------------我只是一条可爱的分界线--------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
struct U{
long long int a;
long long int b;
}s[1100];
bool cmp(struct U c,struct U d)
{
return c.a<d.a;
}
int main()
{
long long int w[1001];
int i,j,k=0,c,n,z,x=1;
scanf("%d",&n);
memset(w,0,sizeof(w));
for(i=0;i<=n-1;i++)
scanf("%lld",&s[i].a);
for(i=0;i<=n-1;i++)
scanf("%lld",&s[i].b);
sort(s,s+n,cmp);
for(i=0;i<=n-1;i++){
if(s[i].a>0){
k=i;
break;
}
}
for(z=0;z<=k;){
j=1;c=1;
if(k-z>=0){
j=1;
for(i=z;i<=n-1;i++){
w[x]+=j*s[i].a;
s[i].b--;
j++;
if(s[i].b>0){
i--;c++;
continue;
}
s[i].b+=c;
c=1;
}
x++;
if(s[z].b==1)
z++;
else
{
s[z].b-=1;
}
}
else{
z=1;
break;
}
if((w[x]<=w[x-1])&&x>0)
break;
}
if(x==1)
x++;
if(w[x-1]<0)
w[x-1]=0;
printf("%lld\n",w[x-1]);
return 0;
}
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~dalao们没事多来瞅瞅咯~~
欢迎各位dalao没事的时候来博瞅瞅https://blog.csdn.net/lxt_Lucia~~