1037 Magic Coupon
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 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, 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
题意:
给出两个数组,求出这两个数组中,任意选取一个数字的乘积和的最大值。(每个数字只能使用一次)
思路:
简单模拟。
Code:
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 int nc, np;
7 cin >> nc;
8 vector<int> coupons(nc);
9 for (int i = 0; i < nc; ++i) cin >> coupons[i];
10 sort(coupons.begin(), coupons.end(), greater<int>());
11 cin >> np;
12 vector<int> products(np);
13 for (int i = 0; i < np; ++i) cin >> products[i];
14 sort(products.begin(), products.end(), greater<int>());
15 vector<bool> couponsUsed(nc + 1, false);
16 vector<bool> productsUsed(np + 1, false);
17 int i = 0, j = 0, res = 0;
18 while (i < nc && j < np) {
19 if (coupons[i] * products[j] > 0) {
20 couponsUsed[i] = true;
21 productsUsed[j] = true;
22 res += coupons[i++] * products[j++];
23 } else
24 break;
25 }
26 if (i == nc || j == np)
27 cout << res << endl;
28 else {
29 i = nc - 1;
30 j = np - 1;
31 while (i >= 0 && j >= 0) {
32 if (couponsUsed[i] || productsUsed[j]) break;
33 if (coupons[i] * products[j] > 0) {
34 res += coupons[i--] * products[j--];
35 } else
36 break;
37 }
38 cout << res << endl;
39 }
40 return 0;
41 }
1037 Magic Coupon的更多相关文章
- 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 甲级 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 Advanced 1037 Magic Coupon (25) [贪⼼算法]
题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...
- 1037 Magic Coupon (25分)
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- 1037. Magic Coupon (25)
#include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...
- PAT甲题题解-1037. Magic Coupon (25)-贪心,水
题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include ...
随机推荐
- mysql添加远程连接权限
查看登录用户 mysql> select host,user,password from user; 想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可. 这里有多个root,对应着 ...
- Java开发不懂Docker,学尽Java也枉然,阿里P8架构师手把手带你玩转Docker实战
转: Java开发不懂Docker,学尽Java也枉然,阿里P8架构师手把手带你玩转Docker实战 Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一 ...
- Arrays.Sort()中的那些排序算法
本文基于JDK 1.8.0_211撰写,基于java.util.Arrays.sort()方法浅谈目前Java所用到的排序算法,仅个人见解和笔记,若有问题欢迎指证,着重介绍其中的TimSort排序,其 ...
- 【HTB系列】 靶机Swagshop的渗透测试详解
出品|MS08067实验室(www.ms08067.com) 本文作者:是大方子(Ms08067实验室核心成员) 总结与反思 使用vi提权 magento漏洞的利用 magescan 工具的使用 靶机 ...
- python学习之常用数据结构
前言:数据结构不管在哪门编程语言之中都是非常重要的,因为学校的课程学习到了python,所以今天来聊聊关于python的数据结构使用. 一.列表 list 1.列表基本介绍 列表中的每个元素都可变的, ...
- MySQL 表的约束与数据库设计
DQL 查询语句 排序 # 单列排序 * 只按某一个字段进行排序,单列排序 # 组合排序 * 同时对多个字段进行排序,如果第1个字段相等,则按照第2个字段排序,依次类推 * 语法: # 具体操作 * ...
- HTTP 状态码(转载)
本文由 简悦 SimpRead 转码, 原文地址 www.cnblogs.com HTTP 状态码 (HTTP Status Code) 状态码并不是每个都有,为了后期扩展.[update201705 ...
- 提升Idea启动速度与Tomcat日志乱码问题
提升Idea启动速度与Tomcat日志乱码问题 前言 由于重装了一次Idea,所以有些设置时间就忘了,在此做个记录,以便以后忘记后可以来翻阅 Idea启动速度 一.将Idea所在的 安装文件夹 在wi ...
- 云原生的弹性 AI 训练系列之一:基于 AllReduce 的弹性分布式训练实践
引言 随着模型规模和数据量的不断增大,分布式训练已经成为了工业界主流的 AI 模型训练方式.基于 Kubernetes 的 Kubeflow 项目,能够很好地承载分布式训练的工作负载,业已成为了云原生 ...
- Git基本操作流程
技术背景 Gitee是一款国内的git托管服务,对于国内用户较为友好,用户可以访问Gitee地址来创建自己的帐号和项目,并托管在Gitee平台上.既然是git的托管服务,那我们就可以先看看git的一些 ...