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 ...
随机推荐
- easyui datagrid 列拖动
实现代码-code <script type="text/javascript"> $.extend($.fn.datagrid.methods, { columnMo ...
- 关于json动态拼接响应数据
在EasyUI http://www.jeasyui.com/demo/main/get_users.php 响应数据如下格式: { "total": "11" ...
- sql 使用整理
今天使用视图查询东西,为了方便直接select * 查出来的都行全部都错乱了,看来sql 超过20个以上的字段为了效率和安全,禁止用select * -------------查一个表的所有字段的-- ...
- MRF能量优化
一个外国博客,写的比较清晰 http://nghiaho.com/?page_id=1366 MRF优化牛人 重庆大学的教授 1 http://qianjiye.de/2015/09/reparame ...
- 转载 Python导入模块的几种姿势
作为一名新手Python程序员,你首先需要学习的内容之一就是如何导入模块或包.但是我注意到,那些许多年来不时使用Python的人并不是都知道Python的导入机制其实非常灵活.在本文中,我们将探讨以下 ...
- 【BZOJ】1625: [Usaco2007 Dec]宝石手镯(01背包)
http://www.lydsy.com/JudgeOnline/problem.php?id=1625 太水了. #include <cstdio> #include <cstri ...
- (转)spring IOC、DI理解
转自: http://www.cnblogs.com/xdp-gacl/p/4249939.html 个人理解: IOC控制反转,反转的是获取依赖对象的方式.传统的应用在存在依赖关系时,比如A依赖于B ...
- c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)
下面是 Queryable 类 中最常用的两个排序的扩展方法: 1 2 public static IOrderedQueryable<TSource> OrderBy<TSourc ...
- Struts2_day03--从值栈获取数据_EL表达式获取值栈数据(为什么)
从值栈获取数据 1 使用struts2的标签+ognl表达式获取值栈数据 (1)<s:property value=”ognl表达式”/> 获取字符串 1 向值栈放字符串 2 在jsp使用 ...
- iOS开发之 -- NSStringFromSelector的使用
很多时候,我们要触发一个时间,需要设置点击时间,当然了,有很多,比如:按钮,手势,tableview和其他一些空间自带的点击方法, 还有一个就是NSStringFromSelector的使用,废话不多 ...