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

 #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//牛客这道题涉及数相乘会int溢出,而PAT没有,所以pat使用int类型就可以,而牛客需要使用longlong型
long long Nc, Np, p = , q = , a, res = ;
int main()
{
cin >> Nc;
vector<long>Vc, Vp;
for (int i = ; i < Nc; ++i)
{
cin >> a;
Vc.push_back(a);
}
cin >> Np;
for (int i = ; i < Np; ++i)
{
cin >> a;
Vp.push_back(a);
}
sort(Vc.begin(), Vc.end(), [](long long u, long long v) {return u < v; });
sort(Vp.begin(), Vp.end(), [](long long u, long long v) {return u < v; });
while (p < Nc&&q < Np&&Vc[p] < && Vp[q] < )//先负数相乘
{
res += Vc[p] * Vp[q];
++p;
++q;
}
p = Nc - ;
q = Np - ;
while (p >= & q >= && Vc[p] > && Vp[q] > )//正数从后开始乘
{
res += Vc[p] * Vp[q];
--p;
--q;
}
cout << res << endl;
return ;
}

PAT甲级——A1037 Magic Coupon的更多相关文章

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

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

  2. PAT 甲级 1037 Magic Coupon

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

  3. A1037. Magic Coupon

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

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

  5. A1037 Magic Coupon (25 分)

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

  6. PAT_A1037#Magic Coupon

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

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  8. PAT 1037 Magic Coupon[dp]

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

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. sql数据库还原,出现媒体簇的结构不正确,SQLServer无法处理此媒体簇的解决方法

    问题: sql数据库还原,出现媒体簇的结构不正确,SQL Server无法处理此媒体簇. 异常如下图. 造成问题的原因: 我的电脑上安装了sql2005和sql2008,问题就在于我用sql2008的 ...

  2. 云-腾讯云-云通信:云通信(IM)

    ylbtech-云-腾讯云-云通信:云通信(IM) 企业数字化转型的通信助手,让通信触达全球,智联万物 1.返回顶部 1. 云通信(Instant Messaging,IM)承载亿级 QQ 用户即时通 ...

  3. tcp通信,解决断包、粘包的问题

    1.TCP和UDP的区别 TCP(transport control protocol,传输控制协议)是面向连接的,面向流的,提供高可靠性服务.收发两端(客户端和服务器端)都要有一一成对的socket ...

  4. <Python基础>装饰器的基本原理

    1.装饰器 所谓装饰器一般是对已经使用(上线)的函数增加功能. 但是因为一般的大公司的严格按照开放封闭原则(对扩展是开放的,对修改是封闭的),不会让你修改原本的函数. 装饰器就是在不改变原本的函数且不 ...

  5. <每日一题>题目12:列表解析及zip、dict函数的简单应用

    ''' 分析: 1.列表解析:迭代机制的一种应用 语法: [expression for iter_val in iterable] [expression for iter_val in itera ...

  6. nodejs实现读取文件

    今天后端同事下班,想让我读取一个文件的内容.我想这个是小忙啊,就立马答应了. 我知道这个是nodejs读取,可是我又想,平时我们都要起一个服务才能够运行node服务器, 比如如下代码 var http ...

  7. ubuntu下apache服务器操作方法小结,具有参考借鉴价值

    这篇文章主要介绍了ubuntu下apache服务器操作方法小结,非常不错,具有参考借鉴价值,需要的朋友可以参考下(http://www.0831jl.com)Linux系统为Ubuntu 一.Star ...

  8. Centos--swoole平滑重启服务

    平滑重启: 已经打开的服务: 首先在server服务中为进程添加名字: /** * @param $server */ public function onStart($server) { swool ...

  9. light oj 1095 组合数学

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  10. iOS之CATiledLayer的属性简介和使用

    1.CATiledLayer简介 CATiledLayer用于大型图片进行分割显示,需要显示的图片才会加载,直接上代码: - (void)viewDidLoad { [super viewDidLoa ...