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 − }, and a set of product values { 7 6 − − } (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 N​C​​, followed by a line with N​C​​ coupon integers. Then the next line contains the number of products N​P​​, followed by a line with N​P​​ product values. Here 1, and it is guaranteed that all the numbers will not exceed 2​30​​.

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
题目分析:排序就好
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
bool compare(const int& a, const int& b)
{
return a < b;
}
int main()
{
int NC, NP;
int total = ;
cin >> NC;
vector<int> C(NC);
for (int i = ; i < NC; i++)
cin >> C[i];
cin >> NP;
vector<int>P(NP);
for (int i = ; i < NP; i++)
cin >> P[i];
sort(C.begin(), C.end(), compare);
sort(P.begin(), P.end(), compare);
auto it1 = C.begin();
auto it2 = P.begin();
for(; it1!=C.end()&&it2!=P.end()&&*it1<&&*it2<;)
{
total += (*it1) * (*it2);
it1++;
it2++;
}
it1 = (C.end() - );
it2 = (P.end() - );
while (it1>=C.begin() &&it2>= P.begin()&&*it1>&&*it2>)
{
total += (*it1) * (*it2);
it1--;
it2--;
}
cout << total;
return ;
}

1037 Magic Coupon (25分)的更多相关文章

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

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

  2. 【PAT甲级】1037 Magic Coupon (25 分)

    题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. AAAAAccep ...

  3. PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]

    题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...

  4. A1037 Magic Coupon (25 分)

    一.技术总结 这也是一个贪心算法问题,主要在于想清楚,怎么解决输出和最大,两个数组得确保符号相同位相乘,并且绝对值尽可能大. 可以用两个vector容器存储,然后排序从小到大或是从大到小都可以,一次从 ...

  5. 1037. Magic Coupon (25)

    #include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...

  6. PAT甲题题解-1037. Magic Coupon (25)-贪心,水

    题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include ...

  7. PAT (Advanced Level) 1037. Magic Coupon (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  8. 1037 Magic Coupon (25 分)

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

  9. PAT 1037 Magic Coupon[dp]

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

随机推荐

  1. 一套代码同时支持.NET Framework和.NET Core

    转自:https://www.cnblogs.com/tianqing/p/11614303.html 在.NET Core的迁移过程中,我们将原有的.NET Framework代码迁移到.NET C ...

  2. ADO.NET连接数据库DBHelper工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. C语言程序设计(八) 数组

    第八章 数组 //L8-1 #include <stdio.h> int main() { int score1, score2, score3, score4, score5; int ...

  4. selenium 操作 获取动态页面数据

    # selenium from selenium import webdriver import time driver_path = r"G:\Crawler and Data\chrom ...

  5. IE8使用chrome内核渲染

    1  第一步  https://download.csdn.net/download/qq_34626479/11223448  下载chrome frame安装包; 2  第二步  网页头部添加一行 ...

  6. node.js中http.respone.end方法概述

    方法说明: 结束响应,告诉客户端所有消息已经发送.当所有要返回的内容发送完毕时,该函数必须被调用一次. 如果不调用该函数,客户端将永远处于等待状态. 语法: response.end([data], ...

  7. sql 语句系列(多表之链)[八百章之第三章]

    新增连接查询而不影响其他连接查询 请看图: 这种情况我们一般会使用左连接的方式. select e.ENAME,d.LOC,eb.RECEIVED from emp e join dept d on( ...

  8. 图解汉诺塔问题( Java 递归实现)

    汉诺塔简介 最近在看数据结构和算法,遇到了一个非常有意思的问题--汉诺塔问题. 先看下百度百科是怎么定义汉诺塔的规则的: 汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候 ...

  9. 9. selenium+request方式的cookie绕过

    1. 首先确认POST请求的content-type类型 2. 查看cookies数据 3. 找到对应的headers数据 4. 如果是application/json,传入的json数据需要时jso ...

  10. F版本SpringCloud 3—大白话Eureka服务注册与发现

    引用:服务注册与发现,就像是租房子一样 前言 今天洛阳下雨了,唉,没有想到有裹上了羽绒服,不穿冷穿了热的尴尬温度.上学工作这么多年都在外面,家里竟然没有一件春天的外套. 日常闲聊之后,开始今天的芝士环 ...