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. 【Visual C++】游戏编程学习笔记之四:透明动画实现

    本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44224963 作者:ZeeCod ...

  2. 使用GDB命令行调试器调试C/C++程序

    原文:http://xmodulo.com/gdb-command-line-debugger.html作者: Adrien Brochard 没有调试器的情况下编写程序时最糟糕的状况是什么?编译时跪 ...

  3. Android Studio使用Lint进行代码检查

    Android Studio目前已经更新到1.4版本,它作为Google官方推荐的IDE,功能非常强大,其中提供了一套静态代码分析工具,它可以帮助我们检查项目中存在的问题,让我们更有规范性的开发App ...

  4. OAF更改动态头行

    选择头信息,动态刷新行信息.本文将详细介绍该种需求的做法. 本例沿用<OAF-头行结构>的am与vo,所以在进行本例之前,请先完成<OAF-头行结构> 一.创建页面 在test ...

  5. “《编程珠玑》(第2版)第2章”:B题(向量旋转)

    B题是这样子的: 将一个n元一维向量向左旋转(即循环移位)i个位置.例如,当n=8且i=3时,向量abcdefgh旋转为defghabc.简单的代码使用一个n元的中间向量在n步内完成该工作.你能否仅使 ...

  6. Hbase 备份的方式

    HBase 备份的方式有三种: 1.下线备份 (1)停止集群. (2)Distcp (3)restore 2.在线备份 -replication 3.在线北大 -CopyTable 4.在线备份-Ex ...

  7. HBase Muti-Master

    为了保证HBase集群的高可靠性,HBase支持多Backup Master 设置.当Active Master挂掉后,Backup Master可以自动接管整个HBase的集群. 该配置极其简单: ...

  8. MarioTCP:一个单机可日30亿的百万并发长连接服务器

    原文:http://blog.csdn.net/everlastinging/article/details/10894493 注:如果用此服务器做变长data的传输,请在业务处理函数中为input ...

  9. 面试之路(13)-android apk之间共享数据的方式以及shareUserId详解

    1.通过content Provider/sharedPreferrence 2.通过shareUserId 我们详细介绍一下shareUserId: Android App Sandbox(andr ...

  10. 演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史

    演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史 原文出处: 少数派 9 月 18 日苹果就将推出 iOS 8 正式版了,从 2007 年发布第一代 iPhone 时搭载在 iPh ...