https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472

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 <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N, M;
int a[maxn], b[maxn], c[maxn], d[maxn];
int v1[maxn], v2[maxn];
int num1 = 0, num2 = 0, num3 = 0, num4 = 0; bool cmp(int x, int y) {
return x > y;
} int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i ++) {
scanf("%d", &v1[i]);
if(v1[i] >= 0)
a[num1 ++] = v1[i];
else
b[num2 ++] = v1[i];
}
scanf("%d", &M);
for(int i = 1; i <= M; i ++) {
scanf("%d", &v2[i]);
if(v2[i] >= 0)
c[num3 ++] = v2[i];
else
d[num4 ++] = v2[i];
} sort(a, a + num1, cmp);
sort(b, b + num2);
sort(c, c + num3, cmp);
sort(d, d + num4); int len1 = min(num1, num3);
int len2 = min(num2, num4); int sum = 0;
for(int i = 0; i < len1; i ++) {
sum += a[i] * c[i];
}
for(int i = 0; i < len2; i ++) {
if(b[i] * d[i] >= 0)
sum += b[i] * d[i];
}
printf("%d\n", sum);
return 0;
}

  

PAT 甲级 1037 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甲级——A1037 Magic Coupon

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

  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. PAT 1037 Magic Coupon[dp]

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

  5. 1037 Magic Coupon (25 分)

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

  6. 【PAT甲级】1037 Magic Coupon (25 分)

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

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

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

  8. PAT 1037 Magic Coupon

    #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> ...

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

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

随机推荐

  1. javascript原生API总结

    一.查找: getElementById() 方法返回带有指定 ID 的元素(唯一): getElementsByTagName() 返回包含带有指定标签名称的所有元素的节点列表(集合/节点数组). ...

  2. PowerDesigner 图表导出Excel格式

    快捷方式打开运行界面:ctrl+shift+X '*************************************************************************** ...

  3. DFS练习-HDU1010

    题目来源:HDU1010 DFS的基本原则已经差不多了,但是一些技巧仍然比较难想,所以还是加强练习,然后总结一下. 还是先看题意 ,指定迷宫的长,宽以及走出迷宫的具体时间N,M,T. 其中(1 < ...

  4. tar工具(打包,压缩)

    tar工具(打包,压缩)========================= tar打包工具 -c:表示建立一个tar包或者压缩文件包-x:表示解包或者解压缩-v:表示可视化-f: 后面跟文件名(即-f ...

  5. Shell学习——列出当前路径下所有目录

    1.ls -d */[root@client02 ~]# ls -d */Desktop/ Documents/ Downloads/ jq-1.5/ Music/ Pictures/ Public/ ...

  6. yii2 的登录注册 轮子

    //利用到了yii2 框架之中的验证规则 进行判定而已 也不是很高深的东西  但是 使用框架自身的轮子 会有安全性能的隐患 1注册reg controller 中 我都以admin 为例子 publi ...

  7. C6748的GPIO口配置使用

    2018年1月17日更新: 这几天用了创龙的C6748的库,对于GPIO配置十分不爽,我移植了RK6748的库,用起来十分酸爽,把下面的文件加入到工程中,然后include头文件后就可以使用.非常好使 ...

  8. python+scrapy环境搭建步骤描述

    Python3(3.5.4)搭建爬虫系统步骤描述: 1.下载python安装包,路径:https://www.python.org/downloads/windows/  选择3.5.4版本64位的安 ...

  9. POJ 2084 Catalan

    Game of Connections Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8772   Accepted: 43 ...

  10. PTA 7-12(图) 社交网络图中结点的“重要性”计算 最短路

    7-12(图) 社交网络图中结点的“重要性”计算 (30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的 ...