zoukankan      html  css  js  c++  java
  • 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陈小旭

  • 相关阅读:
    tensorflow日志信息如何屏蔽?
    测试图像篡改模型fps逐渐降低的原因?
    np.float32()和np.float64
    hive on spark运行时 ,读取不到hive元数据问题
    Hadoop之HDFS(二)HDFS工作机制和集群安全模式
    Spark中的Driver和Executor详解及相关调优
    Linux查看物理CPU个数、核数、逻辑CPU个数
    shell命令查看进程id,并杀死进程,停止脚本
    Flink CDC 2.0 正式发布,详解核心改进
    Kafka 如何保证消息的消费顺序一致性
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6786076.html
Copyright © 2011-2022 走看看