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

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 ival.
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

“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

思路:先把所有价值加入到一个vector中,然后从小到大排序,因为题目要求必须是连续卖物品,所以我们必须把价值为负的放在最前面卖(如果我们要卖负价值的物品的话),剩下的所有正价值的物品从小到大依次卖,越大越往后卖价值肯定越大,因为每个物品的实际卖的钱时它的价值乘所在天数,所以这道题目我只需要考虑到底加哪几个负值的物品可以是价值尽量大,因为前面加了负值的物品后面的物品会依次后移,天数就增加1,所以每增加一个负值的物品,正值的物品全部后移一天就相当于,多加了一个所有正值的和。所以我们只需要判断第k天卖负值时,这天减去的价值只要小于正值和,那么总价值一定是增加的。

因为每增加一个负值物品相当于增加价值 sum-a[i]*k,只要a[i]*k<sum,sum-a[i]*k就大于零,总价值就可以增加

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> a;
int main(){
    int n;
    scanf("%d",&n);
    int q;
    for(int i = 0; i < n; i++){
        scanf("%d",&q);
        a.push_back(q);
    }
    for(int i = 0; i < n; i++){
        scanf("%d",&q);
        if(q > 1){
            for(int j = 0; j < q - 1; j++){
                a.push_back(a[i]);
            }
        }
    }
    sort(a.begin(),a.end());
    int pos = upper_bound(a.begin(),a.end(),0) - a.begin();
    ll sum = 0;
    for(int i = pos; i < a.size(); i++){
        sum += a[i];
    }
    bool ok;
    int record;
    for(int i = 0; i < pos; i++){
        int k = 1;
        for(int j = i; j < pos; j++){
            ll sum2 = 0;
            ok = 0;
            sum2 += a[j] * k;
            k++;
            if(abs(sum2) > sum){
                ok = 1;
                break;
            }
        }
        if(ok == 0){
            record = i;
            break;
        }
    }
    ll ans = 0;
    int cnt = 1;
    for(int i = record; i < a.size(); i++){
        ans += a[i] * cnt;
        cnt++;
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/80183840