A12261 | Boxes Packing
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$ . Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$ -th object is $a_i$ .
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$ -th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$ ), he puts it in the box, and the remaining size of the box decreases by $a_i$ . Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$ -th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$ ), he puts it in the box, and the remaining size of the box decreases by $a_i$ . Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
输入格式
The first line of the input contains three integers $n$ , $m$ , $k$ ( $1 \le n, m \le 2 \cdot 10^5$ , $1 \le k \le 10^9$ ) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le k$ ), where $a_i$ is the size of the $i$ -th object.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le k$ ), where $a_i$ is the size of the $i$ -th object.
输出格式
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
输入输出样例
输入 #1
5 2 6 5 2 1 4 2
输出 #1
4
输入 #2
5 1 4 4 2 3 4 1
输出 #2
1
输入 #3
5 3 3 1 2 3 1 1
输出 #3
5
In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$ . Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$ . So the answer is $4$ .
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$ ), but he can pack the last object ( $[1]$ ).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$ .
In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$ ), but he can pack the last object ( $[1]$ ).
In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted