PAT 甲级 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 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
题意:
给出两个集合,从这两个集合里面选出数量相同的元素进行一对一相乘,求能够得到的最大乘积之和。
题解:
对每个集合,将正数和负数分开考虑,将每个集合里的整数从大到小排序;将每个集合里的负数从小到大排序,然后同位置的正数与正数相乘,负数与负数相乘。
注意点:
输入为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 分) (较简单,贪心)的更多相关文章
- 【PAT甲级】1037 Magic Coupon (25 分)
题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. AAAAAccep ...
- 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, ...
- 1037 Magic Coupon (25分)
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 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 ...
- 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 ...
随机推荐
- 微服务学习及.net core入门教程
https://www.cnblogs.com/jackyfei/p/12067708.html https://www.cnblogs.com/jesse2013/ http://video.jes ...
- SQL SERVER表变量和临时表
一.表变量 表变量在SQL Server 2000中首次被引入.表变量的具体定义包括列定义,列名,数据类型和约束.而在表变量中可以使用的约束包括主键约束,唯一约束,NULL约束和CHECK约束(外键约 ...
- jquery模仿淘宝星星打分
今天做论坛页面有星星评分功能,以下是代码.用的时候引入jquery <span> <ul class="hs_df_xx"> <li><i ...
- 51、[源码]-Spring容器创建-容器创建完成
51.[源码]-Spring容器创建-容器创建完成 12.finishRefresh();完成BeanFactory的初始化创建工作:IOC容器就创建完成: 1).initLifecycleProce ...
- 50、[源码]-Spring容器创建-Bean创建完成
50.[源码]-Spring容器创建-Bean创建完成 11.finishBeanFactoryInitialization(beanFactory);初始化所有剩下的单实例bean: beanFac ...
- Spring第二次案例和AOP
Spring加上AOP com.mapper.entity.UserInfo package com.Spring.entity; public class UserInfo { private In ...
- 使用 Java 创建聊天客户端-2
1.项目截图 java聊天核心代码: MyJavaChatClient ================================================================ ...
- JS的一些总结(函数声明和函数表达式的区别,函数中的this指向的问题,函数不同的调用方式,函数也是对象,数组中的函数调用)
一.函数声明和函数表达式的区别: 函数声明放在if——else语句中,在IE8中会出现问题 函数表达式则不会 <script> if(true){ function f1(){ conso ...
- CSP考前总结
10.2 考试: 1.数位DP 或者找规律 2.SB题,扫一遍找最大最小即可 3.莫比乌斯反演 出题人相出个数论和数据结构的综合题,但是找不到NOIP级别的,没办法只能忍痛割爱出个莫比乌斯,话说回来, ...
- [转]C++重载()(强制类型转换运算符)
在 C++ 中,类型的名字(包括类的名字)本身也是一种运算符,即类型强制转换运算符. 类型强制转换运算符是单目运算符,也可以被重载,但只能重载为成员函数,不能重载为全局函数.经过适当重载后,(类型名) ...