1037 Magic Coupon (25分)
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
题目分析:排序就好
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
bool compare(const int& a, const int& b)
{
return a < b;
}
int main()
{
int NC, NP;
int total = ;
cin >> NC;
vector<int> C(NC);
for (int i = ; i < NC; i++)
cin >> C[i];
cin >> NP;
vector<int>P(NP);
for (int i = ; i < NP; i++)
cin >> P[i];
sort(C.begin(), C.end(), compare);
sort(P.begin(), P.end(), compare);
auto it1 = C.begin();
auto it2 = P.begin();
for(; it1!=C.end()&&it2!=P.end()&&*it1<&&*it2<;)
{
total += (*it1) * (*it2);
it1++;
it2++;
}
it1 = (C.end() - );
it2 = (P.end() - );
while (it1>=C.begin() &&it2>= P.begin()&&*it1>&&*it2>)
{
total += (*it1) * (*it2);
it1--;
it2--;
}
cout << total;
return ;
}
1037 Magic Coupon (25分)的更多相关文章
- 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 (25 分)
题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. AAAAAccep ...
- 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容器存储,然后排序从小到大或是从大到小都可以,一次从 ...
- 1037. Magic Coupon (25)
#include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...
- PAT甲题题解-1037. Magic Coupon (25)-贪心,水
题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include ...
- 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 ...
随机推荐
- Git使用ssh公钥
Git使用ssh公钥 一. 何谓公钥 1.很多服务器都是需要认证的,ssh认证是其中的一种.在客户端生成公钥,把生成的公钥添加到服务器,你以后连接服务器就不用每次都输入用户名和密码了. 2.很多gi ...
- 痞子衡嵌入式:恩智浦SDK驱动代码风格、模板、检查工具
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是恩智浦 SDK 驱动的代码风格. 上周痞子衡受领导指示,给 SE 同事做了一个关于 SDK 代码风格的分享.随着组内新人的增多,这样的培训 ...
- background-attachment 制造视差滚动效果案例
简介 background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动.可能的值有三个: scroll 默认值.背景图像会随着页面其余部分的滚动而移动. fixed 当页 ...
- Python3对比合并Excel表格
##安装模块pip install pandas as pdpip install xlsxwriterpip install openpyxl ##带入模块import pandas as pdim ...
- XiaoQi.Study项目(二)
一.EF Core 使用的补充 1) 创建 接口 IEFCoreService 2) 实现 接口 EFCoreService 3) 在Startup.cs 中注册 ef 服务 并在控制器中注入使用 ...
- 网络安全从入门到精通 (第二章-2) 后端基础SQL—MySQL数据库简介及SQL语法
本文内容: 什么是数据库 常见数据库 数据库的基本知识 基本SQL语法 1,什么是数据库? 数据库就是将大量数据保存起来,通过计算机加工,可以高效访问的数据聚合. 数据库就是长期存储在计算机内,有组织 ...
- nuxt创建项目的步骤
nuxt创建项目的步骤 1.基本步骤 // 创建package.json依赖管理文件 npm init -y // 在package.json文件中添加运行nuxt的命令,之后npm run dev启 ...
- Natas31 Writeup(Perl 远程命令执行)
Natas31: 源码如下: my $cgi = CGI->new; if ($cgi->upload('file')) { my $file = $cgi->param('file ...
- payload分离免杀
shellcode loader 借助第三方加载器,将shellcode加载到内存中来执行. https://github.com/clinicallyinane/shellcode_launcher ...
- 数据库安装和基本sql语句
数据库概念 文件作为数据进行存储,数据格式千差万别 将保存数据的地方统一起来 MYSQL--------->一款应用软件 用来帮你操作文件的 只要是基于网络通信,底层就是socket 服务端 - ...