PAT1037:Magic Coupon
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 -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的更多相关文章
- PAT1037. Magic Coupon (25)
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
- 1037 Magic Coupon (25 分)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an i ...
- 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_A1037#Magic Coupon
Source: PAT A1037 Magic Coupon (25 分) Description: The magic shop in Mars is offering some magic cou ...
- A1037. Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- 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 ...
- PAT甲级——A1037 Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
随机推荐
- AngularJS进阶(十四)AngularJS灵异代码事件
AngularJS灵异代码事件 注:请点击此处进行充电! 事情原委 router_sys.js源代码如下: 自己在html路由跳转的代码如下: 但是在实际路由过程中,却路由到了下面的状态,相应的页面中 ...
- Windows2008+MyEclipse10+Android开发环境搭配
Windows2008+MyEclipse10+Android开发环境搭配 知识要点:64位系统中离线安装MyEclipse的ADT插件步骤办法 功能描述:解决Windows2008+MyEclips ...
- leetcode之旅(6)-Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- Oracle 内连接和外连接
内连接用于返回满足连接条件的记录:而外连接则是内连接的扩展,它不仅会返回满足连接条件的所有记录,而且还会返回满足不满足连接条件的记录!从Oracle9i开始,可以在From 子句中指定连接语法.语法如 ...
- Kubernetes如何支持有状态服务的部署?
作者:Jack47 转载请保留作者和原文出处 PS:如果喜欢我写的文章,欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. Kubernetes对无状态服务有完善的支持 ...
- angular中的jqLite的基本使用方法
angular.element() 参数要求是HTML string or DOMElement, angular.element 虽然很接近 jQuery,但是直接通过 HTML tag 去获取元素 ...
- 【个人学习笔记】走近H5
一.HTML5概述 1.HTML5新特性 兼容性(ie9+).合理性.效率.安全性.分离.简化.通用性.无插件 2.HTML5构成 主要包括下面这些功能:Canvas(2D和3D).Channel消息 ...
- 网站SEO优化问答精选
1.百度每更新一次,网站的收录就减少很多,但是我每天都增加伪原创的内容啊? 这个问题大多数是因为网站权重导致百度不够重视你:另外就是文章质量度不高,没有可读性或是原创度太低,尽管百度会收录,但是经过一 ...
- 怎么分别javascript写在<head>里还是<body>里面?
怎么分别javascript写在<head>里还是<body>里面? 具体哪些语句写在<body>里,哪些语句写在<head>里 满意答案 BeginN ...
- Linux的关机详解
Linux如果是关机不想Windows,特别是命令界面.如果使用的是虚拟机,我们经常通过虚拟机来关闭.有点笨重啊.着特意找到关于Linux关机命令分享给大家. 在linux下一些常用的关机/重启命令有 ...