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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 使用OxyPlot在WPF中创建图表
目录(?)[+] Using Nuget 包括OxyPlot在你的应用程序的最简单方法是使用NuGet包管理器在Visual Studio 运行 Visual Studio并开始创建一个新的WPF项目 ...
- js中一个标签在按顺序执行没有被读取到时可以用window.onload
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <!DOCTYPE html PUBLIC " ...
- UBOOT的的 C 语言代码部分
调用一系列的初始化函数 1. 指定初始函数表: init_fnc_t *init_sequence[] = { cpu_init, /* cpu 的基本设置 */ ...
- U-BOOT 对 Nand Flash 命令的支持
U-BOOT 对 Nand Flash 命令的支持 在 UBOOT 下对 Nand Flash 的支持主要是在命令行下实现对 nand flash 的操作.对 nand flash 实现的命令 为: ...
- Extjs & Ext.net中的一些属性
Extjs & Ext.Net 弹出整个浏览器对话框的方法 top.Ext.Msg.alert("值"); top.Ext.Msg.confirm("值" ...
- golang 获取当前可执行程序的当前路径
import ( "errors" // "fmt" "os" "os/exec" "path/filepat ...
- No packages marked for update
问题:用yum安装docker,更新yum存储时,报以下错误,导致yum坏了 [root@localhost yum.repos.d]# vi docker.repo [root@localhost ...
- Django实现简单的图书管理系统
目录 Django写图书管理系统 功能截图 创建Django项目 开始项目 配置文件 建立路由关系 开始写Django项目 编写核心逻辑函数 写前端页面 add_author.html add_boo ...
- LUOGU P3382 【模板】三分法 (三分)
传送门 解题思路 三分,填坑.每次取l与r的中间值mid,然后向左移一点点,向右移一点点进行判断,判断时用秦九韶算法即可. #include<iostream> #include<c ...
- C++编程规范和编译过程详解
前言:因为c++基础打得不牢,所以准备花点时间再学一下c++的基础知识,主要是看网易云课堂里面的免费课程,把一些知识点做个笔记记下来. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...