题目:
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 Input
5
1 2 3 1 2
3 2 0 2 0
4
3 2 1 4
2 3 4 0
题意: (这道题开始并没有看懂题目意思,后开看别人的才知道)题目不难,想想可以做
给你一个数你,表示接下来n个输入,接下来的n个数表示每个房子的楼层数,其中最右边的和这组数中最大的为豪华住宅,问你每个住宅加几层可以成为豪华住宅,就是说
每个住宅的层数都要比右边的大
分析:
应为每个住宅都要是豪华的,且都各自独立,所以我们从这组数的末尾开始遍历,开两数组,一个储存原来的信息,另一个记录当前值与最大值的关系,即我判断如果当前值大于最大值 记录下来这个形式,当前值等于最大值再记录,当前值小于最大值,用数组也记录下来,最后再循环,判断原数组与记录数组间的关系,比较判断就OK了。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=100001; 6 int a[maxn],b[maxn]; 7 int main() 8 { 9 int n; 10 cin>>n; 11 for(int i=0;i<n;i++) 12 cin>>a[i]; 13 int Max=0; 14 for(int i=n-1;i>=0;i--) 15 { 16 if(a[i]>Max) b[i]=a[i]; 17 else if(a[i]==Max) b[i]=0; 18 else 19 b[i]=Max; 20 Max=max(Max,a[i]); 21 } 22 for(int i=0;i<n-1;i++) 23 { 24 if(b[i]==a[i]) printf("0 "); 25 else if(b[i]==0) printf("1 "); 26 else 27 printf("%d ",b[i]-a[i]+1); 28 } 29 printf("0 "); 30 return 0; 31 }