PAT--1037. Magic Coupon

本文介绍了一种算法,用于解决如何使用不同数值的魔法优惠券购买产品以获得最大返利的问题。通过将优惠券和产品按正数、负数分类,并按绝对值大小排序,再配对求和,实现利益最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题解

nc[]和np[] 按照正数, 0, 负数并按绝对值降序的次序排序。
然后两个指针i, j扫描nc和np,符号相同的作乘积累加。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 100010;
int nc, np;
int a[maxn], b[maxn];

int cmp(int x, int y){
    if(x >= 0 && y >= 0) return x > y;
    if(x <= 0 && y <= 0) return abs(x) > abs(y);
    if(x >= 0 && y <= 0) return 1;
    if(x <= 0 && y >= 0) return 0;
}

bool sign(int x, int y){
    return (x > 0 && y > 0) || (x < 0 && y < 0);
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif // ONLINE_JUDGE

    scanf("%d", &nc);
    for(int i = 0; i < nc; ++i) scanf("%d", a + i);
    scanf("%d", &np);
    for(int i = 0; i < np; ++i) scanf("%d", b + i);

    sort(a, a + nc, cmp);
    sort(b, b + np, cmp);

    long long ans = 0;
    int i = 0, j = 0;
    while(i < nc && j < np){
        if(sign(a[i], b[j])) {
            ans += a[i] * b[j];
            i++, j++;
        }else if(a[i] <= 0 && b[j] >= 0) j++;
        else i++;
    }

    printf("%lld\n", ans);

    return 0;
}
<think>我们正在使用Maven 3.9.9,并且需要执行命令:mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U 这个命令涉及几个部分: 1. `-Prelease-nacos`:激活名为`release-nacos`的profile 2. `-Dmaven.test.skip=true`:跳过测试 3. `clean`:清理目标,删除target目录 4. `install`:将构建的工件安装到本地仓库 5. `-U`:强制更新快照依赖 在Maven 3.9.9中,该命令应该可以正常运行。但需要注意以下几点: 1. **Profile的存在**:项目中的pom.xml必须定义了`release-nacos`这个profile,否则这个参数不会生效(但也不会报错)。如果这个profile是在父pom中定义的,那么子模块也会继承。 2. **跳过测试**:`-Dmaven.test.skip=true`会跳过测试的编译和执行。这可以节省时间,但可能会遗漏测试环节。 3. **更新快照**:`-U`选项会强制更新所有快照依赖到最新的版本。这在依赖频繁变更时很有用,但也会增加构建时间。 4. **Maven版本**:Maven 3.9.9是较新的版本(截至2023年),通常兼容性较好。但如果你在构建过程中遇到问题,可以检查是否因为版本更新导致某些插件不兼容。 5. **构建环境**:确保你的Maven环境配置正确(如settings.xml中的仓库配置、镜像等),特别是当项目依赖私有仓库时。 6. **多模块项目**:如果项目是多模块的(如引用[1]中的pom.xml所示),该命令会在所有模块上执行。 执行步骤: 1. 打开命令行终端,进入项目根目录(包含pom.xml的目录)。 2. 运行命令:`mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U` 3. 观察构建输出,如果有错误,根据错误信息进行排查。 常见问题及解决: - **Profile未定义**:如果构建过程中没有应用profile的预期效果,请检查pom.xml中是否定义了`release-nacos`,或者检查是否有拼写错误。 - **依赖下载失败**:检查网络连接,以及Maven的settings.xml中仓库配置是否正确。 - **插件版本问题**:如果构建失败并提示插件问题,可以尝试更新插件版本或在profile中配置插件。 由于引用[1]中展示了一个多模块项目的pom.xml,我们可以假设用户的项目结构也是多模块的。因此,该命令将依次构建所有模块。 另外,注意在引用[1]中,项目打包类型为`pom`,并且包含了多个模块。因此,在根目录执行该命令会构建所有子模块。 引用[2]是关于Kafka的Docker运行命令,与当前Maven命令无关,可以忽略。 综上所述,在Maven 3.9.9中执行该命令是可行的,但需要确保项目配置正确。</think>### Maven 3.9.9 构建命令指南 您使用的命令 `mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U` 在 Maven 3.9.9 中的执行流程和注意事项如下: #### 1. **命令解析** | 参数 | 作用 | 注意事项 | |------|------|----------| | `-Prelease-nacos` | 激活名为 `release-nacos` 的 profile | 需在 pom.xml 中定义该 profile | | `-Dmaven.test.skip=true` | 跳过测试编译和执行 | 节省时间但跳过质量检查 | | `clean` | 清理 target 目录 | 删除历史构建产物 | | `install` | 安装工件到本地仓库 | 路径:`~/.m2/repository` | | `-U` | 强制更新快照依赖 | 确保获取最新快照版本 | #### 2. **执行流程** ```mermaid graph LR A[开始] --> B[激活 release-nacos profile] B --> C[清理 target 目录] C --> D[跳过测试阶段] D --> E[下载最新快照依赖] E --> F[编译并安装工件] F --> G[结束] ``` #### 3. **关键注意事项** 1. **Profile 配置** 确保 `pom.xml` 中正确定义了 `release-nacos` profile(如引用[1]的多模块配置): ```xml <profiles> <profile> <id>release-nacos</id> <build> <plugins> <!-- Nacos 相关插件配置 --> </plugins> </build> </profile> </profiles> ``` 2. **多模块构建** 若项目如引用[1]是聚合项目(`<packaging>pom</packaging>`),命令会在所有子模块(`gulimall-coupon` 等)上递归执行。 3. **网络与仓库** `-U` 参数要求: - 稳定网络连接(尤其涉及私有仓库时) - `settings.xml` 配置正确的镜像仓库(如阿里云镜像) 4. **Maven 3.9.9 特性** - 增强 HTTPS 仓库安全性验证 - 改进依赖解析算法(与旧版行为略有差异) - 更严格的插件版本校验 #### 4. **常见问题排查** | 问题现象 | 解决方案 | |----------|----------| | `ProfileNotFoundException` | 检查 profile ID 拼写及 pom.xml 位置 | | 快照依赖更新失败 | 运行 `mvn dependency:purge-local-repository` 清理本地缓存 | | 构建时间过长 | 添加 `-T 1C` 参数启用多线程构建(如:`mvn -T 1C ...`) | | 插件不兼容 | 在 profile 中显式指定插件版本 | > **执行建议**:首次运行时移除 `-Dmaven.test.skip=true` 验证测试通过率,后续构建可加回以提升效率[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值