The topics are as follows:
The Magic shop in Mars is offering some magic coupons. Each coupon have an integer n printed on it, meaning if you use this coupon with a product, you may get n times the Value of that product back! What's more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive n to this bonus product, and you'll have the. The shop N times the value of The bonus product ... but hey, magically, they has some coupons with negative N ' s!
For example, given a set of coupons {1 2 4-1}, and a set of product values {7 6-2-3} (in Mars dollars m$) where a Negat Ive value corresponds to a bonus product. You can apply coupon 3 (with N being 4) to product 1 (with value m$7) to get m$28 back; Coupon 2 to Product 2 to get m$12 back; and coupon 4 to product 4 to get m$3 back. On the other hand, if you apply coupon 3 to product 4, you'll have a to pay m$12 to the shop.
Each coupon and each product is selected at the most once. Your task is to get as much money back as possible.
Input Specification:
Each input file contains the one test case. For each case, the first line contains the number of coupons NC, followed by a line with NC coupon integers. Then the next line contains the number of product NP, followed by a line with NP product values. Here 1<= NC, NP <=, and it's guaranteed that all the numbers would not exceed 230.
Output Specification:
For each test case, simply print in a line the maximum amount of money can get back.
Sample Input:
41 2 4-147 6-2-3
Sample Output:
43
The topic is very simple, in order to get the money, we can only through two ways, ① coupon corresponding positive commodity, ② negative coupon corresponding to negative merchandise.
For positive numbers, we sort in descending order to get the biggest coupons and the highest prices.
For negative numbers, we sort in ascending order to get the most absolute coupons and merchandise.
Therefore, we need to separate positive and negative statistics, so we need 4 containers (coupon and Product +,-two).
After the sorting is finished, the product of the coupon value and the product value is accumulated from the front to the back, note that the size of the container is set to the lower end condition when traversing the container.
#include <iostream> #include <vector> #include <stdio.h> #include <algorithm>using namespace Std;int Comparepos (int a, int b) {return a > B;} int Compareneg (int a, int b) {return a < b;} int main () {vector<int> poscoupons,negcoupons; Vector<int> posproducts,negproducts; int N; Cin >> N; int buffer; for (int i = 0; i < N; i++) {scanf ("%d", &buffer); if (buffer >= 0) {poscoupons.push_back (buffer); }else{negcoupons.push_back (buffer); }} cin >> N; for (int i = 0; i < N; i++) {scanf ("%d", &buffer); if (buffer >= 0) {posproducts.push_back (buffer); }else{negproducts.push_back (buffer); }} sort (Poscoupons.begin (), Poscoupons.end (), comparepos); Sort (Negcoupons.begin (), Negcoupons.end (), Compareneg); Sort (Posproducts.begin (), Posproducts.end (), comparepos); Sort (Negproducts.begin (), NEGPRoducts.end (), Compareneg); First combine positive coupons and positive products int thres = Poscoupons.size () > Posproducts.size ()? Posproducts.size (): Poscoupons.size (); int sum = 0; for (int i = 0; i < thres; i++) {sum + = poscoupons[i] * Posproducts[i]; }//Then combine negative coupons and negative products thres = Negcoupons.size () > Negproducts.size ()? Negproducts.size (): Negcoupons.size (); for (int i = 0; i < thres; i++) {sum + = negcoupons[i] * Negproducts[i]; } cout << sum << endl; return 0;}
1037. Magic Coupon (25)