Codeforces Problem 712A Memory and Crow(公式推导)

来源:互联网 发布:国外对网络直播的研究 编辑:程序博客网 时间:2024/05/27 20:15

此文章可以使用目录功能哟↑(点击上方[+])

比赛链接→Codeforces Round #370 (Div. 2)

 Codeforces Problem 712A Memory and Crow

Accept: 0    Submit: 0
Time Limit: 2 seconds    Memory Limit : 256 megabytes

 Problem Description

There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure:

  • The crow sets ai initially 0.
  • The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus, ai = bi - bi + 1 + bi + 2 - bi + 3....

Memory gives you the values a1, a2, ..., an, and he now wants you to find the initial numbers b1, b2, ..., bn written in the row? Can you do it?

 Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row.

The next line contains n, the i'th of which is ai ( - 10^9 ≤ ai ≤ 10^9) — the value of the i'th number.

 Output

Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.

 Sample Input

5
6 -4 8 -2 3
5
3 -2 -1 5 6

 Sample Output

2 4 6 1 3
1 -3 4 11 6

 Hint

In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and  - 4 = 4 - 6 + 1 - 3.

In the second sample test, the sequence 1,  - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6.

 Problem Idea

解题思路:

【题意】
有n个数b1, b2, ..., bn

a1, a2, ..., an是通过等式ai = bi - bi + 1 + bi + 2 - bi + 3....(±)bn得到的

现给你a1, a2, ..., an这n个数,问b1, b2, ..., bn是多少


【类型】
公式推导

【分析】

由此可见,数组b中的第i项等于数组a中的第i项与第i+1项之和

特别地,数组b中的第n项等于数组a中的第n项


【时间复杂度&&优化】
O(n)

题目链接→Codeforces Problem 712A Memory and Crow

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<bitset>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define eps 1e-9#define LL long long#define PI acos(-1.0)#define bitnum(a) __builtin_popcount(a)using namespace std;const int N = 100005;const int M = 20005;const int inf = 1000000007;const int mod = 7;int main(){    int n,i,a,b;    scanf("%d",&n);    for(i=1;i<=n;i++)    {        scanf("%d",&a);        if(i>1)            printf("%d ",a+b);        b=a;    }    printf("%d\n",a);    return 0;}
菜鸟成长记

0 0
原创粉丝点击