田忌赛马 - 华为OD统一考试

OD统一考试(C卷)

分值: 200分

题解: Java / Python / C++

alt

题目描述

给定两个只包含数字的数组a,b,调整数组a里面数字的顺序,使得尽可能多的a[i] > b[i]。

数组a和b中的数字各不相同。输出所有可以达到最优结果的a数组数量。

输入描述

输入的第一行是数组a中的数字,其中只包含数字,每两个数字之间相隔一个空格,a数组大小不超过10

输入的第一行是数组b中的数字,其中只包含数字,每两个数字之间相隔一个空格,b数组大小不超过10

输出描述

输出所有可以达到最优结果的a数组数量

示例1

输入:
11 8 20
10 13 7

输出:
1

说明:
最优结果只有一个,a=[11,20,8] ,故输出 1 。

示例2

输入:
11 12 20
10 13 7

输出:
2

说明:
有两个 a 数组的排列可以达到最优结果, [12,20,11] 和 [11,20,12] ,故输出 2 。

题解

题目要求调整数组a中数字的顺序,使得尽可能多的a[i] > b[i]。可以通过穷举a的所有排列,然后统计满足条件的排列数量。

主要步骤:

  1. 读取输入的数组a和b。
  2. 初始化变量,包括数组长度n,最大满足条件的个数maxCnt,以及统计满足条件的排列数量resultCnt。
  3. 使用递归进行排列组合的搜索,遍历a的所有排列,统计满足条件的个数和数量。
  4. 输出结果。

Java

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author code5bug
 */
public class Main {
   
    public static void main(String[] args) {
   
        Scanner in = new Scanner(System.in);
        int[] a = Arrays.stream(in.nextLine().split(" "))
                .mapToInt(Integer::parseInt).toArray();
        int[] b = Arrays.stream(in.nextLine().split(" "))
                .mapToInt(Integer::parseInt).toArray();

        Solution solution = new Solution(a, b);
        solution.solve(0, 0);
        System.out.println(solution.resultCnt);
    }
}

class Solution {
   
    int[] a, b;
    int n, maxCnt, resultCnt;
    boolean[] vis;

    public Solution(int[] a, int[] b) {
   
        this.a = a;
        this.b = b;
        this.n = a.length;
        this.maxCnt = 0;
        this.resultCnt = 0;
        this.vis = new boolean[n];
    }

    void solve(int idx, int cnt) {
   
        // 剪枝:找不到最优结果a数组
        if (n - idx + cnt < maxCnt) return;

        // 所有数字排列完
        if (idx == n) {
   
            if (cnt > maxCnt) {
    // 找到更优的结果a数组
                maxCnt = cnt;
                resultCnt = 1;
            } else if (cnt == maxCnt) {
    // 找了新的一种最优结果a数组
                resultCnt++;
            }
            return;
        }

        for (int i = 0; i < n; i++) {
   
            if (vis[i]) continue;
            vis[i] = true;
            solve(idx + 1, cnt + (a[i] > b[idx] ? 1 : 0));
            vis[i] = false;
        }
    }
}

Python

a = list(map(int, input().split()))
b = list(map(int, input().split()))

# 数组长度, 最大a[i]>b[i]最大个数, 达到最优结果的a数组数量
n, max_cnt, result_cnt = len(a), 0, 0
vis = [False] * n


def solve(idx: int, cnt: int):
    """
    :param idx: 当前排列的数组下标
    :param cnt: 当前排列的数组中,满足 a[i] > b[i]的个数
    """
    global vis, n, max_cnt, result_cnt

    if n - idx + cnt < max_cnt:  # 剪枝:找不到最优结果a数组
        return 0

    if idx == n:                # 所有数字排列完
        if cnt > max_cnt:       # 找到更优的结果a数组
            max_cnt, result_cnt = cnt, 1
        elif cnt == max_cnt:    # 找了新的一种最优结果a数组
            result_cnt += 1
        return

    for i in range(n):
        if vis[i]:
            continue
        vis[i] = True
        solve(idx + 1, cnt + (1 if a[i] > b[idx] else 0))
        vis[i] = False


solve(0, 0)
print(result_cnt)

C++

#include <iostream>
#include <vector>

using namespace std;

vector<int> a, b;
int n, max_cnt, result_cnt;
vector<bool> vis;

// 读取一行数组
vector<int> readArray() {
   
    vector<int> arr;
    int num;
    while (cin >> num) {
   
        arr.push_back(num);
        if (cin.get() == '\n') {
   
            break;
        }
    }
    return arr;
}

void solve(int idx, int cnt) {
   
    // 剪枝:找不到最优结果a数组
    if (n - idx + cnt < max_cnt) {
   
        return;
    }

    // 所有数字排列完
    if (idx == n) {
   
        if (cnt > max_cnt) {
       // 找到更优的结果a数组
            max_cnt = cnt;
            result_cnt = 1;
        }else if (cnt == max_cnt) {
    // 找了新的一种最优结果a数组
            result_cnt += 1;
        }
        return;
    }

    for (int i = 0; i < n; ++i) {
   
        if (vis[i]) continue;

        vis[i] = true;
        solve(idx + 1, cnt + (a[i] > b[idx] ? 1 : 0));
        vis[i] = false;
    }
}

int main() {
   
    // 读取数组a
    a = readArray();

    // 读取数组b
    b = readArray();

    // 初始化变量
    n = a.size();
    vis = vector<bool>(n, false);

    solve(0, 0);

    // 输出结果
    cout << result_cnt << endl;

    return 0;
}

相关练习题

题号 题目 难易
LeetCode 46 46. 全排列 中等
LeetCode 47 47. 全排列 II 中等

‍❤️‍华为OD机试面试交流群每日真题分享): 加V时备注“华为od加群”

🙏整理题解不易, 如果有帮助到您,请给点个赞 ‍❤️‍ 和收藏 ⭐,让更多的人看到。🙏🙏🙏

相关推荐

  1. 贪心算法之赛马,多种语言实现

    2024-02-07 20:38:02       25 阅读
  2. B3928 [GESP202312 四级] 赛马

    2024-02-07 20:38:02       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-07 20:38:02       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-07 20:38:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-07 20:38:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-07 20:38:02       18 阅读

热门阅读

  1. Unity类银河恶魔城学习记录4-3 P56 On Hix FX源代码

    2024-02-07 20:38:02       29 阅读
  2. 【C++ 二维前缀和】约会

    2024-02-07 20:38:02       26 阅读
  3. svn常用命令及过滤文件 global ignore pattern

    2024-02-07 20:38:02       30 阅读
  4. 随机森林分类器原理简述

    2024-02-07 20:38:02       32 阅读
  5. 【Android】手机端使用NanoHTTPD搭建服务器

    2024-02-07 20:38:02       28 阅读
  6. 力扣:78. 子集

    2024-02-07 20:38:02       32 阅读
  7. 2024年-视觉AI检测的面试题目总结

    2024-02-07 20:38:02       29 阅读
  8. SouthLeetCode-打卡24年02月第1周

    2024-02-07 20:38:02       27 阅读
  9. CDN的深入理解+搭建自己的CDN

    2024-02-07 20:38:02       35 阅读
  10. IDEA2023SpingBoot只能勾选17和21

    2024-02-07 20:38:02       35 阅读