Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n;
int* a = new int[n];
int* b = new int[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
if (n == 1)
cout << 0;
else {
m = a[n - 1];
b[n - 1] = 0;
for (int i = n - 2; i >= 0; --i) {
if (m < a[i]) {
b[i] = 0;
m = a[i];
} else
b[i] = m - a[i] + 1;
}
for (int i = 0; i < n; ++i) {
cout << b[i] << " ";
}
}
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class B {
int n;
long h[];
void readInput() throws NumberFormatException, IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Scanner sc = new Scanner(System.in);
n = Integer.parseInt(br.readLine());
String s = br.readLine();
String ss[] = s.split(" ");
h = new long[n];
for(int i=0; i<n; i++)
h[i] = Integer.parseInt(ss[i]);
}
void solve(){
long a[] = new long[n];
long mm = h[n-1];
for(int i=n-2;i>=0;i--){
if (h[i]<=mm)
a[i] = mm-h[i]+1;
if (h[i]>mm)
mm = h[i];
}
for(int i=0; i<n; i++)
System.out.print(a[i]+" ");
}
public static void main(String[] args) throws NumberFormatException, IOException {
B a = new B();
a.readInput();
a.solve();
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n], f[n];
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
f[n - 1] = a[n - 1];
f[n] = 0;
for (int i = n - 2; i >= 0; i--) f[i] = max(f[i + 1], a[i]);
for (int i = 0; i < n; i++)
if (f[i] == a[i] && f[i + 1] != f[i])
printf("0 ");
else
printf("%d ", f[i] - a[i] + 1);
cout << endl;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
m = map(int, raw_input().split())
max_so_far = 0
vals = [0] * n
for i in range(n - 1, -1, -1):
val = 0
if max_so_far >= m[i]:
val = max_so_far - m[i] + 1
vals[i] = val
max_so_far = max(max_so_far, m[i])
print ' '.join(str(v) for v in vals)
| PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
public class test {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int[] anss = new int[n];
int[] dh = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
dh[n-1] = 0;
anss[n-1]=0;
String ans = "";
for (int j = n - 2; j >= 0; j--) {
if (arr[j] <= arr[j+1]) {
dh[j] = arr[j+1] - arr[j] + 1;
arr[j] = arr[j+1];
anss[j] = dh[j];
}
else {
dh[j] = 0;
anss[j] = dh[j];
}
}
ans="";
for(int i=0; i<n; i++){
//System.out.print(anss[i]+" ");
ans=ans.concat(anss[i]+" ");
if((i+1)%1000==0){
System.out.print(ans);
ans="";
}
}
System.out.print(ans);
}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
bool upmin(T1 &x, T2 v) {
if (x > v) {
x = v;
return true;
}
return false;
}
template <class T1, class T2>
bool upmax(T1 &x, T2 v) {
if (x < v) {
x = v;
return true;
}
return false;
}
void solve() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
vector<int> ans(N);
for (int i = N - 1, mxv; i >= 0; --i) {
if (i == N - 1) {
ans[i] = 0;
mxv = A[i];
} else {
ans[i] = max(0, mxv + 1 - A[i]);
upmax(mxv, A[i]);
}
}
for (int i = 0; i < N; ++i) cout << ans[i] << " \n"[i + 1 == N];
}
int main() {
ios::sync_with_stdio(false);
solve();
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
int b[] = new int[n+1];
int maxi = 0;
for(int i = n - 1; i >= 0; i--) {
maxi = Math.max(a[i], maxi);
b[i] = maxi;
}
for(int i = 0; i < n; i++) {
if(a[i] > b[i+1]) {
System.out.print(0 + " ");
}
else {
System.out.print((b[i+1] - a[i] + 1) + " ");
}
}
scanner.close();
}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws Exception {
ModScanner ms=new ModScanner();
int n=ms.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)
a[i]=ms.nextLong();
long max=-99;
long[] b=new long[n];
for(int i=n-1;i>=0;i--){
if(a[i]>max){
max=a[i];
b[i]=0;
}
else
b[i]=(max-a[i]+1);
}
for(int i=0;i<n;i++)
System.out.print(b[i]+" ");
}
}
class ModScanner {
BufferedReader br;
StringTokenizer st;
public ModScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() throws Exception {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws Exception, Exception {
return Integer.parseInt(nextToken());
}
long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | '''
Created on 28-Sep-2015
@author: Venkatesh
'''
def read_int_list():
return [int(x) for x in raw_input().split()]
def read_int():
return int(raw_input())
def print_next_max_num(houses):
last_max = houses[-1]
result = [0]
houses.pop()
for ele in houses[::-1]:
if ele <= last_max:
result.append(last_max - ele + 1)
else:
result.append(0)
last_max = max(last_max, ele)
for ele in result[::-1]:
print ele,
def main():
_ = read_int()
houses = read_int_list()
print_next_max_num(houses)
if __name__ == '__main__':
main()
| PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
import java.lang.*;
import java.io.*;
/*
int n = scan.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = scan.nextInt();
long n = scan.nextLong();
long[] arr = new long[n];
for(int = 0; i < n; i++) arr[i] = scan.nextLong();
*/
public class Main {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
int[] nextG = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
nextG[i] = arr[i];
}
if(n == 1) {
System.out.println("0");
return;
}
for(int i = n - 2; i >= 0; i--) {
if(nextG[i + 1] > nextG[i]) {
nextG[i] = nextG[i + 1];
}
}
for(int i = 0; i + 1 < n; i++) {
System.out.print((nextG[i] == arr[i] ? (nextG[i] == nextG[i + 1] ? 1 : 0) : (nextG[i] - arr[i] + 1)) + " ");
}
System.out.println(0);
}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner ;
public class jkl {
public static void main (String [] args){
Scanner s = new Scanner (System.in);
int size = s.nextInt();
int [] a = new int [size];
int [] b = new int [size];
for (int j = 0 ; j<size ; j++){
int k = s.nextInt();
a[j] = k ;
}
int g = size-1 ;
int max = a[g];
b[g] = 0 ;
for (int i = g-1 ; i>=0 ; i--){
if (a[i]<max){
b[i] = max-a[i]+1 ;
}
else if (a[i]==max){
b[i] = 1 ;
max = a[i];
}else {
b[i] = 0 ;
max = a[i];
}
}
for (int y = 0 ; y<size ; y++){
System.out.print(b[y]+" ");
}
}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100100];
pair<int, int> p[100100];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
p[i] = make_pair(a[i], i);
}
sort(p, p + n);
reverse(p, p + n);
int len = 0;
for (int i = 0; i < n - 1; i++) {
while (p[len].second < i) {
len++;
}
if (p[len].second == i) {
printf("0 ");
continue;
}
printf("%d ", p[len].first - a[i] + 1);
}
printf("0");
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
map<string, long long int> mapp;
long long int a[1000000], b[1000000];
int main() {
long long int n;
cin >> n;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
long long int max = 0;
for (long long int i = n - 1; i >= 0; i--) {
if (a[i] > max)
b[i] = 0;
else
b[i] = max - a[i] + 1;
if (max < a[i]) max = a[i];
}
for (long long int i = 0; i < n; i++) cout << b[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
int main() {
int a[100005], m[100005], n, t, i, temp;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
m[n - 1] = a[n - 1];
m[n] = 0;
for (i = n - 2; i >= 0; i--) {
if (a[i] > m[i + 1])
m[i] = a[i];
else
m[i] = m[i + 1];
}
for (i = 0; i < n; i++) {
temp = m[i + 1] - a[i];
if (temp >= 0) {
printf("%d ", temp + 1);
} else
printf("0 ");
}
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
arr = list(map(int, input().split()))
maxi=0
e=[]
for i in range(len(arr)):
if arr[len(arr)-1-i] > maxi:
maxi = arr[len(arr)-1-i]
flag=len(arr)-1-i
if len(arr)-1-i ==flag:
e.append((arr[flag]- arr[len(arr)-1-i]))
else:
e.append((arr[flag]- arr[len(arr)-1-i])+1)
for i in range(len(e)):
print(e[len(e)-i-1], end=" ")
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | # In this template you are not required to write code in main
import sys
inf = float("inf")
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd
#from bisect import bisect_left,bisect_right
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod,MOD=1000000007,998244353
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
n=int(input())
Arr=get_array()
result=[0]*n
top=Arr[-1]
for i in range(n-2,-1,-1):
if Arr[i]>top:
result[i]=0
top=Arr[i]
elif Arr[i]==top:
result[i]=1
else:
result[i]=top-Arr[i]+1
print(*result,sep=' ')
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | /* / οΎοΎβ β β β β β β β β β β β γ
/ )\β β β β β β β β β β β β Y
(β β | ( Ν‘Β° ΝΚ Ν‘Β°οΌβ β(β γ
(β οΎβ Y βγ½-γ __οΌ
| _β qγ| γq |/
(β γΌ '_δΊΊ`γΌ οΎ
β |\ οΏ£ _δΊΊ'彑οΎ
β )\β β qβ β /
β β (\β #β /
β /β β β /α½£====================D-
/β β β /β \ \β β \
( (β )β β β β ) ).β )
(β β )β β β β β ( | /
|β /β β β β β β | /
[_] β β β β β [___] */
// Main Code at the Bottom
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
//Fast IO class
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
boolean env=System.getProperty("ONLINE_JUDGE") != null;
if(!env) {
try {
br=new BufferedReader(new FileReader("src\\input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long MOD=1000000000+7;
//Euclidean Algorithm
static long gcd(long A,long B){
if(B==0) return A;
return gcd(B,A%B);
}
//Modular Exponentiation
static long fastExpo(long x,long n){
if(n==0) return 1;
if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD;
return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD;
}
//Modular Inverse
static long inverse(long x) {
return fastExpo(x,MOD-2);
}
//Prime Number Algorithm
static boolean isPrime(long n){
if(n<=1) return false;
if(n<=3) return true;
if(n%2==0 || n%3==0) return false;
for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false;
return true;
}
//Reverse an array
static void reverse(int arr[],int l,int r){
while(l<r) {
int tmp=arr[l];
arr[l++]=arr[r];
arr[r++]=tmp;
}
}
//Print array
static void print1d(int arr[]) {
out.println(Arrays.toString(arr));
}
static void print2d(int arr[][]) {
for(int a[]: arr) out.println(Arrays.toString(a));
}
// Pair
static class pair{
long x,y;
pair(long a,long b){
this.x=a;
this.y=b;
}
public boolean equals(Object obj) {
if(obj == null || obj.getClass()!= this.getClass()) return false;
pair p = (pair) obj;
return (this.x==p.x && this.y==p.y);
}
public int hashCode() {
return Objects.hash(x,y);
}
}
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
//Main function(The main code starts from here)
public static void main (String[] args) throws java.lang.Exception {
int test=1;
while(test-->0) {
int n=sc.nextInt(),a[]=new int[n],suf[]=new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
suf[n-1]=a[n-1];
for(int i=n-2;i>=0;i--) suf[i]=Math.max(a[i], suf[i+1]);
for(int i=0;i<n-1;i++) out.print((suf[i+1]>=a[i]?suf[i]-a[i]+1:0)+" ");
out.print(0);
}
out.flush();
out.close();
}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.ArrayList;
import java.util.Scanner;
public class B {
/**
* @param args
*/
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int n=x.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++){
arr[i]=x.nextInt();
}
int large=-1;
ArrayList<Integer> al=new ArrayList();
for(int i=n-1;i>=0;i--){
if(arr[i]>large){large=arr[i];
al.add(0);
}
else{
al.add(large-arr[i]+1);
}
}
for(int i=0;i<al.size();i++){
System.out.println(al.get(al.size()-i-1));
}
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | // CoDE deVELOPed uNDEr AcE proDUcTiONS (enJOY!!!!)
import java.util.*;
public class Ace
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j,k,max=0;
int[] ar=new int[n];
int[] br=new int[n];
for(i=0;i<n;i++) ar[i]=sc.nextInt();
max=ar[n-1];
ar[n-1]=0;
for(i=n-2;i>=0;i--)
{
if(ar[i]>max) {
max=ar[i];
ar[i]=0;
}
else br[i]=max-ar[i]+1;
}
for(i=0;i<n;i++) System.out.print(br[i]+" ");
}
}
// CoDE deVELOPed uNDEr AcE proDUcTiONS (enJOY!!!!) | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, k, l, x, z, i;
cin >> n;
long long a[n + 5];
map<long long, long long> mp;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
long long b[n + 5];
x = 0;
for (i = n; i >= 1; i--) {
x = max(a[i], x);
b[i] = x;
mp[b[i]]++;
}
for (i = 1; i <= n; i++) {
x = (b[i] + 1) - a[i];
mp[b[i]]--;
if (mp[b[i]] == 0) {
cout << x - 1 << " ";
} else {
cout << x << " ";
}
}
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int, input().split())) + [0]
h_max = [0] * n
res = ['0'] * n
for i in range(n - 1, 0, -1):
h_max[i - 1] = max(h_max[i], h[i] + 1)
res[i] = str(max(h_max[i] - h[i], 0))
res[0] = str(max(h_max[0] - h[0], 0))
print(" ".join(res)) | PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
s=list(map(int,input().split()))
s.reverse()
d=s[0]
a=[]
for i in range(1,n):
f=int(s[i])
if f <= d:
k=str((d-f)+1)
a.append(k)
else:
a.append(0)
d=f
a.insert(0,"0")
a.reverse()
for j in a:
print(j,end=" ")
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = map(int, input().split()[::-1])
m_h = 0
res = []
for h in h:
if m_h < h:
res.append(0)
m_h = h
else:
res.append(m_h - h + 1)
print(" ".join(map(str, res[::-1]))) | PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int x[100009];
int a[100009];
int main() {
int n, mx;
cin >> n;
for (int i = 0; i < n; ++i) cin >> x[i];
a[n - 1] = 0;
mx = x[n - 1];
for (int i = n - 2; i >= 0; --i) {
if (mx == x[i]) {
a[i] = 1;
} else if (mx < x[i]) {
a[i] = 0;
mx = x[i];
} else {
a[i] = mx + 1 - x[i];
}
}
for (int i = 0; i < n; ++i) cout << a[i] << " ";
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//Removed extra array
public class LuxuriousHouses {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bf.readLine());
long[] houses = new long[n];
String[] ss = bf.readLine().split(" ");
for (int i = 0; i < n; i++) {
houses[i] = Integer.parseInt(ss[i]);
}
long max = 0;// houses[n - 1];
for (int i = n - 1; i >= 0; i--) {
if (max < houses[i]) {
max = houses[i];
houses[i] = 0;
} else {
houses[i] = max - houses[i] + 1;
}
}
for (int i = 0; i < n; i++) {
System.out.print(houses[i] + " ");
}
System.out.println();
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
h = map(int, raw_input().split())
res = []
mx = 0
while h:
c = h.pop()
if c > mx:
res.append(0)
mx = c
else:
res.append((mx - c) + 1)
res.reverse()
print ' '.join(map(str, res))
| PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
vector<int> One(1000007);
vector<int> Two(1000007);
int main() {
int n;
int maximun = -1000007;
cin >> n;
for (int i = 0; i < n; i++) cin >> One[i];
for (int i = n - 1; i >= 0; i--) {
if (One[i] > maximun)
Two[i] = One[i];
else
Two[i] = maximun + 1;
maximun = max(maximun, One[i]);
}
for (int i = 0; i < n; i++) cout << Two[i] - One[i] << " ";
cout << endl;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int, input().split()))
maxi = h[n - 1]
maxes = [0] * n
maxes[n - 1] = 0
res = ''
for i in range(n - 1, 0, -1):
if h[i - 1] > maxi:
maxi = h[i - 1]
maxes[i - 1] = 0
else:
maxes[i - 1] = maxi - h[i - 1] + 1
for i in range(n):
res += str(maxes[i]) + ' '
print(res[: -1]) | PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
h=list(map(int,input().split()))
ans=[0]*n
ans[n-1]=0
maxh=h[n-1]
for i in range(n-2,-1,-1):
if h[i]>maxh:
ans[i]=0
else:
ans[i]=maxh-h[i]+1
maxh=max(maxh,h[i])
print(' '.join(map(str,ans)))
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #from time import time
n = int(input())
x = list(map(int, input().split()))
#start_time = time()
abc = x[-1]
a = [0]
for i in range(2, n + 1):
if x[-i] <= abc:
a.append(abc - x[-i] + 1)
elif x[-i] > abc:
abc = x[-i]
a.append(0)
print(*a[::-1])
#print(time() - start_time)
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | l=lambda:map(int,raw_input().split())
n=input()
h=l()
a=[0]*n
maxi=h[n-1]
for i in range(n-2,-1,-1):
if h[i]>maxi:
a[i]=0
maxi=h[i]
else:
a[i]=maxi+1-h[i]
for v in a:
print v, | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
l=[int(x) for x in input().split()]
g=[]
h=0
for i in range(n-1, -1, -1):
x=max(0, h+1-l[i])
h=max(h, l[i])
g.append(x)
g=g[::-1]
print(*g)
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[100001];
int main() {
int n, i, m, s;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
m = a[n];
a[n] = 0;
for (i = n - 1; i > 0; i--) {
s = m + 1 - a[i];
m = max(m, a[i]);
a[i] = s < 0 ? 0 : s;
}
for (i = 1; i <= n; i++) printf("%d%c", a[i], i < n ? ' ' : '\n');
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class LuxoriousHouses {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int []max=new int [n];
int []a=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
max[n-1]=a[n-1];
for(int i=n-2;i>=0;i--)
max[i]=Math.max(max[i+1], a[i]);
for(int i=0;i<n-1;i++)
System.out.print(Math.max(max[i+1]-a[i]+1,0)+" ");
System.out.println(0);
}
}
class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
public class test {
public static void main (String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
OutputStream out = new BufferedOutputStream ( System.out );
int n = sc.nextInt();
int[] arr = new int[n];
int[] anss = new int[n];
int[] dh = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
dh[n-1] = 0;
anss[n-1]=0;
String ans = "";
for (int j = n - 2; j >= 0; j--) {
if (arr[j] <= arr[j+1]) {
dh[j] = arr[j+1] - arr[j] + 1;
arr[j] = arr[j+1];
anss[j] = dh[j];
}
else {
dh[j] = 0;
anss[j] = dh[j];
}
}
for(int i=0; i<n; i++){
//out.write((Integer.toString(anss[i]).concat(" ")).getBytes());
ans=ans.concat(Integer.toString(anss[i]).concat(" "));
if((i+1)%100==0){
out.write(ans.getBytes());
out.flush();
ans="";
}
}
out.write(ans.getBytes());
out.flush();
}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
int a[100005], b[100005];
int main() {
int n, max, l;
scanf("%d", &n);
int i;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
max = 0;
for (i = n - 1; i >= 0; i--) {
l = 1;
if (a[i] > max) {
max = a[i];
l = 0;
}
b[i] = max - a[i] + l;
}
for (i = 0; i < n; i++) {
printf("%d ", b[i]);
}
printf("\n");
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double pi = acos(-1);
int a[100010], d[100010], flag[100010];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
d[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (d[i + 1] >= a[i]) {
d[i] = d[i + 1];
flag[i] = 1;
} else
d[i] = a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] == d[i] && !flag[i])
printf("0 ");
else
printf("%d ", d[i] + 1 - a[i]);
}
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, max = 0;
cin >> n;
int a[n], h[n];
for (int i = 0; i < n; i++) cin >> a[i];
h[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
if (a[i + 1] > max) {
max = a[i + 1];
h[i] = max;
} else {
h[i] = max;
}
}
for (int i = 0; i < n - 1; i++) {
if (h[i] <= a[i]) {
if (h[i] == a[i])
cout << "1 ";
else
cout << "0 ";
} else
cout << h[i] - a[i] + 1 << " ";
}
cout << "0 ";
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class P581B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] heights = new int[n];
int[] result = new int[n];
String[] heights_str = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
heights[i] = Integer.parseInt(heights_str[i]);
}
int max_to_right = 0;
result[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
if (heights[i + 1] > max_to_right)
max_to_right = heights[i + 1];
if (heights[i] > max_to_right)
continue;
result[i] = max_to_right - heights[i] + 1;
}
for (int i = 0; i < n; i++)
System.out.print(result[i] + " ");
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | # -*- coding:utf-8 -*-
import sys
import math
def some_func():
"""
"""
n = input()
max =0
cache = []
n_list = map(int,sys.stdin.readline().split())
for v in n_list[::-1]:
if v>max:
cache.append(0)
max=v
elif v ==max:
cache.append(1)
else:
cache.append(max-v+1)
for v in cache[::-1]:
print v,
if __name__ == '__main__':
some_func()
| PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
int a[MAXN];
int n, maxx;
int ans[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
reverse(a + 1, a + 1 + n);
maxx = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] > maxx)
ans[n - i + 1] = 0;
else
ans[n - i + 1] = maxx + 1 - a[i];
maxx = max(maxx, a[i]);
}
for (int i = 1; i < n; ++i) printf("%d ", ans[i]);
printf("%d\n", ans[n]);
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Building {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String S[] = br.readLine().split(" ");
int A[] = new int[n];
int B[] = new int[n];
for(int i=0;i<n;i++)
{
A[i] = Integer.parseInt(S[i]);
}
int max = A[n-1];
for(int i = n-2 ; i>=0;i--)
{
int x = max+1-A[i];
if(x<0)
x = 0;
B[i] = x;
if(A[i]>max)
max = A[i];
}
for(int i=0;i<n;i++)
System.out.print(B[i] + " ");
System.out.println("");
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int h[N], ans[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", h + i);
}
for (int mx = 0, i = n; i >= 1; --i) {
if (mx >= h[i]) ans[i] = mx - h[i] + 1;
mx = max(mx, h[i]);
}
for (int i = 1; i <= n; ++i) {
printf("%d ", ans[i]);
}
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n;
int h[100100], b[100100];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (0 & 1) freopen("input", "r", stdin);
if (0 & 2) freopen("output", "w", stdout);
cin >> n;
for (int i = 0; i < n; i++) cin >> h[i];
for (int i = n - 1; i >= 0; i--) b[i] = max(b[i + 1], h[i + 1]);
for (int i = 0; i < n; i++) {
if (i) cout << " ";
cout << (h[i] > b[i] ? 0 : (b[i] - h[i] + 1));
}
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int h[n];
for (int i = 0; i < n; i++) cin >> h[i];
int z = 0;
vector<int> ans;
for (int i = n - 1; i >= 0; i--) {
if (z == h[i]) {
ans.push_back(1);
continue;
}
z = max(z, h[i]);
if (z - h[i] != 0)
ans.push_back(z - h[i] + 1);
else
ans.push_back(0);
}
for (int i = ans.size() - 1; i >= 0; i--) {
cout << ans[i] << " ";
}
cout << endl;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, arr[100000], r, arr1[100000], z;
bool boo[100000];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> r;
arr[i] = r;
}
for (int i = 0; i < n; i++) arr1[i] = arr[i];
reverse(arr1, arr1 + n);
z = arr1[0];
boo[0] = 1;
for (int i = 0; i < n; i++) {
if (arr1[i] > z) boo[i] = 1;
if (arr1[i] >= z)
z = arr1[i];
else
arr1[i] = z;
}
reverse(arr1, arr1 + n);
reverse(boo, boo + n);
for (int i = 0; i < n; i++) {
int k = arr1[i] - arr[i];
if (boo[i] == 1)
cout << 0 << endl;
else
cout << k + 1 << endl;
}
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
arr = list(map(int, input().split()))
maxArr = arr.copy()
maxArr.reverse()
maxArr[0] = maxArr[0] * -1
for i in range(1, len(arr)):
if maxArr[i] > abs(maxArr[i-1]):
maxArr[i] = maxArr[i] * -1
else:
maxArr[i] = abs(maxArr[i-1])
maxArr.reverse()
resultArr = []
for i in range(len(arr)):
if maxArr[i] < 0:
print('0', end=' ')
else:
print((maxArr[i] - arr[i] + 1), end=' ')
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m[100000], mn[100000], mx;
cin >> n;
for (int i = 0; i < n; ++i) cin >> m[i];
reverse(m, m + n);
mn[0] = 0, mx = m[0];
for (int i = 1; i < n; i++)
if (m[i] > mx)
mn[i] = 0, mx = m[i];
else
mn[i] = mx - m[i] + 1;
for (int i = n - 1; i > -1; --i) cout << mn[i] << ' ';
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, i, m;
int b[100001];
int a[100001];
int main() {
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
m = a[n - 1];
for (i = n - 2; i >= 0; i--) {
if (m >= a[i]) b[i] = m - a[i] + 1;
if (m < a[i]) m = a[i];
}
for (i = 0; i < n; i++) cout << b[i] << ' ';
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
arr=list(map(int,input().split()))
dp=[0]*(n+1)
dp[-1]=-1;
for i in range(n-1,-1,-1):
maxx=max(dp[i+1],arr[i])
dp[i]=maxx
#print(*dp)
ans=[]
for i in range(n):
if arr[i]>dp[i+1]:ans.append(0)
else:ans.append(dp[i+1]-arr[i]+1)
print(*ans)
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), m(n);
for (int& x : a) cin >> x;
m[n - 1] = 0;
for (int i = n - 2; i >= 0; --i) m[i] = max(a[i + 1], m[i + 1]);
for (int i = 0; i < n; ++i)
cout << (a[i] > m[i] ? 0 : m[i] - a[i] + 1) << ' ';
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class LuxuriousHouses {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = scan.nextInt();
int max = a[n-1];
int[] h = new int[n];
for(int i = n-2; i >= 0; i--){
if(max > a[i]) h[i] = max-a[i]+1<0?0:max-a[i]+1;
else if(max < a[i]) {max = Math.max(max, a[i]); h[i] = 0;}
else {max = Math.max(a[i], max); h[i] = 1;}
}
for(int i = 0; i < n; i++) System.out.print(h[i]+" ");
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
a = input().split()
for i in range(n):
a[i] = int(a[i])
m = -1
res = [0]*n
for i in range(n-1, -1, -1):
if a[i] > m:
res[i] = 0
else:
res[i] = m + 1 - a[i]
m = max(m, a[i])
for b in res:
print(b, end=" ") | PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LuxuriousHouses {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
int houses = Integer.parseInt(scan.readLine());
String[] in = scan.readLine().split(" ");
int[] out = new int[houses];
out[0] = 0;
int max = Integer.parseInt(in[houses-1]);
for (int i = houses - 2; i >= 0; i--) {
out[houses-(i+1)] = Math.max(0, max - Integer.parseInt(in[i]) + 1);
max = Math.max(max, Integer.parseInt(in[i]));
}
for (int i = houses-1; i >= 0; i--) {
System.out.print(out[i] + " ");
}
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int h[maxn], mx[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", h + i);
int cur = 0;
for (int i = n - 1; i >= 0; --i) {
mx[i] = cur;
cur = max(cur, h[i]);
}
for (int i = 0; i < n - 1; ++i) {
printf("%d ", max(0, mx[i] - h[i] + 1));
}
printf("%d\n", 0);
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (scanf("%d", &n) == 1) {
vector<int> x(n);
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
int maxi = INT_MIN;
vector<int> ans(n);
for (int i = n - 1; i >= 0; --i) {
if (maxi >= x[i]) {
ans[i] = maxi + 1 - x[i];
}
maxi = max(maxi, x[i]);
}
for (int i = 0; i < n; i++) printf("%d%c", ans[i], (i + 1 == n) ? 10 : 32);
}
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int a[100005], n, i, max;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
}
max = a[n - 1];
a[n - 1] = 0;
for (i = n - 2; i >= 0; i--) {
if (max > a[i]) {
a[i] = max - a[i] + 1;
} else if (a[i] > max) {
max = a[i];
a[i] = 0;
} else if (a[i] == max) {
a[i] = 1;
}
}
for (i = 0; i < n; i++) {
cout << a[i] << " ";
}
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(raw_input())
a = map(int, raw_input().split())
b = []
height = 0
for i in reversed(xrange(n)):
if a[i] <= height:
b.append(height - a[i] + 1)
else:
height = a[i]
b.append(0)
for x in reversed(b):
print x,
| PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long a[N];
long long dp[N];
int main() {
long long n;
scanf("%lld", &n);
int max_key = 0;
int fg = 0;
for (int i = 0; i < n; ++i) {
scanf("%lld", &a[i]);
}
dp[n - 1] = 0;
for (int i = n - 2; i >= 0; --i) {
dp[i] = max(dp[i + 1], a[i + 1]);
}
for (int i = 0; i < n - 1; ++i) {
if (a[i] > dp[i])
printf("0 ");
else if (a[i] == dp[i]) {
printf("1 ");
} else
printf("%lld ", dp[i] - a[i] + 1);
}
printf("0\n");
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
a=list(map(int,input().split()))
m=n-1
b=[]
b.append(0)
for i in range(n-2,-1,-1):
if a[i]<=a[m]:
b.append(a[m]+1-a[i])
else:
m=i
b.append(0)
print(*b[::-1])
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class Codeforce
{
public static void main(String[] args)
{
int n;
Scanner s =new Scanner (System.in);
n=s.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++)
arr[i]=s.nextInt();
s.close();
int arr1[]=new int [n];
int max=arr[n-1];
arr1[n-1]=0;
for(int i=n-2;i>=0;i--)
{
if(arr[i]>max)
{
max=arr[i];
arr1[i]=0;
}
else if(arr[i]<=max)
{
arr1[i]=max-arr[i]+1;
}
}
for(int i=0;i<n;i++)
System.out.print(arr1[i]+" ");
System.out.println();
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <class T>
T _abs(T n) {
return (n < 0 ? -n : n);
}
template <class T>
T _max(T a, T b) {
return (!(a < b) ? a : b);
}
template <class T>
T _min(T a, T b) {
return (a < b ? a : b);
}
template <class T>
T sq(T x) {
return x * x;
}
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
template <class T>
T power(T N, T P) {
return (P == 0) ? 1 : N * power(N, P - 1);
}
int main() {
long long m, n, pair_juta, pair_chara, maxi = 0, i;
long long a[100010], b[100010];
cin >> m;
for (i = 0; i < m; i++) cin >> a[i];
b[m - 1] = 0;
maxi = a[m - 1];
for (i = m - 2; i >= 0; i--) {
if (maxi < a[i]) {
maxi = a[i];
b[i] = (maxi - a[i]);
} else {
b[i] = (maxi - a[i]) + 1;
}
}
for (i = 0; i < m; i++) cout << b[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.util.Scanner;
public class Luxurious_Houses {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
int[] ans = new int[n];
for(int i = 0; i<n; i++)
{
arr[i] = in.nextInt();
}
int max = -1;
for(int i = n-1; i >= 0; i--)
{
if(i == n-1)
{
ans[i] = 0;
max = Math.max(max, arr[i]);
continue;
}
if(max >= arr[i])
{
ans[i] = max - arr[i] + 1;
}
else
{
ans[i] = 0;
}
//System.out.println(max+" " + arr[i] + " " +ans[i]);
max = Math.max(max, arr[i]);
}
for(int i = 0; i<n;i++)
System.out.print(ans[i] + " ");
System.out.println();
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, index = -1, max;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
max = 0;
for (int j = i + 1; j < n; j++)
if (max < a[j]) max = a[j];
if (max < a[i])
cout << "0 ";
else
cout << max - a[i] + 1 << " ";
}
cout << "0 ";
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
def main():
num_of_house = int(raw_input())
house_heights = map(int, raw_input().split())
cur_max = 0
answer = [0 for i in range(num_of_house)]
for i in range(num_of_house-1, -1, -1):
if i < num_of_house - 1:
if cur_max + 1 > house_heights[i]:
answer[i] = cur_max + 1 - house_heights[i]
else:
answer[i] = 0
else:
answer[i] = 0
cur_max = max((cur_max, house_heights[i]))
for i in range(num_of_house):
print answer[i],
if __name__ == '__main__':
main() | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, hs[100001], ans[100001];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> hs[i];
int maxx = 0;
for (int i = n; i; --i) {
ans[i] = max(maxx - hs[i] + 1, 0);
maxx = max(maxx, hs[i]);
}
for (int i = 1; i < n; ++i) cout << ans[i] << ' ';
cout << ans[n] << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, Max = 0;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
v.push_back(a);
}
Max = v.back();
v[v.size() - 1] = 0;
for (int i = v.size() - 2; i >= 0; i--) {
if (v[i] < Max)
v[i] = (Max + 1) - v[i];
else if (v[i] == Max)
v[i] = 1;
else if (v[i] > Max) {
Max = v[i];
v[i] = 0;
}
}
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
const int N = 1e5;
int h[N], max[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", h + i);
}
max[n - 1] = h[n - 1];
for (int i = n - 2; i >= 0; --i) {
max[i] = h[i] > max[i + 1] ? h[i] : max[i + 1];
}
for (int i = 0; i < n - 1; ++i) {
printf("%d ", h[i] > max[i + 1] ? 0 : max[i + 1] - h[i] + 1);
}
puts("0");
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[400000];
void Up(int l, int r, int k, int u, int v) {
if (l == r) {
a[u] = v;
return;
}
int m = (l + r) / 2;
if (m >= k)
Up(l, m, k, u * 2, v);
else
Up(m + 1, r, k, u * 2 + 1, v);
a[u] = max(a[u * 2], a[u * 2 + 1]);
}
int main() {
int n, b[200000];
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
Up(1, n, i, 1, b[i]);
}
for (int i = 1; i <= n; i++) {
Up(1, n, i, 1, 0);
if (b[i] > a[1])
cout << 0 << " ";
else
cout << a[1] - b[i] + 1 << " ";
}
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
l=list(map(int,input().split()))[::-1]
ma=0
ans=[]
for i in range(n):
if l[i]>ma: ans+=['0']; ma=l[i]
else: ans+=[str(ma-l[i]+1)]
print(' '.join(ans[::-1])) | PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | def solve(arr):
maxRight = arr[-1]
arr[-1] = 0
for i in range(len(arr) - 2, - 1, - 1):
if maxRight >= arr[i]:
arr[i] = maxRight + 1
else:
maxRight = arr[i]
return arr
n = input()
arr = map(int,raw_input().split())
ans = arr
ans = list(ans)
arr = solve(arr)
for i in range(n - 1):
print arr[i] - ans[i],
print arr[-1] | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | a=int(input())
l=list(map(int,input().split()))
d,c=[0]*a,l[-1]
for x in range(2,a+1):
d[-x]=max(0,c-l[-x]+1)
c=max(c,l[-x])
print(*d)
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(raw_input())
N=map(int,raw_input().split())
a=0
b=[]
for i in range(n):
b.append(max(0,a+1-N[n-i-1]))
a=max(a,N[n-i-1])
for i in range(n):
print b[n-i-1], | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | var n = parseInt(readline());
var input = readline().split(' ').map(function (item) {
return parseInt(item);
})
var result = [];
input.reduceRight(function (prev, curr) {
if(curr > prev){
result.push(0);
} else if (curr === prev) {
result.push(1);
} else {
result.push(prev - curr + 1);
}
return Math.max(prev, curr);
}, 0)
result = result.reverse();
print(result.join(' ')); | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int a[maxn], o[maxn];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
int _max = 0;
for (int i = n - 1; i >= 0; --i)
if (a[i] > _max)
o[i] = 0, _max = a[i];
else
o[i] = _max - a[i] + 1;
for (int i = 0; i < n; ++i) {
if (i > 0) printf(" ");
printf("%d", o[i]);
}
printf("\n");
}
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
int v[N], seg[4 * N + 1];
int a, b;
void init(int r, int i, int j) {
if (i == j)
seg[r] = v[i];
else {
init(2 * r, i, (i + j) / 2);
init(2 * r + 1, (i + j) / 2 + 1, j);
seg[r] = max(seg[2 * r], seg[2 * r + 1]);
}
}
int query(int r, int i, int j) {
if (b < i or a > j or j >= n or i < 0) return 0;
if (i >= a and j <= b)
return seg[r];
else {
int L = query(2 * r, i, (i + j) / 2);
int R = query(2 * r + 1, (i + j) / 2 + 1, j);
return max(L, R);
}
}
int main(void) {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
init(1, 0, n - 1);
b = n - 1;
for (int i = 0; i < n; i++) {
a = i + 1;
int que = query(1, 0, n - 1);
if (v[i] > que)
cout << "0"
<< " ";
else
cout << que - v[i] + 1 << " ";
}
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
int maxsize = 30;
using namespace std;
int main() {
int n;
int A[100005];
cin >> n;
for (int i = 1; i <= n; i++) cin >> A[i];
int m = 0;
int ans[100005];
ans[n] = 0;
for (int i = n - 1; i >= 1; i--) {
m = max(m, A[i + 1]);
ans[i] = m + 1 - A[i];
if (ans[i] < 0) ans[i] = 0;
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
a=map(int,input().split(" "))
rmax=0
res=[]
for x in reversed(list(a)):
t = rmax - x
res.append(0 if t<0 else t+1)
rmax=max(x, rmax)
for x in reversed(res):
print(x,end=" ")
print()
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(raw_input())
numbers = map(int, raw_input().split())
maxx = numbers[n - 1]
numbers[n - 1] = 0
for it in xrange(n - 2, -1, -1):
if numbers[it] > maxx:
maxx = numbers[it]
numbers[it] = 0
else:
numbers[it] = maxx + 1 - numbers[it]
print ' '.join(map(str, numbers)) | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[100009];
int main() {
int i, j, m, n, t, k;
cin >> n;
int ma = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
}
int sum = 0;
int b[100009];
for (i = n - 1, j = 0; i >= 0; i--) {
if (a[i] > ma) {
ma = a[i];
b[j++] = 0;
continue;
} else {
b[j++] = ma - a[i] + 1;
}
}
for (i = n - 1; i >= 0; i--) cout << b[i] << " ";
cout << endl;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 100005;
const int maxm = 10005;
int h[maxn], ans[maxn];
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
int maxh = 0;
for (int i = n; i >= 1; i--) {
if (h[i] > maxh)
ans[i] = 0;
else {
ans[i] = maxh - h[i] + 1;
}
maxh = max(maxh, h[i]);
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << endl;
}
int main() {
solve();
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int mx, n, ara[1000000];
int main() {
scanf("%d", &(n));
for (int i = (0); i < (n); i++) scanf("%d", &(ara[i]));
mx = ara[n - 1];
for (int i = (n - 2); i >= (0); i--) {
if (mx >= ara[i])
ara[i] = mx - ara[i] + 1;
else {
mx = ara[i];
ara[i] = 0;
}
}
ara[n - 1] = 0;
for (int i = (0); i < (n); i++) printf("%d ", ara[i]);
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Ruins He
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] arr = in.nextInts(n);
int[] res = new int[n];
int max = 0;
for (int i = n - 1; i >= 0; i--) {
if (arr[i] > max) res[i] = 0;
else res[i] = max + 1 - arr[i];
max = Math.max(max, arr[i]);
}
out.printInts(res);
}
}
static class OutputWriter {
private final PrintWriter writer;
private OutputWriter(Writer writer, boolean autoFlush) {
this.writer = new PrintWriter(new BufferedWriter(writer, 32767), autoFlush);
}
public OutputWriter(Writer writer) {
this(writer, true);
}
public OutputWriter(OutputStream outputStream) {
this(new OutputStreamWriter(outputStream), false);
}
public OutputWriter println() {
writer.println();
return this;
}
public void print(int value) {
writer.print(value);
}
public void print(Object object) {
writer.print(object);
}
public void close() {
writer.close();
}
public void printInts(int[] values) {
for (int i = 0; i < values.length; i++) {
if (i > 0) print(" ");
print(values[i]);
}
println();
}
}
static class InputReader {
public final BufferedReader reader;
public StringTokenizer tokenizer = null;
public InputReader(Reader reader) {
this.reader = new BufferedReader(reader, 32767);
}
public InputReader(InputStream inputStream) {
this(new InputStreamReader(inputStream));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextInts(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const double pi = acos(-1.0);
const double eps = 1e-9;
int n, a[200100], m[200100];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) m[i] = max(m[i + 1], a[i]);
for (int i = 1; i <= n; i++) {
if (a[i] <= m[i + 1])
cout << m[i + 1] - a[i] + 1 << " ";
else
cout << 0 << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int maxi[100000 + 100];
bool mark[100000 + 100];
int main() {
int n;
cin >> n;
int h[n];
for (int i = 0; i < n; i++) cin >> h[i];
maxi[n - 1] = h[n - 1];
mark[n - 1] = true;
for (int i = n - 2; i >= 0; i--) {
if (h[i] > maxi[i + 1]) {
maxi[i] = h[i];
mark[i] = true;
} else
maxi[i] = maxi[i + 1];
}
for (int i = 0; i < n; i++) {
if (mark[i])
cout << 0 << " ";
else
cout << maxi[i] - h[i] + 1 << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(raw_input())
a=map(int,raw_input().split(' '))
m=0
b=[0]*n
m=a[-1]
for i in xrange(len(a)-2,-1,-1):
if(a[i]<=m):
b[i]=m-a[i]+1
else:
m=a[i]
for i in b:
print i, | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Gots {
public static long mod = 1000000000 + 7;
public static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
public static int counter = 0;
public static boolean isValid(int i, int j, int N, int M) {
return (i >= 0 && i < N && j >= 0 && j < M);
}
public static long f(char[] arr) {
long ans = 0;
return ans;
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
String[] input = reader.readLine().split(" ");
int N = Integer.parseInt(input[0]);
long[] arr = new long[N];
long[] ans = new long[N];
input = reader.readLine().split(" ");
for (int i = 0; i < N; i++) {
arr[i] = Long.parseLong(input[i]);
}
long localMax = arr[N - 1];
ans[N - 1] = 0;
for (int i = N - 2; i >= 0; i--) {
if (localMax < arr[i]) {
ans[i] = 0;
localMax = Math.max(arr[i], localMax);
} else if (localMax == arr[i]) {
ans[i] = 1;
} else {
ans[i] = localMax - arr[i] + 1;
}
}
// //int debug = 0;
for (int i = 0; i < N; i++) {
writer.print(ans[i] + " ");
}
//writer.println(Math.min(N, D) + " " + nasht / 2);
writer.flush();
}
static class Pair implements Comparable<Pair> {
int first;
int second;
Pair(int f, int s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.first, o.first);
}
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
z=list(map(int,input().split()))
lst=[0]*n;m=z[-1];lst[-1]=0
for i in range(n-2,-1,-1):
if z[i] > m:
lst[i]=0
m = z[i]
else:
lst[i]= m-z[i]+1
print(*lst)
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
a = list(map(int, input().split()))
maxi = -1
for i in range(n - 1, -1, -1):
t = a[i]
if a[i] > maxi:
a[i] = 0
else:
a[i] = maxi - a[i] + 1
maxi = max(maxi, t)
print(*a)
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> v(n), s(n);
for (int i = 0; i < n; i++) cin >> v[i], s[i] = v[i];
reverse(s.begin(), s.end());
long long mx = 0;
for (int i = 0; i < n; i++) s[i] = max(s[i], mx), mx = s[i];
reverse(s.begin(), s.end());
for (int i = 0; i < n - 1; i++) {
if (v[i] <= s[i + 1])
v[i] = s[i + 1] - v[i] + 1;
else
v[i] = 0;
}
v[n - 1] = 0;
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << v[i];
}
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int, input().split()))
a = [0]*n
m = -1
for i in range(n-1,-1,-1):
a[i] = max(m+1-h[i],0)
m = max(m, h[i])
print(*a)
| PYTHON3 |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long int a[1000000], b[1000000], n, k, l, x, m, s, p, i, j, t;
int main() {
map<long long, long long> v;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
v[a[i]]++;
}
for (i = 0; i < n; i++) b[i] = a[i];
sort(b, b + n);
reverse(b, b + n);
m = b[0];
x = 0;
for (i = 0; i < n; i++) {
if (m == a[i]) {
if (v[m] > 1) {
cout << 1 << " ";
v[m]--;
} else if (v[m] == 1) {
cout << 0 << " ";
v[m]--;
for (j = x; j < n; j++) {
if (v[b[j]] > 0) {
m = b[j];
x = j;
break;
}
}
}
} else {
k = m - a[i] + 1;
v[a[i]]--;
cout << k << " ";
}
}
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class a {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
YY solver = new YY();
solver.solve(1, in, out);
out.close();
}
static class YY {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++) arr[i] = in.nextInt();
int max = -1;
for(int i=n-1; i>-1; i--){
if(arr[i]>max){
max = arr[i];
arr[i] = 0;
}else arr[i] = max-arr[i]+1;
}
for(int x : arr) out.printf("%d ", x);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> h, ans;
int n, t = 0;
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
h.resize(n + 1);
for (int i = 1; i <= n; i++) cin >> h[i];
ans.push_back(0);
t = h[n];
for (int i = n - 1; i > 0; i--) {
if (h[i] > t)
ans.push_back(0);
else
ans.push_back(t - h[i] + 1);
t = max(t, h[i]);
}
for (int i = n - 1; i >= 0; i--) cout << ans[i] << ' ';
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[100005];
int suff[100005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = n; i >= 1; i--) {
suff[i] = max(suff[i + 1], a[i]);
}
for (int i = 1; i <= n; i++) {
printf("%d ", max(0, suff[i + 1] - a[i] + 1));
}
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | num = int(raw_input())
houses = [int(x) for x in raw_input().split()]
maxAt = [0] * num
highest = 0
for i in range(num-1, -1, -1):
if houses[i] > highest:
highest = houses[i]
maxAt[i] = highest
for i in range(0, num-1):
highest = maxAt[i+1]
if houses[i] <= highest:
print (highest + 1 - houses[i]),
else:
print 0,
print 0 | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | //package TestOnly.Div2B_322.Code1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main
{
FastScanner in;
PrintWriter out;
public void solve() throws IOException
{
int n = in.nextInt();
int a[] = new int[n];
int b[] = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = in.nextInt();
}
int max = 0;
for (int i = n - 1; i >= 0; i--)
{
b[i] = Math.max(0, max + 1 - a[i]);
max = Math.max(max, a[i]);
}
for (int i = 0; i < b.length; i++)
{
System.out.print(b[i] + " ");
}
}
public void run()
{
try
{
in = new FastScanner();
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
class FastScanner
{
BufferedReader br;
StringTokenizer st;
FastScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreTokens())
{
try
{
st = new StringTokenizer(br.readLine());
} catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
}
public static void main(String[] arg)
{
new Main().run();
}
}
| JAVA |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(raw_input())
li=map(int, raw_input().split())
ans=[li[n-1]]
temp=li[n-1]
for i in xrange(n-2, -1, -1):
if li[i]<=temp:
ans.append(temp+1)
else:
ans.append(li[i])
temp=li[i]
ans=ans[::-1]
for i in xrange(len(ans)):
print ans[i]-li[i], | PYTHON |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e5;
int n;
bool flag;
int a[MaxN + 5], b[MaxN + 5];
int Max = 1 >> 30;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 1; i--) {
flag = 0;
if (a[i] > Max) {
Max = a[i];
flag = 1;
}
b[i] = Max - a[i] + 1;
if (flag) b[i]--;
}
for (int i = 1; i <= n; i++) printf("%d ", b[i]);
printf("\n");
}
| CPP |
581_B. Luxurious Houses | 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.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> q(n);
for (int i = 0; i < n; i++) {
cin >> q[i];
}
int mx = 0;
vector<int> ans;
for (int i = n - 1; i >= 0; i--) {
ans.push_back(max(0, mx + 1 - q[i]));
mx = max(mx, q[i]);
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.