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. ubuntu 去除开机背景

    sudo apt remove plymouth sudo sed -i 's/ splash//g' /etc/default/grub sudo update-grub

  2. Appium初始化设置:手写代码连接手机、appium-desktop连接手机

    一.包名获取的三种方式 1)找开发要2)mac使用命令:adb logcat | grep START win使用命令:adb logcat | findstr START 或者可以尝试使用第3条命令 ...

  3. Django token 学前小知识

    1,base64 '防君子不防小人' 方法 作用 参数 返回值 b64encode 将输入的参数转化为base64规则的串 预加密的明文,类型为bytes:例:b‘guoxiaonao’ base64 ...

  4. web前端知识体系大全【转载】

    自己总结的web前端知识体系大全[欢迎补充]   1. 前言 大约在几个月之前,让我看完了<webkit技术内幕>这本书的时候,突然有了一个想法.想把整个web前端开发所需要的知识都之中在 ...

  5. angularJS 入门知识

    模块:模块可以定义自己的控制器.服务.工厂类以及指令 模块可以依赖其他模块 模块两大常见错误: 定义模块的时候忘记第二个参数,变成使用模块而不是定义模块 使用模块的时候忘记引用依赖模块

  6. git 上传本地代码

    新增本地代码到远程库 http://blog.csdn.net/hanhailong726188/article/details/46738929 github配置教程 http://www.runo ...

  7. shape和reshape

    import numpy as np a = np.array([1,2,3,4,5,6,7,8]) #一维数组 print(a.shape[0]) #值为8,因为有8个数据 print(a.shap ...

  8. python系列——文件操作的代码

    import numpy as np import os,sys #获取当前文件夹,并根据文件名 def path(fileName): p=sys.path[0]+'\\'+fileName ret ...

  9. Compile Linux Kernel on Ubuntu 12.04 LTS (Detailed)

    This tutorial will outline the process to compile your own kernel for Ubuntu. It will demonstrate bo ...

  10. mapreduce实验

    代码: public class WordCount { public static void main(String[] args) throws IOException, ClassNotFoun ...