Down the Pyramid

Do you like number pyramids? Given a number sequence that represents the base, you are usually supposed to build the rest of the "pyramid" bottom-up: For each pair of adjacent numbers, you would compute their sum and write it down above them. For example, given the base sequence [1, 2, 3][1,2,3], the sequence directly above it would be [3, 5][3,5], and the top of the pyramid would be [8][8]:

Down the Pyramid

However, I am not interested in completing the pyramid – instead, I would much rather go underground. Thus, for a sequence of nn non-negative integers, I will write down a sequence of n + 1n+1 non-negative integers below it such that each number in the original sequence is the sum of the two numbers I put below it. However, there may be several possible sequences or perhaps even none at all satisfying this condition. So, could you please tell me how many sequences there are for me to choose from?

Input Format

The input consists of:

  • one line with the integer nn (1 \le n \le 10^6)(1≤n≤106), the length of the base sequence.
  • one line with n integers a_1, \cdots , a_na1​,⋯,an​ (0 \le ai \le 10^8(0≤ai≤108 for each i)i), forming the base sequence.

Output Format

Output a single integer, the number of non-negative integer sequences that would have the input sequence as the next level in a number pyramid.

样例输入1

6
12 5 7 7 8 4

样例输出1

2

样例输入2

3
10 1000 100

样例输出2

0

 

#include <iostream>
using namespace std;
const int inf=1e6+10;
int arr[inf];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&arr[i]);
        int minl=1e8+10;
        int minr=1e8+10;   
        int temp=0;
        minl=temp;
        for(int i=0;i<n;i++)                     
        {
            if(i%2==0)
            {
                temp=arr[i]-temp;
                if(temp<minr)
                    minr=temp;    
            }
            else
            {
                temp=arr[i]-temp;
                if(temp<minl)
                    minl=temp;
            }
        }
        if(minr>0)               
        {
            int flag=minr+1;
            if( flag+minl>0 )         //零也是一个特殊的,使用零可以,使用其他的应该也可以。 
                printf("%d\n",flag+minl);
            else
                printf("0\n");
        } 
        else if(minl==0&&minr==0) 
        {
            printf("1\n");
        }
        else
        {
            printf("0\n");
        }
    }
    return 0;
}