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:

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

Sample Output:

43
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(long long a, long long b){
return a > b;
}
long long C[], P[], ans = ;
int main(){
int NC, NP;
scanf("%d", &NC);
for(int i = ; i < NC; i++)
scanf("%lld", &C[i]);
scanf("%d", &NP);
for(int i = ; i < NP; i++)
scanf("%lld", &P[i]);
sort(C, C + NC, cmp);
sort(P, P + NP, cmp);
for(int i = ; i < NC && i < NP && C[i] >= && P[i] >= ; i++)
ans += C[i] * P[i];
for(int i = NC - , j = NP - ; i >= && j >= && C[i] <= && P[j] <= ; i--, j--)
ans += C[i] * P[j];
printf("%lld", ans);
cin >> NP;
return ;
}

总结:

1、题意:给出两个集合,每个集合都分别有正数、负数或0。分别从两集合里选出相同个数的数,求他们的乘积之和最大为多少。需要注意的是,本题选取的乘积的组数是任意的,不需要用掉所有的数。因为正正相乘则正数越大积越大,负负相乘负数越小积越大。因此只需要将两个集合从大到小排序,从下标0开始依次正正相乘,直到遇见二者异号。同理从尾部向前负负相乘,直到异号。

A1037. Magic Coupon的更多相关文章

  1. PAT甲级——A1037 Magic Coupon

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

  2. A1037 Magic Coupon (25 分)

    一.技术总结 这也是一个贪心算法问题,主要在于想清楚,怎么解决输出和最大,两个数组得确保符号相同位相乘,并且绝对值尽可能大. 可以用两个vector容器存储,然后排序从小到大或是从大到小都可以,一次从 ...

  3. PAT_A1037#Magic Coupon

    Source: PAT A1037 Magic Coupon (25 分) Description: The magic shop in Mars is offering some magic cou ...

  4. 1037 Magic Coupon (25 分)

    1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an i ...

  5. PAT1037:Magic Coupon

    1037. Magic Coupon (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The magi ...

  6. PAT 1037 Magic Coupon[dp]

    1037 Magic Coupon(25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an in ...

  7. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

  8. PAT 甲级 1037 Magic Coupon

    https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472 The magic shop in Mars ...

  9. PTA(Advanced Level)1037.Magic Coupon

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

随机推荐

  1. Js基础---红宝书读书日记(1)-------基本类型和引用类型

    JS的变量可能包含两种不同数据类型的值,基本类型和引用类型; 基本类型是指简单的数据段,引用类型是指可能由多个值构成的对象; JS高级程序设计第三章介绍了变量分为 5种简单数据类型(string/nu ...

  2. 老牌阅读器nook2刷机整理

    kindle肯定是现在大多数人了解电纸书这个产品的开端,也给我留下了一段美好的回忆,不折腾,不死机,官方书城让人省心不少,不过作为半个折腾爱好者,kindle显然不符合我的理念,遂慢慢入了安卓电纸书的 ...

  3. MySQL两种存储引擎: MyISAM和InnoDB 简单总结

    MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然性能极佳,但却有一个缺点 ...

  4. Buy the Ticket HDU 1133

    传送门 [http://acm.hdu.edu.cn/showproblem.php?pid=1133] 题目描述和分析 代码 #include<iostream> #include< ...

  5. <编写有效用例>读书笔记3

    <编写有效用例>读书笔记3 第三部分主要内容是对忙于编写用例的人的提示第20章:对每个用例的提示1.每个用例都是一篇散文:这个提示提醒我们将注意力集中与文字而不是图画上,同时帮助了解将要遇 ...

  6. python 中的列表(list)

    一.生成一个列表 直接生成 L1 = [1, 2, 3, 4, 5] 列表解析式 >>> L2 = [x for x in range(1, 10, 2)] #从1到10的迭代,步长 ...

  7. The import * cannot be resolved

    背景 使用eclipse jee做练习的时候,下载了老师的项目源码.考虑到老师用的时myeclipse,目录结构略有不同,所有不想直接导入项目,又考虑到,可能环境不一样,会出现这样那样的问题,所以我的 ...

  8. Java使用HTTPClient3.0.1开发的公众平台消息模板的推送功能

    package com.company.product.manager.busniess.impl; import java.io.IOException;import java.nio.charse ...

  9. back to top 回到顶部按钮 css+js

    效果 html <p id="back-to-top"><a href="#top"><span></span> ...

  10. number (2)变量相关错误

    变量没有被定义 fw cannot be resolved 变量没有被初始化 正确代码 package com.itheima_01; import java.io.FileWriter;import ...