CF1700D.River Locks

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Recently in Divanovo, a huge river locks system was built. There are now nn locks, the ii-th of them has the volume of viv_i liters, so that it can contain any amount of water between 00 and viv_i liters. Each lock has a pipe attached to it. When the pipe is open, 11 liter of water enters the lock every second.

The locks system is built in a way to immediately transfer all water exceeding the volume of the lock ii to the lock i+1i + 1. If the lock i+1i + 1 is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river.

The picture illustrates 55 locks with two open pipes at locks 11 and 33. Because locks 11, 33, and 44 are already filled, effectively the water goes to locks 22 and 55.

Note that the volume of the ii-th lock may be greater than the volume of the i+1i + 1-th lock.

To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in qq independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the jj-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after tjt_j seconds.

Please help the mayor to solve this tricky problem and answer his queries.

最近,迪瓦诺沃市建成了一套庞大的船闸系统。目前共有 nn 座船闸,其中第 ii 座船闸的容积为 viv_i 升,因此它可容纳 00 到 viv_i 升之间的任意水量。每座船闸均连接有一根进水管。当某根进水管开启时,每秒向对应船闸注入 11 升水。

该船闸系统的设计使得:一旦第 ii 座船闸中的水量超过其容积 viv_i,超出部分将立即流入第 i+1i+1 座船闸;若第 i+1i+1 座船闸也已满,则水继续向后传递;最终,超出最后一座船闸容积的水量将直接溢入河流。


图中展示了 55 座船闸,其中第 11 和第 33 座船闸的进水管处于开启状态。由于第 11、33 和 44 座船闸已满,水实际上流入了第 22 和第 55 座船闸。

注意:第 ii 座船闸的容积可能大于第 i+1i+1 座船闸的容积。

为使所有船闸正常运行,必须将每座船闸完全注满。迪瓦诺沃市长提出了 qq 个相互独立的询问。对每个询问,假设初始状态下所有船闸均为空且所有进水管均关闭,然后同时开启若干进水管。对于第 jj 个询问,市长希望你计算:为确保所有船闸在 tjt_j 秒内(含)全部注满,至少需要开启多少根进水管。

请帮助市长解决这一难题,并回答他的所有询问。

输入格式

The first lines contains one integer nn (1≤n≤200 0001 \le n \le 200\,000) — the number of locks.

The second lines contains nn integers v1,v2,…,vnv_1, v_2, \dots, v_n (1≤vi≤1091 \le v_i \le 10^9)) — volumes of the locks.

The third line contains one integer qq (1≤q≤200 0001 \le q \le 200\,000) — the number of queries.

Each of the next qq lines contains one integer tjt_j (1≤tj≤1091 \le t_j \le 10^9) — the number of seconds you have to fill all the locks in the query jj.

第一行包含一个整数 nn(1≤n≤200 0001 \le n \le 200\,000)—— 锁的数量。

第二行包含 nn 个整数 v1,v2,…,vnv_1, v_2, \dots, v_n(1≤vi≤1091 \le v_i \le 10^9)—— 各锁的容积。

第三行包含一个整数 qq(1≤q≤200 0001 \le q \le 200\,000)—— 查询的数量。

接下来的 qq 行中,每行包含一个整数 tjt_j(1≤tj≤1091 \le t_j \le 10^9)—— 第 jj 次查询中用于填满所有锁的时间(单位:秒)。

输出格式

Print qq integers. The jj-th of them should be equal to the minimum number of pipes to turn on so that after tjt_j seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print −1-1.

输出 qq 个整数。其中第 jj 个整数应等于:使得所有锁在 tjt_j 秒后均被注满所需的最少开启管道数量。若在给定时间内无法注满所有锁,则输出 −1-1。

输入输出样例

  • 输入#1

    5
    4 1 5 4 1
    6
    1
    6
    2
    3
    4
    5

    输出#1

    -1
    3
    -1
    -1
    4
    3
  • 输入#2

    5
    4 4 4 4 4
    6
    1
    3
    6
    5
    2
    4

    输出#2

    -1
    -1
    4
    4
    -1
    5

说明/提示

There are 66 queries in the first example test.

In the queries 1,3,41, 3, 4 the answer is −1-1. We need to wait 44 seconds to fill the first lock even if we open all the pipes.

In the sixth query we can open pipes in locks 11, 33, and 44. After 44 seconds the locks 11 and 44 are full. In the following 11 second 11 liter of water is transferred to the locks 22 and 55. The lock 33 is filled by its own pipe.

Similarly, in the second query one can open pipes in locks 11, 33, and 44.

In the fifth query one can open pipes 1,2,3,41, 2, 3, 4.

第一个样例测试中有 66 个查询。

在第 11、33、44 个查询中,答案为 −1-1。即使我们打开所有管道,也需要等待 44 秒才能将第一个闸室注满。

在第六个查询中,我们可以打开位于闸室 11、33 和 44 的管道。经过 44 秒后,闸室 11 和 44 被注满。接下来的 11 秒内,有 11 升水被输送到闸室 22 和 55。闸室 33 则由其自身的管道注满。

类似地,在第二个查询中,可以打开位于闸室 11、33 和 44 的管道。

在第五个查询中,可以打开位于闸室 11、22、33 和 44 的管道。

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

首页