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
题目分析:排序就好
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
bool compare(const int& a, const int& b)
{
return a < b;
}
int main()
{
int NC, NP;
int total = ;
cin >> NC;
vector<int> C(NC);
for (int i = ; i < NC; i++)
cin >> C[i];
cin >> NP;
vector<int>P(NP);
for (int i = ; i < NP; i++)
cin >> P[i];
sort(C.begin(), C.end(), compare);
sort(P.begin(), P.end(), compare);
auto it1 = C.begin();
auto it2 = P.begin();
for(; it1!=C.end()&&it2!=P.end()&&*it1<&&*it2<;)
{
total += (*it1) * (*it2);
it1++;
it2++;
}
it1 = (C.end() - );
it2 = (P.end() - );
while (it1>=C.begin() &&it2>= P.begin()&&*it1>&&*it2>)
{
total += (*it1) * (*it2);
it1--;
it2--;
}
cout << total;
return ;
}

1037 Magic Coupon (25分)的更多相关文章

  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 (25 分)

    题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. AAAAAccep ...

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

  4. A1037 Magic Coupon (25 分)

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

  5. 1037. Magic Coupon (25)

    #include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...

  6. PAT甲题题解-1037. Magic Coupon (25)-贪心,水

    题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include ...

  7. PAT (Advanced Level) 1037. Magic Coupon (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  8. 1037 Magic Coupon (25 分)

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

  9. PAT 1037 Magic Coupon[dp]

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

随机推荐

  1. 基于VR三维全景的虚拟展馆展览实现

    VR三维全景虚拟现实技术的应用,能够通过全方位互动式来还原真实场景,令人产生一种身临其境的感觉,由于三维全景虚拟现实技术具有一定应用优势,其在企业与院校展示.建筑规划展示.酒店宾馆展示等方面都逐步得到 ...

  2. PHP变量存储结构

    首先声明,我并没有去读PHP的源码,只是对于PHP的有时候诡异的表现感兴趣,找了一下开发人员laruence的博客结合PHP提供的函数debug_zval_dump刺探得到了本博客所阐述的工作机理.如 ...

  3. 基于linux下的NIST数字测试包安装过程

    基于linux下的NIST数字测试包安装过程 1. 首先解决windows文件不能粘贴到Ubuntu的问题 选择利用VMware Tools进行解决 打开虚拟机VMware Workstation,启 ...

  4. 一个和与后台数据连接的模板get post put 以及延伸的query

    /* example: require.config({ paths: { "httpClient": "../../core/http-client" } } ...

  5. Android开发走过的坑(持续更新)

    1 华为 nova真机 打印不出Log 参考资料:http://www.apkbus.com/thread-585228-1-1.html 解决:针对权限问题,我们当然也可以解决的,华为手机在你的拨号 ...

  6. 当AI遇上K8S:使用Rancher安装机器学习必备工具JupyterHub

    Jupyter Notebook是用于科学数据分析的利器,JupyterHub可以在服务器环境下为多个用户托管Jupyter运行环境.本文将详细介绍如何使用Rancher安装JupyterHub来为数 ...

  7. spring Boot登录验证之验证码 邮箱

    一 验证码 登录login.jsp <%@ page contentType="text/html;charset=UTF-8" language="java&qu ...

  8. 微信小程序开发-小程序之间的跳转

    前几天开发微信小程序,其中有个需要联动宣传的业务,就是正在开发的小程序跳转到别的小程序去, 然后去看了下大家的做法与看法,总结下这小程序跳转之间应该注意到的几个问题 首先是跳转的方法, https:/ ...

  9. 33. CentOS7 静态ip设置

    1.网络连接选择NAT模式: 2.关闭vmware的dhcp:选择编辑-->虚拟网络编辑器,选择VMnet8,去掉使用本地DHCP服务将ip地址分配给虚拟机(D). 3. 点击NAT设置(S)查 ...

  10. udp和tcp特点 实现文件上传

    本周课程安排: 网络编程结束 并发网络开头 进程 线程 IO模型 上周内容回顾: 1.osi七层:应用层,表示层,会话层,传输层,网络层,数据链路层,物理连接层 也有人把他们归纳为五层: 应用层, 传 ...