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. NB-IOT_BC95_B5常用AT指令集

    .AT+<cmd>=? 测试命令,用于向模块询问支持的设置项目. .AT+<cmd>? 读取命令,用于让模块上报某个命令代表的设置项当前的值. .AT+<cmd>= ...

  2. linux下core file size设置笔记

    现象说明:突然发现一台测试机器的java程序莫名其妙地没了,但是没有core dump!这就需要打开服务器的core文件生成的功能了,(即core dump文件),方便程序调试.1)core文件简介c ...

  3. Docker容器学习梳理 - 容器间网络通信设置(Pipework和Open vSwitch)

    自从Docker容器出现以来,容器的网络通信就一直是被关注的焦点,也是生产环境的迫切需求.容器的网络通信又可以分为两大方面:单主机容器上的相互通信,和跨主机的容器相互通信.下面将分别针对这两方面,对容 ...

  4. Notes of Daily Scrum Meeting(12.24)

    平安夜了,我们还在这里辛苦地赶代码,整个人都不好了... 今天的团队任务总结如下: 团队成员 今日团队工作 陈少杰 寻找大神帮助,调试网络连接 王迪 建立搜索的UI界面,把算法加入进去 金鑫 调试网络 ...

  5. 作业二 —— 分布式版本控制系统Git的安装与使用

    作业要求源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103 1.安装Git,配置用户名与邮箱. 安装Windows版的Git ...

  6. shell脚本--制作自己的服务脚本

    首先注意一下,我用的环境是centos6.5,中间有一些操作和在Ubuntu上有一些地方的操作是不同的, 编写脚本 首先看一个实例:假设有一个test的服务,可以通过命令对test进行启动.关闭或者重 ...

  7. Java基础知识中的注意事项

    设置Java的相关路径,举例: JAVA_HOME  -->   C:\Program Files\Java\jdk1.8.0_191 Path --->   C:\Program Fil ...

  8. [百家号]雷电3和USB Type-C究竟有什么区别?

    雷电3和USB Type-C究竟有什么区别? https://baijiahao.baidu.com/s?id=1617271490773519582&wfr=spider&for=p ...

  9. 【转帖】M1、M2增速

    M1.M2增速的背离与广义财政的资金滞留有关 作者: 万钊 2016-07-19 17:20 近期M1.M2增速的背离,引起了各方的极大关注.我们知道,M1以活期存款为主,具有高波动性,其增速与M2背 ...

  10. [转帖] infiniband的协议速度