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 ...
随机推荐
- 在后台运行Python脚本服务
在服务器,程序都是后台运行的,当写的python脚本时,需要: 你要是想python robot.py & 是不行的,一旦用户登出,脚本就自动退出了.用at, cron也可以实现不过我发现 ...
- js学习笔记25----Event对象
Event : 事件对象,当一个事件发生的时候,和当前这个对象发生的这个事件有关的一些详细的信息都会被临时保存到一个指定的地方-event 对象,供我们在需要时调用. 事件对象必须在一个事件调用的函数 ...
- APP纯黑盒测试—某些可以试试的操作
一.多次快速点击一处功能入口: 该测试方法可以在某些应用中打开俩次目标界面,举一些具体一点的例子: 1.比如现在很多APP需要登陆,如果打开了俩次登录页面,就容易造成登录成功后应用跳转界面又是登录界面 ...
- 008杰信-创建购销合同Excel报表系列-1-建四张表
本博客的内容来自于传智播客: 我们现在开始要做表格了,根据公司要求的表格的形式,来设计数据库.规划针对这个表格要设计几张表,每张表需要哪些字段. 根据公司原有的表格,设计数据库: 原有的表格
- 5.3 SpEL语法
SqEL是一个可以独立于spring的表达式语言,即它可以用在XML中对语法进行简化 5.3 SpEL语法5.3.1 基本表达式一.字面量表达式: SpEL支持的字面量包括:字符串.数字类型(int. ...
- jquery -- 同时监听多个事件
多个事件触发同一反应 $("#redrow").on("click touchend",function(){});//每个监听事件之间用 “空格” 隔开 多个 ...
- ThinkPHP项目笔记之RBAC(权限)基础篇
今天,总结一下,RBAC(基于角色的访问控制),直白一点,就是权限管理.说到这,不得不“小叙”一下,我第一次 开发权限管理功能的“插曲”.第一次做这个,真的不会,我只知道“有点印象”,当时任务落到我的 ...
- 使用spring + ActiveMQ 总结
使用spring + ActiveMQ 总结 摘要 Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 目录[-] Spring 整合JMS 基于ActiveMQ 实现消息的发送接 ...
- 深入.net调用webservice的总结分析
最近做一个项目,由于是在别人框架里开发app,导致了很多限制,其中一个就是不能直接引用webservice .我们都知道,调用webserivice 最简单的方法就是在 "引用" ...
- Appium自动化测试3之获取apk包名和launcherActivity后续
接着“Appium自动化测试3之获取apk包名和launcherActivity”章节介绍 测试脚本 1.测试脚本如下: # -*- coding:utf-8 -*- import os, time, ...