company(山东省-省赛-J-company-思维题)

题目链接:http://exam.upc.edu.cn/problem.php?id=3400

这个题目真的不错,一开始没理解,后来写一写真的不错。(北邮今年虽然有失误,但是题目出的真不错!!!)

这个题   题意是给定          N个货物的价值,    N个货物的数量。

                                它所求的最大值=(序号×价值)的总和。

我们可以第一时间想到的就是把:        价值大的   ×序号大的    贪心想法。

如示例    -1         -100        5         6

                   1              1         1          2

答案51

6            6            6            6

6            6            6    

5            5

-1    

首先第一步就是需要把它离散化然后就需要排序。

如图所示,就好比三角形不断加斜边,    至于怎么加呢,把这个val的种类记录下来,

tmp的值=    ans(上一次的)+    tot(种类)    +    当前    b[i];

 不断更新ans的值,只要ans比之前小了,break;

具体看代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef struct point{
    ll x,num;
}p;
int cmp(ll a,ll b){
    return a>b;
}
int main()
{
    ll n;
    scanf("%lld",&n);
    p a[1005];
    ll cnt=0,sum=0,b[100500];
    for(int i=0;i<n;i++){
        scanf("%lld",&a[i].x);
    }for(int i=0;i<n;i++){
        scanf("%lld",&a[i].num);
    }
    for(int i=0;i<n;i++){
        while(a[i].num--){
            b[cnt++]=a[i].x;
        }
    }
    sort(b,b+cnt,cmp);
    ll tot=0,tmp=0,ans=0;
    
    if(b[0]<=0){
        return 0*printf("0\n");
    }else{
        for(int i=0;i<cnt;i++){
            tmp=ans+tot+b[i];
            tot+=b[i];
            if(tmp<ans){
                break;
            }
            else ans=tmp;
        }
    }

    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/z_sea/article/details/80288415
今日推荐