1037. Magic Coupon (25)

不念不忘少年蓝@ 2022-05-31 10:40 249阅读 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 -1}, and a set of product values {7 6 -2 -3} (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 NC, followed by a line with NC coupon integers. Then the next line contains the number of products NP, followed by a line with NP product values. Here 1<= NC, NP <= 105, and it is guaranteed that all the numbers will not exceed 230.

Output Specification:

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

Sample Input:

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

Sample Output:

  1. 43

题目大意:

火星上的魔法商店提供一些魔法优惠券。每张优惠券都有一个整数N,这意味着当你使用这个优惠券时,你可能会得到这个产品的N倍的价值!此外,该商店还提供一些免费的商品奖励。如果你用一个带有正数N的优惠券购买商品,你将必须支付商店N倍于奖励品的价值。。。。。。但是,嘿,神奇的是,他们有一些负N的优惠券!
例如,给定一组优惠券{1 2 4 -1},以及一组产品值{7 6 -2 -3}(在火星美元M$),其中负值对应一个奖励品。你可以将优惠券3(N为4)应用到产品1(价值为7$)来得到28$,将优惠券2应用到商品2得到12$;将优惠券4应用到商品4得到3$.另一方面,如果你将优惠券3应用到产品4上,你将必须向商店支付12$.
每张优惠券和每个产品最多可以选择一次。你的任务是尽可能的多得到钱。
输出规格:
每个输入文件包含一个测试用例。对于每种情况,第一行都包含了优惠券NC的数量,然后是NC个优惠券。然后下一行包含了产品NP的数量,然后是NP个产品值。1<= NC, NP <= 105,保证所有数字不超过2的30次方。
输出规范:
对于每个测试用例,只需在一行中打印返回的最大金额。

代码:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<vector>
  4. using namespace std;
  5. vector<long long> arrp1;
  6. vector<long long> arrp2;
  7. vector<long long> arrn1;
  8. vector<long long> arrn2;
  9. bool cmpp(long long a,long long b)
  10. {
  11. return a>b;
  12. }
  13. bool cmpn(long long a,long long b)
  14. {
  15. return a<b;
  16. }
  17. int main()
  18. {
  19. int i,j;
  20. long long n,m,k,t,flag;
  21. scanf("%lld",&n);
  22. for(i=1;i<=n;i++)
  23. {
  24. scanf("%lld",&k);
  25. if(k>=0)
  26. arrp1.push_back(k);
  27. else
  28. arrn1.push_back(k);
  29. }
  30. scanf("%lld",&m);
  31. for(i=1;i<=m;i++)
  32. {
  33. scanf("%lld",&k);
  34. if(k>=0)
  35. arrp2.push_back(k);
  36. else
  37. arrn2.push_back(k);
  38. }
  39. sort(arrp1.begin(),arrp1.end(),cmpp);
  40. sort(arrp2.begin(),arrp2.end(),cmpp);
  41. sort(arrn1.begin(),arrn1.end(),cmpn);
  42. sort(arrn2.begin(),arrn2.end(),cmpn);
  43. t=0;
  44. for(i=0;i<arrp1.size()&&i<arrp2.size();i++)
  45. {
  46. t+=arrp1[i]*arrp2[i];
  47. }
  48. for(i=0;i<arrn1.size()&&i<arrn2.size();i++)
  49. {
  50. t+=arrn1[i]*arrn2[i];
  51. }
  52. printf("%lld\n",t);
  53. return 0;
  54. }

发表评论

表情:
评论列表 (有 0 条评论,249人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT乙级1037

    1037 在霍格沃茨找零钱 (20 分) 如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Gal