#P8193. [USACO22FEB] Sleeping in Class P

    ID: 9267 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心USACO2022数论素数判断,质数,筛法前缀和

[USACO22FEB] Sleeping in Class P

Problem Description

Offline classes have finally resumed, and the cow Bessie is very excited. Unfortunately, Farmer John is a very boring lecturer, so she often falls asleep in class.

Farmer John noticed that Bessie was distracted in class. He asked another student in the class, Elsie, to track and record how many times Bessie fell asleep in a given class. There are a total of NN classes, and Elsie recorded that in the ii-th class Bessie fell asleep aia_i times. The total number of times Bessie fell asleep across all classes is at most 101810^{18}.

Elsie considers herself Bessie’s rival, so she wants FJ to believe that in every class Bessie slept the same number of times—making FJ think this is obviously entirely Bessie’s fault, rather than the fact that FJ is sometimes boring.

Elsie can modify the record in only the following two ways: merge the records of two adjacent classes, or split the record of one class into two classes. For example, if a=[1,2,3,4,5]a=[1,2,3,4,5], then if Elsie merges the records of the second and third classes, the record becomes [1,5,4,5][1,5,4,5]. If Elsie then continues by splitting the record of the third class into two classes, the record could become [1,5,0,4,5][1,5,0,4,5], [1,5,1,3,5][1,5,1,3,5], [1,5,2,2,5][1,5,2,2,5], [1,5,3,1,5][1,5,3,1,5], or [1,5,4,0,5][1,5,4,0,5].

Given QQ candidate numbers q1,,qQq_1,\ldots,q_Q that Bessie dislikes the most, for each number, please help Elsie compute the minimum number of operations she needs so that all numbers in the record become this number.

Input Format

The first line contains an integer NN (1N1051\le N\le 10^5).

The second line contains NN integers a1,a2,,aNa_1,a_2,\ldots, a_N (1ai10181\le a_i\le 10^{18}).

The third line contains an integer QQ (1Q1051\le Q\le 10^5).

The next QQ lines each contain an integer qiq_i (1qi10181\le q_i\le 10^{18}), representing the number Bessie dislikes the most.

Output Format

For each qiq_i, output the minimum number of operations Elsie needs to make every number in the record equal to qiq_i. If it is impossible to make all numbers equal to qiq_i, output 1-1.

6
1 2 3 1 1 4
7
1
2
3
4
5
6
12
6
6
4
5
-1
4
5

Hint

Sample Explanation

Elsie needs at least 44 modifications to make all numbers in the record equal to 33.

$$\begin{aligned} &\ 1\ 2\ 3\ 1\ 1\ 4\\ \rightarrow&\ 3\ 3\ 1\ 1\ 4\\ \rightarrow&\ 3\ 3\ 1\ 5\\ \rightarrow&\ 3\ 3\ 6\\ \rightarrow&\ 3\ 3\ 3\ 3\\ \end{aligned}$$

Elsie cannot make all numbers in the record equal to 55, so the output is 1-1. This is correct.

Constraints

  • For testdata groups 242\sim 4, N,Q5000N,Q\le 5000.
  • For testdata groups 575\sim 7, all aia_i are at most 10910^9.
  • For testdata groups 8268\sim 26, there are no additional constraints.

Translated by ChatGPT 5