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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- day22_6-re模块
# 参考资料:# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园# https://www.cnblogs.com/guojintao/articles/9070485.html ...
- ArrayList 和linkedList 插入比较
从学Java开始, 就一直大脑记着 arrayList 底层是数组 ,查询快, 插入慢, 有移动的动作.linkedList 底层链表, 插入快 查询慢,今天写了例子跑了跑, 果然. public ...
- WPF实现Drag/Drop操作
原文:WPF实现Drag/Drop操作 有时候我们方便用户操作,总会把一下Copy/Paste 或者 input操作转换为Drag/Drop, WPF 跟之前WinForm 一样提供了一些实现方式方便 ...
- 如何在 Apache Flink 中使用 Python API?
本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink PMC,阿里巴巴高级技术专家 孙金城 分享.重点为大家介绍 Flink Python API 的现状及未来规划, ...
- SG函数博弈——poj2311
关于SG函数的博弈 首先定义必败态 x : SG[x]=0 设任意一个状态y,到所有y能到达的状态连一条边,令这些后继为z y : SG[y]=mex(SG[z]) SG[y]==0 : y就是必败态 ...
- springboot与任务(邮件任务)
邮件发送需要引入spring-boot-starter-mail Spring Boot 自动配置MailSenderAutoConfiguration 定义MailProperties内容,配置在a ...
- 杂项-公司:Apple
ylbtech-杂项-公司:Apple 苹果公司(Apple Inc. )是美国的一家高科技公司.由史蒂夫·乔布斯.斯蒂夫·沃兹尼亚克和罗·韦恩(Ron Wayne)等人于1976年4月1日创立,并命 ...
- PHP实现图片的汉明码提取与降维
作者感言:数学不好,遇到算法问题分分钟狗带,毫无转寰的余地-_-||| 最近心血来潮,看了相似图片的搜索,最最最初级的方法即提取汉明码,之后匹配汉明距离.当然,在数以亿计的汉明码中,要筛出需要的图片, ...
- PAT甲级——A1083 List Grades
Given a list of N student records with name, ID and grade. You are supposed to sort the records with ...
- 03_springmvc整合mybatis
一.整合思路 springmvc+mybaits的系统架构: 第一步整合dao层:mybatis和spring整合:通过spring管理mapper接口,使用mapper的扫描器自动扫描mapper接 ...