【2024华为OD-E卷-100分-箱子之字形摆放】((题目+思路+JavaC++Python解析)

embedded/2025/2/9 7:16:01/

题目描述

给定一个宽度为 width 的仓库,要求将 n 个箱子按之字形(Zigzag)方式摆放。每个箱子的宽度都是 1,箱子必须摆放在仓库的同一层上,且摆放过程中不能重叠。

之字形摆放的定义是:箱子交替地向左和向右对齐。即第1行从左对齐,第2行从右对齐,第3行再次从左对齐,以此类推。

需要输出按之字形摆放箱子后,每一层的最大高度。

输入

  • 第一行包含两个整数 n 和 width,分别表示箱子的数量和仓库的宽度。
  • 第二行包含 n 个整数,表示每个箱子的高度。

输出

  • 输出一个整数数组,表示每一层的最大高度。

示例

输入

6 4
2 3 4 1 2 3

输出

[4 3 3]

解释

仓库宽度为4,6个箱子按之字形摆放如下:

2 3 4 1
      2
      3

每一层的最大高度分别是:4、3、3。

思路

  1. 计算层数:由于箱子是按之字形摆放,我们可以计算出所需的层数。层数可以通过 ceil(n / width) 计算得到,其中 ceil 表示向上取整。
  2. 记录每层的最大高度:使用一个数组来记录每一层的最大高度。
  3. 遍历箱子:按序遍历每个箱子,计算其在第几层以及是向左还是向右对齐。
  4. 更新当前层的最大高度:根据箱子的当前位置和高度,更新对应层的最大高度。

Java 实现

import java.util.*;

public class ZigzagBoxes {
    public static int[] getMaxHeights(int n, int width, int[] heights) {
        int layers = (int) Math.ceil((double) n / width);
        int[] maxHeights = new int[layers];

        for (int i = 0; i < n; i++) {
            int layer = i / width;
            int positionInLayer = i % width;
            boolean leftAligned = layer % 2 == 0;

            int columnIndex = leftAligned ? positionInLayer : width - 1 - positionInLayer;
            maxHeights[layer] = Math.max(maxHeights[layer], heights[i]);

            // To keep track of the effective max height at each "virtual" column position within the layer
            // (for debugging/visualization purposes, not needed for final solution)
            // This part is commented out as it's not required by the problem statement
            /*
            if (leftAligned) {
                // Left-aligned layer visualization (not needed for solution)
                System.out.print(heights[i] + " ");
            } else {
                // Right-aligned layer visualization with adjusted indexing (not needed for solution)
                System.out.print(heights[i] + " ".repeat(width - positionInLayer - 1));
                System.out.print(heights[i] + " | ");
            }
            if ((i + 1) % width == 0) System.out.println();
            */
        }

        return maxHeights;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int width = scanner.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; i++) {
            heights[i] = scanner.nextInt();
        }
        int[] result = getMaxHeights(n, width, heights);
        for (int height : result) {
            System.out.print(height + " ");
        }
    }
}

C++ 实现

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

std::vector<int> getMaxHeights(int n, int width, const std::vector<int>& heights) {
    int layers = std::ceil(static_cast<double>(n) / width);
    std::vector<int> maxHeights(layers, 0);

    for (int i = 0; i < n; ++i) {
        int layer = i / width;
        int positionInLayer = i % width;
        bool leftAligned = layer % 2 == 0;

        int columnIndex = leftAligned ? positionInLayer : width - 1 - positionInLayer;
        // Note: columnIndex is used here just for logical understanding,
        // but actually we directly update the max height of the current layer
        maxHeights[layer] = std::max(maxHeights[layer], heights[i]);
    }

    return maxHeights;
}

int main() {
    int n, width;
    std::cin >> n >> width;
    std::vector<int> heights(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> heights[i];
    }
    std::vector<int> result = getMaxHeights(n, width, heights);
    for (int height : result) {
        std::cout << height << " ";
    }
    return 0;
}

Python 实现

def get_max_heights(n, width, heights):
    layers = math.ceil(n / width)
    max_heights = [0] * layers

    for i in range(n):
        layer = i // width
        position_in_layer = i % width
        left_aligned = layer % 2 == 0

        column_index = position_in_layer if left_aligned else width - 1 - position_in_layer
        # Note: column_index is not used directly in final calculations,
        # but helps in understanding the logical arrangement
        max_heights[layer] = max(max_heights[layer], heights[i])

    return max_heights

import math
import sys
input = sys.stdin.read
data = input().split()

n = int(data[0])
width = int(data[1])
heights = list(map(int, data[2:n+2]))

result = get_max_heights(n, width, heights)
print(" ".join(map(str, result)))


http://www.ppmy.cn/embedded/160747.html

相关文章

Spring JDBC模块解析 -深入SqlParameterSource

在前面的博客中&#xff0c;我们探讨了Spring Data Access Module中的主要组件&#xff1a; JdbcTemplate和SimpleJdbcInsert。在这两部分的基础上&#xff0c;我们将继续探讨更详细 的用法&#xff0c;包括如何使用RowMapper和SqlParameterSource等高级主题。 JdbcTemplate …

Git、Github和Gitee完整讲解:丛基础到进阶功能

第一部分&#xff1a;Git 是什么&#xff1f; 比喻&#xff1a;Git就像是一本“时光机日记本” 每一段代码的改动&#xff0c;Git都会帮你记录下来&#xff0c;像是在写日记。如果出现问题或者想查看之前的版本&#xff0c;Git可以带你“穿越回过去”&#xff0c;找到任意时间…

指导初学者使用Anaconda运行GitHub上One - DM项目的步骤

以下是指导初学者使用Anaconda运行GitHub上One - DM项目的步骤&#xff1a; 1. 安装Anaconda 下载Anaconda&#xff1a; 让初学者访问Anaconda官网&#xff08;https://www.anaconda.com/products/distribution&#xff09;&#xff0c;根据其操作系统&#xff08;Windows、M…

Ubuntu 24.10 安装Deepseek(Ollama+openwebui)

一、Ollama安装 1.在线安装 curl -fsSL https://ollama.com/install.sh | sh 如果curl工具没有安装先执行如下命令 sudo apt install curl 验证curl是否安装成功 curl --version 安装的过程中会提示输入当前系统登录用户的密码。 安装提示success后,验证安装 ollama -…

redis之GEO 模块

文章目录 背景GeoHash 算法redis中的GeoHash 算法基本使用增加距离获取元素位置获取元素的 hash 值附近的元素 注意事项原理 背景 如果我们有需求需要存储地理坐标&#xff0c;为了满足高性能的矩形区域算法&#xff0c;数据表需要在经纬度坐标加上双向复合索引 (x, y)&#x…

ScrapeGraphAI颠覆传统网络爬虫技术

ScrapeGraphAI颠覆传统网络爬虫技术&#xff01; 引言 在互联网时代&#xff0c;数据如同油田&#xff0c;丰富而深邃。但如何有效地提取这些数据&#xff0c;仍然是许多开发者面临的艰巨任务。你有没有想过&#xff0c;传统的网络爬虫技术是否已经过时&#xff1f;如今&…

Dify Ollama本地私有化模型实践

今天给大家带来一篇deepseek本地部署&#xff0c;笔者最近由于研究AI大模型应用开发&#xff0c;笔记较少&#xff0c;后面将持续输出关于AI行业应用知识&#xff0c;请大家继续关注&#xff0c;话不多说&#xff0c;开始吧&#xff0c;啊哈哈。 DeepSeek 呢&#xff0c;最近十…

Debian安装Seafile

前言 Debian 无图形化界面通过 docker 安装 Seafile。我安装 Seafile12 没有成功&#xff0c;按照之前经验安装。 方法 安装docker 参考官方文档 Install Docker Engine on Debian。 设置 Docker’s apt repository. # Add Dockers official GPG key: sudo apt-get updat…