codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1933E.md (4012B)


      1 Source: https://codeforces.com/problemset/problem/1933/E
      2 
      3 E. Turtle vs. Rabbit Race: Optimal Trainings
      4 
      5 time limit per test
      6 
      7 5 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Isaac begins his training. There are $n$ running tracks available, and the $i$-th track ($1 \le i \le n$) consists of $a_i$ equal-length sections.
     22 
     23 Given an integer $u$ ($1 \le u \le 10^9$), finishing each section can increase Isaac's ability by a certain value, described as follows:
     24 
     25 * Finishing the $1$-st section increases Isaac's performance by $u$.
     26 * Finishing the $2$-nd section increases Isaac's performance by $u-1$.
     27 * Finishing the $3$-rd section increases Isaac's performance by $u-2$.
     28 * $\ldots$
     29 * Finishing the $k$-th section ($k \ge 1$) increases Isaac's performance by $u+1-k$. (The value $u+1-k$ can be negative, which means finishing an extra section decreases Isaac's performance.)
     30 
     31 You are also given an integer $l$. You must choose an integer $r$ such that $l \le r \le n$ and Isaac will finish each section of each track $l, l + 1, \dots, r$ (that is, a total of $\sum_{i=l}^r a_i = a_l + a_{l+1} + \ldots + a_r$ sections).
     32 
     33 Answer the following question: what is the optimal $r$ you can choose that the increase in Isaac's performance is maximum possible?
     34 
     35 If there are multiple $r$ that maximize the increase in Isaac's performance, output the smallest $r$.
     36 
     37 To increase the difficulty, you need to answer the question for $q$ different values of $l$ and $u$.
     38 
     39 Input
     40 
     41 The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
     42 
     43 The descriptions of the test cases follow.
     44 
     45 The first line contains a single integer $n$ ($1 \le n \le 10^5$).
     46 
     47 The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^4$).
     48 
     49 The third line contains a single integer $q$ ($1 \le q \le 10^5$).
     50 
     51 The next $q$ lines each contain two integers $l$ and $u$ ($1 \le l \le n, 1 \le u \le 10^9$) — the descriptions to each query.
     52 
     53 The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. The sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.
     54 
     55 Output
     56 
     57 For each test case, output $q$ integers: the $i$-th integer contains the optimal $r$ for the $i$-th query. If there are multiple solutions, output the smallest one.
     58 
     59 Example
     60 
     61 Input
     62 
     63 ```
     64 5
     65 
     66 6
     67 
     68 3 1 4 1 5 9
     69 
     70 3
     71 
     72 1 8
     73 
     74 2 7
     75 
     76 5 9
     77 
     78 1
     79 
     80 10
     81 
     82 1
     83 
     84 1 1
     85 
     86 9
     87 
     88 5 10 9 6 8 3 10 7 3
     89 
     90 5
     91 
     92 8 56
     93 
     94 1 12
     95 
     96 9 3
     97 
     98 1 27
     99 
    100 5 45
    101 
    102 5
    103 
    104 7 9 2 5 2
    105 
    106 10
    107 
    108 1 37
    109 
    110 2 9
    111 
    112 3 33
    113 
    114 4 32
    115 
    116 4 15
    117 
    118 2 2
    119 
    120 4 2
    121 
    122 2 19
    123 
    124 3 7
    125 
    126 2 7
    127 
    128 10
    129 
    130 9 1 6 7 6 3 10 7 3 10
    131 
    132 5
    133 
    134 10 43
    135 
    136 3 23
    137 
    138 9 3
    139 
    140 6 8
    141 
    142 5 14
    143 ```
    144 
    145 Output
    146 
    147 ```
    148 3 4 5 
    149 1 
    150 9 2 9 4 9 
    151 5 2 5 5 5 2 4 5 4 2 
    152 10 6 9 7 7
    153 ```
    154 
    155 Note
    156 
    157 For the $1$-st query in the first test case:
    158 
    159 * By choosing $r = 3$, Isaac finishes $a_1 + a_2 + a_3 = 3 + 1 + 4 = 8$ sections in total, hence his increase in performance is $u+(u-1)+\ldots+(u-7)=8+7+6+5+4+3+2+1 = 36$.
    160 * By choosing $r = 4$, Isaac finishes $a_1 + a_2 + a_3 + a_4 = 3 + 1 + 4 + 1 = 9$ sections in total, hence his increase in performance is $u+(u-1)+\ldots+(u-8)=8+7+6+5+4+3+2+1+0 = 36$.
    161 
    162 Both choices yield the optimal increase in performance, however we want to choose the smallest $r$. So we choose $r = 3$.
    163 
    164 For the $2$-nd query in the first test case, by choosing $r = 4$, Isaac finishes $a_2 + a_3 + a_4 = 1 + 4 + 1 = 6$ sections in total, hence his increase in performance is $u+(u-1)+\ldots+(u-5)=7+6+5+4+3+2 = 27$. This is the optimal increase in performance.
    165 
    166 For the $3$-rd query in the first test case:
    167 
    168 * By choosing $r = 5$, Isaac finishes $a_5 = 5$ sections in total, hence his increase in performance is $u+(u-1)+\ldots+(u-4)=9+8+7+6+5 = 35$.
    169 * By choosing $r = 6$, Isaac finishes $a_5 + a_6 = 5 + 9 = 14$ sections in total, hence his increase in performance is $u+(u-1)+\ldots+(u-13)=9+8+7+6+5+4+3+2+1+0+(-1)+(-2)+(-3)+(-4) = 35$.
    170 
    171 Both choices yield the optimal increase in performance, however we want to choose the smallest $r$. So we choose $r = 5$.
    172 
    173 Hence the output for the first test case is $[3, 4, 5]$.