[PAT A1037]Magic Coupon

博客围绕PAT A1037题目展开,介绍了题目描述、输入输出格式及样例。题目要求从前n个数和后m个数中各选一个相乘,求最大结果。解析采用贪心策略,将两数组升序排列,用指针操作,还考虑了一个数组全负、一个全正的特殊情况。

[PAT A1037]Magic Coupon

题目描述

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.

输入格式

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

输出格式

For each test case, simply print in a line the maximum amount of money you can get back.

输入样例

4
1 2 4 -1
4
7 6 -2 -3

输出样例

43

解析

  1. 这道题目大致意思就是先输入n个数,后输入m个数,我们可以从前n个数中选一个数与后m个数中选一个数相乘,最后需要我们求最大的结果,以样例为例,我们选择4和7,2和6,-1和-3,这样加起来结果是28+12+3=43,剩下的两个数就没必要取了,因为它们相乘的结果必然会使得结果减少。
  2. 我的思路是将它们按升序排列,使用两个指针分别指向两个数组中最前面的数字,如果它们都是负数,那么使它们相乘,直到其中有一个为正为止;然后使两个指针都指向最后面的数,如果他们都是正数,那么使它们相乘,直到有一个为负为止。
  3. 这其实是贪心的策略,这样我们需要考虑的只有一种特殊情况,就是两个数组,一个全为负,一个全为正,这样我们只需要判断一下这个情况给一个输出就行了。另外不要自作聪明的使用num[i]*num[j]>0表示它们都是负数,因为在两个数组负数个数相等的时候,这样会导致出错,读者可以自己试试。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
 int n, m;
 vector<int> num1, num2; //两个数组,分别存放第一行数和第二行数
 scanf("%d", &n);
 for (int i = 0; i < n; i++) {
  int temp;
  scanf("%d", &temp);
  num1.push_back(temp);
 }
 scanf("%d", &m);
 for (int i = 0; i < m; i++) {
  int temp;
  scanf("%d", &temp);
  num2.push_back(temp);
 }
 sort(num1.begin(), num1.end());  //按升序排序
 sort(num2.begin(), num2.end());
 int i = 0, j = 0, ans = 0;
 while (num1[i] < 0 && num2[j] < 0) {
  ans += num1[i] * num2[j];
  i++; j++;
 }
 i = n - 1; j = m - 1;
 while (num1[i] > 0 && num2[j] > 0) {
  ans += num1[i] * num2[j];
  i--; j--;
 }
 if (ans == 0)ans += (num1[0] * num2[m - 1] > num1[n - 1] * num2[0]) ? num1[0] * num2[m - 1] : num1[n - 1] * num2[0];
 printf("%d", ans);
 return 0;
}
水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值