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 − }, 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 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 N​C​​, followed by a line with N​C​​ coupon integers. Then the next line contains the number of products N​P​​, followed by a line with N​P​​ product values. Here 1, and it is guaranteed that all the numbers will not exceed 2​30​​.

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

题意:

给出两个集合,从这两个集合里面选出数量相同的元素进行一对一相乘,求能够得到的最大乘积之和。

题解:

对每个集合,将正数和负数分开考虑,将每个集合里的整数从大到小排序;将每个集合里的负数从小到大排序,然后同位置的正数与正数相乘,负数与负数相乘。

注意点:

输入为0的不要管,直接忽略,否则测试点1过不去

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll na[];
ll pa[];
ll nb[];
ll pb[];
int kna=,kpa=,knb=,kpb=;
int n1,n2;
bool cmp(ll x, ll y){
return x>y;
}
ll s=;
int main(){
cin>>n1;
ll x;
for(int i=;i<=n1;i++){
cin>>x;
if(x>){
pa[++kpa]=x;
}else if(x<){//=0的不要管
na[++kna]=x;
}
}
cin>>n2;
for(int i=;i<=n2;i++){
cin>>x;
if(x>){
pb[++kpb]=x;
}else if(x<){//=0的不要管
nb[++knb]=x;
}
}
sort(pa+,pa++kpa,cmp);
sort(pb+,pb++kpb,cmp);
sort(na+,na++kna);
sort(nb+,nb++knb);
int min_l=min(kpa,kpb);
for(int i=;i<=min_l;i++){
s+=pa[i]*pb[i];
}
min_l=min(kna,knb);
for(int i=;i<=min_l;i++){
s+=na[i]*nb[i];
}
cout<<s<<endl;
return ;
}

PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)的更多相关文章

  1. 【PAT甲级】1037 Magic Coupon (25 分)

    题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. AAAAAccep ...

  2. PAT 甲级 1037 Magic Coupon

    https://pintia.cn/problem-sets/994805342720868352/problems/994805451374313472 The magic shop in Mars ...

  3. 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, ...

  4. 1037 Magic Coupon (25分)

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

  5. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  6. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  7. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  8. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  9. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

随机推荐

  1. 用CSS 实现 浮动元素的 水平居中

    问题描述: 基本的html结构: <div> <!-- <span>1</span> <span>2</span> <span& ...

  2. python中的lambda()函数

    语句:print map(lambda x:x ** 2,[1,2,3,4,5]) 其中lambda()函数在Python文档,文档中解释如下: lambda An anonymous inline ...

  3. location对象方法(assign()、reload()、replace())

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 使用jQuery快速高效制作网页交互特效--JavaScript操作BOM对象

    JavaScript操作BOM 一.window对象: 二.window对象的属性和方法 1.windows对象的常用属性: 语法:window.属性名="属性值" 2.windo ...

  5. 工作流学习之--TPFlow数据库分析

    一.TPFlow项目数据库表: 1. 流程相关: a. leipi_flow工作流表: b. leipi_flow_process流程步骤表: c. leipi_run_process运行过程表:记录 ...

  6. hdu 6183

    给出二维平面$opt1.$ 对点 $(x, y)$ 增减颜色 $c$,$opt2.$ 询问矩形 $(1, y_1), (x, y_2)$ 内出现过的颜色种数$x, y <= 1e6, c < ...

  7. RabbitMQ的安装和管理

    c:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.5.1\sbin>------------------------------- ...

  8. SDN与IXP

    IXP 互联网交换中心(IXP)在互联网生态系统中发挥着关键作用.在全球范围内,100多个国家/地区有超过400个IXP,其中最大的IXP具有接近10 Tbps的峰值数据速率并连接数百个网络.IXP提 ...

  9. 物聯網安全黑客松 IoT Security and Privacy Hackathon

    感覺這次黑客松的程度屬於初階,但是節奏很快,內容緊湊.概念部分解說較多,以致實驗時間縮短,有些只能看demo有點遺憾.幸好有video-taped,事後回溯可以看看能不能replicate實驗.總體而 ...

  10. vue的ui组件库

    https://www.cnblogs.com/dupd/p/7735450.html