Source:

PAT A1037 Magic Coupon (25 分)

Description:

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 Nbeing 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

Keys:

Code:

 /*
Data: 2019-07-23 19:10:20
Problem: PAT_A1037#Magic Coupon
AC: 11:55 题目大意:
集合A和集合B,求集合A中各元素与集合B中各元素乘积之和的最大值
*/
#include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
const int M=1e5+;
int c[M],p[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int nc,np,pos,ans=;
scanf("%d", &nc);
for(int i=; i<nc; i++)
scanf("%d", &c[i]);
scanf("%d", &np);
for(int i=; i<np; i++)
scanf("%d", &p[i]);
sort(c,c+nc,greater<int>() );
sort(p,p+np,greater<int>() );
pos=;
while(pos<nc && pos<np && p[pos]> && c[pos]>)
ans += p[pos]*c[pos++];
sort(c,c+nc,less<int>() );
sort(p,p+np,less<int>() );
pos=;
while(pos<nc && pos<np && p[pos]< && c[pos]<)
ans += p[pos]*c[pos++];
printf("%d", ans); return ;
}

PAT_A1037#Magic Coupon的更多相关文章

  1. PAT1037:Magic Coupon

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

  2. 1037 Magic Coupon (25 分)

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

  3. PAT 1037 Magic Coupon[dp]

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

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

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

  5. A1037. Magic Coupon

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

  6. PAT 甲级 1037 Magic Coupon

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

  7. 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 ...

  8. PAT甲级——A1037 Magic Coupon

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

  9. PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]

    题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...

随机推荐

  1. win7系统 无线路由关闭了ssid广播 我手动设置了SSID和密码仍然连接不上

    http://zhidao.baidu.com/link?url=KwDGWPc67avpj2OUPg5UqvtqE_80R80P3xzhNIRI1_X5WnSLG7PLEpybb4TnzDAYAB6 ...

  2. 记录js中的兼容问题及解决办法

    1.获取非行内样式的兼容问题: 2.获取事件对象的兼容问题: 3.事件冒泡的兼容: 4.keyCode的兼容问题: 5.处理默认事件的兼容问题: 6.事件的绑定兼容问题:

  3. 深入理解__proto__ 、constructor和prototype的关系

    深入理解__proto__ .constructor和prototype的关系 2013-11-12 09:56 1390人阅读 评论(3) 收藏 举报  分类: 前端之Javascript(59)  ...

  4. ubuntu下MySQL的安装及远程连接配置(转)

    1.命令窗口中输入sudo apt-get install mysql-server mysql-client 即可(配置文件位置:/etc/mysql/my.cnf 启动文件位置:/etc/init ...

  5. Python的基本数据类型,用户交互

    整数: int 常见的数字都是int类型. 用于计算或者大小的比较 在32位机器上int的范围是: -2**31-2**31-1,即-2147483648-2147483647 在64位机器上int的 ...

  6. Swift——(六)Swift中的值类型

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/twlkyao/article/details/34855597     在Swift中,结构体和枚举 ...

  7. 58.Partition Equal Subset Sum(判断一个数组是否可以分成和相等的两个数组)

    Level:   Medium 题目描述: Given a non-empty array containing only positive integers, find if the array c ...

  8. Python3学习笔记——异常处理

    #!/usr/bin/env python # 1.异常处理 try: # 主要执行的代码 except IndexError as e: # 对于某些错误需要特殊处理的,可以对特殊错误进行捕捉 pr ...

  9. docker 安装cat

    1.下载cat cat 地址:https://github.com/dianping/cat 进入opt 创建cat文件夹 cd /opt/ mkdir cat cd cat 下载cat git cl ...

  10. python socket的长连接和短连接

    前言 socket中意为插座,属于进程间通信的一种方式.socket库隐藏了底层,让我们更好的专注于逻辑.如果短连接和长连接两概率没搞明白,会被坑的爬不起来. 短连接 一次完整的传输过程,发送方输出流 ...