现在的位置: 首页 > 综合 > 正文

acm亚洲区网赛(长沙赛区) Candies (数值约束不等式)

2015年12月26日 ⁄ 综合 ⁄ 共 2144字 ⁄ 字号 评论关闭


Candies


Time Limit: 1 Second     
Memory Limit:
32768 KB


As we know, the majority of students in the world-class university like candy and game so much. With some candies, the students are playing a guessing game with you.

These students are standing in a line. Every student has some candies in the hand. (Of course the quantity of the candies will not be negative.) Some kindhearted students will tell you the exactly quantity of candies they have in hand, but some of them won't
do that. You may think that THE GAME CAN'T PLAY, but the fact is, every student in line will tell you the exact total quantities of previous one (if exists), himself, and next one (if exists).

You should guess the maximum possible quantity the queried student might have.

Input

The input will consist of multiple testcases.
The first line of each test case is an integer n, indicating the amount of students, 3 ≤n ≤100000.
The second line contains n integers, the ith numberai represent the exact quantity of candies the
ith student has, it will be a nonnegative number not more than 10000, ifai equals to -1, it means that the corresponding student haven't tell you the candies' quantity.
The third line also contains n integers, the ith number represents the sum ofai-1,
ai and ai+1 (the first and last student's number contain only two guys' summation).
The forth line contains an integer m, indicating the amount of queries, 1 ≤m ≤100.Following
m integers in a line indicate the 0-base number we queried.

Output

For each test case, you should output exactly m lines, each line contains an integer which is the answer of the corresponding query. If the queried quantity had been told in the input, just output that number.

Sample Input

5
-1 -1 -1 -1 -1 
2 3 3 3 2
2
0 3

Sample Output

2
2

Hint

The quantities they have might be "2 0 1 2 0", "0 2 1 0 2", "1 1 1 1 1" and so on.

题目意思是,队伍里的小朋友们,每个人都知道他自己和旁边两个人的蜡烛总和。求每个小朋友手上的蜡烛的可能的最大值。

通过找规律,可以发现,第2个,第5个,第8个....小朋友的蜡烛值是一定可以确定的(无论输入给定了几个小朋友的确切值)。

同理,第n-3(倒数第三个), n-6, n-9....也一定是确定的。

因此我们作如下断言:

当n % 3 == 1 或 n % 3 == 0 ,则所有位置一定可以确定。

当n % 3 == 2 且 有任意i % 3 != 2 的位置元素确定,则所有位置 也一定可以确定。

当n % 3 == 2 且 没有 i%3!=2的位置元素确定,则要利用解不等式的思想。(这是我们重点要考虑的情况)

因此我们想到如下方法:

假设第一个元素为0,即 c[0] = 0,所有i % 2 != 2 的位置都会确定出一个相对的值c[i]。且i % 3 = 0 的位置 于c[0]正相关,即c[0] 加一,则c[i]加一。 而i % 3 = 1 的位置与c[0]成负相关,即c[0] 加一,则c[i] 减一。

知道了这个前提后,我们现在需要保证每一个位置上的元素都 >= 0,因此会有不等式:

假设第一个元素为a(非负数), 则:

当i % 3 == 0 时, c[i] + a >= 0     =>   a >= -c[i]

当i % 3 == 1 时, -a + c[i] >= 0   => a <= c[i]

因此初始设置为 mina = 0, maxa= oo; 然后i 从0 -> n-1 进行遍历,依次按照上面两个不等式,更新mina和maxa。

最后求出来mina 和 maxa,我们就可以利用 mina 和maxa 来求出各个位置的 最大值。

代码:

抱歉!评论已关闭.