Candies
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 theith 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 andai+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. Followingm 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.
我觉得这题其实是水题的(勿急喷,听我解释)。只需用最简单的设一个未知数x就可以解决的(这个初一就学过了)。方法是这样的:设第一个小孩有a0=x个糖,则第二小孩就有a1=s0-x(s数列是小孩说的周围人的糖数总和),第三个小孩就有a2=s1-a1-a0=s1-s0,a3=s2-a2-a1.......就这么一直推下去(不是让你用手推,是编程让计算机推),ai一定是一个常数项加上一个一次项。如果这题数列是不确定,那么是不能解出x的,但我们可以根据ai>=0来解除x的范围;若数列是确定的,那么在小孩子说自己的糖数的或者是在sn-1=an-2+an-1式子上,我们能求得x的具体值,故数列确定了。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define N 100010
using namespace std;
int num[N];
int sum[N];
int stan[N];
struct Node{
int a,b;
}node[N];
int min(int a,int b){
if(a<b)
return a;
return b;
}
int max(int a,int b){
if(a>b)
return a;
return b;
}
int main(){
int n;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
int i,j,k,q,smax,smin;
memset(node,0,sizeof(node));
for(i=0;i<n;i++)
scanf("%d",&stan[i]);
for(i=0;i<n;i++)
scanf("%d",&sum[i]);
int x=-1;
smax=sum[0];
smin=0;
node[0].a=0,node[0].b=1;
if(stan[0]!=-1){
node[0].a=stan[0];
node[0].b=0;
x=0;
node[1].a=sum[0]-node[0].a;
node[1].b=0;
}
else{
node[1].a=sum[0]-node[0].a;
node[1].b=node[1].b-node[0].b;
if(node[1].b==-1&&node[1].a<smax)
smax=node[i].a;
if(stan[1]!=-1){
node[1].a=stan[1];
node[1].b=0;
node[0].a=sum[0]-stan[1];
node[0].b=0;
x=0;
}
}
for(i=2;i<n;i++){
node[i].a=sum[i-1]-node[i-2].a-node[i-1].a;
node[i].b=node[i].b-node[i-2].b-node[i-1].b;
if(stan[i]!=-1&&node[i].b!=0&&x==-1){
x=(stan[i]-node[i].a)*(node[i].b);
node[i].a=stan[i];
node[i].b=0;
node[i-1].a=node[i-1].a+(node[i-1].b*x);
node[i-1].b=0;
}
if(x==-1){
if(node[i].b==-1&&node[i].a<smax) //heart broken ,1&i mistake
smax=node[i].a;
if(node[i].b==1&&node[i].a+smin<0)
smin=(0-node[i].a);
}
}
if(x==-1&&(node[n-2].b+node[n-1].b)!=0){
x=(sum[n-1]-node[n-1].a-node[n-2].a)*(node[n-2].b+node[n-1].b);
}
scanf("%d",&q);
if(x!=-1)
for(i=0;i<q;i++){
int tm;
scanf("%d",&tm);
printf("%d\n",node[tm].a+node[tm].b*x);
}
else {
for(i=0;i<q;i++){
int tm,ma;
scanf("%d",&tm);
if(node[tm].a+node[tm].b*smin>node[tm].a+node[tm].b*smax)
ma=node[tm].a+node[tm].b*smin;
else
ma=node[tm].a+node[tm].b*smax;
printf("%d\n",ma);
}
}
// printf("\n");
// cout<<smin<<' '<<smax;
}
return 0;
}