洛谷P11044

发布于:2025-03-17 ⋅ 阅读:(12) ⋅ 点赞:(0)

洛谷 P11044

目录


题目描述

(根据题目内容补充)


示例

示例 1

输入:

(输入示例)

输出:

(输出示例)

解释:
(解释示例)


思路分析

问题核心

(简要描述问题的核心要求)

思路拆解

  1. 步骤 1
    • (详细描述第一步的思路)
  2. 步骤 2
    • (详细描述第二步的思路)
  3. 步骤 3
    • (详细描述第三步的思路)

代码段

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        while (q > 0) {
            int a2 = sc.nextInt();
            int a3 = sc.nextInt();
            int a4 = sc.nextInt();
            int b4 = sc.nextInt();
            int b6 = sc.nextInt();
            int ans = 0;
            while (a3 >= 2 && b6 >= 1) {
                a3 = a3 - 2;
                b6 = b6 - 1;
                ans += 6;
            }
            while (a4 >= 1 && a2 >= 1 && b6 >= 1) {
                a4--;
                a2--;
                b6--;
                ans += 6;
            }
            while (a4 >= 1 && b4 >= 1) {
                a4--;
                b4--;
                ans += 4;
            }
            while (a2 >= 3 && b6 >= 1) {
                a2 = a2 - 3;
                b6--;
                ans += 6;
            }
            while (a2 >= 2 && b4 >= 1) {
                a2 = a2 - 2;
                b4--;
                ans += 4;
            }
            while (a3 >= 1 && a2 >= 1 && b6 >= 1) {
                a3--;
                a2--;
                b6--;
                ans += 5;
            }
            while (a3 >= 1 && b4 >= 1) {
                a3--;
                b4--;
                ans += 3;
            }
            while (a4 >= 1 && b6 >= 1) {
                a4--;
                b6--;
                ans += 4;
            }
            while (a3 >= 1 && b6 >= 1) {
                a3--;
                b6--;
                ans += 3;
            }
            while (a2 >= 1 && b6 >= 1) {
                a2--;
                b6--;
                ans += 2;
            }
            while (a2 >= 1 && b4 >= 1) {
                a2--;
                b4--;
                ans += 2;
            }
            System.out.println(ans);
            q--;
        }
        sc.close();
    }
}

在这里插入图片描述


代码逐行讲解

  1. 输入处理

    Scanner sc = new Scanner(System.in);
    int q = sc.nextInt();
    
    • 使用 Scanner 读取输入的整数 q,表示查询的次数。
  2. 循环处理每个查询

    while (q > 0) {
        int a2 = sc.nextInt();
        int a3 = sc.nextInt();
        int a4 = sc.nextInt();
        int b4 = sc.nextInt();
        int b6 = sc.nextInt();
        int ans = 0;
    
    • 对于每个查询,读取五个整数 a2, a3, a4, b4, b6,并初始化答案 ans 为 0。
  3. 组合计算

    while (a3 >= 2 && b6 >= 1) {
        a3 = a3 - 2;
        b6 = b6 - 1;
        ans += 6;
    }
    
    • 如果 a3 大于等于 2 且 b6 大于等于 1,则减少相应的数量,并将 ans 增加 6。
  4. 其他组合计算

    while (a4 >= 1 && a2 >= 1 && b6 >= 1) {
        a4--;
        a2--;
        b6--;
        ans += 6;
    }
    
    • 类似地,根据不同的条件组合,减少相应的数量,并增加 ans
  5. 输出结果

    System.out.println(ans);
    q--;
    
    • 输出当前查询的结果,并减少查询次数 q
  6. 关闭输入流

    sc.close();
    
    • 关闭 Scanner 以释放资源。

复杂度分析

时间复杂度

  • 每个查询中,最多有 11 个 while 循环,每个循环的时间复杂度为 O(1)
  • 假设每个查询中每个 while 循环最多执行 k 次,则每个查询的时间复杂度为 O(k)
  • 总共有 q 个查询,因此总时间复杂度为 O(q * k)

空间复杂度

  • 只使用了常数级别的额外空间,因此空间复杂度为 O(1)

总结的知识点

  1. 输入处理

    • 使用 Scanner 读取输入,并处理多个查询。
  2. 循环与条件判断

    • 使用 while 循环和条件判断来处理不同的组合情况。
  3. 组合计算

    • 根据不同的条件组合,减少相应的数量,并累加结果。
  4. 输出结果

    • 使用 System.out.println 输出结果。

整合

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        while (q > 0) {
            int a2 = sc.nextInt();
            int a3 = sc.nextInt();
            int a4 = sc.nextInt();
            int b4 = sc.nextInt();
            int b6 = sc.nextInt();
            int ans = 0;
            while (a3 >= 2 && b6 >= 1) {
                a3 = a3 - 2;
                b6 = b6 - 1;
                ans += 6;
            }
            while (a4 >= 1 && a2 >= 1 && b6 >= 1) {
                a4--;
                a2--;
                b6--;
                ans += 6;
            }
            while (a4 >= 1 && b4 >= 1) {
                a4--;
                b4--;
                ans += 4;
            }
            while (a2 >= 3 && b6 >= 1) {
                a2 = a2 - 3;
                b6--;
                ans += 6;
            }
            while (a2 >= 2 && b4 >= 1) {
                a2 = a2 - 2;
                b4--;
                ans += 4;
            }
            while (a3 >= 1 && a2 >= 1 && b6 >= 1) {
                a3--;
                a2--;
                b6--;
                ans += 5;
            }
            while (a3 >= 1 && b4 >= 1) {
                a3--;
                b4--;
                ans += 3;
            }
            while (a4 >= 1 && b6 >= 1) {
                a4--;
                b6--;
                ans += 4;
            }
            while (a3 >= 1 && b6 >= 1) {
                a3--;
                b6--;
                ans += 3;
            }
            while (a2 >= 1 && b6 >= 1) {
                a2--;
                b6--;
                ans += 2;
            }
            while (a2 >= 1 && b4 >= 1) {
                a2--;
                b4--;
                ans += 2;
            }
            System.out.println(ans);
            q--;
        }
        sc.close();
    }
}

总结

掌握了如何处理多个查询,并根据不同的条件组合计算结果。关键点包括:

  • 使用 Scanner 读取输入。
  • 使用 while 循环和条件判断处理不同的组合情况。
  • 根据条件减少相应的数量,并累加结果。
  • 输出每个查询的结果。