zoj Candies

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 number ai represent the exact quantity of candies the ith student has, it will be a nonnegative number not more than 10000, if ai 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 of ai-1ai 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.

这题和codejam的2008 wordfincal c是同一类型的题目,不过那题要复杂的多,而这道题,只能说我比赛的时候想复杂了。其实深入找规律的话,可以发现除了n%3 == 2 的情况,整个数列的值是确定的,而当n%3==2且有一个位置i( ( i+1)%3)!= 0 )的数是已知的,这个数列也是确定的,其他情况下数列不确定。这个情况下,只要知道一个a[0],数列也就确定了,看到n的范围较大,于是想二分a[0],但找了一下发现没有单调性,做到这里就卡住了。其实,因为大部分数据是随机生成的,且很难构造出每次检验都需要把整个数列都扫一编的数据,所以不妨直接暴力a[0]一试。注意这里如果一开始就直接暴力是会超时的,但是如果我们预处理出一些可以通过简单推导得出的数,就可以大大降低复杂度。哎,以后比赛做题,不要太担心O得出的复杂度,有时大胆一试,也许数据水了,就过了,谨记谨记!

#include<cstdio>
#include<iostream>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 100000 + 5;
const int INF = 10000 + 5;

int a[maxn],sum[maxn];
int aa[maxn];
int n;

bool test(int j){
    aa[0] = j;aa[1] = sum[0]-aa[0];aa[2] = sum[1]-sum[0];
    for(int i = 3;i < n;i++){
        aa[i] = sum[i-1]-sum[i-2]+aa[i-3];
        if(aa[i] < 0 || (a[i] != -1 && aa[i] != a[i])) return false;
    }
    if(aa[n-1]+aa[n-2] != sum[n-1]) return false;
    return true;
}

int main(){
    while(scanf("%d",&n) != EOF){
        memset(a,-1,sizeof(a));
        for(int i = 0;i < n;i++) scanf("%d",&a[i]);
        for(int i = 0;i < n;i++) scanf("%d",&sum[i]);
        int tem = sum[0];
        for(int i = 2;i < n;i+=3){
            a[i] = sum[i-1]-tem;
            tem = sum[i+1]-a[i];
        }
        tem = sum[n-1];
        for(int i = n-3;i >= 0;i-=3){
            a[i] = sum[i+1]-tem;
            tem = sum[i-1]-a[i];
        }
        int q;
        scanf("%d",&q);
        while(q--){
            int x;scanf("%d",&x);
            if(a[x] != -1){
                printf("%d\n",a[x]);
            }
            else{
                int ans = 0;
                for(int j = 0;j <= min(INF,sum[0]);j++){
                    if(test(j)) ans = max(ans,aa[x]);
                }
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值