CF1933E.Turtle vs. Rabbit Race: Optimal Trainings

普及/提高-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Isaac begins his training. There are nn running tracks available, and the ii-th track (1≤i≤n1 \le i \le n) consists of aia_i equal-length sections.

Given an integer uu (1≤u≤1091 \le u \le 10^9), finishing each section can increase Isaac's ability by a certain value, described as follows:

  • Finishing the 11-st section increases Isaac's performance by uu.
  • Finishing the 22-nd section increases Isaac's performance by u−1u-1.
  • Finishing the 33-rd section increases Isaac's performance by u−2u-2.
  • …\ldots
  • Finishing the kk-th section (k≥1k \ge 1) increases Isaac's performance by u+1−ku+1-k. (The value u+1−ku+1-k can be negative, which means finishing an extra section decreases Isaac's performance.)

You are also given an integer ll. You must choose an integer rr such that l≤r≤nl \le r \le n and Isaac will finish each section of each track l,l+1,…,rl, l + 1, \dots, r (that is, a total of ∑i=lrai=al+al+1+…+ar\sum_{i=l}^r a_i = a_l + a_{l+1} + \ldots + a_r sections).

Answer the following question: what is the optimal rr you can choose that the increase in Isaac's performance is maximum possible?

If there are multiple rr that maximize the increase in Isaac's performance, output the smallest rr.

To increase the difficulty, you need to answer the question for qq different values of ll and uu.

艾萨克开始他的训练。目前有 nn 条跑道可供使用,其中第 ii 条跑道(1≤i≤n1 \le i \le n)由 aia_i 个等长的路段组成。

给定一个整数 uu(1≤u≤1091 \le u \le 10^9),完成每个路段将使艾萨克的能力提升某一特定数值,具体规则如下:

  • 完成第 11 个路段,使艾萨克的表现提升 uu;
  • 完成第 22 个路段,使艾萨克的表现提升 u−1u-1;
  • 完成第 33 个路段,使艾萨克的表现提升 u−2u-2;
  • …\ldots
  • 完成第 kk 个路段(k≥1k \ge 1),使艾萨克的表现提升 u+1−ku+1-k。(注意:u+1−ku+1-k 可能为负数,即额外完成一个路段反而会降低艾萨克的表现。)

同时给定一个整数 ll。你必须选择一个整数 rr,满足 l≤r≤nl \le r \le n,使得艾萨克完成从第 ll 条到第 rr 条(含)所有跑道上的每一个路段(即总共完成 ∑i=lrai=al+al+1+…+ar\sum_{i=l}^r a_i = a_l + a_{l+1} + \ldots + a_r 个路段)。

请回答以下问题:应选择哪个 rr,才能使艾萨克的表现提升值达到最大?

若存在多个 rr 均可使表现提升值达到最大,请输出其中最小的 rr。

为增加难度,你需要对 qq 组不同的 (l,u)(l, u) 参数分别回答该问题。

输入格式

The first line of input contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The descriptions of the test cases follow.

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1041 \le a_i \le 10^4).

The third line contains a single integer qq (1≤q≤1051 \le q \le 10^5).

The next qq lines each contain two integers ll and uu (1≤l≤n,1≤u≤1091 \le l \le n, 1 \le u \le 10^9) — the descriptions to each query.

The sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5. The sum of qq over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

随后是各测试用例的描述。

第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1041 \le a_i \le 10^4)。

第三行包含一个整数 qq(1≤q≤1051 \le q \le 10^5)。

接下来的 qq 行,每行包含两个整数 ll 和 uu(1≤l≤n, 1≤u≤1091 \le l \le n,\ 1 \le u \le 10^9)—— 表示每个查询的参数。

所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5;所有测试用例中 qq 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output qq integers: the ii-th integer contains the optimal rr for the ii-th query. If there are multiple solutions, output the smallest one.

对于每个测试用例,输出 qq 个整数:第 ii 个整数为第 ii 个查询的最优 rr。若存在多个解,则输出最小的那个。

输入输出样例

  • 输入#1

    5
    6
    3 1 4 1 5 9
    3
    1 8
    2 7
    5 9
    1
    10
    1
    1 1
    9
    5 10 9 6 8 3 10 7 3
    5
    8 56
    1 12
    9 3
    1 27
    5 45
    5
    7 9 2 5 2
    10
    1 37
    2 9
    3 33
    4 32
    4 15
    2 2
    4 2
    2 19
    3 7
    2 7
    10
    9 1 6 7 6 3 10 7 3 10
    5
    10 43
    3 23
    9 3
    6 8
    5 14

    输出#1

    3 4 5 
    1 
    9 2 9 4 9 
    5 2 5 5 5 2 4 5 4 2 
    10 6 9 7 7

说明/提示

For the 11-st query in the first test case:

  • By choosing r=3r = 3, Isaac finishes a1+a2+a3=3+1+4=8a_1 + a_2 + a_3 = 3 + 1 + 4 = 8 sections in total, hence his increase in performance is u+(u−1)+…+(u−7)=8+7+6+5+4+3+2+1=36u+(u-1)+\ldots+(u-7)=8+7+6+5+4+3+2+1 = 36.
  • By choosing r=4r = 4, Isaac finishes a1+a2+a3+a4=3+1+4+1=9a_1 + a_2 + a_3 + a_4 = 3 + 1 + 4 + 1 = 9 sections in total, hence his increase in performance is u+(u−1)+…+(u−8)=8+7+6+5+4+3+2+1+0=36u+(u-1)+\ldots+(u-8)=8+7+6+5+4+3+2+1+0 = 36.

Both choices yield the optimal increase in performance, however we want to choose the smallest rr. So we choose r=3r = 3.

For the 22-nd query in the first test case, by choosing r=4r = 4, Isaac finishes a2+a3+a4=1+4+1=6a_2 + a_3 + a_4 = 1 + 4 + 1 = 6 sections in total, hence his increase in performance is u+(u−1)+…+(u−5)=7+6+5+4+3+2=27u+(u-1)+\ldots+(u-5)=7+6+5+4+3+2 = 27. This is the optimal increase in performance.

For the 33-rd query in the first test case:

  • By choosing r=5r = 5, Isaac finishes a5=5a_5 = 5 sections in total, hence his increase in performance is u+(u−1)+…+(u−4)=9+8+7+6+5=35u+(u-1)+\ldots+(u-4)=9+8+7+6+5 = 35.
  • By choosing r=6r = 6, Isaac finishes a5+a6=5+9=14a_5 + a_6 = 5 + 9 = 14 sections in total, hence his increase in performance is u+(u−1)+…+(u−13)=9+8+7+6+5+4+3+2+1+0+(−1)+(−2)+(−3)+(−4)=35u+(u-1)+\ldots+(u-13)=9+8+7+6+5+4+3+2+1+0+(-1)+(-2)+(-3)+(-4) = 35.

Both choices yield the optimal increase in performance, however we want to choose the smallest rr. So we choose r=5r = 5.

Hence the output for the first test case is [3,4,5][3, 4, 5].

对于第一个测试用例的第 11 次查询:

  • 若选择 r=3r = 3,Isaac 总共完成 a1+a2+a3=3+1+4=8a_1 + a_2 + a_3 = 3 + 1 + 4 = 8 个章节,因此其性能提升为 u+(u−1)+…+(u−7)=8+7+6+5+4+3+2+1=36u+(u-1)+\ldots+(u-7)=8+7+6+5+4+3+2+1 = 36。
  • 若选择 r=4r = 4,Isaac 总共完成 a1+a2+a3+a4=3+1+4+1=9a_1 + a_2 + a_3 + a_4 = 3 + 1 + 4 + 1 = 9 个章节,因此其性能提升为 u+(u−1)+…+(u−8)=8+7+6+5+4+3+2+1+0=36u+(u-1)+\ldots+(u-8)=8+7+6+5+4+3+2+1+0 = 36。

两种选择均达到最优性能提升,但我们需要选择最小的 rr,因此选择 r=3r = 3。

对于第一个测试用例的第 22 次查询,若选择 r=4r = 4,Isaac 总共完成 a2+a3+a4=1+4+1=6a_2 + a_3 + a_4 = 1 + 4 + 1 = 6 个章节,因此其性能提升为 u+(u−1)+…+(u−5)=7+6+5+4+3+2=27u+(u-1)+\ldots+(u-5)=7+6+5+4+3+2 = 27。此即最优性能提升。

对于第一个测试用例的第 33 次查询:

  • 若选择 r=5r = 5,Isaac 总共完成 a5=5a_5 = 5 个章节,因此其性能提升为 u+(u−1)+…+(u−4)=9+8+7+6+5=35u+(u-1)+\ldots+(u-4)=9+8+7+6+5 = 35。
  • 若选择 r=6r = 6,Isaac 总共完成 a5+a6=5+9=14a_5 + a_6 = 5 + 9 = 14 个章节,因此其性能提升为 u+(u−1)+…+(u−13)=9+8+7+6+5+4+3+2+1+0+(−1)+(−2)+(−3)+(−4)=35u+(u-1)+\ldots+(u-13)=9+8+7+6+5+4+3+2+1+0+(-1)+(-2)+(-3)+(-4) = 35。

两种选择均达到最优性能提升,但我们需要选择最小的 rr,因此选择 r=5r = 5。

因此,第一个测试用例的输出为 [3,4,5][3, 4, 5]。

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

首页