PAT 1037 Magic Coupon[dp]
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 −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 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≤NC,NP≤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
题目大意:有n个勺子,每个勺子都有一个参数N,有m个积,每个勺子可以和一个积配对,那么求可以产生的最大正整数。
//就是将两者排序,从大到小,但是有负数怎么办呢?
代码转自: https://www.liuchuo.net/archives/2253
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int m, n, ans = , p = , q = ;
scanf("%d", &m);
vector<int> v1(m);
for(int i = ; i < m; i++)
scanf("%d", &v1[i]);
scanf("%d", &n);
vector<int> v2(n);
for(int i = ; i < n; i++)
scanf("%d", &v2[i]);
sort(v1.begin(), v1.end());//从小到大排序。
sort(v2.begin(), v2.end());
while(p < m && q < n && v1[p] < && v2[q] < ) {
ans += v1[p] * v2[q];//都小于0的相加。
p++; q++;
}
p = m - , q = n - ;
while(p >= && q >= && v1[p] > && v2[q] > ) {//这里将0算上了。
ans += v1[p] * v2[q];
p--; q--;
}
printf("%d", ans);
return ;
}
//看完题解感觉真的是水题。
1.将其从大到小或者从小到大排序均可,假设从小到大拍;
2.那么将左边的负数分别相乘得到结果,右边的整数相乘得到结果即可。
//emmm,这么简单的吗
PAT 1037 Magic Coupon[dp]的更多相关文章
- PAT 1037 Magic Coupon
#include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> ...
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472 The magic shop in Mars ...
- 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 (25)-贪心,水
题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include ...
- PAT (Advanced Level) 1037. Magic Coupon (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 【PAT甲级】1037 Magic Coupon (25 分)
题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. AAAAAccep ...
- 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, m ...
随机推荐
- 在Asp.net中为图像加入水印信息
using System.Drawing; using System.IO; using System.Drawing.Imaging; private void AddTextToImg(strin ...
- Javascript代码执行过程-《悟透Javascript》笔记
本文摘录自李战老师<悟透Javascript>一书的部分章节,为适应博客发表作了一点点修改. 1) 预编译分析. JavaScript执行引擎将所有定义式函数直接创建为作用域上的函数变量, ...
- JS侦测设备旋转方向
window.onload = window.onresize = function initialLoad(){updateOrientation();} function updateOrient ...
- Hadoop源码分析之数据节点的握手,注册,上报数据块和心跳
转自:http://www.it165.net/admin/html/201402/2382.html 在上一篇文章Hadoop源码分析之DataNode的启动与停止中分析了DataNode节点的启动 ...
- POST GET 请求示例
HTTP GET 请求代码: HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baidu.com ...
- ztree获取选中节点时不能进入可视区域出现BUG如何解决
zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree 的特点编辑 ● zTree v3.0 将核心代码按照功能进 ...
- Gradle -- buildScript块与allprojects块及根级别的repositories区别
http://blog.sina.com.cn/s/blog_72ef7bea0102vvg3.html
- PowerShell----Automatic_Variables(预定义变量)
以下这些变量是由powershell创建和维护的.ls Variable: 可以获取到所有默认的变量, 每个版本的Powershell可能有差异 $$包含会话所收到的最后一行中的最后一个令牌. $? ...
- sql语法:从一个表update到另外一个表
sql语法:从一个表update到另外一个表 一. update a set a.name=b.name1 from a,b where a.id=b.id 二. update table1 set ...
- 插件—jquery.validate.js
前言 在学习jquery.validate.js中的一个小案例,只是这个插件的简单使用. 案例代码如下: <head> <title></title> ...