首页 > 其他 > 详细

1037 Magic Coupon (25分)

时间:2019-12-10 15:07:51      阅读:78      评论:0      收藏:0      [点我收藏+]

The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product... but hey, magically, they have some coupons with negative N‘s!

For example, given a set of coupons { 1 2 4 − }, and a set of product values { 7 6 − − } (in Mars dollars M$) where a negative 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 will have to pay M$12 to the shop.

Each coupon and each product may be selected at most once. Your task is to get as much money back as possible.

Input Specification:

Each input file contains one test case. For each case, the first line contains the number of coupons N?C??, followed by a line with N?C?? coupon integers. Then the next line contains the number of products N?P??, followed by a line with N?P?? product values. Here 1, and it is guaranteed that all the numbers will not exceed 2?30??.

Output Specification:

For each test case, simply print in a line the maximum amount of money you can get back.

Sample Input:

4
1 2 4 -1
4
7 6 -2 -3

Sample Output:

43
题目分析:排序就好
技术分享图片
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<stack>
 8 #include<algorithm>
 9 #include<string>
10 #include<cmath>
11 using namespace std;
12 bool compare(const int& a, const int& b)
13 {
14     return a < b;
15 }
16 int main()
17 {
18     int NC, NP;
19     int total = 0;
20     cin >> NC;
21     vector<int> C(NC);
22     for (int i = 0; i < NC; i++)
23         cin >> C[i];
24     cin >> NP;
25     vector<int>P(NP);
26     for (int i = 0; i < NP; i++)
27         cin >> P[i];
28     sort(C.begin(), C.end(), compare);
29     sort(P.begin(), P.end(), compare);
30     auto it1 = C.begin();
31     auto it2 = P.begin();
32     for(; it1!=C.end()&&it2!=P.end()&&*it1<0&&*it2<0;)
33     {
34         total += (*it1) * (*it2);
35         it1++;
36         it2++;
37     }
38     it1 = (C.end() - 1);
39     it2 = (P.end() - 1);
40     while (it1>=C.begin() &&it2>= P.begin()&&*it1>0&&*it2>0)
41     {
42         total += (*it1) * (*it2);
43         it1--;
44         it2--;
45     }
46     cout << total;
47     return 0;
48 }
View Code

 

1037 Magic Coupon (25分)

原文:https://www.cnblogs.com/57one/p/12016743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!