PAT刷题:1037. Magic Coupon (25)
来源:互联网 发布:战斗妖精雪风小说淘宝 编辑:程序博客网 时间:2024/06/08 15:21
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:41 2 4 -147 6 -2 -3Sample Output:
43
#include <iostream>#include <string>#include <vector>#include <algorithm>#include <cstdio>using namespace std;bool cmp(const int &a , const int &b){return a>b;}int main(){ int m=0 , n=0; vector<int>coupons , products; cin>>m; coupons.resize(m); for(int i=0;i<m;i++)cin>>coupons[i]; cin>>n; products.resize(n); for(int i=0;i<n;i++)cin>>products[i]; sort(coupons.begin() , coupons.end() , cmp); sort(products.begin() , products.end() , cmp); int total=0; while(coupons.size()>0 && products.size()>0){ if(coupons.front() > 0 && products.front() >0){ total+=coupons.front() * products.front(); coupons.erase(coupons.begin()); products.erase(products.begin()); } else if(coupons.back() < 0 && products.back() <0){ total+=coupons.back()*products.back(); coupons.pop_back(); products.pop_back(); } else break; } cout<<total; return 0;}
#include <iostream>#include <string>#include <vector>#include <algorithm>#include <cstdio>using namespace std;bool cmp1(const int &a , const int &b){return a>b;}bool cmp2(const int &a , const int &b){return a<b;}int main(){ int m=0 , n=0; vector<int>p_coupons , p_products ,n_coupons , n_products; cin>>m; p_coupons.resize(m); n_coupons.resize(m); for(int i=0;i<m;i++){ int t=0; cin>>t; if(t>0)p_coupons.push_back(t); else n_coupons.push_back(t); } cin>>n; p_products.resize(n); n_products.resize(n); for(int i=0;i<n;i++){ int t=0; cin>>t; if(t>0)p_products.push_back(t); else n_products.push_back(t); } sort(p_coupons.begin() , p_coupons.end() , cmp1); sort(p_products.begin() , p_products.end() , cmp1); sort(n_coupons.begin() , n_coupons.end() , cmp2); sort(n_products.begin() , n_products.end() , cmp2); int total=0; int _min=std::min(p_coupons.size() , p_products.size()); for(int i=0;i<_min;i++){ total+=p_coupons[i]*p_products[i]; } _min=std::min(n_coupons.size() , n_products.size()); for(int i=0;i<_min;i++){ total+=n_coupons[i]*n_products[i]; } cout<<total; return 0;}
- PAT刷题:1037. Magic Coupon (25)
- 1037. Magic Coupon (25)-PAT
- PAT 1037. Magic Coupon (25)
- pat 1037. Magic Coupon (25)
- PAT 1037. Magic Coupon (25)
- 【PAT】1037. Magic Coupon (25)
- PAT 1037. Magic Coupon (25)
- PAT 1037. Magic Coupon
- 【PAT】1037. Magic Coupon
- PAT--1037. Magic Coupon
- PAT A 1037. Magic Coupon (25)
- 【PAT甲级】1037. Magic Coupon (25)
- 1037. Magic Coupon (25)PAT甲级
- PAT甲级练习1037. Magic Coupon (25)
- PAT甲级1037. Magic Coupon (25)
- PAT-A 1037. Magic Coupon (25)
- PAT-A-1037. Magic Coupon (25)
- 【PAT】【Advanced Level】1037. Magic Coupon (25)
- Mac下快速搭建PHP开发环境
- 戒骄戒躁戒自傲
- bzoj3293/1045 [Cqoi2011]分金币/[HAOI2008] 糖果传递 贪心
- 记录如何解决通过vpn链接公司内网,但是上不了外网的问题
- Adobe CC2018最新系列所有软件全家桶 破解版 赢政天下@vposy
- PAT刷题:1037. Magic Coupon (25)
- 树莓派3b 远程桌面使用opencv窗口不能显示(Window system doesn't support OpenGL)
- HDU 5437by cyl优先队列
- Coursera Deep Learning 第四课 卷积神经网络 第四周 编程作业 Face Recognition for the Happy House-v3
- JSP页面中的隐含对象
- PHP 分页(limit)
- 安装oracle出现Could not execute auto check for display colors using command
- 文章标题
- cuda优化