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. LeetCode(Two Sum)

    一.题目要求 Given an array of integers, return indices of the two numbers such that they add up to a spec ...

  2. HTML页面生成ASPX页面

    这个功能是在DTcms Demo网站里面扣出来的一个小功能,他的Demo网站里面可以在HTML写好所有的代码,然后生成一下ASPX页面,就可以访问了.具体的流程下篇文章来分析.这篇文章主要是讲HTML ...

  3. 自学youku_web

    仿youku架构 数据库设计 管理员 注册 登录 上传视频 删除视频 发布公告 普通用户 注册 登录 充会员 查看视频 下载免费视频 下载收费视频 查看观影记录 查看公告 思路 class Field ...

  4. fine ui grid控件添加行号

    grid控件的列中添加如下代码<f:RowNumberField EnablePagingNumber="true" TextAlign="Center" ...

  5. =>符号的意义

    => 是 Oracle 中调用存储过程的时候, 指定参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用. 下面是具体的例子. 参数的默认值SQL> CREATE ...

  6. 【学时总结】◆学时·VI◆ SPLAY伸展树

    ◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...

  7. 多任务版udp聊天器

    import socket import threading def send_msg(udp_socket): """获取键盘数据,并将其发送给对方"&quo ...

  8. HTML+CSS : H5+CSS3

    HTML5语义化标签: header nav(导航) article section(章节) aside(侧边栏) footer------------------------------------ ...

  9. input标签中的name

    <input>标签是java web的jsp页面中最常用的标签,特别是用来在页面和servlet中传递内容, 但是我们看到<input>标签中有很多内容,这边我们只提一下主要的 ...

  10. list函数及list对象的reverse方法

    list的reverse方法,是就地reverse,不返回值 如a是一个list,a.reverse()直接将a这个list给reverse了,所以如果print(a.reverse())就是None ...