1037. Magic Coupon (25)

1.题目给出一些coupon和product,求最大的value。

2.最大的正数coupon与最大的正数product相乘。

3.最小的负数coupon与最小的负数product相乘(最小的负数即绝对值最大)。

4.其他没有匹配的就不购买了。

时间限制
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

AC代码:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
bool cmp(const int&a, const int&b)
{
	return a > b;
}
int main(void)
{
	int n;
	
	vector<int> couponP(0);
	vector<int> couponN(0);
	vector<int> productP(0);
	vector<int> productN(0);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int a;
		scanf("%d", &a);
		if (a < 0) couponN.push_back(a);
		else couponP.push_back(a);
	}

	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int a;
		scanf("%d", &a);
		if (a < 0) productN.push_back(a);
		else productP.push_back(a);
	}
	sort(couponP.begin(), couponP.end(), cmp);
	sort(couponN.begin(), couponN.end());
	sort(productP.begin(), productP.end(), cmp);
	sort(productN.begin(), productN.end());
	int sum = 0;
	for (int i = 0; i < couponP.size() && i < productP.size(); i++)
		sum += couponP[i] * productP[i];
	for (int i = 0; i < couponN.size() && i < productN.size(); i++)
		sum += couponN[i] * productN[i];
	cout << sum << endl;
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注