CF1933E.Turtle vs. Rabbit Race: Optimal Trainings
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Isaac begins his training. There are n running tracks available, and the i -th track ( 1≤i≤n ) consists of ai equal-length sections.
Given an integer u ( 1≤u≤109 ), finishing each section can increase Isaac's ability by a certain value, described as follows:
- Finishing the 1 -st section increases Isaac's performance by u .
- Finishing the 2 -nd section increases Isaac's performance by u−1 .
- Finishing the 3 -rd section increases Isaac's performance by u−2 .
- …
- Finishing the k -th section ( k≥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.)
You are also given an integer l . You must choose an integer r such that l≤r≤n and Isaac will finish each section of each track l,l+1,…,r (that is, a total of ∑i=lrai=al+al+1+…+ar sections).
Answer the following question: what is the optimal r you can choose that the increase in Isaac's performance is maximum possible?
If there are multiple r that maximize the increase in Isaac's performance, output the smallest r .
To increase the difficulty, you need to answer the question for q different values of l and u .
输入格式
The first line of input contains a single integer t ( 1≤t≤104 ) — the number of test cases.
The descriptions of the test cases follow.
The first line contains a single integer n ( 1≤n≤105 ).
The second line contains n integers a1,a2,…,an ( 1≤ai≤104 ).
The third line contains a single integer q ( 1≤q≤105 ).
The next q lines each contain two integers l and u ( 1≤l≤n,1≤u≤109 ) — the descriptions to each query.
The sum of n over all test cases does not exceed 2⋅105 . The sum of q over all test cases does not exceed 2⋅105 .
输出格式
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.
输入输出样例
输入#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 1 -st query in the first test case:
- By choosing r=3 , Isaac finishes a1+a2+a3=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=36 .
- By choosing r=4 , Isaac finishes a1+a2+a3+a4=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=36 .
Both choices yield the optimal increase in performance, however we want to choose the smallest r . So we choose r=3 .
For the 2 -nd query in the first test case, by choosing r=4 , Isaac finishes a2+a3+a4=1+4+1=6 sections in total, hence his increase in performance is u+(u−1)+…+(u−5)=7+6+5+4+3+2=27 . This is the optimal increase in performance.
For the 3 -rd query in the first test case:
- By choosing r=5 , Isaac finishes a5=5 sections in total, hence his increase in performance is u+(u−1)+…+(u−4)=9+8+7+6+5=35 .
- By choosing r=6 , Isaac finishes a5+a6=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)=35 .
Both choices yield the optimal increase in performance, however we want to choose the smallest r . So we choose r=5 .
Hence the output for the first test case is [3,4,5] .