1037 Magic Coupon (25 point(s))

用STM32CubeMX和HAL库,5分钟搞定TB6612驱动直流电机(附完整代码) 本文详细介绍了如何使用STM32CubeMX和HAL库快速配置TB6612驱动直流电机,包括硬件连接、CubeMX工程配置、电机驱动逻辑实现及进阶优化技巧。通过图形化工具和代码示例,即使是新手也能在5分钟内完成电机调速系统的搭建,大幅提升开发效率。 阅读详情

1037 Magic Coupon (25 point(s))

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

贪心策略。

贪心证明:对于同号的数组,显然两个相对大的整数乘积加上两个相对小的整数乘积得到的和更大。

思路

先分别排序(从小到大)。

然后从两个序列尾部开始,将正数和正数的乘积加到结果ans中;然后从两个序列的头部开始,将负数和负数的乘积加到ans中。

需要注意的是,没有要求要把coupon或者product用完,所以忽略正数和负数相乘的。

#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int main(void){
	int nc,np;
	cin>>nc;ll *c = new ll[nc];
	for(int i=0;i<nc;i++) cin>>c[i];
	cin>>np;ll *p = new ll[np];
	for(int i=0;i<np;i++) cin>>p[i];
	sort(c,c+nc);
	sort(p,p+np);
	ll ans = 0;
	for(int i=nc-1,j=np-1;i>=0&&j>=0;i--,j--){
		if(c[i]>0&&p[j]>0) ans+=c[i]*p[j];
		else break;
	} 
	for(int i=0,j=0;i<nc&&j<np;i++,j++){
		if(c[i]<0&&p[j]<0) ans+=c[i]*p[j];
	}
	cout<<ans<<endl;
	return 0;
}

 

【AHE数据集】历史/未来全球网格热排放数据 PF-AHF AHE数据集 阅读详情

相关推荐

物联网开发笔记(34)- 使用Micropython开发ESP32开发板之控制步进电机(28BYJ-48)

使用Micropython开发ESP32开发板之控制步进电机(28BYJ-48)

zhusongziye的博客 1万+

【PAT(甲级)】1037 Magic Coupon

【PAT(甲级)】1037 Magic Coupon 测试点1,4的猜测

weixin_55202895的博客 337

MediaCreationToolW11:Windows11系统升级与修复利器

在日常使用Windows11系统的过程中,我们可能会遇到一些棘手的问题,比如系统更新时出现异常导致无法成功更新,或者系统本身出现异常需要进行无损修复。此外,当我们的电脑尚未升级到Windows11,而我们又希望进行升级时,一款高效且可靠的工具就显得尤为重要。接下来,MediaCreationToolW11将开始下载Windows11的安装文件,并创建Windows11介质。在“选择你要执行的操作”界面中,选择“ISO文件”。此时,你可以使用你的账户登录系统,并开始使用全新的Windows11系统了。

nntxthml的博客 2409

1037 Magic Coupon(25分)

给定两个集合,集合中的数为任意整数,请你每次都从集合A和B中取一个进行乘积,不断加和到sum,已经选取过的元素不能再次选取,请输出sum的最大值。对集合中的元素进行排序,从左向右依次是:大正数、小正数、0、小负数、大负数,也就是说将绝对值大的负数放在前面。然后用一个双指针从头遍历即可。:考察的是0,对于0来说,遍历到的时候直接跳过即可。

a131529的博客 187

【JavaSE】之流程控制与方法

Java基础之Java流程控制与方法相关知识介绍~

小新要变强的博客 5799

银河麒麟V10系统grub崩溃急救指南:从光盘救援到完整恢复(附常见错误排查)

本文详细介绍了银河麒麟V10系统grub崩溃的急救方法,从使用安装光盘进入救援环境到完整恢复系统的全流程。针对不同损坏程度(轻度、中度、严重)提供了分级恢复策略,包括grub2-install和grub2-mkconfig等关键命令的使用,并附有常见错误排查表,帮助用户快速解决引导问题。

weixin_30955617的博客 483

1037. Magic Coupon (25)-PAT甲级真题(贪心算法)

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 th

柳婼 の blog 2434

【RKNN】RKNN-Toolkit2 Python API函数详解

RKNN-Toolkit2 的 API 设计以开发者效率为核心,覆盖模型转换、量化、部署全流程。通过灵活的参数配置(如多核调度、混合量化)和调试工具(性能分析、精度对比),可快速适配复杂边缘场景。

浩瀚之水的专栏 787

1037 Magic Coupon——PAT甲级

【代码】1037 Magic Coupon——PAT甲级。

flyidj的博客 946

危废焚烧优化控制系统设计与实现:基于数据驱动的控制参数预测

本文提出了一种基于数据驱动的危险废物焚烧过程优化控制方法。系统利用历史运行数据和实时监测数据,通过深度学习模型预测关键控制参数,实现焚烧过程的智能化控制。系统采用Python开发,结合TensorFlow/PyTorch框架,构建了完整的数据采集、预处理、特征工程、模型训练和实时预测闭环系统。该系统能够处理多变量非线性关系,适应工况变化,从历史数据中学习最优控制策略,相比传统PID控制方法具有显著优势。论文详细介绍了系统架构、算法设计和实现细节,为危废焚烧过程的优化控制提供了有效解决方案。

huanghm88的专栏 207

飞牛NAS新增虚拟机功能,如果使用虚拟机网卡直通安装ikuai软路由(如何解决OVS网桥绑定失败以及打开ovs后无法访问飞牛nas等问题)

飞牛NAS新增虚拟机功能,如果使用虚拟机网卡直通安装ikuai软路由

XiaoqiangClub的博客 1万+

【PAT甲级】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 b...

Y_CanFly的小小博客 391

android 14.0 去掉usb授权提示框 默认给予权限

在14.0的系统rom产品开发中,在进行iot关于插入usb设备的开发过程中,在插入usb设备时会弹出usb授权提示框,也带来一些不便, 这个需要默认授予USB权限,插拔usb都不弹出usb弹窗所以这要从usb授权相关管理页默认给与usb权限,接下来分析下 相关的实现功能流程

安卓兼职framework和app工程师的博客 2263

1037 Magic Coupon (25point(s))

文章目录问题解决方法后记 问题 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 o...

weixin_43725617的博客 156

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

1037 Magic Coupon (25 point(s)) 踩坑心得

1037 Magic Coupon (25 point(s)) 原题链接 一开始代码写成这个样子 #include<stdio.h> #include<iostream> #include<algorithm> bool cmp(int a, int b) { return a > b; } using namespace std; int main()...

fire_for_you的博客 386

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

1037 Magic Coupon (25 point(s))

题解 负负得正,正正得正。 #include&lt;iostream&gt; #include&lt;algorithm&gt; #include&lt;vector&gt; #include&lt;cstdio&gt; using namespace std; int n, m; int main() { scanf("%d", &amp;n); vector&lt;int&gt;..

徐伯莱 232

1037 Magic Coupon (25 point(s)) - C语言 PAT 甲级

设计思路: 贪心算法: * 分别对两个序列按相同顺序排序,这里是从小到大的顺序 * 前面都是负数的元素相乘,后面都是正数的元素相乘,累计求和,即为最大值

有三只土豆 240

PAT 甲级1037 Magic Coupon (25 point(s))魔法优惠券

1037 Magic Coupon (25 point(s)) The magic shop in Mars is offering some magic coupons. Each coupon has an integerNprinted on it, meaning that when you use this coupon with a product, you may getNtimes the value of that product back! What is more, the ...

m0_51711089的博客 355

PAT(甲级)1037 Magic Coupon (25point(s))

题目 题目链接 思路 题目大意:给两个数组,每次从两个数组中各取一个数字相乘后相加,使其结果最大; 使用贪心就可以了,由于正数 * 正数,负数 * 负数都是正数,相加都会使结果变大,所以我们不让其进行负数 * 正数的运算; 除此之外,a中最大的正数与b中最大的正数或负数相乘结果值是最大的,即最优解; 先把两个数组都从小到大排序,这样负数肯定都在前面,从前往后都使两个数组对应位都是负数的位相乘,只要有一个出现正数,结束此次迭代; 然后从后往前遍历,使两个数组都是正数的位相乘,只要有一个出现负数,结束此次迭代;

hjyp的博客 183

1037 Magic Coupon (25 分)

1037 Magic Coupon (25 分) Sample Input: 4 1 2 4 -1 4 7 6 -2 -3 Sample Output: 43 一刷4测试点超时代码 #include <iostream> #include <cstdio> using namespace std; int C[100002], P[100002]; int main() ...

恒治学 425

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

大山喵写博客的地方 522

1037 Magic Coupon (25 分)

The magic shop in Mars is offering some magic coupons(优惠卷). Each coupon has an integerNprinted on it, meaning that when you use this coupon with a product, you may getNtimes the value of that prod...

韩江雪de 小屋 801

1037 Magic Coupon (25 分)

1037Magic Coupon(25分) The magic shop in Mars is offering some magic coupons. Each coupon has an integerNprinted on it, meaning that when you use this coupon with a product, you may getNtimes t...

树叶子的博客 356

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 b

小滋 227

1037.Magic Coupon (25)

1037.Magic Coupon (25)pat-al-10372017-02-06 要得到最大的和,每次要从两行中各取出一个数,使之乘积为当前能找到的最大,那肯定是正数和正数相乘、负数和负数相乘(取绝对值最大的)。 先从小到大排序,把负数相乘;再从大到小排序,把正数相乘。 /** * pat-al-1037 * 2017-02-06 * Cpp version * Author: fen

一剑钦安 357

1037 Magic Coupon (25)(25 point(s)(cj)

1037 Magic Coupon (25)(25 point(s)) 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 ge...

Cute_jinx的博客 244

1037 Magic Coupon (25)(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 b...

Chuyuan_li的博客 286
上一篇: 1077 Kuchiguse (20 point(s))
下一篇: 1038 Recover the Smallest Number (30 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值