PAT_A1037#Magic Coupon
Source:
Description:
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 Nbeing 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
Keys:
Code:
/*
Data: 2019-07-23 19:10:20
Problem: PAT_A1037#Magic Coupon
AC: 11:55 题目大意:
集合A和集合B,求集合A中各元素与集合B中各元素乘积之和的最大值
*/
#include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
const int M=1e5+;
int c[M],p[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int nc,np,pos,ans=;
scanf("%d", &nc);
for(int i=; i<nc; i++)
scanf("%d", &c[i]);
scanf("%d", &np);
for(int i=; i<np; i++)
scanf("%d", &p[i]);
sort(c,c+nc,greater<int>() );
sort(p,p+np,greater<int>() );
pos=;
while(pos<nc && pos<np && p[pos]> && c[pos]>)
ans += p[pos]*c[pos++];
sort(c,c+nc,less<int>() );
sort(p,p+np,less<int>() );
pos=;
while(pos<nc && pos<np && p[pos]< && c[pos]<)
ans += p[pos]*c[pos++];
printf("%d", ans); return ;
}
PAT_A1037#Magic Coupon的更多相关文章
- PAT1037:Magic Coupon
1037. Magic Coupon (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The magi ...
- 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 ...
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
- A1037. Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- PAT 甲级 1037 Magic Coupon
https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472 The magic shop in Mars ...
- 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 ...
- PAT甲级——A1037 Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- 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, ...
随机推荐
- 如何高效地学好R语言?
如何高效地学好R语言? 学R语言主要在于5点三阶段: 第一阶段有一点:基础的文件操作(read.*, write.*).数据结构知识,认识什么是数据框(data.frame).列表(list).矩阵( ...
- laravel 中url使用
url() 通过url辅助函数(路由)生成:location.href = "{{url('user/index')}}"; 或者:location.href = "{{ ...
- python 类和对象下
类的常用函数 issubclass() 检测一个类是否是另外一个类的子类 格式1:issubclass(被检测类,父类) 返回值:布尔值 格式1:issubclass(被检测类,(父类1,父类2,父类 ...
- 解决Ubuntu与Windows双系统时间不同步问题
目录 1.Windows修改法 1.1设置UTC 1.2恢复LocalTime 2.Ubuntu修改法 2.1设置LocalTime 2.2恢复UTC 切换系统后,往往发现时间差了8小时.这恰恰是北京 ...
- luoguP1514 引水入城 题解(NOIP2010)(Bfs+贪心)
P1514 引水入城 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<c ...
- vue基础学习一
写一个例子,告诉你VUE的方便之处,就是双向绑定,不需要操作DOM对象,而是操作数据 div中msg 和JS中msg是一一对应的 然后看浏览器中 然后如果我想改变浏览器中值,我在console这么操作 ...
- docker--container之间的link,bridge create
container的name和ID一样,也是唯一的,当不知道container的IP时,可以用name替代,但需要先配置link 下面创建两个container 时,未配置link所以ping nam ...
- SQL数据库—<7>事务、异常和游标
事务 一.什么是事务能够保证数据的一致性的代码控制,要么执行提交,要么滚回事务的初始状态 二.事务的四大特性:ACIDA:原子性-------事务不可拆开,要么执行要么回滚无中间状态C:一致性---- ...
- android jni控制gpio (rk3288)
1.添加驱动程序 2.编写jni c程序编译为库给java调用 3.app调用jni静态链接库操作底层驱动 1.添加驱动程序 修改/work/rk3288/firefly-rk3288_android ...
- 【服务端开发-杂】REST 和 Graphql
基本知识链接: 1.[译]对比GraphQL与REST——两种HTTP API的差异:https://www.jianshu.com/p/2ad286397f7a 2. 怎样用通俗的语言解释REST, ...