• 1037. Magic Coupon (25)


    题目链接:http://www.patest.cn/contests/pat-a-practise/1037

    题目:

    1037. Magic Coupon (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 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<= NC, NP <= 105, 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

    分析:

    抽象出来就是找到两个数列相应乘积的最大值,数仅仅能用一次,且能够不用。

    要求优惠券组合的最大值,基于这样一个事实,把两边的优惠券都按递增顺序排好,然后选取都是绝对值最大的两个负数相乘,也能够选择绝对值最大的两个正数相乘。依次直到没有能够配对的,得到的累加和就是最大的。

    特别要注意while中成立的条件,不然可能v_begin会越界导致V[v_begin]没有值

    AC代码:

    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    #include<vector>
    using namespace std;
    vector<long long>V1;
    vector<long long>V2;
    int main(){
     //freopen("F://Temp/input.txt", "r", stdin);
     int n, m;
     cin >> n;
     for (int i = 0; i < n; i++){
      long long tmp;
      cin >> tmp;
      V1.push_back(tmp);
     }
     cin >> m;
     if (n == 0 || m == 0){
      cout << 0 << endl;
      return 0;
     }
     for (int i = 0; i < m; i++){
      long long tmp;
      cin >> tmp;
      V2.push_back(tmp);
     }
     sort(V1.begin(), V1.end());
     sort(V2.begin(), V2.end());
     int v1_begin = 0, v1_end = V1.size() - 1 , v2_begin = 0, v2_end = V2.size() - 1;
     long long sum = 0;
     while (v1_begin <V1.size() && v2_begin < V2.size() && V1[v1_begin] <0 &&  V2[v2_begin] < 0){//找到各自中绝对值最大的负数进行相乘
      sum += V1[v1_begin] * V2[v2_begin];
      v1_begin++;
      v2_begin++;
     }
     while (v1_end >= 0 && v2_end >= 0 && V1[v1_end] > 0 && V2[v2_end] > 0){
      sum += V1[v1_end] * V2[v2_end];//找到各自中最大的正数进行相乘
      v1_end--;
      v2_end--;
     }
     cout << sum << endl;
     return 0;
    }


    截图:


    ——Apie陈小旭

  • 相关阅读:
    用外部表的方式查询当天数据库alert日志文件
    比较数据泵和exp/imp对相同数据导出/导入的性能差异
    理解Oracle TM和TX锁
    Rocky4.2下安装金仓v7数据库(KingbaseES)
    理解listagg函数
    sql*loader的直接加载方式和传统加载方式的性能差异
    Rocky4.2下安装达梦(DM)6数据库
    演示对sys用户和普通用户进行审计的示例
    演示一个通过触发器进行审计的示例
    演示一个使用db vault进行安全控制的示例
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6786076.html
Copyright © 2020-2023  润新知