PAT甲级——A1037 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 − }, 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 NC, followed by a line with NC coupon integers. Then the next line contains the number of products NP, followed by a line with NP product values. Here 1, 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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//牛客这道题涉及数相乘会int溢出,而PAT没有,所以pat使用int类型就可以,而牛客需要使用longlong型
long long Nc, Np, p = , q = , a, res = ;
int main()
{
cin >> Nc;
vector<long>Vc, Vp;
for (int i = ; i < Nc; ++i)
{
cin >> a;
Vc.push_back(a);
}
cin >> Np;
for (int i = ; i < Np; ++i)
{
cin >> a;
Vp.push_back(a);
}
sort(Vc.begin(), Vc.end(), [](long long u, long long v) {return u < v; });
sort(Vp.begin(), Vp.end(), [](long long u, long long v) {return u < v; });
while (p < Nc&&q < Np&&Vc[p] < && Vp[q] < )//先负数相乘
{
res += Vc[p] * Vp[q];
++p;
++q;
}
p = Nc - ;
q = Np - ;
while (p >= & q >= && Vc[p] > && Vp[q] > )//正数从后开始乘
{
res += Vc[p] * Vp[q];
--p;
--q;
}
cout << res << endl;
return ;
}
PAT甲级——A1037 Magic Coupon的更多相关文章
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
- PAT 甲级 1037 Magic Coupon
https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472 The magic shop in Mars ...
- A1037. Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- 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, ...
- A1037 Magic Coupon (25 分)
一.技术总结 这也是一个贪心算法问题,主要在于想清楚,怎么解决输出和最大,两个数组得确保符号相同位相乘,并且绝对值尽可能大. 可以用两个vector容器存储,然后排序从小到大或是从大到小都可以,一次从 ...
- PAT_A1037#Magic Coupon
Source: PAT A1037 Magic Coupon (25 分) Description: The magic shop in Mars is offering some magic cou ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 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甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 尚学linux课程---3、linux网络说明
尚学linux课程---3.linux网络说明 一.总结 一句话总结: 如果NAT模式:linux,VMnet8,虚拟出来的路由器 要在同一个网段, 那么 linux才能 通过 网络地址转换 经过wi ...
- PandorBox 中安装aria2失败的解决办法
来自:http://www.right.com.cn/forum/thread-174358-1-1.html 不论luci界面还是opkg中安装,都提示缺少依赖包uclibc,pandorabox默 ...
- win7下mysql5.5与mysql5.6同时安装
5.5己正常的情况下,用官方下载的安装包总是不成功,用的官方解压版5.6.44 1.复制my-default.ini到my.ini,只需要改端口就行了,设置base-dir/data-dir反而无法启 ...
- ASP.NET的一些概念
简述实体框架(EF): EF是一种ORM工具,ORM表示对象关联映射. 在RDMS中,对象称为表格和列对象,而在.net中(面向对象)称为类,对象以及属性. EF提供了三种方式来实现项目: l 数据库 ...
- STM32之glossary
glossary Word: data/instruction of 32-bit length. Half word: data/instruction of 16-bit length. Byte ...
- SpringBoot--springboot启动类和controller的配置
作为一个springboot初学者,在探索过程中难免遇到一些坑,边看书边动手,发现书本中的版本是1.0,而我使用的是最新版2.0,所以有些东西不能完全按照书本进行操作,因为2.0中已经不支持1.0中的 ...
- idea git 下载项目,解决冲突,提交代码
git安装 1. 安装git工具上篇文章说过请参考 https://mp.weixin.qq.com/s/A8MkjYTXYSMVRlg25TWemQ idea下载coding代码 打开idea准备下 ...
- 判断语句(if...else)if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码
判断语句(if...else) if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码. 语法: if(条件) { 条件成立时执行的代码 } else { 条件不成立时 ...
- 位运算 - 左移右移运算符 >>, <<, >>>
1-左移运算符m<<n,表示把m左移n位.左移n位的时候,最左边的n位数将被丢弃,同时在最右边补上n个0.例如: 00001010<<2 = 00101000 10001010 ...
- Django缓存机制以及使用redis缓存数据库
目录 Django 配置缓存机制 缓存系统工作原理 Django settings 中 默认cache 缓存配置 利用文件系统来缓存 使用Memcache来缓存: 使用Local-memory来缓存: ...