CodeForces 581B Luxurious Houses(简单题)——Codeforces Beta Round #322 (Div. 2)

B. Luxurious Houses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.

Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.

The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.

Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).

Input

The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland.

The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house.

Output

Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.

All houses are numbered from left to right, starting from one.

Sample test(s)
input
5
1 2 3 1 2
output
3 2 0 2 0 
input
4
3 2 1 4
output
2 3 4 0 

/*********************************************************************/

题意:n座建筑从左到右排列,给你每座建筑的层数,n次询问,每次需给出对于第i座建筑,我们需要增加多少层,才能使得该建筑右边的所有建筑都比它矮

解题思路:其实该题还是比较简单的,要求需要增加多少层才能比右边所有的建筑高,那么我们只需找到右边最高的建筑就可以了,这样需增加的层数为hmax+1-hi(该值若为负数,则直接记为0,表示不用添加层数就已经被右侧建筑高),这样的话,我们只需从右向左遍历,时刻记录最大值,再对第i座建筑进行处理即可

#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<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 100001;
const int inf = 1000000000;
const int mod = 2009;
int s[N];
int main()
{
    int n,i,Max,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&s[i]);
    for(Max=s[n],s[n]=0,i=n-1;i>0;i--)
    {
        k=Max+1-s[i];
        Max=max(Max,s[i]);
        s[i]=k<0?0:k;
    }
    for(i=1;i<=n;i++)
        printf("%d%c",s[i],i<n?' ':'\n');
    return 0;
}
菜鸟成长记

你可能感兴趣的:(ACM,implementation)