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, 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 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≤N**C,N**P≤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
思路
- 简单来说题意就是从
a
和b
两个数字集合各取1个数字,最后看能算出来的最大和是多少 - 很容易想到的贪心策略是:①正数从大到小乘;②负数从小到大乘
代码
#include<bits/stdc++.h>
using namespace std;
int a[100010];
int b[100010];
int main()
{
int nc,np;
scanf("%d", &nc);
for(int i=0;i<nc;i++) scanf("%d", &a[i]);
scanf("%d", &np);
for(int i=0;i<np;i++) scanf("%d", &b[i]);
sort(a, a+nc);
sort(b, b+np);
int p1 = 0;
int p2 = 0;
long long ans = 0;
while(p1 < nc && p1 < np && a[p1] < 0 && b[p1] < 0) //处理负数和负数相乘的情况
{
ans += a[p1] * b[p1];
p1++;
}
p1 = nc - 1;
p2 = np - 1;
while(p1 >= 0 && p2 >= 0 && a[p1] > 0 && b[p2] > 0)
{
ans += a[p1] * b[p2];
p1--;
p2--;
}
printf("%lld\n", ans);
return 0;
}
引用
https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472
PTA(Advanced Level)1037.Magic Coupon的更多相关文章
- PAT (Advanced Level) 1037. Magic Coupon (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 1037 Magic Coupon (25 分)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an i ...
- PAT 1037 Magic Coupon[dp]
1037 Magic Coupon(25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an in ...
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- 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, ...
- PAT 甲级 1037 Magic Coupon
https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472 The magic shop in Mars ...
- 1037 Magic Coupon (25分)
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- 1037 Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
随机推荐
- PHP mysqli_debug() 函数
定义和用法 mysqli_debug() 函数用于执行调试操作. 注释:为了使用该函数,您必须编译 MySQL 客户端库来支持调试.
- VueCropper 图片裁剪
基于vue的图片裁剪vue-cropper 简小咖 关注 0.2 2018.12.12 15:42 字数 164 阅读 3900评论 1喜欢 3 vue-cropper官网http://xyxiao ...
- 053_修改 Linux 系统的最大打开文件数量
#!/bin/bash#往/etc/security/limits.conf 文件的末尾追加两行配置参数,修改最大打开文件数量为 65536 cat >> /etc/security/li ...
- [Luogu] 消息扩散
https://www.luogu.org/problemnew/show/2002 Tarjan 缩点 + 入度判断 #include <iostream> #include <c ...
- 串结构练习——字符串连接(SDUT 2124)
Problem Description 给定两个字符串string1和string2,将字符串string2连接在string1的后面,并将连接后的字符串输出. 连接后字符串长度不超过110. Inp ...
- 【C/C++】Linux的gcc和g++的区别
Windows中我们常用vs来编译编写好的C和C++代码:vs把编辑器,编译器和调试器等工具都集成在这一款工具中,在Linux下我们能用什么工具来编译所编写好的代码呢,其实Linux下这样的工具有很多 ...
- Resize image online 调整图片大小
https://resizeimage.net/ 第一步,先上传图片 第二步,可选的(裁剪图片),这里的type的ratio是指图片的宽高比例(和图片最后的size大小无关) 3. Rotate yo ...
- <JavaScript> 寄生继承详解
// 将原型继承和非原型继承组合为一体的继承方式叫做组合继承,但是这种方法的继承是有一点小缺陷的,下级函数继承了无用的属性,所以我们有了寄生继承来解决污染问题; //创建上级构造函数-食物 funct ...
- CPU分几核几核的是什么意思?
CPU的几核分类其实是按照CPU的地址总线数量来分类的 单核心的地址总线数量是8条 双核心就是16条 三核心就是24 依次类推,核心的多少,只需要知道地址总线的多少,然后除以8就得出来了 地址总线:地 ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_01-自定义查询页面-服务端-Dao
在页面输入查询条件,查询符合条件的页面信息. 查询条件如下: 站点Id:精确匹配 模板Id:精确匹配 页面别名:模糊匹配 spring mongoDB如何自定义条件 在Repository的findA ...