Magic Coupons

Magic Coupons

There is a magic shop on Mars, providing magic coupon. An integer K printed on each coupon face value, this represents a return if you use coupons when purchasing a commodity, you can be K times the value of the goods! The shops also give away free valuable commodity, but if you receive a free gift when using the coupon face value is positive, you have to lose money to the store K times the value of the goods ...... but it does not matter, as well as the face value is negative the coupon can be used! (Mars really magical)
For example, given a set of coupons in denominations of 1,2,4, -1; corresponds to a group of products, Mars token value of M $ 7,6, -2, -3, wherein the negative value indicates that the goods are free gifts. We can coupon 3 used in Items 1, rewarded M $ 28; and coupon 2 used in Item 2, rewarded M $ 12; and coupon 4 for use on merchandise 4 to give M return $ 3. But if you accidentally use the coupon 3 4 on the goods, you have to lose out to the store M $ 12. Similarly, when you accidentally use 4 coupons on commodity 1, you have to lose out to the store M $ 7.
Each provisions of coupons and each item can only be used once at most, find the greatest reward you can get.
Input formats:
Enter two lines. Firstly, a first row coupon number N, and then given an integer N number of denominations of coupon. The second line gives the number M of first goods, then the integer value of M given commodity. Between M and N [1, 106], all data size of not more than 230, separated by a space between the numbers.
Output formats:
Output can get the greatest return.

Sample input:
. 4. 4. 1 2 -1
. 4. 6. 7 -2 -3
Output Sample:
43

#include<bits/stdc++.h>
using namespace std;
int cmp(int a, int b)
{
    return a>b;
}
int a[1111111], b[1111111];
int main()
{
    int n, m;
    cin>>n;
    for(int i = 0; i < n; i++)
        cin>>a[i];
    cin>>m;
    for(int i = 0; i < m; i++)
        cin>>b[i];
    sort(a, a+n, cmp);
    sort(b, b+m, cmp);
    int apo=0,bpo=0,ane=0,bne=0;///记录正数负数的个数
    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        if(a[i] > 0)
            apo++;
        else
            ane++;
    }
    for(int i = 0; i < m; i++)
    {
        if(b[i] > 0)
            bpo++;
        else
            bne++;
    }
    for(int i = 0; i < min(apo,bpo); i++)//正向加正数乘积
        sum+=a[i]*b[i];
    int ita = n-1, itb = m-1;
    for(int i = 0; i < min(ane,bne); i++)///反向加负数乘积
    {
        sum += a[ita]*b[itb];
        ita--;
        itb--;
    }
    cout<<sum<<endl;
    return 0;
}

Published 29 original articles · won praise 4 · Views 4693

Guess you like

Origin blog.csdn.net/onion___/article/details/88875590