A1037. Magic Coupon
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
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(long long a, long long b){
return a > b;
}
long long C[], P[], ans = ;
int main(){
int NC, NP;
scanf("%d", &NC);
for(int i = ; i < NC; i++)
scanf("%lld", &C[i]);
scanf("%d", &NP);
for(int i = ; i < NP; i++)
scanf("%lld", &P[i]);
sort(C, C + NC, cmp);
sort(P, P + NP, cmp);
for(int i = ; i < NC && i < NP && C[i] >= && P[i] >= ; i++)
ans += C[i] * P[i];
for(int i = NC - , j = NP - ; i >= && j >= && C[i] <= && P[j] <= ; i--, j--)
ans += C[i] * P[j];
printf("%lld", ans);
cin >> NP;
return ;
}
总结:
1、题意:给出两个集合,每个集合都分别有正数、负数或0。分别从两集合里选出相同个数的数,求他们的乘积之和最大为多少。需要注意的是,本题选取的乘积的组数是任意的,不需要用掉所有的数。因为正正相乘则正数越大积越大,负负相乘负数越小积越大。因此只需要将两个集合从大到小排序,从下标0开始依次正正相乘,直到遇见二者异号。同理从尾部向前负负相乘,直到异号。
A1037. Magic Coupon的更多相关文章
- PAT甲级——A1037 Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- A1037 Magic Coupon (25 分)
一.技术总结 这也是一个贪心算法问题,主要在于想清楚,怎么解决输出和最大,两个数组得确保符号相同位相乘,并且绝对值尽可能大. 可以用两个vector容器存储,然后排序从小到大或是从大到小都可以,一次从 ...
- PAT_A1037#Magic Coupon
Source: PAT A1037 Magic Coupon (25 分) Description: The magic shop in Mars is offering some magic cou ...
- 1037 Magic Coupon (25 分)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an i ...
- PAT1037:Magic Coupon
1037. Magic Coupon (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The magi ...
- 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 ...
- 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 ...
随机推荐
- Git常用命令梳理
在日常的Git版本库管理工作中用到了很多操作命令,以下做一梳理: 查看分支列表,带有*的分支表示是当前所在分支 [root@115~~]#git branch 查看分支详细情况 (推荐这种方式) [r ...
- C语言与汇编衔接1
研究实验二 问题研究过程: 发问:C语言中的变量究竟是什么,通过下面的程序进行C语言中的变量的学习 图1 URL.EXE函数 为了研究main函数的首地址,我首先自作聪明的用了一条_DX=main, ...
- Git科普来一发:【rebase】与【merge】
rebase 假设你现在基于远程分支"origin",创建一个叫"mywork"的分支. $ git checkout -b mywork origin 现在我 ...
- 团队作业 week 14
0. 在吹牛之前,先回答这个问题: 如果你的团队来了一个新队员,有一台全新的机器, 你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新.最稳定版本的软件编译出 ...
- 【Beta阶段】第八次Scrum Meeting!
每日任务内容: 本次会议为第八次Scrum Meeting会议~ 由于本次会议项目经理身体不适,未参与会议,会议精神由卤蛋代为转达,其他同学一起参与了会议 队员 昨日完成任务 明日要完成任务 刘乾 今 ...
- [2017BUAA软件工程]第0次个人作业
第一部分: 结缘计算机 1. 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 有时候我也问自己这个问题,是因为认识的人中有人从事这个工作并且做得很好而产生了艳羡?是因为家长一次次催逼,想 ...
- PAT 1004 成绩排名
https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448 读入n名学生的姓名.学号.成绩,分别输出成绩 ...
- js拷贝实例;
,]]; // var arr2 =arr1.slice(0); // arr1[0] ="z"; // arr1[2][0] = "v"; // consol ...
- 关于splice()方法,slice() 、split()方法讲解,reverse()方法、replace()方法
1.slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数 描述 start 必需.规定从何处开始选取.如果是负数,那么它规定从数组 ...
- delphi DBGRID 刷新定位问题 [问题点数:0分]
我程序是 adoquery+datasource+dbgrid 做的我有一个窗体:有四个按钮.分别是新建,修改,删除,刷新. 新建第一条记录,dbgrid显示一条记录,新建第二条记录.DBGRID总共 ...