1037. Magic Coupon (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<iostream>
#include<vector>
#include<algorithm>
using namespace std; typedef long long ll; int main()
{
int NC,NP;
cin >> NC;
vector<ll> coupons(NC);
for(int i = 0;i < NC;i++)
{
cin >> coupons[i];
}
cin >> NP;
vector<ll> products(NP);
for(int i = 0;i < NP;i++)
{
cin >> products[i];
}
sort(coupons.begin(),coupons.end());
sort(products.begin(),products.end());
int sum = 0;
int i = 0,j = 0,lc = coupons.size() - 1,lp = products.size() - 1;
for(;i <= lc && j <= lp;i++,j++)
{
if( coupons[i] <= 0 && products[j] <= 0 )
{
sum += coupons[i] * products[j];
}
}
for(int u = lc,v = lp;u >=0 && v >=0;u--,v--)
{
if( coupons[u] > 0 && products[v] > 0 )
{
sum += coupons[u] *products[v];
} }
cout << sum << endl;
}

  

PAT1037:Magic Coupon的更多相关文章

  1. PAT1037. Magic Coupon (25)

    #include <iostream> #include <algorithm> #include <vector> using namespace std; in ...

  2. 1037 Magic Coupon (25 分)

    1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an i ...

  3. PAT 1037 Magic Coupon[dp]

    1037 Magic Coupon(25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an in ...

  4. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

  5. PAT_A1037#Magic Coupon

    Source: PAT A1037 Magic Coupon (25 分) Description: The magic shop in Mars is offering some magic cou ...

  6. A1037. Magic Coupon

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

  7. PAT 甲级 1037 Magic Coupon

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

  8. 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 ...

  9. PAT甲级——A1037 Magic Coupon

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

随机推荐

  1. Xcode模拟器中无法播放音频文件的原因分析

    在本猫的Mac Mini上开发iOS app,发现当执行到播放音频的代码时,发生错误,log如下: 2015-10-05 07:22:17.122 LearnSpriteBuilder[10321:5 ...

  2. 基于ARM-contexA9按键驱动开发

    之前我们写过LED和蜂鸣器的驱动,其实那两个都是一个模版的,因为都是将IO口配置成输出模式,然后用高低电平来驱动这些设备.其实linux设备驱动,说白了跟单片机开发的方式是差不多的,只不过内核的开发基 ...

  3. 苹果新的编程语言 Swift 语言进阶(十三)--类型检查与类型嵌套

    一 类型检查 1. 类型检查操作符 类型检查用来检查或转换一个实例的类型到另外的类型的一种方式. 在Swift中,类型检查使用is和as操作符来实现. is操作符用来检查一个实例是否是某种特定类型,如 ...

  4. objc写一个NSMutableArray不连续索引替换对象的方法

    NSMutableArray内置的方法-(void)replaceObjectsAtIndexes:(NSIndexSet*)set withObjects:(NSArray*)objs 只能替换一段 ...

  5. How tomcat works 读书笔记十二 StandardContext 上

    在tomcat4中,StandardContext.java是最大的一个类,有117k.废话不说,开始分析吧. 其实要分析StandardContext,也就主要分析两个方法,一个start,一个in ...

  6. 【42】android Context深度剖析

    android程序和java程序的区别 Android程序不像Java程序一样,随便创建一个类,写个main()方法就能跑了,而是要有一个完整的Android工程环境,在这个环境下,我们有像Activ ...

  7. C++语言之静态变量的运用

    #include <iostream> using namespace std ; class Banana { public: static int id ; Banana(void) ...

  8. 关于getchar函数缓冲区的问题

    最近,看到有同学问我关于getchar()这个函数缓冲区的问题,结合我以前的学习,我将对getchar()进行一次总结,当然,这些都是别人已经提过的东西,我只是总结,接下来我们来看看. 首先,用get ...

  9. rails关于user密码hash的重构

    rails应用程序中一个model名为User,其中存放了用户名和对应的密码.User模式类中建立了1个虚拟属性password用来存放用户实际输入的密码;而最终数据库的密码需要计算password的 ...

  10. Kubernetes如何支持有状态服务的部署?

    作者:Jack47 转载请保留作者和原文出处 PS:如果喜欢我写的文章,欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. Kubernetes对无状态服务有完善的支持 ...