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



#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

bool cmp(const int &a , const int &b){return a>b;}

int main()
{
    int m=0 , n=0;
    vector<int>coupons , products;
    cin>>m;
    coupons.resize(m);
    for(int i=0;i<m;i++)cin>>coupons[i];
    cin>>n;
    products.resize(n);
    for(int i=0;i<n;i++)cin>>products[i];

    sort(coupons.begin() , coupons.end() , cmp);
    sort(products.begin() , products.end() , cmp);
    int total=0;
    while(coupons.size()>0 && products.size()>0){
        if(coupons.front() > 0 && products.front() >0){
            total+=coupons.front() * products.front();
            coupons.erase(coupons.begin());
            products.erase(products.begin());
        }
        else if(coupons.back() < 0 && products.back() <0){
            total+=coupons.back()*products.back();
            coupons.pop_back();
            products.pop_back();
        }
        else break;
    }

    cout<<total;
    return 0;
}








#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

bool cmp1(const int &a , const int &b){return a>b;}
bool cmp2(const int &a , const int &b){return a<b;}

int main()
{
    int m=0 , n=0;
    vector<int>p_coupons , p_products ,n_coupons , n_products;
    cin>>m;
    p_coupons.resize(m);
    n_coupons.resize(m);
    for(int i=0;i<m;i++){
        int t=0;
        cin>>t;
        if(t>0)p_coupons.push_back(t);
        else n_coupons.push_back(t);
    }
    cin>>n;
    p_products.resize(n);
    n_products.resize(n);
    for(int i=0;i<n;i++){
        int t=0;
        cin>>t;
        if(t>0)p_products.push_back(t);
        else n_products.push_back(t);
    }

    sort(p_coupons.begin() , p_coupons.end() , cmp1);
    sort(p_products.begin() , p_products.end() , cmp1);
    sort(n_coupons.begin() , n_coupons.end() , cmp2);
    sort(n_products.begin() , n_products.end() , cmp2);
    int total=0;
    int _min=std::min(p_coupons.size() , p_products.size());
    for(int i=0;i<_min;i++){
        total+=p_coupons[i]*p_products[i];
    }

    _min=std::min(n_coupons.size() , n_products.size());
    for(int i=0;i<_min;i++){
        total+=n_coupons[i]*n_products[i];
    }

    cout<<total;
    return 0;
}








分析下以下报文: 2025-10-31T15:39:54.089+0800 [http-nio-8443-exec-1] [-] INFO org.apache.catalina.core.ContainerBase.[Tomcat-1].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet' 2025-10-31T15:39:54.089+0800 [http-nio-8443-exec-1] [-] INFO org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet' 2025-10-31T15:39:54.105+0800 [http-nio-8443-exec-1] [-] INFO org.springframework.web.servlet.DispatcherServlet - Completed initialization in 16 ms 2025-10-31T15:40:53.393+0800 [RBEVC-Scheduler-MQRE] [-] WARN com.alibaba.druid.pool.DruidAbstractDataSource - discard long time none received connection. , jdbcUrl : jdbc:mysql://192.168.0.80:3306/phoenix?useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&useUnicode=yes&characterEncoding=utf-8&useSSL=no&zeroDateTimeBehavior=CONVERT_TO_NULL&rewriteBatchedStatements=true, version : 1.2.5, lastPacketReceivedIdleMillis : 60010 2025-10-31T15:41:46.147+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.trade.api.trade.tradeReceipt-processor-1] [a43a002691c743ca9ac99944be99fe51] INFO com.hd123.phoenix.common.events.subscriber.AmqpDomainEventDispatcher - amqp consume,message="{\"payload\":\"{\\\"id\\\":\\\"f7031f9a-09ee-4ad3-9c5c-355f3647f642\\\",\\\"aggregateId\\\":\\\"677539924644036723\\\",\\\"time\\\":\\\"2025-10-31T07:41:46.121+0000\\\"}\",\"properties\":{\"eventType\":\"TradeReceiptCreatedEvent\",\"aggregateType\":\"com.hd123.phoenix.trade.api.trade.tradeReceipt\"}}" 2025-10-31T15:41:46.149+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.trade.api.trade.tradeReceipt-processor-1] [a43a002691c743ca9ac99944be99fe51] INFO com.hd123.phoenix.custom.core.impl.handler.TradeCreatedEvenConsumer - 未开启交易邮件通知,忽略。 2025-10-31T15:41:46.150+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.trade.api.trade.tradeReceipt-processor-1] [-] INFO extsys.RabbitMQ - {"__EVENT__":"RabbitMQ request begin","apiVersion":"2.1.10.RELEASE","apiName":"Channel::basicAck","request.deliveryTag":1,"request.multiple":false,"request.message.id":"a43a002691c743ca9ac99944be99fe51","request.message.queueName":"production-PhoenixCustomWebApplication-com.hd123.phoenix.trade.api.trade.tradeReceipt"} 2025-10-31T15:41:46.150+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.trade.api.trade.tradeReceipt-processor-1] [-] INFO extsys.RabbitMQ - {"__EVENT__":"RabbitMQ request end","apiVersion":"2.1.10.RELEASE","apiName":"Channel::basicAck","requestDuration":0} 2025-10-31T15:41:51.054+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon-processor-2] [07d4547c784248ebbca7ab0eac8043a2] INFO com.hd123.phoenix.common.events.subscriber.AmqpDomainEventDispatcher - amqp consume,message="{\"payload\":\"{\\\"id\\\":\\\"be0604fb-feaf-4c46-a424-e478ddb99278\\\",\\\"aggregateId\\\":\\\"388dbb12-f88a-4b2c-8434-477ebf7e951e\\\",\\\"time\\\":\\\"2025-10-31T07:41:51.025+0000\\\"}\",\"properties\":{\"eventType\":\"CouponIssuedEvent\",\"aggregateType\":\"com.hd123.phoenix.coupon.api.coupon.Coupon\"}}" 2025-10-31T15:41:51.054+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon-processor-2] [-] INFO extsys.RabbitMQ - {"__EVENT__":"RabbitMQ request begin","apiVersion":"2.1.10.RELEASE","apiName":"Channel::basicAck","request.deliveryTag":10,"request.multiple":false,"request.message.id":"07d4547c784248ebbca7ab0eac8043a2","request.message.queueName":"production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon"} 2025-10-31T15:41:51.054+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon-processor-2] [-] INFO extsys.RabbitMQ - {"__EVENT__":"RabbitMQ request end","apiVersion":"2.1.10.RELEASE","apiName":"Channel::basicAck","requestDuration":0} 2025-10-31T15:42:16.067+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon-processor-5] [d314d1f8df44400684cbb79587f5d6d2] INFO com.hd123.phoenix.common.events.subscriber.AmqpDomainEventDispatcher - amqp consume,message="{\"payload\":\"{\\\"id\\\":\\\"b1c9ec0a-93d5-477b-97d0-9d596cafe6b7\\\",\\\"aggregateId\\\":\\\"3b402042-3dbc-4269-a41b-3c7a32ef3076\\\",\\\"time\\\":\\\"2025-10-31T07:42:16.011+0000\\\"}\",\"properties\":{\"eventType\":\"CouponWrittenOffEvent\",\"aggregateType\":\"com.hd123.phoenix.coupon.api.coupon.Coupon\"}}" 2025-10-31T15:42:16.068+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon-processor-5] [-] INFO extsys.RabbitMQ - {"__EVENT__":"RabbitMQ request begin","apiVersion":"2.1.10.RELEASE","apiName":"Channel::basicAck","request.deliveryTag":11,"request.multiple":false,"request.message.id":"d314d1f8df44400684cbb79587f5d6d2","request.message.queueName":"production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon"} 2025-10-31T15:42:16.068+0800 [production-PhoenixCustomWebApplication-com.hd123.phoenix.coupon.api.coupon.Coupon-processor-5] [-] INFO extsys.RabbitMQ - {"__EVENT__":"RabbitMQ request end","apiVersion":"2.1.10.RELEASE","apiName":"Channel::basicAck","requestDuration":0}
最新发布
11-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值