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 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≤N​C​​,N​P​​≤10​5​​, 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

题目大意:有n个勺子,每个勺子都有一个参数N,有m个积,每个勺子可以和一个积配对,那么求可以产生的最大正整数。

//就是将两者排序,从大到小,但是有负数怎么办呢?

代码转自: https://www.liuchuo.net/archives/2253

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int m, n, ans = , p = , q = ;
scanf("%d", &m);
vector<int> v1(m);
for(int i = ; i < m; i++)
scanf("%d", &v1[i]);
scanf("%d", &n);
vector<int> v2(n);
for(int i = ; i < n; i++)
scanf("%d", &v2[i]);
sort(v1.begin(), v1.end());//从小到大排序。
sort(v2.begin(), v2.end());
while(p < m && q < n && v1[p] < && v2[q] < ) {
ans += v1[p] * v2[q];//都小于0的相加。
p++; q++;
}
p = m - , q = n - ;
while(p >= && q >= && v1[p] > && v2[q] > ) {//这里将0算上了。
ans += v1[p] * v2[q];
p--; q--;
}
printf("%d", ans);
return ;
}

//看完题解感觉真的是水题。

1.将其从大到小或者从小到大排序均可,假设从小到大拍;

2.那么将左边的负数分别相乘得到结果,右边的整数相乘得到结果即可。

//emmm,这么简单的吗

PAT 1037 Magic Coupon[dp]的更多相关文章

  1. PAT 1037 Magic Coupon

    #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> ...

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

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

  3. 1037 Magic Coupon (25 分)

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

  4. PAT 甲级 1037 Magic Coupon

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

  5. 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, ...

  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. 【PAT甲级】1037 Magic Coupon (25 分)

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

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

随机推荐

  1. php -- php数组相关函数

    array range ( mixed $low , mixed $high [, number $step ] ) 创建一个连续的数组 range('a','z'); foreach (range( ...

  2. Cocos2d-X中的Slider控件

    Slider控件事实上就是滑块控件.经常使用于音乐中的音量控制,在Windows编程中开发音乐播放器就须要用到滑块控件控制音量 首先在project文件夹下的Resource文件夹中放 在Skider ...

  3. Qualcomm Vuforia SDK背景

    参考视频:http://edu.csdn.net/course/detail/1467/23125?auto_start=1 一:概述 官网:www.vuforia.com 应用方向:产品交互.虚拟购 ...

  4. 剑指 offer set 23 n 个骰子的点数

    题目 把 n 个骰子扔到地上, 所有骰子朝上一面的点数之和为 s. 输入 n, 打印出 s 所有可能的值出现的概率. 思路 1. 基于递归的求解方法. 深度为 n 的 dfs, 相当于求全排列, 时间 ...

  5. ios开发之--CGRect/CGSize/CGPoint/CGVector/CGAffineTransform/UIEdgeInsets/UIOffset和NSString之间的转换

    仅做记录,一个函数和字符串之间的互相转换 方法如下: UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UIKIT_EXTERN N ...

  6. 让asp.net程序在修改web.config后不重启

    默认情况下web.config修改后,asp.net程序会被重启.为了防止程序重启,要修改machine.config 文件. machine.config默认在C:\WINDOWS\Microsof ...

  7. 【BZOJ4071】[Apio2015]巴邻旁之桥 Treap

    [BZOJ4071][Apio2015]巴邻旁之桥 Description 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 A 和区域 B. 每一块区域沿着河岸都建了恰好 1000000001 ...

  8. IE edge是怎么了??

    IE edge 怎么不能通过$.getJSON(url,function(data){ alert(''); });获取数据呢?,其他浏览器和IE的10以下版本都没问题获取到了,这是什么情况!本来是想 ...

  9. jsp->jar

    (2)新建 WEB-INF目录 (3)在 WEB-INF/web.xml 中输入如下内容 <web-app xmlns="http://java.sun.com/xml/ns/j2ee ...

  10. Xcode自带工具symbolicatecrash解析Crash文件

    项目中遇到一台手机运行测试包闪退的现象,而且是一个设备闪退其他设备没有再现的情况 可以看到Crash信息,但是指定的问题给出的是16进制内存地址,根本无法定位问题发生在哪个类的哪个函数中 所以需要解析 ...