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
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++) if(a[i]>max[i+1]) System.out.print(0+" "); else System.out.print(max[i+1]-a[i]+1+" "); 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
#include <bits/stdc++.h> int main() { int n, i, s[200000], t[200000], j, x = 1, y, max = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &s[i]); } max = s[n]; t[n] = 0; for (i = n - 1; i >= 1; i--) { if (s[i] > max) { t[i] = 0; max = s[i]; } else { t[i] = max + 1 - s[i]; } } for (i = 1; i <= n; i++) { printf("%d ", t[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; long long a[100009]; long long max2[100009]; bool isnew[100009]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } max2[n - 1] = a[n - 1]; isnew[n - 1] = true; for (int i = n - 2; i >= 0; i--) { if (a[i] > max2[i + 1]) { max2[i] = a[i]; isnew[i] = true; } else max2[i] = max2[i + 1]; } for (int i = 0; i < n; i++) { cout << (max2[i] + !isnew[i]) - 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('')) s=raw_input('') a=s.split(" ") arr=[] for i in range(n): arr.append(int(a[i])) max=-1 b=[] for i in range(n): k=arr[n-i-1] if k>max: b.append(0) max=k elif k==max: b.append(1) else: b.append(max-k+1) b=b[::-1] print ' '.join(map(str,b))
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.HashSet; import java.util.Scanner; public class codeforcesB { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] h = new long[n]; for (int i = 0; i<n; i++) h[i] = in.nextLong(); long max = h[n-1]; HashSet<Integer> e = new HashSet<Integer>(); e.add(n-1); for (int i = n-1; i>=0; i--){ if (h[i]>max){ e.add(i); max = h[i]; } } max = h[n-1]; long[] ans = new long[n]; for (int i = n-1; i>=0; i--){ if (e.contains(i)){ ans[i] = 0; if (h[i]>max) max = h[i]; } else { if (h[i]<=max){ ans[i] = max-h[i]+1; if (h[i]>max) max = h[i]; } } } for (int i = 0; i<n; i++) System.out.print(ans[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=list(map(int,input().split())) ans=[0]*n curr_max=-1 for i in range(n-1,-1,-1): if a[i]>curr_max: ans[i]=0 curr_max=a[i] else: ans[i]=curr_max-a[i]+1 for i in range(n): print(ans[i],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; cin >> n; int a[n + 1]; int b[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; int mx = INT_MIN; for (int i = n - 2; i >= 0; i--) { mx = max(mx, a[i + 1]); b[i] = mx; } for (int i = 0; i < n - 1; i++) { cout << max(b[i] - a[i] + 1, 0) << " "; } cout << 0 << 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() { long long int n, max1 = 0; cin >> n; long long int a[n]; long long int b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } b[n - 1] = 0; max1 = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > max1) { b[i] = 0; max1 = a[i]; } else { b[i] = max1 - 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
#include <bits/stdc++.h> using namespace std; int main() { int n, i, max = 0, a[100005], b[100005]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } max = a[n - 1]; b[n - 1] = 0; for (i = n - 2; i >= 0; i--) { if (a[i] <= max) { b[i] = max - a[i] + 1; } else { b[i] = 0; max = a[i]; } } for (i = 0; i < n; i++) printf("%d ", 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> using namespace std; int n; vector<int> h, s; int main() { ios_base::sync_with_stdio(false); cin >> n; h.resize(n); for (int i = 0; i < n; ++i) cin >> h[i]; s = h; for (int i = (int)s.size() - 2; i >= 0; --i) s[i] = max(s[i], s[i + 1]); for (int i = 0; i < (int)s.size(); ++i) { if (i + 1 == (int)s.size()) { cout << 0 << endl; } else if (s[i + 1] >= h[i]) { cout << s[i + 1] - h[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 main() { long long int n, a; cin >> n; vector<long long int> vec(n), copie; for (long long int i(0); i < n; i++) { cin >> a; vec.push_back(a); } reverse(begin(vec), end(vec)); copie.push_back(0); int maxi = vec[0]; for (long long int i(1); i < n; i++) { if (vec[i] <= maxi) copie.push_back(maxi - vec[i] + 1); else { copie.push_back(0); maxi = vec[i]; } } for (long long int i(n - 1); i >= 0; i--) { cout << copie[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 = input() buildings = input().split(' ') m = 0 lenght = len(buildings) - 1 result = [] for i in range(lenght, -1, -1): b = int(buildings[i]) x = m - b if x < 0: result.append('0') m = b else: result.append(str(x + 1)) print(' '.join([result[i] for i in range(lenght, -1, -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 = input() buildings = list(map(int, input().split(' '))) m = 0 result = [] for b in buildings[::-1]: x = m - b if x < 0: result.append(0) m = b else: result.append(x + 1) print(' '.join(map(str, result[::-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; const double eps = 1e-9; const int MaxN = int(2e5) + 256; const int MOD = int(1e9) + 7; template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } inline bool Palindrome(const string& s) { return equal(s.begin(), s.end(), s.rbegin()); } int s[MaxN], a[MaxN]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", a + i); for (int i = n - 1; i >= 0; --i) s[i] = max(s[i + 1], a[i]); for (int i = 0; i < n; ++i) { if (s[i + 1] < a[i]) { printf("0 "); } else { printf("%d ", abs(s[i] - a[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
from collections import Counter n=int(input()) a=[int(x) for x in input().split()] mx=-1 ans=[0]*n for i in reversed(range(n)): ans[i]=max(0,mx+1-a[i]) mx=max(mx,a[i]); 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 n, a[100001]; int m[100002]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; m[n] = 0; for (int i = n - 1; i >= 0; i--) m[i] = max(a[i], m[i + 1]); for (int i = 0; i < n; i++) cout << max(0, m[i + 1] - a[i] + 1) << (i == n - 1 ? '\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
n = int(raw_input()) a = [int(x) for x in raw_input().split()] a.append(0) c = 0 b = [] for i in xrange(n): j = a[n - i - 1] c = max(c , a[n - i]) if j > c: k = 0 else: k = c + 1 - j b.append(k) for i in xrange(n): print b[n - 1 - 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
n = int(input()) house = list(map(int,input().split())) house.reverse() final = [] largestVal = 0 for i in house: if i > largestVal: final.append("0") else: final.append(f"{largestVal - i + 1}") largestVal = max([largestVal,i]) final.reverse() print(' '.join(final))
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()) l=[int(i) for i in input().split()] b = n*[0] maxh = l[n-1] for i in range(n-2,-1,-1): b[i] = max(0,maxh+1-l[i]) maxh = max(maxh,l[i]) print(*b)
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; set<long long> st; long long b[200007], a[200007], t, r, x1, l, y2, x2, y4, n, i, k, ma, p, j, mi, p1, p2, p3, p4, l1, l2, l3, l4, c; string s, s1, s2, s3; char ch; int main() { cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; ma = a[n]; b[n] = 0; for (i = n - 1; i >= 1; i--) { b[i] = (ma - a[i]) + 1; if (b[i] < 0) b[i] = 0; if (a[i] > ma) ma = a[i]; } for (i = 1; 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> using namespace std; int n, a[100005], maxi, b[100005]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } maxi = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > maxi) { maxi = a[i]; b[i] = 0; } else b[i] = maxi + 1 - a[i]; } for (int i = 0; i < n; i++) printf("%d ", 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.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()); } 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
import java.io.*; import java.math.*; import java.util.*; /** * Created by Sai on 2015/9/28. */ public class B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] ans = new int[n]; String[] row = br.readLine().split(" "); int max = Integer.parseInt(row[n - 1]) - 1; for (int i = n - 1; i >= 0; i--) { int cur = Integer.parseInt(row[i]); if (cur > max) { max = cur; ans[i] = 0; } else { ans[i] = max - cur + 1; } } for (int i = 0; i < n; i++) { System.out.print(ans[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 inf = 1e9 + 2; const double pi = acos(-1.0); const int N = 300010; int n, a[N], suf[N], res[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); suf[n] = a[n]; for (int i = n - 1; i >= 1; i--) suf[i] = max(suf[i + 1], a[i]); for (int i = 1; i <= n; i++) { if (i != n) { if (a[i] > suf[i + 1]) res[i] = 0; else res[i] = suf[i + 1] - a[i] + 1; } else res[i] = 0; } for (int i = 1; i <= n; i++) printf("%d ", res[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; void solve() { long long int n; cin >> n; vector<long long int> v(n); vector<long long int> v1(n); for (long long int i = 0; i < n; i++) cin >> v[i]; long long int max = v[n - 1]; long long int cnt = 0; for (long long int i = n - 1; i > -1; i--) { if (v[i] > max) { max = v[i]; cnt = 0; } if (v[i] == max) cnt++; if (v[i] == max && cnt == 1) v1[i] = 0; else v1[i] = max - v[i] + 1; } for (long long int i = 0; i < n; i++) cout << v1[i] << " "; } int main() { long long int t; t = 1; ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; while (t--) { solve(); } }
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 codeforces581B { public static void main(String[] args) { Scanner s=new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n=s.nextInt(),i; int[] a = new int[n]; int[] m = new int[n]; for(i=0;i<n;i++) a[i]=s.nextInt(); m[n-1]=a[n-1]; for(i=n-2;i>-1;i--) m[i]=Math.max(m[i+1],a[i+1]); for(i=0;i<n-1;i++) { if(a[i]<=m[i]) sb.append(m[i]-a[i]+1); else sb.append(0); sb.append(" "); } sb.append(0); System.out.print(sb); } }
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, a[100000], i, m[100000]; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; m[n - 1] = a[n - 1]; 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 - 1; i++) { if (a[i] > m[i + 1]) cout << 0 << ' '; else cout << m[i + 1] + 1 - a[i] << ' '; } cout << 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 x[100009], 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
#include <bits/stdc++.h> using namespace std; int main() { long long int n, ar[1000001], i, m; vector<long long int> vec; cin >> n; for (i = 0; i < n; i++) { cin >> ar[i]; } m = ar[n - 1]; for (i = n - 2; i >= 0; i--) { if (ar[i] == m + 1) vec.push_back(0); else if (ar[i] > m) vec.push_back(0); else { long long int p = m + 1 - ar[i]; vec.push_back(p); } m = max(ar[i], m); } for (auto it = vec.rbegin(); it != vec.rend(); it++) cout << *it << " "; cout << 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; vector<int> vec; vector<int> dp; vector<int> rein; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; vec.push_back(x); } int big = -2; dp.resize(n); rein.resize(n); fill(rein.begin(), rein.end(), 0); for (int j = n - 1; j >= 0; j--) { if (big == vec[j]) { rein[j] = 1; dp[j] = big; } else { big = max(big, vec[j]); dp[j] = big; } } for (int i = 0; i < n; i++) { if (vec[i] == dp[i] && rein[i] == 1) { cout << dp[i] - vec[i] + 1; } else if (vec[i] == dp[i] && rein[i] == 0) { cout << "0"; } else { cout << dp[i] - vec[i] + 1; } if (i != (n - 1)) cout << " "; } 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())) max=-1 b=[] for i in range(0,n): p=arr[n-1-i] if p>max: b.append(0) max=p else: b.append(max-p+1) for i in range(0,n): print(b[n-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
num=int(input()) a=list(map(int,input().split())) b=[0]*num maxx=a[-1] for i in range(num - 2, -1, -1): if a[i] <= maxx: b[i] = maxx - a[i] + 1 else: maxx = a[i] print(*b)
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[101000], g[101000]; 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--) g[i] = max(g[i + 1], a[i]); for (int i = 1; i <= n; i++) { if (a[i] > g[i + 1]) printf("0 "); else printf("%d ", g[i + 1] + 1 - a[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
n=int(input()) a=list(map(int,input().split())) max_num=0 res=[0]*n for i in range(n-1,-1,-1): res[i]=max(0,max_num-a[i]+1) max_num=max(max_num,a[i]) print(' '.join(map(str,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()) h = list(map(int, input().split())) + [0] h_max = 0 res = [] for i in range(n, 0, -1): h_max = max(h_max, h[i] + 1) res.append(max(h_max - h[i - 1], 0)) 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
import java.util.*; import java.io.*; public class B581 { public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); int n = input.nextInt(); int[] a = new int[n]; for(int i = 0; i<n; i++) a[i] = input.nextInt(); int max = a[n-1]; int[] res = new int[n]; for(int i = n-2; i>=0; i--) { int add = Math.max(0, max + 1 - a[i]); res[i] = add; max = Math.max(max, a[i]); } for(int x : res) out.print(x+" "); out.close(); } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(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
input() a=list(map(int, input().split()))[::-1] x=-1 b=[] for v in a: b+=[max(0,x-v+1)] x=max(x,v) 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
from __future__ import print_function t = int(input()) a = list(map(int , input().split())) b=[] max = a[t-1] i=t-2 while i >= 0: if a[i] <= max: b.append(max-a[i]+1) else: max = a[i] b.append(0) i = i-1 b.insert( 0, "0") i = t-1 while(i>=0): print(b[i] , end = " ") i = i-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.ArrayList; import java.util.Scanner; public class cf581b { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); ArrayList<Integer> houses = new ArrayList<>(); ArrayList<Integer> floors = new ArrayList<>(); for(int i=0;i<n;i++){ houses.add(in.nextInt()); } int max = 0; for(int i=n-1;i>=0;i--){ if(houses.get(i)>max){ floors.add(0); max = houses.get(i); }else{ floors.add(max-houses.get(i)+1); } } for(int i=n-1;i>=0;i--){ System.out.print(floors.get(i)+" "); } in.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
n = int(input()) L = list(map(int,input().split())) maxlist = [L[-1] for i in range(n)] for i in range(n-2,-1,-1): maxlist[i] = max(L[i],maxlist[i+1]) M = [None for i in range(n)] for i in range(n): if L[i] == maxlist[i]: if i+1 < n and maxlist[i] == maxlist[i+1]: M[i] = str(1) else: M[i] = str(0) else: M[i] = str(maxlist[i]+1-L[i]) print(' '.join(M))
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() { ios::sync_with_stdio(false); long long n, i; cin >> n; long long a[n], revMax[n]; for (i = 0; i < n; i++) cin >> a[i]; revMax[n - 1] = a[n - 1]; for (i = n - 2; i >= 0; i--) { revMax[i] = max(revMax[i + 1], a[i]); } for (i = 0; i < n - 1; i++) { if (a[i] > revMax[i + 1]) cout << "0 "; else cout << revMax[i + 1] - a[i] + 1 << " "; } cout << "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
import java.util.*; public class LuxuriousHouses { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int a[] = new int[n]; for(int j = 0; j < n; ++j){ a[j] = scanner.nextInt(); } int maxi = a[n-1]; int b[] = new int[n]; b[n-1] = 0; for(int i = n-2; i >= 0; i--){ if(a[i] > maxi){ maxi = a[i]; b[i] = 0; }else{ b[i] = maxi - a[i] + 1; } } for(int k = 0; k < n; ++k){ System.out.print(b[k]+" "); } } }
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 = list([int(x) for x in input().split()]) maxs = a[-1] needed = [0] * n for i in range(n - 2, -1, -1): need = max(0, maxs - a[i] + 1) needed[i] = need maxs = max(maxs, a[i]) print(*needed)
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[100100], h[100100], mx[100100]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", a + i); } for (int i = n; i >= 1; i--) { mx[i] = max(mx[i + 1], a[i]); } for (int i = 1; i <= n; i++) { if (a[i] > mx[i + 1]) continue; h[i] = mx[i + 1] + 1 - a[i]; } for (int i = 1; i <= n; i++) cout << h[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 main() { long long n, s; cin >> n; vector<long long> vc; for (long long i = 0; i < n; i++) { cin >> s; vc.push_back(s); } long long mx = -9999; vector<long long> build; mx = vc[n - 1]; build.push_back(0); for (long long i = n - 2; i >= 0; i--) { if (vc[i] > mx) build.push_back(0); else build.push_back(mx - vc[i] + 1); mx = max(mx, vc[i]); } for (long long i = (long long)((build).size()) - 1; i >= 0; i--) cout << build[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 sys n = int(sys.stdin.next()) floors = map(int, sys.stdin.next().split(' ')) m = 0 s = [] for h in floors[::-1]: s.append('%i' % ((m - h + 1) if (m - h + 1) > 0 else 0)) m = max(h, m) print(' '.join(s[::-1]).strip())
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 luxurioushouses { public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int a[]=new int[n]; int b[]=new int[n]; int i,pos=0,s=0,max=0; for(i=0;i<n;i++) a[i]=in.nextInt(); /*do { max=0; for(i=s;i<n;i++) { if(a[i]>max) { pos=i;max=a[i]; } } b[pos]=0; for(i=s;i<pos;i++) b[i]=max-a[i]+1; s=pos+1; }while(s<n); b[n-1]=0;*/ for(i=n-1;i>=0;i--) { if(a[i]>max) { max=a[i]; pos=i; } if(pos==i) b[i]=0; else b[i]=max+1-a[i]; } for(i=0;i<n-1;i++) System.out.print(b[i]+" "); System.out.print(b[n-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
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j; long long int a[100003], b[100003]; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } b[n - 1] = 0; long long int max = a[n - 1]; for (j = n - 2; j >= 0; j--) { if (a[j] > max) { max = a[j]; b[j] = 0; } else { b[j] = max + 1 - a[j]; } } for (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.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Stack; import java.util.StringTokenizer; public class LuxHouse { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); Stack<Integer> stack = new Stack<Integer>(); int[] a = new int[n]; for(int i = 0 ; i < n; i++) a[i] = in.nextInt(); int max = a[n-1]; stack.push(0); for(int i = n-2; i >= 0; i--){ stack.push(a[i] > max ? 0 : max - a[i] + 1); max = max < a[i] ? a[i] : max; } while(!stack.isEmpty()) out.print(stack.pop() + " "); } } 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
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class cf { public static void main(String[] args) throws Exception { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] a=new int[n]; int[] ans=new int[n]; for(int i=0;i<n;i++) a[i]=in.nextInt(); int max=a[n-1]; for(int i=n-2;i>=0;i--) { int t=max-a[i]; if(t>=0) { ans[i]=t+1; } else { ans[i]=0; max=a[i]; } } for(int i=0;i<n;i++) System.out.print(ans[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
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+=anss[i]+" "; } //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
n = int(input()) #n, m = map(int, input().split()) #s = input() c = list(map(int, input().split())) a = [0] * n l = c[-1] for i in range(n - 2, -1, -1): if c[i] > l: l = c[i] else: a[i] = l - c[i] + 1 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
def GetLuxHts(hts): heights = [] for i in hts: heights.append(int(i)) lux_hts_revrsd = '' lux_hts = '' max_height = heights[len(hts)-1] for i in range(len(hts)-1,-1,-1): if heights[i] <= max_height and i!=len(hts)-1: diff = max_height - heights[i] + 1 #print diff lux_hts_revrsd += str(diff) + ' ' else: max_height = heights[i] lux_hts_revrsd += '0' + ' ' #print max_height lux_hts_copy = lux_hts_revrsd.split() for i in range(len(lux_hts_copy)-1,-1,-1): lux_hts += lux_hts_copy[i]+' ' return lux_hts def main(): n = int(raw_input()) hts = raw_input().split() print GetLuxHts(hts) 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.io.*; import java.util.*; public class b1 { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int h[] = new int[n]; for (int i = 0; i < n; i++) { h[i] = in.nextInt(); } int ans[] = new int[n]; int max = 0; for (int i = n - 1; i > -1; i--) { ans[i] = Math.max(max - h[i] + 1, 0); max = Math.max(max, h[i]); } for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } 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
n = int(input()) array = list(map(int, input().split())) maxH = -1 luxury = list() for i in range(n - 1, -1, -1): luxury.append(max(maxH + 1 - array[i], 0)) maxH = max(maxH, array[i]) print(*luxury[::-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()) a = list(map(int, input().split())) b = [0] * n ma = a[-1] for i in range(n - 2, -1, -1): if a[i] <= ma: b[i] = ma - a[i] + 1 else: ma = a[i] print(*b)
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 n, i, a[100001], b[100001], k; int main() { cin >> n; for (; i < n; i++) cin >> a[i]; while (i--) { k = max(k, a[i]); b[i] = k; } for (i = 0; i < n; i++) cout << (b[i] == b[i + 1] ? b[i] - a[i] + 1 : 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(): input() hh = list(map(int, input().split()))[::-1] h0 = 0 for i, h in enumerate(hh): if h0 < h: h0 = h hh[i] = 0 else: hh[i] = h0 + 1 - h print(*hh[::-1]) if __name__ == '__main__': main()
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; const int MAXN = 1e5 + 10; int n, h[MAXN], mx[MAXN]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = n; i >= 0; i--) mx[i] = max(h[i + 1], mx[i + 1]); for (int i = 0; i < n; i++) cout << max(h[i], mx[i] + 1) - h[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; cin >> n; vector<long long> a(n), res(n); for (int i = 0; i < n; i++) cin >> a[i]; long long mx = INT_MIN; for (int i = n - 1; i >= 0; i--) { res[i] = (mx >= a[i] ? mx - a[i] + 1 : 0); mx = max(mx, a[i]); } for (int i = 0; i < n; i++) { cout << res[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
import java.io.*; import java.util.*; public class Main { public static void main(String[] args)throws IOException{ PrintWriter pw = new PrintWriter(System.out); InputReader in = new InputReader(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i]=in.nextInt(); } int[] max = new int[n]; for(int i=n-1;i>=0;i--){ if(i==n-1) max[i]=Integer.MIN_VALUE; else max[i] = Math.max(max[i+1],a[i+1]); } for(int i=0;i<n;i++){ if(a[i]>max[i]) pw.print("0 "); else pw.print((max[i]-a[i]+1)+" "); } pw.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
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; scanf("%d", &n); int h[n], freq[n], mflr = 0; for (int i = 0; i < n; i++) scanf("%d", &h[i]); for (int i = n - 1; i >= 0; i--) { if (h[i] > mflr) { freq[i] = 0; mflr = h[i]; } else freq[i] = mflr + 1 - h[i]; } for (int i = 0; i < n; i++) printf("%d ", freq[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 = 1007, K = 1007, INF = LONG_MAX; int n, a[150000], i, j, m[150000]; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } int mx = 0; for (i = n - 1; i >= 0; i--) { mx = max(mx, a[i]); m[i] = mx; } for (i = 0; i < n; i++) { cout << max(0, m[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
#include <bits/stdc++.h> int a[1000005]; int b[100005]; int main() { int n; scanf("%d", &n); int i; for (i = 1; i <= n; i++) scanf("%d", &a[i]); int ma = 0; for (i = n; i >= 1; i--) { if (a[i] > ma) { ma = a[i]; b[i] = 0; } else { b[i] = ma + 1 - a[i]; } } for (i = 1; i <= n; i++) printf("%d ", 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.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class Main { //http://codeforces.com/problemset/problem/496/A static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static void input(int a[], int n) throws IOException { for (int i = 0; i < n; ++i) a[i] = sc.nextInt(); } public static void main(String[] args) throws IOException { // -------------------- Main ------------------ int n = sc.nextInt() , a[] = new int[n] , b[] = new int[n] , max = 0; input(a ,n ); for(int i = n-2 ; i >-1 ; i--) max = b[i] = Math.max(a[i+1] , max ) ; for (int i = 0; i < b.length; i++) pw.print(Math.max(b[i] - a[i] +1 , 0 )+" "); pw.flush();pw.close(); } } 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
/* * Author- Kishan_25 * BTech 2nd Year DAIICT */ import java.io.*; import java.math.*; import java.util.*; import javax.print.attribute.SetOfIntegerSyntax; public class code { private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static long count = 0,mod=1000000007; private static TreeSet<Integer>ts[]=new TreeSet[200000]; private static HashSet hs=new HashSet(); public static void main(String args[]) throws Exception { InputReader(System.in); pw = new PrintWriter(System.out); //ans(); soln(); pw.close(); } public static long gcd(long x, long y) { if (x == 0) return y; else return gcd( y % x,x); } private static int BinarySearch(int a[], int low, int high, int target) { if (low > high) return -1; int mid = low + (high - low) / 2; if (a[mid] == target) return mid; if (a[mid] > target) high = mid - 1; if (a[mid] < target) low = mid + 1; return BinarySearch(a, low, high, target); } public static String reverseString(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return (sb.toString()); } public static long pow(long n, long p) { if(p==0) return 1; if(p==1) return n%mod; if(p%2==0){ long temp=pow(n, p/2); return (temp*temp)%mod; }else{ long temp=pow(n,p/2); temp=(temp*temp)%mod; return(temp*n)%mod; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i]; int[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i]; int[] d = f; f = to; to = d; } return f; } public static int nextPowerOf2(final int a) { int b = 1; while (b < a) { b = b << 1; } return b; } public static boolean PointInTriangle(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) { int s = p2 * p5 - p1 * p6 + (p6 - p2) * p7 + (p1 - p5) * p8; int t = p1 * p4 - p2 * p3 + (p2 - p4) * p7 + (p3 - p1) * p8; if ((s < 0) != (t < 0)) return false; int A = -p4 * p5 + p2 * (p5 - p3) + p1 * (p4 - p6) + p3 * p6; if (A < 0.0) { s = -s; t = -t; A = -A; } return s > 0 && t > 0 && (s + t) <= A; } public static float area(int x1, int y1, int x2, int y2, int x3, int y3) { return (float) Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); } public static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } //merge Sort static long sort(int a[]) { int n=a.length; int b[]=new int[n]; return mergeSort(a,b,0,n-1);} static long mergeSort(int a[],int b[],long left,long right) { long c=0;if(left<right) { long mid=left+(right-left)/2; c= mergeSort(a,b,left,mid); c+=mergeSort(a,b,mid+1,right); c+=merge(a,b,left,mid+1,right); } return c; } static long merge(int a[],int b[],long left,long mid,long right) {long c=0;int i=(int)left;int j=(int)mid; int k=(int)left; while(i<=(int)mid-1&&j<=(int)right) { if(a[i]>a[j]) {b[k++]=a[i++]; } else { b[k++]=a[j++];c+=mid-i;}} while (i <= (int)mid - 1) b[k++] = a[i++]; while (j <= (int)right) b[k++] = a[j++]; for (i=(int)left; i <= (int)right; i++) a[i] = b[i]; return c; } public static boolean isSubSequence(String large, String small, int largeLen, int smallLen) { //base cases if (largeLen == 0) return false; if (smallLen == 0) return true; // If last characters of two strings are matching if (large.charAt(largeLen - 1) == small.charAt(smallLen - 1)) isSubSequence(large, small, largeLen - 1, smallLen - 1); // If last characters are not matching return isSubSequence(large, small, largeLen - 1, smallLen); } // To Get Input // Some Buffer Methods public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private static int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } private static int[][] next2dArray(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = nextInt(); } } return arr; } private static char[][] nextCharArray(int n,int m){ char [][]c=new char[n][m]; for(int i=0;i<n;i++){ String s=nextLine(); for(int j=0;j<s.length();j++){ c[i][j]=s.charAt(j); } } return c; } private static long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private static void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(boolean[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } //----------------------------------------My Code------------------------------------------------// private static void soln() { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = in.nextInt(); } int max = 0; int temp[] = new int[n]; for(int i = n-1;i>=0;i--){ if(arr[i] > max){ temp[i] = arr[i]; max = arr[i]; }else{ temp[i] = max; } } for(int i = 0;i<n-1;i++){ if(arr[i] > temp[i+1]){ System.out.print(0 + " "); }else{ System.out.print(temp[i+1]-arr[i]+1 + " "); } } System.out.print(0); }//-----------------------------------------The End--------------------------------------------------------------------------// } class node{ int p; int v; } class Pair implements Comparable<Pair>{ long id; long w; long h; Pair(long id,long w,long h){ this.id=id; this.w=w; this.h=h; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub // Sort in increasing order if(w>o.w) return 1; else if(w<o.w) return -1; else{ if(h>o.h){ return 1; }else if(o.h>h) return -1; else return 0; } } } class Graph{ private static int V,level[][],count=-1,lev_dfs[],degree=0,no_vert_conn_comp=0; private Stack <Integer>st=new Stack(); private static LinkedList<Integer > adj[]; private boolean[][] Visite; private static boolean [] Visited; private static TreeSet<Integer> ts=new TreeSet(); Graph(int V){ V++; this.V=(V); adj=new LinkedList[V]; Visite=new boolean[100][100]; Visited=new boolean[V]; level=new int[100][100]; lev_dfs=new int[V]; for(int i=0;i<V;i++) adj[i]=new LinkedList<Integer>(); } void addEdge(int v,int w){ if(adj[v]==null){ adj[v]=new LinkedList(); } if(adj[w] == null){ adj[w] = new LinkedList(); } adj[v].add(w); adj[w].add(v); } public static int conCop(int startVert){ Visited=new boolean[V]; int totalcon = 1; Queue<Integer> q=new LinkedList<Integer>(); q.add(startVert); Visited[startVert] = true; while(!q.isEmpty()){ int top=q.poll(); Iterator<Integer> i= adj[top].listIterator(); while(i.hasNext()){ int n=i.next(); if(!Visited[n]){ q.add(n); Visited[n]=true; totalcon++; } } } q.clear(); return totalcon; } public static int NoConEdge(int startVert){ Visited=new boolean[V]; Queue<Integer> q=new LinkedList<Integer>(); q.add(startVert); Visited[startVert] = true; long ed = adj[startVert].size(); while(!q.isEmpty()){ int top=q.poll(); Iterator<Integer> i= adj[top].listIterator(); while(i.hasNext()){ int n=i.next(); if(!Visited[n]){ q.add(n); Visited[n]=true; ed+= adj[n].size(); } } } q.clear(); return (int)ed/2; } public static int BFS2(int startVert,int dest){ Visited=new boolean[V]; for(int i=1;i<V;i++){ lev_dfs[i]=-1; } Queue<Integer> q=new LinkedList<Integer>(); q.add(startVert); lev_dfs[startVert]=0; while(!q.isEmpty()){ int top=q.poll(); Iterator<Integer> i= adj[top].listIterator(); while(i.hasNext()){ int n=i.next(); if(!Visited[n]){ q.add(n); Visited[n]=true; lev_dfs[n]=lev_dfs[top]+1; if(n==dest){ q.clear(); return lev_dfs[n]; } } } } q.clear(); return -1; } public int getEd(){ return degree/2; } public void get(int from,int to){ int h=lev_dfs[from]-lev_dfs[to]; if(h<=0){ System.out.println(-1); }else{ System.out.println(h-1); } } private static boolean check(int x,int y,char c[][]){ if((x>=0 && y>=0) && (x<c.length && y<c[0].length) && c[x][y]!='#'){ return true; } return false; } public int BFS(int x,int y,int k,char[][] c) { LinkedList<Pair> queue = new LinkedList<Pair>(); //Visited[s]=true; // queue.add(new Pair(x,y)); int count=0; level[x][y]=-1; c[x][y]='M'; while (!queue.isEmpty()) { Pair temp = queue.poll(); //x=temp.idx; //y=temp.val; c[x][y]='M'; // System.out.println(x+" "+y+" ---"+count); count++; if(count==k) { for(int i=0;i<c.length;i++){ for(int j=0;j<c[0].length;j++){ if(c[i][j]=='M'){ System.out.print("."); } else if(c[i][j]=='.') System.out.print("X"); else System.out.print(c[i][j]); } System.out.println(); } System.exit(0); } // System.out.println(x+" "+y); // V--; } return V; } private void getAns(int startVertex){ for(int i=0;i<adj[startVertex].size();i++){ int ch=adj[startVertex].get(i); for(int j=0;j<adj[ch].size();j++){ int ch2=adj[ch].get(j); if(adj[ch2].contains(startVertex)){ System.out.println(startVertex+" "+ch+" "+ch2); System.exit(0); } } } } public long dfs(int startVertex){ // getAns(startVertex); if(!Visited[startVertex]) { return dfsUtil(startVertex,Visited); //return getAns(); } return 0; } private long dfsUtil(int startVertex, boolean[] Visited) {//0-Blue 1-Pink int c=1; long cout=0; degree=0; Visited[startVertex]=true; lev_dfs[startVertex]=1; st.push(startVertex); while(!st.isEmpty()){ int top=st.pop(); int child=adj[top].size(); if(top!=startVertex) child--; ts.add(top); Iterator<Integer> i=adj[top].listIterator(); degree+=adj[top].size(); while(i.hasNext()){ int n=i.next(); if( !Visited[n]){ Visited[n]=true; st.push(n); if(adj[n].size()-1>child) cout++; c++; lev_dfs[n]=lev_dfs[top]+1; } } } return cout; // System.out.println("NO"); // return c; } } class Dsu{ private int rank[], parent[] ,n; Dsu(int size){ this.n=size+1; rank=new int[n]; parent=new int[n]; makeSet(); } void makeSet(){ for(int i=0;i<n;i++){ parent[i]=i; } } int find(int x){ if(parent[x]!=x){ parent[x]=find(parent[x]); } return parent[x]; } void union(int x,int y){ int xRoot=find(x); int yRoot=find(y); if(xRoot==yRoot) return; if(rank[xRoot]<rank[yRoot]){ parent[xRoot]=yRoot; }else if(rank[yRoot]<rank[xRoot]){ parent[yRoot]=xRoot; }else{ parent[yRoot]=xRoot; rank[xRoot]++; } } } class Heap{ public static void build_max_heap(long []a,int size){ for(int i=size/2;i>0;i--){ max_heapify(a, i,size); } } private static void max_heapify(long[] a,int i,int size){ int left_child=2*i; int right_child=(2*i+1); int largest=0; if(left_child<size && a[left_child]>a[i]){ largest=left_child; }else{ largest=i; } if(right_child<size && a[right_child]>a[largest]){ largest=right_child; } if(largest!=i){ long temp=a[largest]; a[largest]=a[i]; a[i]=temp; max_heapify(a, largest,size); } } private static void min_heapify(int[] a,int i){ int left_child=2*i; int right_child=(2*i+1); int largest=0; if(left_child<a.length && a[left_child]<a[i]){ largest=left_child; }else{ largest=i; } if(right_child<a.length && a[right_child]<a[largest]){ largest=right_child; } if(largest!=i){ int temp=a[largest]; a[largest]=a[i]; a[i]=temp; min_heapify(a, largest); } } public static void extract_max(int size,long a[]){ if(a.length>1){ long max=a[1]; a[1]=a[a.length-1]; size--; max_heapify(a, 1,a.length-1); } } } class MyComp implements Comparator<node>{ @Override public int compare(node o1, node o2) { if(o1.v>o2.v){ return 1; }else if(o1.v<o2.v){ return -1; } return 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
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] = max(h_max[i] - h[i], 0) res[0] = max(h_max[0] - h[0], 0) print(" ".join(map(str, 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
#include <bits/stdc++.h> using namespace std; long long a[100008], a2[100008], n; int main() { cin >> n; for (long int i = 1; i <= n; i++) cin >> a[i]; long long maxi = a[n]; for (long i = n - 1; i >= 1; i--) { if (a[i] > maxi) { a2[i] = a[i] - 1; maxi = a[i]; } else if (a[i] == maxi) a2[i] = a[i]; else a2[i] = maxi; } for (long i = 1; i <= n - 1; i++) cout << a2[i] - a[i] + 1 << " "; cout << 0 << 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; import java.util.Stack; import java.util.StringTokenizer; public class B581Alt { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ 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; } } public static void main(String[] args) { FastReader input=new FastReader(); int n=input.nextInt(); Stack<Integer> stack=new Stack<>(); int[] array=new int[n]; for(int i=0;i<array.length;++i){ array[i]=input.nextInt(); } int bigger=array[array.length-1]; int biggerIndex=array.length-1; int deff=0; for(int i=array.length-1;i>-1;--i){ if(array[i]>bigger) { bigger=array[i]; biggerIndex=i; } deff=bigger-array[i]; if(deff!=0)deff++; else if(biggerIndex!=i)deff++; stack.push(deff); } while(!stack.isEmpty()){ System.out.print(stack.pop()+" "); } } }
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()) lst = list(map(int, input().split())) res = [0] k = 0 for i in range(-2, -len(lst)-1, -1): if k < lst[i+1]: k = lst[i+1] if k >= lst[i]: res.append(k-lst[i]+1) else: res.append(0) 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 = [int(_) for _ in input().split()] suffix_max = [0] * (N + 1) for i in range(N - 1, -1, -1): suffix_max[i] = max(H[i], suffix_max[i + 1]) ans = [0] * N for i in range(N): ans[i] = max(suffix_max[i + 1] - H[i] + 1, 0) print(' '.join(str(_) for _ in 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
n=int(input()) l=list(map(int,input().split())) m=l[-1] z=[0]*n for i in range(n-2,-1,-1): if l[i]>m: m=l[i] else: z[i]=m-l[i]+1 print(*z)
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()) lst = list(map(int, input().strip().split(' '))) #n,m = map(int, input().strip().split(' ')) i=n-1 l2=[] m=0 while(i>=0): if lst[i]>m: l2.append(0) m=lst[i] elif m==lst[i]: l2.append(1) else: l2.append(m+1-lst[i]) i-=1 for j in range(n): print(l2[n-j-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
import java.io.*; import java.util.*; import java.util.stream.Collectors; public class Solution { public static void main(String[] args) throws IOException { IO io = new IO(); int n = io.getInt() ; List<Integer> ls = io.getIntegerArray(n) ; List<Integer> res = new ArrayList<>() ; int[] post = new int[n] ; post[n - 1] = ls.get(n - 1) ; for(int i = n - 2 ; i >= 0 ; i--) post[i] = Math.max(ls.get(i + 1), post[i + 1]) ; for(int i = 0 ; i < n - 1 ; i++) res.add(Math.max(0, post[i] - ls.get(i) + 1)) ; res.add(0) ; System.out.println(Utils.join(res, " ")) ; } } class Utils { public static class IndexedElement<T> { int idx; T val; public IndexedElement(int idx, T val) { this.idx = idx; this.val = val; } } public static <T> ArrayList<IndexedElement<T>> getIndexedArray(ArrayList<T> ip) { ArrayList<IndexedElement<T>> op = new ArrayList<>(); for (int i = 0; i < ip.size(); i++) { op.add(new IndexedElement<>(i, ip.get(i))); } return op; } public static <T extends Comparable<T>> int getMax(T... arr) { if (arr.length == 0) return -1; int idx = 0; for (int i = 1; i < arr.length; i++) { if (arr[idx].compareTo(arr[i]) > 0) idx = i; } return idx; } public static <T> String join(List<T> ls, String delim) { StringJoiner sj = new StringJoiner(delim); for (T a : ls) sj.add(a.toString()); return sj.toString(); } public static <T> void print2DArray(List<List<T>> mat) { for (List<T> m : mat) { System.out.println("{ " + join(m, ", ") + " }"); } } } class IO { private BufferedReader br = null; private StringTokenizer st = null; public IO() { this(System.in); } public IO(InputStream is) { this.br = new BufferedReader(new InputStreamReader(is)); } public List<String> getStringArray(int n) throws IOException { if (n == 0) return new ArrayList<>(); if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); List<String> res = new ArrayList<>(); for (int i = 0; i < n; i++) { if (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); res.add(st.nextToken()); } return res; } public String getString() throws IOException { List<String> res = this.getStringArray(1); return res.size() == 0 ? "" : res.get(0); } public List<Integer> getIntegerArray(int n) throws IOException { if (n == 0) return new ArrayList<>(); List<String> res = getStringArray(n); return res.stream().map(Integer::parseInt).collect(Collectors.toList()); } public Integer getInt() throws IOException { List<Integer> res = this.getIntegerArray(1); return res.size() == 0 ? 0 : res.get(0); } public List<Long> getLongArray(int n) throws IOException { if (n == 0) return new ArrayList<>(); List<String> res = getStringArray(n); return res.stream().map(Long::parseLong).collect(Collectors.toList()); } public Long getLong() throws IOException { List<Long> res = this.getLongArray(1); return res.size() == 0 ? 0L : res.get(0); } public List<Double> getDoubleArray(int n) throws IOException { if (n == 0) return new ArrayList<>(); List<String> res = getStringArray(n); return res.stream().map(Double::parseDouble).collect(Collectors.toList()); } public Double getDouble() throws IOException { List<Double> res = this.getDoubleArray(1); return res.size() == 0 ? 0.0 : res.get(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 sys try: while True: n = int(input()) val = list(map(int, input().split())) maxval = 0 for i in range(n-1, -1, -1): if maxval >= val[i]: val[i] = maxval - val[i] + 1 else: maxval = val[i] val[i] = 0 print(*val) except EOFError: pass
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 flag, cnt, j, i, k, ans, maxy; long long n; int main() { cin >> n; vector<long long> v(n), temp(n), ans(n); ans[n - 1] = 0; maxy = v[n - 1]; for (i = 0; i < n; i++) { cin >> v[i]; temp[i] = v[i]; } for (i = n - 2; i >= 0; i--) { if (maxy <= v[i + 1]) maxy = v[i + 1]; ans[i] = maxy - v[i] + 1; if (ans[i] < 0) ans[i] = 0; } for (i = 0; i < n; i++) cout << ans[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 a[100010]; int m[100010]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { long long k; scanf("%lld", &k); a[i] = k; } m[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; i--) { m[i] = max(m[i + 1], a[i]); } for (int i = 0; i < n - 1; i++) { if (a[i] > m[i + 1]) { cout << 0 << " "; } else { cout << m[i + 1] - a[i] + 1 << " "; } } cout << 0 << 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, i, k = 0; cin >> n; int h[n], a[n]; for (i = 0; i < n; i++) { cin >> h[i]; } h[n] = 0; for (i = n - 1; i >= 0; i--) { k = max(k, h[i + 1]); a[i] = k; } for (i = 0; i < n; i++) { cout << max(a[i] - h[i] + 1, 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() { long int n; cin >> n; long int large = 0; vector<int> v(n); vector<int> answer(n); for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = n - 1; i >= 0; i--) { if (v[i] > large) { answer[i] = 0; large = v[i]; } else answer[i] = large - v[i] + 1; } for (auto i : answer) cout << 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
import java.util.*; public class Main{ public static void main(String [] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int a[]=new int[n]; int i; for(i=0;i<n;i++) { a[i]=scan.nextInt(); } int b[]=new int[n]; b[n-1]=0; int max=a[n-1]; for(i=n-2;i>=0;i--) { if(a[i]<=max) { b[i]=max-a[i]+1; } else{ max=a[i]; } } for(i=0;i<n;i++) { System.out.print(b[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; int main() { int n, i, a[100001], max[100001], maxx = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; max[i] = 0; } for (i = n - 1; i >= 0; i--) { if (a[i] > maxx) { maxx = a[i]; max[i] = maxx - 1; continue; } max[i] = maxx; } for (i = 0; i < n; i++) { if (i != 0) cout << " "; if (i == n - 1) cout << "0"; else if (a[i] - max[i] <= 0) cout << max[i] - a[i] + 1; else cout << "0"; } 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 conversion(string p) { int o; o = atoi(p.c_str()); return o; } string toString(int h) { stringstream ss; ss << h; return ss.str(); } long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); } int toi(string p) { int x; istringstream in(p); in >> x; return x; } int main() { cin.sync_with_stdio(false); long long n, m, x, maxi = 0; cin >> n; vector<long long> M; map<long long, int> P; long long arr[n + 2]; memset(arr, 0, sizeof arr); for (int i = 0; i < n; i++) { cin >> x; M.push_back(x); P[x]++; } for (int i = n - 1; i >= 0; i--) arr[i] = max(arr[i + 1], M[i]); for (int i = 0; i < n; i++) { if (arr[i] == M[i] && P[M[i]] == 1) cout << 0 << " "; else cout << (abs(arr[i] - M[i]) + 1LL) << " "; P[M[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; const int maxn = 1e5 + 10; int a[maxn], maxx[maxn]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } maxx[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; i--) { maxx[i] = max(maxx[i + 1], a[i]); } for (int i = 0; i < n; i++) { printf("%d ", max(0, maxx[i + 1] - a[i] + 1)); } puts(""); 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.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class LuxuriousHouses implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out), true); public void solve() { int n = in.ni(); int[] houses = new int[n]; for (int i = 0; i < n; i++) { houses[i] = in.ni(); } int[] result = new int[n]; int max = houses[n - 1]; result[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (houses[i] <= max) { result[i] = max - houses[i] + 1; } else { max = houses[i]; result[i] = 0; } } for (int i : result) { out.print(i + " "); } out.println(); } @Override public void close() throws IOException { in.close(); out.close(); } 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) { new LuxuriousHouses().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
#!/usr/bin/env python from sys import stdin as cin def main(): n = int(next(cin)) a = map(int, next(cin).split()) b = [0] * len(a) for i in range(len(a)-1, 0, -1): b[i-1] = max(a[i], b[i]) return [max(b[i]-a[i]+1, 0) for i in range(len(a))] print ' '.join(map(str, 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
#BeEf_Killer_______ # / _ _ \ # / (.) (.) \ # ( _________ ) # \`-V-|-V-'/ # \ | / # \ ^ / # \ \ # \ `-_ # `-_ -_ # -_ -_ # _- _- # _- _- # _- _- # _- _- # -_ -_ # -_ -_ # -_ -_ # -_ -_ # _- _- # ,-=:_-_-_-_ _ _-_-_-_:=-. # /=I=I=I=I=I=I=I=I=I=I=I=I=\ # |=I=I=I=I=I=I=I=I=I=I=I=I=I=| # |I=I=I=I=I=I=I=I=I=I=I=I=I=I| # \=I=I=I=I=I=I=I=I=I=I=I=I=I=/ # \=I=I=I=I=I=I=I=I=I=I=I=I=/ # \=I=I=I=I=I=I=I=I=I=I=I=/ # \=I=I=I=I=I=I=I=I=I=I=/ # \=I=I=I=I=I=I=I=I=I=/ # `=================' #To(o_C)lose n = int(raw_input()); inp = raw_input().split(); inp.reverse(); mx = -1; ans=[]; for el in inp: x = int(el); if mx == -1 or x > mx: ans.append('0') else: ans.append(str(mx - x + 1)) mx = max(mx, x); ans.reverse(); print " ".join(ans);
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()) b = list(map(int, input().split())) mark = 0 answer = list() for x in range(a): if b[a - x - 1] > mark: answer.append(0) mark = b[a - x - 1] else: answer.append(mark - b[a - x - 1] + 1) for x in range(a): print(answer[a - x - 1], "", end="", flush=True)
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] = {0}, b[100001] = {0}; int i, k, l, m, n, o, p; int main() { scanf("%d\n", &n); for (int i = 0; i < n; i++) { scanf("%d ", &a[i]); } b[n] = a[n - 1] - 1; for (int i = n - 1; i > -1; i--) { b[i] = max(b[i + 1], a[i + 1]); } for (int i = 0; i < n; i++) { printf("%d ", max(0, b[i] - a[i] + 1)); } exit(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.*; import java.util.*; public class P581B{ static int[] run(int[] ar){ int[] max_ar = new int[ar.length]; for (int i=ar.length-1; i>-1; i--) { if(i==ar.length-1) max_ar[i] = 0; else{ if(max_ar[i+1]>ar[i+1]) max_ar[i] = max_ar[i+1]; else max_ar[i] = ar[i+1]; } } int[] res = new int[ar.length]; for (int i=0; i<ar.length; i++) { if(ar[i]>max_ar[i]) res[i] = 0; else res[i] = max_ar[i] - ar[i] + 1; } return res; } static void print(int[] ar){ for(int i=0; i<ar.length-1; i++){ System.out.print(ar[i] + ","); } System.out.println(ar[ar.length-1]); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); String s = sc.nextLine(); String[] s_ar = s.split(" "); int[] int_ar = new int[s_ar.length]; for (int i=0; i<s_ar.length; i++) { int_ar[i] = Integer.parseInt(s_ar[i].trim()); } int[] res_ar = run(int_ar); for (int i=0; i<res_ar.length-1; i++) { System.out.print(res_ar[i] + " "); } System.out.println(res_ar[res_ar.length-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
n = input() a = map(int, raw_input().split()) m = [] mxm = 0 for i in range(n-1, -1, -1): mxm = max(a[i], mxm) m.append(mxm) for i in range(n-1): if a[i]>m[n-2-i]: print 0, else: print m[n-2-i]-a[i]+1, 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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ar[n], br[n], cr[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; br[i] = 0; cr[i] = -1; } int max = ar[n - 1]; br[n - 1] = max; cr[n - 1] = 0; for (int i = n - 1; i >= 0; i--) { if (max < ar[i]) { max = ar[i]; cr[i] = 0; } br[i] = max; } for (int i = 0; i < n; i++) { if (cr[i] != 0) { cr[i] = br[i] - ar[i] + 1; } cout << cr[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 s, i, y; int f = 0; scanf("%d", &s); int n[s]; int r[s]; for (i = 0; i < s; i++) { scanf("%d", &n[i]); } int g = n[s - 1]; for (i = s - 2; i >= 0; i--) { if (n[i] > g) { g = n[i]; r[i] = 0; } else { r[i] = (g - n[i]) + 1; } } r[s - 1] = 0; for (i = 0; i < s; i++) { printf("%d ", r[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.ArrayList; import java.util.Scanner; public class DevelopingSkill { public static void main(String args[]) throws IOException { @SuppressWarnings("resource") Scanner sc=new Scanner(System.in); int totalHouses = sc.nextInt(); int inparr[] = new int[totalHouses]; ArrayList<Integer> inparray = new ArrayList<Integer>(totalHouses); for(int i=0;i<totalHouses;i++){ inparr[i]=sc.nextInt(); } int maxHt=inparr[totalHouses-1]; int out[] = new int[totalHouses]; out[totalHouses-1]=0; for(int j=totalHouses-2;j>=0;j--) { if(inparr[j] > maxHt) { maxHt=inparr[j]; out[j]=0; } else { out[j]=maxHt-inparr[j]+1; } } for(int output=0;output<totalHouses;output++) { System.out.print(out[output] + " "); } } }
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.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner (System.in); int n=s.nextInt(); int arr[] = new int[n]; int ans[] =new int[n]; for (int i=0;i<n;i++) { arr[i]=s.nextInt(); } int mx = arr[n-1]; ans[n-1]=0; for (int i=n-2;i>=0;i--) { if (mx<arr[i]) { mx=arr[i]; ans[i]=0; } else ans[i]=mx-arr[i]+1; } for (int i=0;i<n;i++) System.out.print(ans[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
import java.util.Scanner; import java.util.Stack; public class q2 { public static void main(String aegs[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; int ng[]=new int[n]; Stack<Integer> st=new Stack<Integer>(); for(int i=0;i<n;i++) a[i]=s.nextInt(); int max=0; for(int i=n-1;i>=0;i--) { if(i==n-1) { ng[i]=0; max=a[i]; } else { if(a[i]>max) { max=a[i]; ng[i]=0; } else { ng[i]=max; } } } for(int i=0;i<n;i++) { if(ng[i]!=0) System.out.print(ng[i]+1-a[i]+" "); else System.out.print(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.*; import java.io.*; public class B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] height = new int[n]; int[] max = new int[n]; for (int i = 0; i < n; i++) { height[i] = in.nextInt(); } max[n-1] = height[n-1]; for (int i = n-2; i >= 0; i--) { max[i] = Math.max(height[i], max[i+1]); } for (int i = 0; i < n-1; i++) { int diff = height[i] - max[i+1]; if (diff > 0) { System.out.print(0 + " "); } else { System.out.print((-diff+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.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.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Morgrey */ 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.readInt(); int[] h = IOUtils.readIntArray(in, n); int maxPrev = h[n - 1]; int[] ans = new int[n]; ans[n - 1] = 0; for (int i = n - 2; i > -1; i--) { if (h[i] <= maxPrev) { ans[i] = Math.abs(maxPrev - h[i]) + 1; } maxPrev = Math.max(maxPrev, h[i]); } for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } } } static class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void close() { writer.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
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") bool isPrime(long long int n) { for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } bool isPowerOfTwo(long long int x) { return (x && (!(x & (x - 1)))); } long long int countDigit(long long int num) { return (floor(log10(num)) + 1); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int test_case = 1; while (test_case--) { long long int n; cin >> n; long long int arr[n]; for (long long int i = 0; i < n; i++) { cin >> arr[i]; } long long int maxx = 0; vector<long long int> ans; for (long long int i = n - 1; i >= 0; i--) { if (arr[i] <= maxx) { ans.push_back(maxx - arr[i] + 1); } else { ans.push_back(0); } maxx = max(arr[i], maxx); } reverse(ans.begin(), ans.end()); for (auto it : ans) { cout << it << " "; } } 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.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()); 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
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class CF581B { public static void main(String[] args)throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for(String ln;(ln=in.readLine())!=null;){ int N = Integer.parseInt(ln); long[] m = new long[N]; long maxH = -1; long resp[] = new long[N]; StringTokenizer st = new StringTokenizer(in.readLine()); for(int i=0;i<N;++i){ m[i]=Long.parseLong(st.nextToken()); } for(int i=N-1;i>=0;--i){ if(m[i]>maxH){ maxH = m[i]; }else if(i<N-1){ resp[i] = (maxH+1)-m[i]; } } for(int i=0;i<N;++i){ System.out.print(resp[i]+((i+1==N)?"":" ")); } System.out.println(); } } }
JAVA