J - company
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
use MathJax to parse formulas
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.
题意:商店买东西,有n种商品,每种商品卖出去以后得到的价值是v,每种商品有c件,第i天卖出v价值的商品,时间是从第一天开始,每天只能卖一件,对于每件商品你可以选择卖或者不卖;
第i天卖出v价值的商品,得到的价值是i*v,
思路:把商品价值从小到大排列 ,k个价格相同的商品也加入数组,得到所有价格排序的a[n+j] 然后从后向前扫 sum 记录已经扫过的商品的总价值(val) 最后ans 记录最后的dp[i],终止状态
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdio.h>
#include <string>
typedef long long ll;
using namespace std;
ll a[1000000];
ll dp[1000000]; // 所能得到的最大利润
int main()
{
int k,i,j,n;
while(cin>>n)
{
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0,j=0;i<n;i++)
{
cin>>k;
while(k>1)
{
a[n+j]=a[i];
j++; //统计一通有多少商品
k--; //每种价值k件商品
}
} //所有的商品都放入a数组
sort(a,a+n+j); //所有的商品都进行排序
int len=n+j;
ll ans=0;
ll sum=0;
for(i=len-1;i>=0;i--)
{
sum+=a[i];//
//从大到小买,如果sum<0 接下来的也是负数肯定会亏,所以跳出循环,输出此时的盈利,不继续买了
if(sum<0)
break;
dp[i]=max(dp[i+1],dp[i+1]+sum) ;
// dp[i]=dp[i+1]+sum; //dp[i+1]其实是前一个状态
//if(ans<dp[i])
ans=dp[i];
}
cout<<ans<<endl; //不能用dp[0] 因为不一定能搜到0,中途可能跳出
}
return 0;
}