一、技术总结

  1. 这也是一个贪心算法问题,主要在于想清楚,怎么解决输出和最大,两个数组得确保符号相同位相乘,并且绝对值尽可能大。
  2. 可以用两个vector容器存储,然后排序从小到大或是从大到小都可以,一次从两端开始相乘,保证符号相同。

二、参考代码

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(int a, int b){
return a > b;
}
int main(){
int Nc, Np;
int sum = 0;
int p = 0, q = 0;
scanf("%d", &Nc);
vector<int> coupon(Nc);
for(int i = 0; i < Nc; i++){
scanf("%d", &coupon[i]);
}
scanf("%d", &Np);
vector<int> product(Np);
for(int i = 0; i < Np; i++){
scanf("%d", &product[i]);
}
sort(coupon.begin(), coupon.end(), cmp);
sort(product.begin(), product.end(), cmp);
while(q < Nc && p < Np && coupon[q] > 0 && product[p] > 0){
sum += coupon[q] * product[p];
q++; p++;
}
q = Nc - 1, p = Np - 1;
while(p >= 0 && q >= 0 && coupon[q] < 0 && product[p] < 0){
sum += coupon[q] * product[p];
q--; p--;
}
printf("%d", sum);
return 0;
}

A1037 Magic Coupon (25 分)的更多相关文章

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

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

  2. 1037 Magic Coupon (25分)

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

  3. 【PAT甲级】1037 Magic Coupon (25 分)

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

  4. A1037. Magic Coupon

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

  5. PAT甲级——A1037 Magic Coupon

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

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

  7. 1037. Magic Coupon (25)

    #include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...

  8. PAT甲题题解-1037. Magic Coupon (25)-贪心,水

    题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include ...

  9. PAT1037. Magic Coupon (25)

    #include <iostream> #include <algorithm> #include <vector> using namespace std; in ...

随机推荐

  1. Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(一)

    1. 案例1 - 类型和表之间的EF代码优先映射 从EF6.1开始,有一种更简单的方法可以做到这一点.有关 详细信息,请参阅我的新EF6.1类型和表格之间的映射. 直接贴代码了 从EF6.1开始,有一 ...

  2. Window权限维持(九):端口监视器

    后台打印程序服务负责管理Windows操作系统中的打印作业.与服务的交互通过打印后台处理程序API执行,该API包含一个函数(AddMonitor),可用于安装本地端口监视器并连接配置.数据和监视器文 ...

  3. Elastic 使用索引生命周期管理实现热温冷架构

    Elastic: 使用索引生命周期管理实现热温冷架构 索引生命周期管理 (ILM) 是在 Elasticsearch 6.6(公测版)首次引入并在 6.7 版正式推出的一项功能.ILM 是 Elast ...

  4. mvc ajax跳转controller 的路径

    mvc Controller : url: "../phone/index",(控制器名,方法名) 一般处理程序.ashx :  url: "../bianji.ashx ...

  5. Linux用户和权限——用户和用户组管理

    Linux用户和权限——用户和用户组管理 摘要:本文主要介绍了Linux系统中的用户和用户组管理. 用户和用户组 含义 在使用Linux系统时,虽然输入的是自己的用户名和密码,但其实Linux并不认识 ...

  6. JavaScript全局属性和全局函数

    JavaScript全局属性和全局函数可以与所有内置JavaScript对象一起使用. JavaScript全局属性 属性 描述 Infinity 表示正/负无穷大的数值 NaN "Not- ...

  7. colmap编译过程中出现,无法解析的外部符号错误 “__cdecl google::base::CheckOpMessageBuilder::ForVar1(void)”

    错误提示: >colmap.lib(matching.obj) : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: cl ...

  8. Delphi CreateProcess

    Delphi CreateProcess WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程运行指定的可执行文件 2010-12-27 17:00:17|  分 ...

  9. elasticsearch bulk

    情景介绍 公司2000W的数据从mysql 迁移至elasticsearch,以提供微服务.本文基于elasticsearch-py bulk操作实现数据迁移.相比于elasticsearch-dum ...

  10. hexo笔记

    目录 hexo 常用指令 站内文章跳转 hexo安装 NexT主题 默认主题 评论插件-gitalk 修改内容区宽度 添加文章阴影 文章摘要设置 一篇文章多个 categories hexo的一些配置 ...