ZOJ Candies 观察分析(2013 ACM/ICPC Asia Regional Changsha Online J)
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 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-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.
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <vector> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 using namespace std; 11 #define maxn 110015 12 #define mod 1000000007 13 #define INF 0x7fffffff 14 //#define ll long long 15 #define ll __int64 16 int a[maxn]; 17 int s[maxn]; 18 int x[maxn]; 19 int d[maxn]; 20 int n,m,l; 21 int main(){ 22 int t; 23 while(scanf("%d",&n)!=EOF){ 24 a[0]=a[n+1]=0; 25 for(int i=1;i<=n;i++)scanf("%d",&a[i]); 26 for(int i=1;i<=n;i++)scanf("%d",&s[i]); 27 scanf("%d",&t); 28 int k=n,c; 29 for(int i=3;i<=n;i+=3)a[i]=s[i-1]-s[i-2]+a[i-3]; 30 for(int i=n-2;i>=1;i-=3)a[i]=s[i+1]-s[i+2]+a[i+3]; 31 if(a[1]!=-1)a[2]=s[1]-a[1];if(a[2]!=-1)a[1]=s[1]-a[2]; 32 if(a[n]!=-1)a[n-1]=s[n]-a[n];if(a[n-1]!=-1)a[n]=s[n]-a[n-1]; 33 for(int i=1;i<n;i++)if(a[i]!=-1&&a[i+1]!=-1){k=i;break;} 34 if(k<n){ 35 for(int j=k+2;j<=n;j++)if(a[j]==-1)a[j]=s[j-1]-a[j-1]-a[j-2]; 36 for(int j=k-1;j>=1;j--)if(a[j]==-1)a[j]=s[j+1]-a[j+1]-a[j+2]; 37 } 38 if(k>=n){ 39 int mixx=0; 40 memcpy(d,a,sizeof(a)); 41 d[1]=s[1];x[1]=s[1]; 42 for(int i=2;i<=n;i++){ 43 if(i%3==0)continue; 44 d[i]=s[i-1]-d[i-2]-d[i-1]; 45 if(i%3==1)x[i]=d[i]; 46 else mixx=min(mixx,d[i]); 47 } 48 if(mixx<0)for(int i=1;i<=n;i+=3)x[i]+=mixx; 49 memcpy(d,a,sizeof(a)); 50 mixx=0;d[1]=0;x[2]=s[1];d[2]=s[1]; 51 for(int i=4;i<=n;i++){ 52 if(i%3==0)continue; 53 d[i]=s[i-1]-d[i-2]-d[i-1]; 54 if(i%3==2)x[i]=d[i]; 55 else mixx=min(mixx,d[i]); 56 } 57 if(mixx<0)for(int i=2;i<=n;i+=3)x[i]+=mixx; 58 } 59 for(int i=1;i<=t;i++){ 60 scanf("%d",&c);c++; 61 if(k<n)printf("%d\n",a[c]); 62 else if(c%3==0)printf("%d\n",a[c]); 63 else printf("%d\n",x[c]); 64 } 65 } 66 return 0; 67 }
浙公网安备 33010602011771号