A1037 Magic Coupon (25 分)

一、技术总结

  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;
}

原文地址:https://www.cnblogs.com/tsruixi/p/11849959.html

时间: 2024-10-14 20:07:22

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

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

题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[100007],b[100007];long long c[100007],d[100007];int cnta,cntb,cntc,cntd

PAT 1037. Magic Coupon (25)

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 sho

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 so

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, 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 ofers some bonu

PAT1037. Magic Coupon (25)

#include <iostream> #include <algorithm> #include <vector> using namespace std; int nc,np; vector<int> vc,vp; int tmp; int i,j; int total; int main(){ cin>>nc; for(i=0;i<nc;i++){ cin>>tmp; vc.push_back(tmp); } cin>

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

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

PAT (Advanced Level) 1037. Magic Coupon (25)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; const int maxn=100000+10; int n,m; long long

4-9 二叉树的遍历 (25分)

4-9 二叉树的遍历   (25分) 输出样例(对于图中给出的树): Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H 代码:(都是遍历的算法) 1 // 4-9 二叉树的遍历 2 // 3 // Created by Haoyu Guo on 04/02/2017. 4 // Copyright ? 2017 Haoy

5-24 树种统计 (25分)

5-24 树种统计   (25分) 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(\le 10^5≤10?5??),随后N行,每行给出卫星观测到的一棵树的种类名称.种类名称由不超过30个英文字母和空格组成(大小写不区分). 输出格式: 按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位. 输入样例: 29 Red Alder Ash Aspe