CF1498B.Box Fitting

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn rectangles, each of height 11. Each rectangle's width is a power of 22 (i. e. it can be represented as 2x2^x for some non-negative integer xx).

You are also given a two-dimensional box of width WW. Note that WW may or may not be a power of 22. Moreover, WW is at least as large as the width of the largest rectangle.

You have to find the smallest height of this box, such that it is able to fit all the given rectangles. It is allowed to have some empty space left in this box after fitting all the rectangles.

You cannot rotate the given rectangles to make them fit into the box. Moreover, any two distinct rectangles must not overlap, i. e., any two distinct rectangles must have zero intersection area.

See notes for visual explanation of sample input.

给你 nn 个矩形,每个矩形的高度均为 11。每个矩形的宽度均为 22 的幂(即可以表示为 2x2^x,其中 xx 为非负整数)。

你还给定一个二维盒子,其宽度为 WW。注意:WW 可能不是 22 的幂;此外,WW 不小于所有矩形中最大宽度。

你需要求出该盒子的最小高度,使得所有给定的矩形都能放入其中。允许盒子在放入所有矩形后仍留有空余空间。

不允许旋转给定的矩形以使其适配盒子。此外,任意两个不同的矩形不得重叠,即任意两个不同矩形的交集面积必须为零。

有关样例输入的图示说明,请参见“注释”部分。

输入格式

The first line of input contains one integer tt (1≤t≤5⋅1031 \le t \le 5 \cdot 10^3) — the number of test cases. Each test case consists of two lines.

For each test case:

  • the first line contains two integers nn (1≤n≤1051 \le n \le 10^5) and WW (1≤W≤1091 \le W \le 10^9);
  • the second line contains nn integers w1,w2,…,wnw_1, w_2, \dots, w_n (1≤wi≤1061 \le w_i \le 10^6), where wiw_i is the width of the ii-th rectangle. Each wiw_i is a power of 22;
  • additionally, max⁡i=1nwi≤W\max\limits_{i=1}^{n} w_i \le W.

The sum of nn over all test cases does not exceed 10510^5.

输入的第一行包含一个整数 tt(1≤t≤5⋅1031 \le t \le 5 \cdot 10^3),表示测试用例的数量。每个测试用例由两行组成。

对于每个测试用例:

  • 第一行包含两个整数 nn(1≤n≤1051 \le n \le 10^5)和 WW(1≤W≤1091 \le W \le 10^9);
  • 第二行包含 nn 个整数 w1,w2,…,wnw_1, w_2, \dots, w_n(1≤wi≤1061 \le w_i \le 10^6),其中 wiw_i 表示第 ii 个矩形的宽度;每个 wiw_i 均为 22 的幂;
  • 此外,满足 max⁡i=1nwi≤W\max\limits_{i=1}^{n} w_i \le W。

所有测试用例中 nn 的总和不超过 10510^5。

输出格式

Output tt integers. The ii-th integer should be equal to the answer to the ii-th test case — the smallest height of the box.

输出 tt 个整数。第 ii 个整数应等于第 ii 个测试用例的答案——即盒子的最小高度。

输入输出样例

  • 输入#1

    2
    5 16
    1 2 8 4 8
    6 10
    2 8 8 2 2 8

    输出#1

    2
    3

说明/提示

For the first test case in the sample input, the following figure shows one way to fit the given five rectangles into the 2D box with minimum height:

In the figure above, the number inside each rectangle is its width. The width of the 2D box is 1616 (indicated with arrow below). The minimum height required for the 2D box in this case is 22 (indicated on the left).

In the second test case, you can have a minimum height of three by keeping two blocks (one each of widths eight and two) on each of the three levels.

对于样例输入中的第一个测试用例,下图展示了一种将给定的五个矩形放入二维箱子中的方式,使得箱子高度最小:

在上图中,每个矩形内部的数字表示其宽度。二维箱子的宽度为 1616(由下方箭头标出)。本例中,二维箱子所需的最小高度为 22(由左侧标出)。

在第二个测试用例中,可通过在每一层放置两个方块(每层各一个宽度为 88 和一个宽度为 22 的方块),实现最小高度 33。

输入解题思路,AI测评打分。不知道怎么写?

首页