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 ...
随机推荐
- 004杰信-关于formSubmit('factorycreate.action','_self')路径的疑惑
本文材料来源于传智播客,在此说明. 整个项目结构:
- ti8168平台的tiler memory
DM8168 DMM/TILER简介 1.概述 如图4-1,DMM定位在SDRAM控制器的前端,是所有initiator产生的内存存取的接口. 动态内存管理器DMM,是一个专门的管理模块,广义上说,包 ...
- css -- 运用@media实现网页自适应中的几个关键分辨率
经常为不同分辨率设备或不同窗口大小下布局错位而头疼,可以利用@media screen实现网页布局的自适应,但是怎样兼容所有主流设备就成了问题.到底分辨率是多少的时候设置呢? 先看下面的代码,这是从b ...
- 用公式编辑器编辑n元乘积的方法
在数学中经常会出现很多个元素进行求和或者是乘积的情况,但是在整个数学过程中,不可能将所有的元素都写出来,这样很费时费力同时过程也很赘余,不能很好地理解其中的过程,因此数学中对于这一类的多元相加或者相乘 ...
- Spring_day02--log4j介绍_Spring整合web项目演示
log4j介绍 1 通过log4j可以看到程序运行过程中更详细的信息 (1)经常使用log4j查看日志 2 使用 (1)导入log4j的jar包 (2)复制log4j的配置文件,复制到src下面 3 ...
- Struts2_day02--Struts2封装获取表单数据方式
Struts2封装获取表单数据方式 原始方式获取表单封装到实体类对象 属性封装(会用) 1 直接把表单提交属性封装到action的属性里面 2 实现步骤 (1)在action成员变量位置定义变量 - ...
- CS无线电语
[Radio Commands (" Z "键) - 无线电指令] 1."Cover me" (掩护我) 2."You Take The Point& ...
- jquery如何获取type=hidden的input元素的值?
function setHiddenFields() { var hiddens = $("input:hidden"); $.each(hiddens, function (in ...
- 03.Curator深入使用
1.Apache Curator简介 Curator提供了一套Java类库,可以更容易的使用ZooKeeper.ZooKeeper本身提供了Java Client的访问类,但是API太底层,不 ...
- Servlet------>jsp自定义标签3(不显示余下jsp内容)
2.自定义标签控制,jsp页面余下标签是否继续执行 EndTag.java package tag; import javax.servlet.jsp.JspException; import jav ...