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. StoryLine3变量存储与跳转后台时的使用

    前言 公司项目原因,接触到storyline3(后面简称SL)课件制作工具,类似ppt,但是又多了互动.交互,且页面元素可添加触发器,触发器中可执行js代码. 1.官方教程 在SL中,会有“了解详情. ...

  2. SpringBoot笔记--FastJson

    FastJson配置 ObjectId class ObjectIdSerializer : ObjectSerializer { override fun write(serializer: JSO ...

  3. Redis+Keepalived高可用环境部署记录

    Keepalived 实现VRRP(虚拟路由冗余)协议,从路由级别实现VIP切换,可以完全避免类似heartbeat脑裂问题,可以很好的实现主从.主备.互备方案,尤其是无状态业务,有状态业务就需要额外 ...

  4. Scrum Meeting 6

                第六次会议 由于之前队员一直在做数据库和编译大作业,课业压力较大,所以软工进度往后拖了好多. No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 N ...

  5. Daily Scrum- 12/31

    Meeting Minutes 更新了统计单词背诵精度的统计数字计算方法: 确定了词反转的效果的动画: Burndown     Progress   part 组员 今日工作 Time (h) 明日 ...

  6. JavaScript代码-----位置决定结果

    刚学JavaScript的时候,即使照着书上的代码敲一遍,运行的时候,得到的结果要么总是和书上的结果不同,要么是没产生效果.学到后面,才明白到其实程序的代码是没错的,错误的是代码的位置! 首先看下面这 ...

  7. node之jade和ejs的使用方法 jade篇

    let express = require('express'); let consolidate = require('consolidate'); let app = express(); // ...

  8. ecshop验证码图片无法显示终极解决办法

    ecshop验证码图片无法显示终极解决办法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2014-06-06   客户在安装好ecshop之后所有前台的证码不显示,后 ...

  9. [转帖]Intel新一代Xeon完整曝光

    AMD已经官宣7nm工艺的第二代EPYC霄龙服务器平台,今年上半年就会大规模出货,而在Intel这边,由于10nm工艺进展还是不够快,在服务器上还是需要14nm继续打天下,而且还有两代14nm工艺产品 ...

  10. git 查看远程分支最后一次提交时间

    背景 因为工程创建时间很长了,项目又特别多,导致代码库中远程分支有100多.想要清理一下远程分支,但又不能盲目的删除,需要一定的参考信息. 可以通过代码最后提交时间来进行判断,但是100多个分支,一个 ...