Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i = 0, l = -2, k = INT_MAX, j, a = 0, f = 0, c = 0, b = 0, x = 100000000007, d, y, m, zero = 0, one = 0, z; string s = "qwertyuiopasdfghjkl;zxcvbnm,./", mar, maria; char cha; vector<long long int> vc, vct, vctt; vector<string> ch; vector<pair<int, int>> pa; map<long long int, int> mp; double num, nr, radi; stack<char> bra, bra2; cin >> n; for (i = 0; i < n; i++) { cin >> k; vc.push_back(k); } a = vc[n - 1]; vct.push_back(0); for (i = n - 2; i >= 0; i--) { if (a >= vc[i]) vct.push_back((a - vc[i]) + 1); else { a = vc[i]; vct.push_back(0); } } for (i = vct.size() - 1; i >= 0; i--) cout << vct[i] << " "; cout << endl; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) arr = list(map(int, input().split())) maxarr = [0]*n maxarr[n-1] = arr[-1] curmax = maxarr[-1] index = n-1 for val in arr[::-1]: maxarr[index] = max(val, curmax) curmax = maxarr[index] index -= 1 for index, val in enumerate(arr): out = 0 if maxarr[index] == val: if index + 1 < n and maxarr[index+1] == maxarr[index]: out = 1 else: out = maxarr[index]-val+1 print(out, end=' ') print()
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; long long h[100000], ans[100000]; cin >> n; for (int i = 0; i < n; i++) cin >> h[i]; ans[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (h[i] <= h[i + 1]) ans[i] = h[i + 1] - h[i] + 1; else ans[i] = 0; h[i] = max(h[i], h[i + 1]); } for (int 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
n=int(input()) h=list(map(int,input().split())) l=[] m=h.pop() l.append(0) while h: if h[-1]<=m: l.append(m-h[-1]+1) m=max(m,h.pop()) else: l.append(0) m=max(m,h.pop()) for i in range(len(l)-1,-1,-1): print(l[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
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class B { public B() { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<Integer> list = new ArrayList<Integer>(); for(int i=0; i<n; i++) { list.add(scanner.nextInt()); } StringBuilder sb = new StringBuilder(); int max = -1; for(int i=n-1; i>=0; i--) { int x = list.get(i); if(x > max) { max = x; list.set(i, 0); } else { list.set(i, max-x+1); } } for(int i=0; i<n; i++) { sb.append(list.get(i)).append(" "); } System.out.println(sb.toString().trim()); } public static void main(String[] args) { new B(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int arr[n]; for (int(i) = 0; (i) < (n); ++(i)) { cin >> arr[i]; } long long int m = arr[n - 1]; long long int out[n]; out[n - 1] = 0; for (int(i) = (n - 2); (i) >= (0); --(i)) { if (arr[i] > m) { out[i] = 0; } else { out[i] = m - arr[i] + 1; } m = max(m, arr[i]); } for (int(i) = 0; (i) < (n); ++(i)) { printf("%lld ", out[i]); } cout << '\n'; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.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()); if((i+1)%1000==0) out.flush(); } 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
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
import java.util.Arrays; import java.util.Scanner; import java.util.Vector; public class Luxurious_Houses { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Vector<Object> v = new Vector<>(); long[] arr = new long[n]; // long[] out = new long[n]; for (int i = 0; i < arr.length; i++) { arr[i] = in.nextInt(); } long max = arr[arr.length-1]; v.add(0); for (int i = arr.length-2; i >= 0; i--) { if(arr[i] <= max){ v.add(max-arr[i]+1); } else{ v.add(0); max=arr[i]; } } for(int i = v.size()-1;i>=0;i--){ System.out.print(v.get(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 ar[100001]; int maxi[100001]; int main() { int n, i; scanf("%d", &n); maxi[n] = 0; for (int(i) = (0); (i) < (n); ++(i)) { scanf("%d", &ar[i]); } for (int(i) = (n - 1); (i) >= (0); --(i)) { maxi[i] = max(maxi[i + 1], ar[i]); } for (int(i) = (0); (i) < (n); ++(i)) { if (maxi[i + 1] < ar[i]) printf("0 "); else printf("%d ", maxi[i + 1] - ar[i] + 1); } }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.PrintWriter; import java.util.Scanner; public class luxuryhooses { public static void main(String[] args) { Scanner scan = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = scan.nextInt(); int[] houses = new int[n]; for(int i = 0; i < n; i++) houses[i] = scan.nextInt(); int[] ans = new int[n]; int maximali = 0; boolean w = false; for(int i = n-1; i >= 0; i--){ if(maximali < houses[i]){ ans[i]--; } maximali = Math.max(maximali, houses[i]); ans[i]++; if(houses[i] < maximali) ans[i] = maximali-houses[i]+1; } for(int i = 0; i < n; i++){ out.print(ans[i] + " "); } out.flush(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; multiset<int> ss; int a[100007]; int main() { int n; while (cin >> n) { ss.clear(); for (int i = 1; i <= n; i++) { cin >> a[i]; ss.insert(a[i]); } int t, v; for (int i = 1; i <= n; i++) { if (i == n) { if (i == 1) puts("0"); else puts(" 0"); continue; } ss.erase(ss.lower_bound(a[i])); if ((*(ss.rbegin())) >= a[i]) v = (*(ss.rbegin())) - a[i] + 1; else v = 0; if (i == 1) printf("%d", v); else printf(" %d", v); } } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) h = [int(i) for i in input().split()] l = [] l_2 = [0] * n l_2[n-1] = h[n-1] for i in range (n-2, 0, -1): l_2[i] = max( l_2[i+1], h[i]) l_2 = l_2 + [0] l = [0] * n for i in range (n): m = l_2[i+1] g = max ( m - h[i] +1, 0) l[i] = g for i in l: print(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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Samin Arefin */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = in.nextInt(); int MAX = arr[n - 1]; List<String> ans = new ArrayList<>(); ans.add(String.valueOf(0)); for (int i = n - 2; i >= 0; i--) { if (arr[i] > MAX) { ans.add(String.valueOf(0)); MAX = Math.max(MAX, arr[i]); } else { ans.add(String.valueOf(MAX - arr[i] + 1)); MAX = Math.max(MAX, arr[i]); } } for (int i = ans.size() - 1; i > 0; i--) out.print(ans.get(i) + " "); out.println(ans.get(0)); } } static class InputReader extends BufferedReader { private StringTokenizer token; public InputReader(InputStream stream) { super(new InputStreamReader(stream)); } public String next() { while (token == null || !token.hasMoreElements()) { try { token = new StringTokenizer(readLine(), ", "); } catch (IOException e) { e.printStackTrace(); } } return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { super(writer); } public void close() { super.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; const int MAX = 100005; int main() { int n, A[MAX], M[MAX]; while (scanf("%d", &n) == 1) { for (int i = 0; i < n; i++) scanf("%d", &A[i]); M[n - 1] = -1; for (int i = n - 2; i > -1; i--) M[i] = max(M[i + 1], A[i + 1]); for (int i = 0; i < n; i++) if (M[i] >= A[i]) printf("%d ", M[i] - A[i] + 1); else printf("0 "); 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; using namespace std; template <class T1, class T2> using p = pair<T1, T2>; template <class T1, class T2> using m = vector<pair<T1, T2>>; template <class T> using vv = vector<vector<T>>; template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "{"; for (int(i) = 0; (i) < (t.size()); ++(i)) { os << t[i] << ","; } os << "}" << endl; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } vector<string> s_p_l_i_t(const string &s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void e_r_r(vector<string>::iterator it) {} template <typename T, typename... Args> void e_r_r(vector<string>::iterator it, T a, Args... args) { if (*it == " 1" || *it == "1") cerr << endl; else cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; e_r_r(++it, args...); } const long long MOD = 1e9 + 7; bool cmp(pair<long long, long long> &a, pair<long long, long long> &b) { return a.first < b.first; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long n, curmax = 0; cin >> n; vector<long long> x(n), res(n); for (long long int i = 0; i < n; i++) cin >> x[i]; for (long long i = n - 1; i >= 0; i--) { if (curmax < x[i]) res[i] = 0; else res[i] = curmax - x[i] + 1; curmax = max(curmax, x[i]); } for (long long int i = 0; i < n; i++) cout << 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
n = int(input()) arr = list(map(int,input().split())) cur = arr[n - 1] dp= [0] * (n) for i in range(n - 2 , -1 , - 1): if arr[i] > cur : dp[i] = 0 cur = arr[i] else: dp[i] = cur - arr[i] + 1 print(*dp)
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.math.*; import java.util.*; public class LuxioriousHouses { public static void main(String[] args) { Scanner sc = new Scanner(); int n=sc.nextInt(); int[] arr=new int[n]; long[] b=new long[n]; arr=sc.nextIntArray(n); long max=arr[n-1]; b[n-1]=0; for(int i=n-2;i>=0;i--) { if(max==arr[i]) { b[i]=1; } if(max<arr[i]) { b[i]=0; } if(max>arr[i]) { b[i]=(long)(max-arr[i]+1); } max=Math.max(max, arr[i]); } for(int i=0;i<n;i++) System.out.print(b[i]+" "); } public static long pow(long x, long n, long mod) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) { if ((n & 1) != 0) { res = (res * p % mod); } } return res; } public static long gcd(long n1, long n2) { long r; while (n2 != 0) { r = n1 % n2; n1 = n2; n2 = r; } return n1; } static class Scanner { private byte[] buf = new byte[1024]; private int curChar; private int snumChars; public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public 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(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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; } public 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; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -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, a[200000], maxs[200000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } maxs[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; i--) { maxs[i] = max(maxs[i + 1], a[i]); } for (int i = 0; i < n - 1; i++) { cout << max(maxs[i + 1] - a[i] + 1, 0) << " "; } 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
import java.util.*; import java.io.*; public class Main { public static void main(String [] args)throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int array[] = new int[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0;i < n;i++){ array[i] = Integer.parseInt(st.nextToken()); } int ans [] = new int[n]; int max = array[n - 1]; for(int i = n - 2;i >= 0;i--){ if(array[i] <= max) ans[i] = max - array[i] + 1; max = Math.max(array[i],max); } StringBuilder sb = new StringBuilder(); for(int i = 0;i < n;i++){ sb.append(ans[i]); sb.append(' '); } 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, max = 0; cin >> n; vector<long long> v(n), out(n); for (int i = 0; i < n; i++) { cin >> v[i]; } max = v[n - 1]; out[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (v[i] <= max) { out[i] = (max - v[i] + 1); } else { out[i] = 0; max = v[i]; } } for (int i = 0; i < n; i++) { if (i == 0) cout << out[i]; else { cout << " " << out[i]; } } cout << endl; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) arr = [int(x) for x in input().split()] m = [0] * (n+1) ans = [0] * (n+1) for i in range(n-2,-1,-1): m[i] = max(m[i+1], arr[i+1]) ans[i] = max(m[i] - arr[i] + 1, 0) print(*ans[:-1])
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) input = list(map(int, input().split())) output = [0 for x in range(len(input))] m = 0 for i in range(n - 1, -1, -1): output[i] = max(0, m - input[i] + 1) m = max(m, input[i]) print(' '.join(str(output[i]) for i in range(len(output))))
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[100005]; int Tree[400005]; void build(int node, int left, int right) { if (right < left) return; if (left == right) { Tree[node] = A[left]; return; } build(2 * node, left, (left + right) / 2); build(2 * node + 1, (left + right) / 2 + 1, right); Tree[node] = max(Tree[2 * node], Tree[2 * node + 1]); } int query(int node, int left, int right, int L, int R) { if (left > right || left > R || right < L) return INT_MIN; if (left >= L && right <= R) return Tree[node]; int l = query(2 * node, left, (left + right) / 2, L, R); int r = query(2 * node + 1, (left + right) / 2 + 1, right, L, R); return max(l, r); } int main() { int N; cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; build(1, 0, N - 1); for (int i = 0; i < N - 1; i++) cout << max(0, query(1, 0, N - 1, i + 1, N - 1) - A[i] + 1) << " "; 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> const int mod = 1E9 + 7; const int intmax = 1E9 + 7; using namespace std; int aa[100005]; int bb[100005]; int main() { ios::sync_with_stdio(0); int test, a, b, c; int n; cin >> n; for (int i = 0; i < n; i++) cin >> aa[i]; bb[n - 1] = aa[n - 1]; for (int i = n - 2; i >= 0; i--) { bb[i] = max(aa[i], bb[i + 1]); } for (int i = 0; i < n - 1; i++) { if (aa[i] <= bb[i + 1]) { cout << bb[i] - aa[i] + 1 << " "; } else cout << "0" << " "; } 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
a = input() b = map(int,raw_input().split()) c = [] maxH = 0 for i in b[::-1]: c.append(max(0,maxH-i+1)) maxH = max(i,maxH) u = "" for i in c[::-1]: u += str(i)+" " print u
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()) a = list(map(int, input().split(' ')[:n])) b = [0 for i in range(n)] m = 0 for i in range(n-1, -1, -1): b[i] = max(0, m - a[i] + 1) m = max(m, 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
n=int(input()) l=[int(x) for x in input().split()] a=[0]*n m=l[n-1] for i in range(n-2,-1,-1): if l[i]>m: a[i]=0 m=l[i] else: a[i]=m-l[i]+1 for x in a: print(x,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 ara[100004], ara2[100004]; int main() { int n, i, j, k, l, ans = 0; cin >> n; for (i = 0; i < n; i++) cin >> ara[i]; for (i = n - 1; i >= 0; i--) { ara2[i] = max(ara2[i + 1], ara[i + 1]); } for (i = 0; i < n; i++) { ans = max(ara2[i] - ara[i] + 1, 0); if (i == n - 1) cout << ans << endl; else cout << ans << " "; } }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(),i,max = 0,z; int[] houses = new int[n]; int[] levelup = new int[n]; for(i=0;i<n;i++){ houses[i] = sc.nextInt(); } for(i=n-1;i>=0;i--){ z=0; if(houses[i]<=max) z =max-houses[i]+1; levelup[i] = z; if(houses[i]>max) max = houses[i]; } for(i=0;i<n;i++){ System.out.print(levelup[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()) l = list(map(int,input().split())) max1 = l[-1] stack = [0] for i in range(2,n+1): if l[-i] == max1: stack.append(1) elif l[-i] > max1: max1 = l[-i] stack.append(0) else: stack.append(abs(l[-i]-max1)+1) for i in range(len(stack)): print(stack.pop(),end=" ")
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) h = tuple(map(int, str.split(input()))) hm = [0] * (n) ch = 0 for i, x in enumerate(reversed(h)): hm[i] = max(0, ch - x + 1) ch = max(ch, x) print(str.join(" ", map(str, reversed(hm))))
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()) hs = list(map(int, input().split())) def solve(n, hs): dp = [0] * n zeros = [0] * n maxh = 0 for i in range(n-1, -1, -1): if hs[i] > maxh: zeros[i] = 1 maxh = hs[i] dp[i] = maxh result = [] for i in range(n): if zeros[i]: result.append(0) else: result.append(dp[i] - hs[i] + 1) return result print(*solve(n, hs))
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
__author__ = 'mowayao' n = int(raw_input()) a = map(int,raw_input().split()) M = -1 ans = [] for i in xrange(n-1,-1,-1): if M < a[i]: ans.append(0) M = a[i] elif M==a[i]: ans.append(1) else: ans.append(M-a[i]+1) for i in xrange(n-1,-1,-1): print ans[i],
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int isP(long int hj) { long int op; for (op = 2; op <= sqrt(hj); op++) { if (hj % op == 0) return 0; } return 1; } void swap(long long int *p, long long int *q) { long long int tmp = *p; *p = *q; *q = tmp; } int mind(long long int p) { int mindd = 10; while (p > 0) { if (p % 10 < mindd) mindd = p % 10; p /= 10; } return mindd; } int maxd(long long int p) { int maxdd = -1; while (p > 0) { if (p % 10 > maxdd) maxdd = p % 10; p /= 10; } return maxdd; } string fdi(int hi) { switch (hi) { case 0: return "zero"; case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; } return ""; } long long int bd(long long int mk, long long int nk) { long long int i; for (i = min(mk, nk); i >= 1; i--) { if (mk % i == 0 && nk % i == 0) return i; } return 0; } int dsm(long long int pkk) { if (pkk < 0) pkk *= -1; while (pkk > 0) { if (pkk % 10 == 8) return 1; pkk /= 10; } return 0; } void seving(int ar[], long long int n) { long long int i, j; for (i = 3; i < n; i += 2) ar[i] = 1; ar[2] = 1; for (i = 2; i < n; i++) { if (ar[i] == 1) { for (j = i * i; j < n; j += i) ar[j] = 0; } } } int tellme(int a, int b, int c) { if (c < a + b) return 1; else if (a + b == c) return 2; else return 0; } int vow(string ch) { int i; for (i = 0; i < ch.size(); i++) { if (ch[i] >= 48 && ch[i] <= 57) return 1; } return 0; } int rk(char ch1, char ch2) { char str[9] = {'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'}; int i, j; for (i = 0; i < 9; i++) { if (str[i] == ch1) break; } for (j = 0; j < 9; j++) { if (str[j] == ch2) break; } if (i > j) return 1; else return 0; } long long int gcd(long long int a, long long int b) { if (a == 0) return b; else return gcd(b % a, a); } int isT(long long int b) { long long int i, ct; ct = 0; for (i = 2; i <= (b - 1); i++) { if (b % i == 0) ct++; if (ct > 1) return 0; } if (ct == 1) return 1; else return 0; } int restalp(string st) { long int i, lt; lt = st.size(); for (i = 1; i < lt; i++) { if (st[i] >= 65 && st[i] <= 90) return 1; } return 0; } long long int rerun(long long int i, long long int a, long long int k) { if (i == k) return a; else return rerun(i + 1, a + mind(a) * maxd(a), k); } int bitt(long long int a) { long int ct; ct = 0; while (a > 0) { if (a & 1) ct++; a = a >> 1; } return ct; } int kim(long long int a, long long int b) { while (a > 0) { if (a == 1) break; else if (a % 2 == 0) a /= 2; else if (a % 3 == 0) a /= 3; else return 0; } while (b > 0) { if (b == 1) return 1; else if (b % 2 == 0) b /= 2; else if (b % 3 == 0) b /= 3; else return 0; } return 0; } int smdg(int as, int bs) { int sm2 = 0; while (as > 0) { sm2 += as % bs; as /= bs; } return sm2; } int main() { long long int a, b, c, d, p, q, g1, g2, b1, b2, d2, a1, a2, i, l, lt, n, k, j, r, col, h, f, maxl, minr, maxI, m, mov, prev, t, curr, sm, eng, ct, x, prevI, num, den, s, y, max; cin >> n; long long int A[n]; long long int B[n]; for (i = 0; i < n; i++) { cin >> A[i]; } max = -1; for (i = n - 1, j = 0; j < n; j++, i--) { if (A[i] > max) B[j] = 0; else B[j] = max + 1 - A[i]; if (A[i] > max) max = A[i]; } for (i = n - 1; i >= 0; 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
n = input() h = map(int, input().split()[::-1]) m_h = 0 res = [] for h in h: if m_h < h: res.append('0') m_h = h else: res.append(str(m_h - h + 1)) print(" ".join(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
/** * Created by lq on 15/9/30. */ import java.util.*; import java.io.*; public class b_Luxurious_Houses { public static void main(String[] args){ // FileInputStream fis = null; // try { // fis = new FileInputStream("/Users/lq/leetcode/codeforces/virtual1/1.txt"); // }catch(Exception e){ // System.out.println(e); // } // Scanner cin = new Scanner(fis); Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int[] nums = new int[n]; for(int i = 0; i < n ; i++) nums[i] = cin.nextInt(); int maxs = Integer.MIN_VALUE; StringBuilder s = new StringBuilder(""); for(int i = n-1; i >= 0; i--){ int tmp = maxs; maxs = Math.max(tmp, nums[i]); if(i == n-1) nums[i] = 0; else nums[i] = Math.max(0, - nums[i] + tmp + 1); } for(int i : nums) s.append(i+" "); System.out.println(s.deleteCharAt(s.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
#include <bits/stdc++.h> using namespace std; int n, arr[110000], b[110000]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = n - 2; i >= 0; i--) { b[i] = arr[i + 1] - arr[i] + 1; arr[i] = max(arr[i + 1], arr[i]); if (b[i] < 0) b[i] = 0; } for (int i = 0; i < n; i++) { cout << b[i] << " "; } }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#!/usr/bin/env python I = raw_input def ia(): return map(int, I().split()) def na(n): return [ia() for _ in range(n)] I() arr = ia() res = [0]*len(arr) most = 0 for i in range(len(arr)-1, -1, -1): if arr[i] > most: res[i] = 0 most = arr[i] else: res[i] = most-arr[i] +1 print " ".join(map(str, res))
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> int main() { int a; scanf("%d", &a); long A[a], B[a]; for (int i = 0; i != a; ++i) { scanf("%ld", &A[i]); } B[a - 1] = 0; long mx = A[a - 1]; for (int i = a - 2; i >= 0; --i) { if (mx < A[i]) B[i] = 0; else B[i] = mx + 1 - A[i]; mx = std::max(A[i], mx); } for (int i = 0; i != a; ++i) { printf("%ld ", 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, fl[111111], ans[111111], mfl; int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &fl[i]); } for (int i = N - 1; i >= 0; i--) { ans[i] = max(0, mfl + 1 - fl[i]); mfl = max(mfl, fl[i]); } for (int i = 0; i < N; i++) { printf("%d ", ans[i]); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; public class fence { //static {System.out.println("hello"); } public static void main(String args[]) { Scanner scn=new Scanner(System.in); int n=scn.nextInt(); if (n==1 ){ System.out.println(0); System.exit(0);} int a[]=new int[n]; int max[]=new int[n]; int max2[]=new int[n]; for(int i=0;i<n;i++) { a[i]=scn.nextInt(); } max[0]=a[0]; for(int i=1;i<n;i++) max[i]=Math.max(max[i-1],a[i]); max2[n-1]=a[n-1]; for(int i=n-2;i>=0;i--) max2[i]=Math.max(max2[i+1],a[i]); System.out.print((Math.max(0,max2[1])-a[0] >=0 ? Math.max(0,max2[1])-a[0]+1: 0)+ " " ); for(int i=1;i<n-1;i++)System.out.print((Math.max(0,max2[i+1])-a[i] >=0 ? Math.max(0,max2[i+1])-a[i] +1: 0 )+ " " ); 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.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class a { public static class node implements Comparable<node> { int x; int y; node(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(node o) { if (o.x < x) return 1; else if (o.x > x) return -1; else if (o.y > y) return 1; else if (o.y < y) return -1; return 0; } } public static class point { int x; int y; point(int x, int val) { this.x = x; this.y = val; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static ArrayList<Integer> prime; public static void sieve(int n) { boolean vis[] = new boolean[n + 5]; vis[1] = true; vis[0] = true; for (int i = 2; i <= n; i++) { if (!vis[i]) { prime.add(i); for (long j = (long) i * (long) i; j <= n; j += i) { vis[(int) j] = true; } } } } public static int tree[][]; public static boolean end[][]; public static int counter; public static void trie(String a) { int st = 0; for (int i = 0; i < a.length(); i++) { if (tree[a.charAt(i) - 'a'][st] == 0) { tree[a.charAt(i) - 'a'][st] = counter++; } st = tree[a.charAt(i) - 'a'][st]; } end[a.charAt(a.length() - 1) - 'a'][st] = true; } public static int seg[][]; public static int get(int s, int e, int p, int from, int to, int seg[]) { if (s >= from && e <= to) return seg[p]; if (s > to || e < from) return 0; int mid = (s + e) / 2; int one = get(s, mid, p * 2, from, to, seg); int two = get(mid + 1, e, p * 2 + 1, from, to, seg); return Math.max(one, two); } public static long comp(int n, int r) { long ans = 1; r = Math.min(r, n - r); for (int i = 1; i <= r; i++) { ans = ans * ((long) n - (long) i + 1L) / (long) i; } return ans; } public static ArrayList<Integer> arr[]; public static boolean vis[]; public static Stack<Integer> S; public static void bfs(int node) { for (int i = 0; i < arr[node].size(); i++) { int k = arr[node].get(i); if (vis[k] == false) { vis[k] = true; S.push(k); bfs(k); } } S.push(node); } public static void dfs(int node) { System.out.print(node + " "); for (int i = 0; i < arr[node].size(); i++) { int k = arr[node].get(i); if (vis[k] == false) { vis[k] = true; dfs(k); } } } public static int get(int a) { while (a % 2 == 0) a /= 2; while (a % 3 == 0) a /= 3; return a; } public static int ans[][]; public static int e; public static boolean chEmpty(int x, int y, int dim1, int dim2, int r) { if (x + dim1 - 1 >= e || y + dim2 - 1 >= e) return false; for (int i = x; i < x + dim1; i++) { for (int j = y; j < y + dim2; j++) { if (ans[i][j] != 0) return false; } } for (int i = x; i < x + dim1; i++) { for (int j = y; j < y + dim2; j++) { ans[i][j] = r; } } return true; } public static void solve(int x1, int y1, int ch1, int x2, int y2, int ch2, int x3, int y3, int ch3) { int ee = ch1; int xx = x1; int yy = y1; int rr = 1; for (int i = 0; i < e; i++) { for (int j = 0; j < e; j++) { boolean k = chEmpty(i, j, xx, yy, ee); if (k) rr++; else { boolean kk = chEmpty(i, j, yy, xx, ee); if (kk) rr++; } if (rr == 2) { xx = x2; yy = y2; ee = ch2; } else if (rr == 3) { xx = x3; yy = y3; ee = ch3; } else if (ee == 4) break; } if (ee == 4) break; } } public static boolean ch() { for (int i = 0; i < e; i++) { for (int j = 0; j < e; j++) { if (ans[i][j] == 0) return false; } } return true; } public static void empty() { for (int i = 0; i < e; i++) { for (int j = 0; j < e; j++) { ans[i][j] = 0; } } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // BufferedReader in = new BufferedReader(new FileReader("cubes.in")); StringBuilder qq = new StringBuilder(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); // PrintWriter out = new PrintWriter(new FileWriter("out.txt")); int n = Integer.parseInt(in.readLine()); String y[] = in.readLine().split(" "); int a[] = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(y[i]); } int max[] = new int[n]; max[n - 1] = a[n - 1]; int ans[] = new int[n]; ans[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (a[i] > max[i + 1]) { max[i] = a[i]; ans[i] = 0; } else { max[i] = max[i + 1]; ans[i] = max[i] - a[i] + 1; } } for (int i = 0; i < n; i++) qq.append(ans[i] + " "); System.out.println(qq); 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
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } inline long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } const long long mod = (long long)(1e9 + 7); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, mx; cin >> n; vector<long long> h(n), ans(n); for (int(i) = (0); (i) <= (n - 1); (i)++) cin >> h[i]; mx = h[n - 1]; for (int i = n - 2; i >= 0; i--) { if (mx >= h[i]) ans[i] = mx - h[i] + 1; mx = max(mx, h[i]); } for (int(i) = (0); (i) <= (n - 1); (i)++) cout << ans[i] << ' '; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cout.precision(3); long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; vector<long long> b(n); long long res = 0; for (long long i = n - 1; i >= 0; i--) { b[i] = res - a[i] + 1; res = max(res, a[i]); } for (long long i : b) cout << max(0ll, 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
# =================================== # (c) MidAndFeed aka ASilentVoice # =================================== # import math # import collections # import string # =================================== n = int(input()) q = [int(x) for x in input().split()] vmax = -1 for i in range(n-1, -1, -1): t = q[i] if (q[i] > vmax): q[i] = 0 else: q[i] = vmax - q[i] + 1 vmax = max(vmax, t) print(" ".join(map(str, q)))
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; long long a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long mx = a[n - 1]; b[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (a[i] <= mx) b[i] = mx + 1 - a[i]; else b[i] = 0; mx = max(a[i], mx); } for (int i = 0; i < n; i++) { cout << b[i] << ' '; } cout << endl; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int n, h[100001]; priority_queue<pair<int, int> > pq; void input() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &h[i]); pq.push(pair<int, int>(h[i], -i)); } } void solve() { input(); for (int i = 0; i < n; i++) { while (pq.size() && -pq.top().second <= i) pq.pop(); if (!pq.size() || pq.top().first < h[i]) printf("0 "); else printf("%d ", pq.top().first - h[i] + 1); } } int main(void) { solve(); return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
def luxurious_houses(): houses = int(raw_input().strip()) height_of_houses = list(map(int, raw_input().strip().split())) max_height_array = [0] * houses max_height_array[houses - 1] = height_of_houses[houses - 1] for i in range(houses - 2, -1, -1): max_height_array[i] = max(max_height_array[i + 1], height_of_houses[i]) ans = [] for i in range(houses - 1): ans.append(str(max(0, max_height_array[i + 1] - height_of_houses[i] + 1))) ans.append('0') print ' '.join(ans) luxurious_houses()
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
k = int(input()) houses = list((map(int, input().split()))) maxim = 0 answer = [] for i in range(len(houses)-1, -1, -1): currentVal = houses[i] if maxim < currentVal: answer.append(0) maxim = currentVal else: answer.append(maxim-currentVal+1) print(" ".join(reversed([str(i) for i in answer])))
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 i, j, k, n, t = 1; while (t--) { cin >> n; vector<long> v(n), b(n), ans(n, 0); for (i = 0; i < n; i++) { cin >> v[i]; b[i] = v[i]; } for (i = n - 2; i >= 0; i--) { if (b[i + 1] >= v[i]) ans[i] = 1 + b[i + 1] - v[i]; b[i] = max(b[i], b[i + 1]); } for (i = 0; i < n; i++) { cout << ans[i] << " "; } } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#Solved by Fuad Ashraful BBabu #soled date 12 july 2019 #verdict : AC N=int(input()) tini=list(map(int,input().split())) max_height=0 diff=N*[0] for i in range(N).__reversed__(): if tini[i]>max_height: max_height=tini[i] diff[i]=0 else: diff[i]=max_height-tini[i]+1 print(*diff)
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())) m=max(h) l=h.index(m) i=n-2 a=[0] maxh=h[n-1] while(i>=0): if(maxh==h[i]): a.append(1) else: maxh = max(h[i], maxh) if(maxh==h[i]): a.append(0) else: a.append(maxh-h[i]+1) i-=1 a.reverse() for i in a: print(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; void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> res(n, 0); int mx = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > mx) { mx = a[i]; res[i] = 0; } else { int diff = mx - a[i]; res[i] = diff + 1; } } for (int i = 0; i < n; i++) { cout << res[i] << " "; } cout << "\n"; } int main() { std::ios::sync_with_stdio(false); int t; t = 1; while (t--) { solve(); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; int max; int a[n]; a[n - 1] = 0; max = h[n - 1]; for (int i = n - 2; i >= 0; i--) { if (h[i] > max) { a[i] = 0; max = h[i]; } else { a[i] = max - h[i] + 1; } } for (int i = 0; i < n; i++) { printf("%d ", a[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 E9 = 1e9; const int E8 = 1e8; const int E7 = 1e7; const int E6 = 1e6; const int E5 = 1e5; const int E4 = 1e4; const int E3 = 1e3; const int N = 5e5 + 7; const int INF = 1e9; const long long inf = 1e18; int a[N], mx[N]; bool us[N]; int main() { int n, k = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); } for (int i = n; i > 0; --i) { if (k == a[i]) us[i] = true; k = max(a[i], k); mx[i] = k; } for (int i = 1; i <= n; ++i) { int ans = abs(mx[i] - a[i]); if (ans != 0 || us[i]) ans++; printf("%d", ans); cout << " "; } }
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() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; vector<long long> v; int num; for (int i = 0; i < n; i++) { cin >> num; v.push_back(num); } int mx = 0; vector<int> ans; for (int i = v.size() - 1; i >= 0; i--) { if (v[i] > mx) { ans.push_back(0); mx = v[i]; } else { ans.push_back(mx - v[i] + 1); } } reverse(ans.begin(), ans.end()); for (int i = 0; i < n; i++) { cout << ans[i] << " "; } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; public class L { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); int tallest = -1; int[] ans = new int[n]; for (int i = n-1; i >=0; i--) if (arr[i] > tallest) { ans[i] = 0; tallest = arr[i]; } else { ans[i] = tallest + 1 - arr[i]; } StringBuilder str = new StringBuilder(); str.append(ans[0]); for (int i = 1; i < n; i++) { str.append(" " + ans[i]); } System.out.println(str.toString()); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = input() h= map(int,raw_input().split()) maxh=0 ans=[] for i in xrange(n-1,-1,-1): if h[i]>maxh : ans.append('0') maxh = h[i] else : ans.append(str((maxh-h[i])+1)) print " ".join(ans[-1::-1])
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> int main() { int n, i, j, k, m, l; scanf("%d", &n); int a[n], b[n]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } m = a[n - 1]; b[n - 1] = 0; for (i = n - 2; i >= 0; i--) { if (a[i] > m) { b[i] = 0; m = a[i]; } else { b[i] = m - a[i] + 1; } } 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
if __name__ == '__main__': n = str(input()) line = [int(it) for it in str(input()).split()] line.reverse() res = list() mark = 0 for it in line: res.append(str(max(1 + mark - it, 0))) mark = max(mark, it) res.reverse() print(' '.join(res))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; @SuppressWarnings("unchecked") public class P581B { public void run() throws Exception { int n = nextInt(); int [] h = readInt(n); int [] a = new int [n]; int mh = 0; for (int i = n - 1; i >= 0; i--) { a[i] = (h[i] > mh) ? 0 : (mh - h[i] + 1); mh = Math.max(h[i], mh); } for (int aa : a) { print(aa + " "); } } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P581B().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } static long startTime = System.currentTimeMillis(); static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } }
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; long long a[100010], b[100010]; int main() { long long n, m; cin >> n; m = -2147483647; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { if (a[i] <= m) b[i] = m - a[i] + 1; else b[i] = 0; m = max(m, a[i]); } for (int i = 0; i < n; i++) cout << b[i] << " "; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Scanner; /** * Created by yuu on 26/3/17. */ public class Problem581B { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] heights = new int[n]; int[] maxHeights = new int[n]; int[] results = new int[n]; for (int i = 0; i < n; i++){ heights[i] = sc.nextInt(); } if (n-2 >= 0) maxHeights[n-2] = heights[n-1]; for (int i = n-3; i >= 0; i--){ maxHeights[i] = Math.max(heights[i+1], maxHeights[i+1]); } // for (int i = 0; i < n; i++){ // System.out.print(maxHeights[i] + " "); // } // System.out.println(); for (int i = 0; i < n; i++){ if (heights[i] <= maxHeights[i]) results[i] = maxHeights[i] - heights[i] + 1; else results[i] = 0; System.out.print(results[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()) l=list(map(int,input().split())) k=l[n-1] y=[0] for i in range(1,n): if k<l[n-i-1]: k=l[n-i-1] y.append(0) else: y.append(k+1-l[n-i-1]) for i in range(n): print(y[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
n = int(input()) h = [int(i) for i in input().split()] l = [] l_2 = [] for i in range(n): l_2 += [0] l_2[n-1] = h[n-1] for i in range (n-2, 0, -1): l_2[i] = max( l_2[i+1], h[i]) l_2 = l_2 + [0] for i in range (n): m = l_2[i+1] g = max ( m - h[i] +1, 0) l += [g] for i in l: print(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
import java.util.*; import java.io.*; public class B581 { public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); long[] h = new long[(int)n]; long temp=0; for(int i=0;i<n;i++) h[i]=in.nextLong(); for(int i=(int)n-1;i>=0;i--) { if(h[i]>temp) { temp=h[i]; h[i]=0; } else h[i]=temp+1-h[i]; } for(int i=0;i<n;i++) System.out.print(h[i]+" "); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i]; int mx = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] <= mx) b[i] = mx + 1 - a[i]; else mx = a[i], b[i] = 0; } b[n - 1] = 0; 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
n = int(input()) a = [int(x) for x in input().split()][::-1] ans = [] m = 0 for i in range(len(a)): if i == 0: ans.append(0) m=a[i] else: if a[i] <= m: ans.append(m-a[i]+1) else: ans.append(0) m = a[i] ans = ans[::-1] for i in ans: print(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
import java.util.*; public class lux{ public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=s.nextInt(); } int max=0; for(int i=n-1;i>=0;i--){ if(a[i]>max){ max=a[i]; a[i]=0; } else if(a[i]==max){ a[i]=1; } else{ a[i]=max-a[i]+1; } } for (int i=0;i<n;i++) { System.out.print(a[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; long long int arr[100010]; long long int ans[100010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } ans[n - 1] = 0; long long int maxi = arr[n - 1]; for (int i = n - 2; i >= 0; i--) { if (arr[i] > maxi) { ans[i] = 0; maxi = arr[i]; } else { ans[i] = maxi - arr[i] + 1; } } for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << endl; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.fill; public class Main { public static void main(String[] args) throws IOException { run(); end(); } static void run() throws IOException { int n = nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } int max = 0; int[] ans = new int[n]; for (int i = n - 1; i >= 0; --i) { if (arr[i] <= max) { ans[i] = max - arr[i] + 1; } else { ans[i] = 0; } max = max(max, arr[i]); } for (int i = 0; i < n; i++) { out.print(ans[i] + " "); } } static void end() { out.flush(); out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { String s = br.readLine(); if (s == null) return null; in = new StringTokenizer(s); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) l = list(map(int, input().split())) m = l[-1] l[-1] = 0 for i in range(n-2, -1, -1): if l[i] == m: l[i] = 1 elif l[i] > m: m = l[i] l[i] = 0 else: l[i] = m - l[i] + 1 print(" ".join(map(str, l)))
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; vector<long long> a, b; long long q, max_et; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &q); a.push_back(q); } b.push_back(0); max_et = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > max_et) { max_et = a[i]; b.push_back(0); } else b.push_back(max_et - a[i] + 1); } for (int i = n - 1; i >= 0; i--) printf("%I64d ", 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; const int N = 2e5 + 10; long long a[N], b[N]; int main() { int n, d; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = 0; } long long maxx = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] <= maxx) { b[i] = maxx + 1 - a[i]; } else { maxx = a[i]; } } for (int i = 0; i < n; i++) cout << b[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() { int n; cin >> n; int arr[n], suf[n]; for (int i = 0; i < n; i++) cin >> arr[i]; suf[n - 1] = arr[n - 1]; for (int i = n - 2; i >= 0; i--) { suf[i] = max(arr[i], suf[i + 1]); } for (int i = 0; i < n - 1; i++) { if (suf[i + 1] >= arr[i]) { cout << suf[i + 1] - arr[i] + 1 << " "; } else cout << "0 "; } puts("0\n"); }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n, t; cin >> n; long long int a[n], b[n]; b[n - 1] = 0; for (long long int i = 0; i < n; i++) cin >> a[i]; t = a[n - 1]; for (long long int i = n - 2; i >= 0; i--) { if (a[i] < t) b[i] = t; else if (a[i] > t) { b[i] = 0; t = a[i]; } else { b[i] = t; } } for (long long int i = 0; i < n; i++) cout << max(0ll, b[i] + 1 - a[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
buildingsNum = int(raw_input()) firstLine = raw_input().split(" ") buildings = [] for i in range(0, buildingsNum): buildings.append(firstLine[i]) addedFloors = [] addedFloors.append(0) for i in range (len(buildings) - 2, -1, -1): if addedFloors[len(addedFloors) - 1] == 0: a = int(buildings[i + 1]) + 1 - int(buildings[i]) else: a = int(buildings[i + 1]) + int(addedFloors[len(addedFloors) - 1]) - int(buildings[i]) if a < 0: a = 0 addedFloors.append(a) addedFloors.reverse() print(' '.join(map(str, addedFloors)))
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()) a=map(int,raw_input().split()) i, mh = n-1, a[n-1] while i>=0: if mh<a[i]: mh=a[i] a[i]=0 else: a[i]=mh-a[i] + 1 i-=1 a[n-1]=0 print(" ".join(map(str,a)))
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
# Description of the problem can be found at http://codeforces.com/problemset/problem/581/B n = int(input()) l_n = list(map(int, input().split())) m = 0 l_a = [0 for i in range(n)] for i in range(n): l_a[n - i - 1] = max(0, m + 1 - l_n[n - i - 1]) m = max(m, l_n[n - i - 1]) print(" ".join(str(x) for x in l_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
/* *In the name of Allah the Most Merciful. * Author * Md. Toufiqul Islam * Dept. Of CSE * Ahsanullah University Of Science And Technology */ //package CodeForces; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; public class R581B { public static void main(String[] arg){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } int max = a[n-1]; for(int i=n-2;i>=0;i--){ if(a[i]<=max){ b[i] = (max+1)-a[i]; } else{ max = a[i]; b[i] =0; } } b[n-1] =0; for(int i=0;i<b.length;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; const int N = 100005; int a[N]; int i, j, n; void input() { cin >> n; for (i = 0; i < n; ++i) { cin >> a[i]; } } int ans; void output() { cout << ans << '\n'; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); input(); int b[N]; int mx = -99999; for (i = n - 1; i >= 0; --i) { b[i] = max(0, mx - a[i] + 1); mx = max(mx, a[i]); } 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
#include <bits/stdc++.h> using namespace std; int main() { int a, b[100005], i, j; cin >> a; for (i = 1; i <= a; i++) { cin >> b[i]; } vector<int> v; int maxn = 0; for (i = a; i >= 1; i--) { if (maxn < b[i]) { v.push_back(0); } else if (maxn > b[i]) { v.push_back(maxn - b[i] + 1); } else if (maxn == b[i]) { v.push_back(1); } if (maxn < b[i]) { maxn = b[i]; } } reverse(v.begin(), v.end()); for (i = 0; i < v.size(); i++) { cout << v[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=input() a=map(int,raw_input().split()) maxi=0 l=[] for i in xrange(n-1,0,-1): if a[i]>maxi: maxi=a[i] l.append(maxi) copy=l[::-1] for i in xrange(n-1): if a[i]<=copy[i]: print copy[i]-a[i]+1, else: print 0, print 0
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(raw_input()) a = map(int, raw_input().split()) b = [0]*(n) cur_max = a[n - 1] b[n - 1] = 0 for i in xrange(n - 2, -1, -1): if a[i] <= cur_max: b[i] = cur_max - a[i] + 1 cur_max = max(a[i], cur_max) for i in xrange(len(b)): print b[i],
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.ArrayList; import java.util.Scanner; public class CodeForces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i]=sc.nextInt(); } int[] out = new int[n]; out[n-1]=0; int max = arr[n-1]; for (int i = out.length-2; i >= 0 ; i--) { if(arr[i]<=max){ out[i]=max+1-arr[i]; } else { out[i]=0; max=arr[i]; } } for (int i = 0; i < out.length; i++) { System.out.println(out[i]+" "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int N = 100001; const double eps = 1e-11; const int inf = (int)2e9; const int mod = (int)1e9 + 7; int a[N]; int s[N]; 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++) { printf("%d%c", max(s[i + 1] - a[i] + 1, 0), " \n"[i + 1 == 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; long long n, m = -1000, k, l, i, j, a[110000], b[110000]; int main() { cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; for (i = n; i >= 1; i--) { b[i] = max(m - a[i] + 1, 0ll); m = max(m, 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
import java.io.InputStreamReader; import java.io.BufferedReader; public class BCF518B { public static void main(String[]args)throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; int ar[]=new int[n]; int temp=0; String s[]=br.readLine().split(" "); for(int i=0;i<n;i++) arr[i]=Integer.parseInt(s[i]); int max=arr[n-1]; for(int i=n-2;i>=0;i--) { if(arr[i]>max) { ar[i]=0; max=arr[i]; } else { ar[i]=max-arr[i]+1; } } for(int i=0;i<n;i++) System.out.print(ar[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()) bF = list(map(int, input().split())) max_ahead = [0] for i in range(n - 2, -1, -1): max_ahead.append(max(bF[i + 1], max_ahead[-1])) max_ahead = max_ahead[::-1] for i in range(0, n): if bF[i] > max_ahead[i]: print(0, end=" ") else: print(max_ahead[i] - bF[i] + 1, end=" ")
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; long long mx, n, i, j, a, x, t[100000], v[100000]; int main() { cin >> n; for (i = 0; i < n; i++) cin >> t[i]; v[n - 1] = 0; mx = t[n - 1]; for (i = n - 2; i >= 0; i--) { if (t[i] > mx) { v[i] = 0; mx = t[i]; } else v[i] = mx - t[i] + 1; } for (i = 0; i < n; i++) cout << v[i] << " "; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) a = list(map(int, input().split())) b = [] mx = -(10**10) for i in range(n-1, -1, -1): b.append(max(0, mx+1-a[i])) mx = max(mx, a[i]) b = b[::-1] for i in b: print(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[100000], b[100000]; for (int i = 0; i < n; i++) cin >> a[i]; b[n - 1] = 0; int mx = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > mx) { mx = a[i]; b[i] = 0; } else b[i] = 1 + mx - a[i]; } for (int i = 0; i < n; i++) printf("%d ", b[i]); printf("\n"); return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] numbers = new int[n]; String[] split = br.readLine().split(" "); for (int i = 0; i < n; i++) { numbers[i] = Integer.parseInt(split[i]); } int[] max = new int[n]; max[n - 1] = numbers[n - 1]; for (int i = n - 2; i >= 0; i--) { max[i] = Math.max(max[i + 1], numbers[i]); } for (int i = 0; i < n - 1; i++) { System.out.print(Math.max(max[i + 1] - numbers[i] + 1, 0)); System.out.println(" "); } 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
from itertools import imap I = lambda: map(int, raw_input().split()) n, = I() houses = I()[:n] _max = 0 lux = [0] * n for index in range(n-1, -1, -1): if houses[index] <= _max: lux[index] = _max + 1 else: _max = houses[index] lux[index] = _max print ' '.join(imap(lambda x, y: str(x - y), lux, houses))
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int MOD = 1e9 + 7; const int maxn = 1e6 + 10; const int maxv = 1e3 + 10; const double eps = 1e-9; inline int read() { char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } int x = 0; while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int ans[maxn]; int a[maxn]; int main() { int n; cin >> n; int big = 0; for (int i = 1; i <= n; i++) cin >> a[i]; big = a[n]; for (int i = n - 1; i > 0; i--) { ans[i] = big - a[i] + 1; if (ans[i] < 0) ans[i] = 0; big = max(big, a[i]); } for (int i = 1; i <= n; i++) cout << ans[i] << ' '; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
# https://vjudge.net/contest/379796#problem/M n=int(input()) z=list(map(int,input().split())) lst=[0]*n;m=z[-1];lst[-1]=0 for i in range(n-2,-1,-1): if z[i] > m: lst[i]=0 m = z[i] else: lst[i]= m-z[i]+1 print(*lst)
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) a = [int(s) for s in input().split()] b = [0]*n i = n-1 sum = 0 while i > 0: sum = max(sum, a[i]) b[i-1] = max(0, sum + 1 - a[i-1]) i = i - 1 for elem in b: print(elem, 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 sys import math #import random #sys.setrecursionlimit(1000000) input = sys.stdin.readline ############ ---- USER DEFINED INPUT FUNCTIONS ---- ############ def inp(): return(int(input())) def inara(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) ################################################################ ############ ---- THE ACTUAL CODE STARTS BELOW ---- ############ n=inp() ara=inara() ans=[0]*n curr=0 for i in range(n-1,-1,-1): if ara[i]<=curr: ans[i]=curr-ara[i]+1 curr=max(curr,ara[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 const N = 1e6 + 5; int n, m, arr[N], b[N], cnt = 0, ma; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } ma = arr[n]; for (int i = n - 1; i >= 0; i--) { if (ma < arr[i]) { ma = arr[i]; continue; } else { b[i] = ma - arr[i] + 1; } } for (int i = 0; i < n; i++) { printf("%d ", b[i]); } }
CPP