第八届山东省赛题 J company (贪心,水)

来源:互联网 发布:有sql注入漏洞的网站 编辑:程序博客网 时间:2024/06/05 23:05

2018: J company

时间限制: 1 Sec  内存限制: 128 MB
提交: 4  解决: 4
[提交][状态][讨论版]

题目描述

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 pricechanges, for any goods sold on dayi, if its direct benefit is val, the total benefit would be ival.
Beginning from the first day, you can and must sell only one good per day untilyou can't or don't want to do so. If you are allowed to leave some goodsunsold, 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 maxtotal benefit.

Hint: sell goods whose price with order as -1, 5, 6, 6, thetotal benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51.

样例输入

4-1 -100 5 61 1 1 2

样例输出

51


列出每种物品的的价值以及数量,要求每天只能卖一个商品。

我们先把所有商品的价格存在数组里,从小到大排序。然后在算买全部商品得到的钱数ans

再从数组的最左边开始枚举,逐个删去,在过程中取ans的最大值


删的过程中,会改变其他商品卖的天数,。。。。


#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;typedef long long LL;const int MAX=100000+5;struct ss{    int cnt,val;};int re[MAX];int sum[MAX];int main(){    int n;    while(cin>>n)    {        int flag=0;        ss num[1005];        memset(re,0,sizeof(re));        memset(sum,0,sizeof(sum));        for(int i=0; i<n; i++)        {            scanf("%d",&num[i].val);        }        for(int i=0; i<n; i++)        {            scanf("%d",&num[i].cnt);        }        for(int i=0; i<n; i++)        {            for(int j=0; j<num[i].cnt; j++)            {                re[flag++]=num[i].val;///把所有商品的所有价格存到数组里            }        }        sort(re,re+flag);        sum[0]=re[0];///sum数组记录商品价格和        LL ans=re[0];///ans为最终答案        for(int i=1; i<flag; i++)        {            sum[i]=re[i]+sum[i-1];            ans+=(i+1)*re[i];        }        for(int i=0; i<flag; i++)///从数组最左边开始枚举,逐一删去        {            LL ls=ans-re[i]-sum[flag-1]+sum[i];///改变的价格            if(ls>ans)///取过程中的最大值                ans=ls;            else                break;        }        printf("%lld\n",ans);    }    return 0;}



0 0
原创粉丝点击