Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int no_of_houses; cin >> no_of_houses; vector<int> floors; vector<int> lux_floors; int max_height = 0; int temp; for (int i = 0; i < no_of_houses; i++) { cin >> temp; floors.push_back(temp); } for (int i = no_of_houses - 1; i >= 0; i--) { if (floors[i] > max_height) { max_height = floors[i]; lux_floors.push_back(0); continue; } temp = (0 > max_height + 1 - floors[i]) ? 0 : max_height + 1 - floors[i]; lux_floors.push_back(temp); } for (int i = no_of_houses - 1; i >= 0; i--) { cout << lux_floors[i]; if (i != 0) { cout << " "; } } cout << endl; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * @author pttrung */ public class B_Round_323_Div2 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int[]data = new int[n]; int[]result = new int[n]; for(int i = 0; i < n;i++){ data[i] = in.nextInt(); } int max = data[n - 1]; for(int i = n - 2; i >= 0; i-- ){ if(max >= data[i]){ result[i] = max - data[i] + 1; }else{ max = data[i]; } } for(int i: result){ out.print(i + " "); } out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return x - o.x; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int N; int arr[(int)1e5 + 5]; int m[(int)1e5 + 5]; int main() { ios_base::sync_with_stdio(false); cin >> N; for (int i = 0; i < N; i++) cin >> arr[i]; for (int i = N - 1; i >= 0; i--) m[i] = max(arr[i], m[i + 1]); for (int i = 0; i < N; i++) { if (arr[i] > m[i]) cout << "0" << " "; else if (arr[i] == m[i] && m[i + 1] < arr[i]) cout << "0" << " "; else cout << m[i] - arr[i] + 1 << " "; } cout << endl; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#In the name of God n = int(raw_input()) arr = map(int, raw_input().split()) maxx = [0 for i in range(n)] build = [0 for i in range(n)] maxx[-1] = arr[-1] for i in range(n-2, -1, -1): maxx[i] = max(arr[i+1], maxx[i+1]) build[i] = 0 if arr[i] > maxx[i] else maxx[i]-arr[i]+1 print ' '.join(map(str, build))
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 = [int(x) for x in input().split()] b = [0] * n for i in range(n - 2, -1, -1): b[i] = max(b[i + 1], a[i + 1]) print(' '.join(str(max(0, x - y + 1)) for x, y in zip(b, a)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; map<int, int> mm; int main() { int i, j, n; cin >> n; int a[n]; vector<int> res; for (i = 0; i < n; i++) { cin >> a[i]; } int mx = -1; for (i = n - 1; i >= 0; i--) { mx = max(mx, a[i]); mm[mx]++; if (mx == a[i] && mm[mx] == 1) res.push_back(0); else if (mx == a[i] && mm[mx] > 1) res.push_back(1); else res.push_back(mx - a[i] + 1); } reverse(res.begin(), res.end()); for (auto e : res) cout << e << " "; 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.util.Scanner; public class bb { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int[] l = new int[num]; int max = 0; for (int i=0;i<num;i++) { l[i] = sc.nextInt(); } int[] ans = new int[num]; for (int i=num-1;i>-1;i--) { if (l[i] > max) { max = l[i]; ans[i] = 0; } else { ans[i] = max-l[i]+1; } } for (int i=0;i<num;i++) { System.out.print(ans[i] + " "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; inline int rit() { int x; scanf("%d", &x); return x; } int n; int a[100005], ans[100005]; void read() { int i, d; n = rit(); for (i = 0; i < n; i++) a[i] = rit(); d = a[n - 1] + 1; ans[n - 1] = 0; for (i = n - 2; i >= 0; i--) { ans[i] = max(0, d - a[i]); d = max(d, a[i] + 1); } for (i = 0; i < n - 1; i++) cout << ans[i] << ' '; cout << ans[i] << endl; } int main() { read(); 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.util.*; import java.math.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.String.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //(new FileReader("input.in")); StringBuilder out = new StringBuilder(); StringTokenizer tk; int n = parseInt(in.readLine()),i,mx; int [] h = new int[n]; tk = new StringTokenizer(in.readLine()); for(i=0; i<n; i++) h[i] = parseInt(tk.nextToken()); int [] f = new int[n]; f[n-1] = 0; mx = h[n-1]; for(i=n-2; i>=0; i--) { if(h[i] > mx) { mx = h[i]; f[i] = 0; } else f[i] = mx+1-h[i]; } out.append(f[0]); for(i=1; i<n; i++) out.append(" ").append(f[i]); System.out.println(out); } }
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, m, i, j, k, t, s, x, y, h[100005], hh[100005], d, l; int main() { cin >> n; for (i = 0; i < n; i++) scanf("%d", h + i); m = h[n - 1]; for (i = n - 2; i >= 0; i--) { if (h[i] <= m) hh[i] = m - h[i] + 1; else m = h[i]; } for (i = 0; i < n; i++) printf("%d ", hh[i]); }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; public class LuxuriousHouses { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); int ans[]=new int[n]; int max=a[n-1]; for(int i=n-2;i>=0;i--) { if(a[i]>max) { max=a[i]; ans[i]=0; }else { if(a[i]==max)ans[i]=1; else ans[i]=max-a[i]+1; } } for(int e:ans)System.out.print(e+" "); sc.close(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Scanner; public class LuxuriousHouses { static Scanner sc = new Scanner(System.in); public static void main(String args[]) { int n = sc.nextInt(); int[] t = new int[n]; int[] tab = new int[n]; for (int i = 0; i < n; i++) t[i] = sc.nextInt(); tab[n - 1] = 0; int max = t[n - 1]; for (int i = n - 2; i >= 0; i--) { if (t[i] > max) { max = t[i]; tab[i] = 0;} else { tab[i] = max - t[i] + 1; } } for (int i =0; i <n; i++) { System.out.print(tab[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
z=int(input()) x=list(map(int,input().split())) x.reverse() f=x[0] d=[] for i in range(z): if f>=x[i] and i!=0: d.append(str(f-x[i]+1)) elif f==x[i]: d.append("0") elif f<x[i]: d.append("0") f=x[i] d.reverse() print(" ".join(d).strip())
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 org.omg.Messaging.SYNC_WITH_TRANSPORT; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Houses { public static void main(String... args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { int n = Integer.parseInt(reader.readLine()); String[] str = reader.readLine().split(" "); int[] data = new int[n]; for(int i = 0; i < data.length; i++) data[i] = Integer.parseInt(str[i]); int maximum = data[n - 1]; int[] result = new int[n]; result[n - 1] = 0; for(int i = n - 2; i >= 0; i--) { if(data[i] > maximum) { maximum = data[i]; result[i] = 0; } else result[i] = Math.max(maximum, data[i]) + 1 - data[i]; } for(int i = 0; i < n; i++) System.out.print(result[i] + " "); } catch(IOException e) { } } }
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[100009]; pair<int, int> ma[100009]; cin >> n; stack<int> s; for (int i = 1; i <= n; i++) cin >> a[i]; ma[n].first = a[n], ma[n].second = n; for (int i = n - 1; i > 0; i--) { if (a[i] > ma[i + 1].first) { ma[i].first = a[i]; ma[i].second = i; } else { ma[i].first = ma[i + 1].first; ma[i].second = ma[i + 1].second; } } for (int i = n; i > 0; i--) { if (a[i] == ma[i].first && ma[i].second == i) s.push(0); else s.push(ma[i].first + 1 - a[i]); } while (!s.empty()) { cout << s.top() << " "; s.pop(); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Arrays; import java.util.LinkedList; import java.util.Map; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; public class Main{ public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; st=new StringTokenizer(br.readLine()); for(int i=0;i<n;i++) arr[i]=Integer.parseInt(st.nextToken()); int max=0; int ans[]=new int[n]; for(int i=n-1;i>=0;i--) { if(max<arr[i]) { max=arr[i]; ans[i]=0; } else { ans[i]=max-arr[i]+1; } } for(int i=0;i<n;i++) System.out.print(ans[i]+" "); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
/** * Created by InfoLight on 10/3/2015. */ import java.util.Scanner; public class Main { public static long max(long a, long b){ if (a > b) return a; else return b; } public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int totalCount = Integer.parseInt(scanner.next()); long[] list = new long[100005]; long[] newList = new long[100005]; for (int i = 0; i < totalCount; i++){ list[i] = Long.parseLong(scanner.next()); } newList[totalCount - 1] = 0; long max = list[totalCount - 1]; for (int i = totalCount - 2; i >= 0; i--){ if (list[i] <= max) newList[i] = max + 1; else{ newList[i] = list[i]; max = list[i]; } } for (int i = 0; i < totalCount - 1; i++){ System.out.print((newList[i] - list[i]) + " "); } 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
n=int(input()) a=list(map(int,input().split())) flag=0 b=[0] k=a[-1] for i in range(1,n): if k>a[n-i-1]: b.append(-a[n-i-1]+k+1) else: if k-a[n-i-1]+1>0: b.append(k-a[n-i-1]+1) else: b.append(0) k=a[n-i-1] for i in b[::-1]: 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; const int MOD = 1000000007; void solve() { int n; cin >> n; vector<int> vec(n); for (int& i : vec) cin >> i; vector<int> pref(n); pref[n - 1] = vec[n - 1]; for (int i = (n - 2); i >= 0; i--) { pref[i] = max(pref[i + 1], vec[i]); } vector<int> ans(n); ans[n - 1] = 0; for (int i = 0; i < n - 1; i++) { ans[i] = max(pref[i + 1] - vec[i] + 1, 0); } for (auto i : ans) cout << i << " "; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; vector<long long> g(n); g[n - 1] = v[n - 1]; for (long long i = n - 2; i >= 0; i--) g[i] = max(v[i + 1], g[i + 1]); for (long long i = 0; i < n - 1; i++) { if (v[i] <= g[i]) cout << g[i] - v[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
#include <bits/stdc++.h> using namespace std; int n; long long int num[100005], maxn; int main() { while (~scanf("%d", &n)) { for (int i = 0; i < n; i++) scanf("%lld", &num[i]); maxn = num[n - 1]; num[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (num[i] <= maxn) { num[i] = maxn - num[i] + 1; } else { maxn = num[i]; num[i] = 0; } } for (int i = 0; i < n; i++) { printf("%lld", num[i]); if (i != n - 1) printf(" "); } 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.*; import java.util.*; public class Main{ public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n=sc.nextInt(); long arr[]=new long[n]; for(int i=0;i<n;i++) arr[i]=sc.nextLong(); long max[]=new long[n]; long t=0; for(int i=n-1;i>=0;i--){ if(i==n-1){ max[i]=0; t=arr[i]; } else { if(arr[i]>t) max[i]=0; else if(arr[i]==t) max[i]=1; else max[i]=t-arr[i]+1; t=Math.max(t, arr[i]); } } for(Long x:max) out.print(x+" "); out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 100100; pair<int, int> b[maxn]; int ans[maxn], a[maxn]; int main() { ios::sync_with_stdio(false); int n, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; a[i] = x; b[i] = make_pair(x, i); } sort(b, b + n, greater<pair<int, int> >()); int id = 0; for (int i = 0; i < n; i++) { int curr = b[i].first; int idx = b[i].second; while (id < idx) { ans[id] = curr - a[id] + 1; id++; } if (id == idx) id++; } 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
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a[n]; for (int i = 1; i <= n; i++) cin >> a[i]; int Max = a[n] - 1; for (int i = n; i >= 1; i--) { if (a[i] > Max) { Max = a[i]; a[i] = 0; } else a[i] = Max - a[i] + 1; } for (int i = 1; i <= n; i++) cout << a[i] << " "; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int A[100005]; int ans[100005]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; int ma = 0; for (int i = N - 1; i >= 0; i--) { ans[i] = max(0, ma - A[i] + 1); ma = max(ma, A[i]); } 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemB { public static void main(String[] args) { InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); new ProblemB().solve(in, out); out.close(); } public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) { h[i] = in.nextInt(); } int[] res = new int[n]; res[n - 1] = 0; int max = h[n - 1]; for (int i = n - 2; i >= 0; i--) { if (max >= h[i]) { res[i] = max - h[i] + 1; } else { res[i] = 0; max = h[i]; } } for (int i = 0; i < n; i++) { out.print(res[i] + " "); } } static class InputReader { public BufferedReader br; public StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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; long long a[MAX], b[MAX]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long max = a[n - 1]; b[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (a[i] > max) { max = a[i]; b[i] = 0; } else if (a[i] <= max) { b[i] = max + 1 - 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
l=int(input()) c=list(map(int,input().split())) m=[0] for i in range(l-1,-1,-1): m.append(max(c[i],m[l-1-i])) for i in range(l): if (m[l-1-i]<c[i])or(i==l-1): print(0,end=' ') else: print(m[l-1-i]+1-c[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.Scanner; public class PB { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Communication S=new Communication(); int n=S.i(); int[] a=S.is(); int M=0; for(int i=n-1;i>-1;i--){ if(a[i]>M){ M=a[i]; a[i]=0; }else{ a[i]=M+1-a[i]; } } for(int i=0;i<n;i++){ S.sendw(a[i]+" "); } } ///////////////////////////////////// static class Communication { Scanner sc; public static void send(Object x){ System.out.println(x); } public static void sendw(Object x){ System.out.print(x); } public Communication(){ sc=new Scanner(System.in); } public int i(){ return Integer.valueOf(sc.nextLine()); } public int[] is(){ String s=sc.nextLine(); String[] s2=s.split(" "); int[] j=new int[s2.length]; for(int i=0;i<s2.length;i++){ j[i]=Integer.valueOf(s2[i]); } return j; } public String s(){ return sc.nextLine(); } public String[] ss(){ return sc.nextLine().split(" "); } public char c(){ return sc.nextLine().charAt(0); } public char[] cs(){ return sc.nextLine().toCharArray(); } public long l(){ return Long.valueOf(sc.nextLine()); } } }
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 mod = 1000000007; long double pi = 3.1415926535; long long num_generator() { return rand() % 1000; } void array_generator(long long a[], long long n) { for (long long i = 0; i < n; i++) a[i] = rand() % 100; } void solve() { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long b[n + 1]; long long d = -1; for (long long i = n - 1; i >= 0; i--) { b[i] = max(d, a[i]); d = b[i]; } b[n] = -1; for (long long i = 0; i < n; i++) { if (b[i + 1] < a[i] || b[i] == -1) cout << "0 "; else cout << b[i] - a[i] + 1 << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import itertools n = int(input()) a = list(map(int, input().split())) b = [0] * n m = a[-1] for i in range(n - 2, -1, -1): b[i] = max(0, m - a[i] + 1) m = max(m, a[i]) print(' '.join(map(str, b)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int A[n], B[n]; for (int i = 0; i < n; i++) { cin >> A[i]; } int max = A[n - 1]; B[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (max < A[i]) { max = A[i]; B[i] = 0; } else { B[i] = max - A[i] + 1; } } for (int i = 0; i < n; i++) cout << B[i] << ' '; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) arr = [int(i) for i in input().split()] stack = [] res = [0 for _ in range(n)] for i in range(n-1, -1, -1): if (stack and stack[-1] >= arr[i]): res[i] = stack[-1] - arr[i] + 1 if (not stack): stack.append(arr[i]) elif (stack[-1] < arr[i]): stack.append(arr[i]) print(*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
if __name__ == '__main__': n = int(raw_input()) heights = [int(num) for num in raw_input().split()] max_height, adds = 0, [0]*n for i in reversed(range(n)): if heights[i] <= max_height: adds[i] = max_height - heights[i] + 1 max_height = max(heights[i], max_height) print ' '.join(str(add) for add in adds)
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class j_luxurious_houses { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Reader s = new Reader(); int n=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); int highest=a[n-1]; a[n-1]=0; for(int i=n-2;i>=0;i--) { if(a[i]<=highest) a[i]=highest-a[i]+1; else if(a[i]>highest) { highest=a[i]; a[i]=0; } } 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
n = int(input()) mass1 = list(map(int,input().split())) maxx = 0 mass2 = [] for i in range(n - 1, -1, -1): if mass1[i] > maxx: mass2.append(0) maxx = mass1[i] else: mass2.append(maxx - mass1[i] + 1) for i in range(n - 1, -1, -1): print(mass2[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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class LuxuriousHouses { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); long[] houses = new long[n]; long[] maxRight = new long[n]; int[] maximum = new int[n]; String[] ss = bf.readLine().split(" "); for(int i=0;i<n;i++){ houses[i] = Integer.parseInt(ss[i]); maximum[i] = 0; } long max = houses[n-1]; maximum[n-1] = 1; for(int i=n-1;i>=0;i--){ if(max<houses[i]){ max = houses[i]; maximum[i] = 1; } maxRight[i] = max; } for(int i=0;i<n;i++){ if(maxRight[i]==houses[i] && maximum[i]!=0){ System.out.print((maxRight[i]-houses[i])+" "); }else{ System.out.print((maxRight[i]-houses[i]+1)+" "); } } System.out.println(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
def f(l): n = len(l) rl = [0]*n rm = l[-1] for i in range(n-2,-1,-1): rl[i] = max(rm+1-l[i],0) if l[i]>rm: rm = l[i] return rl _ = input() l = list(map(int,input().split())) print(*f(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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Houses { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String[] temp = in.readLine().split(" "); int[] arr = new int[n]; int[] arr2 = new int[n]; for(int i = 0;i<n;i++) { arr[i] = Integer.parseInt(temp[i]); } int i = n -1; while(i >= 0) { int j = i; while(i > 0 && arr[j] >= arr[i-1]) { arr2[i - 1] = (arr[j] + 1) - arr[i - 1]; i--; } i--; } StringBuilder sb = new StringBuilder(); for(int k = 0;k<n;k++) { if(k < n - 1) { sb.append(arr2[k] + " "); } else { sb.append(arr2[k]); } } System.out.println(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; const long long INF = 1e11 + 5; const int MAX = 1e5 + 10; int arr[MAX], answer[MAX]; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; ++i) scanf("%d", &arr[i]); int ttl = -1; for (int i = t - 1; i >= 0; --i) { if (arr[i] > ttl) answer[i] = 0; else answer[i] = ttl - arr[i] + 1; ttl = max(ttl, arr[i]); } for (int i = 0; i < t; ++i) printf("%d ", answer[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()) h = [int(i) for i in input().split()] maxHeight = h[-1] heightRequired = [0] for i in range(len(h)-2, -1, -1): heightRequired.append(max(0, maxHeight - h[i] + 1)) maxHeight = max(maxHeight, h[i]) heightRequired.reverse() for i in range(len(heightRequired)): print(heightRequired[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> const long long INF = 2e9; const long long N = 1e5 + 1; const long long mod = 1e9 + 7; const long double eps = 1E-7; using namespace std; bool used[10001]; void solve() { int n, a[N]; vector<int> v; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; int mx = 0; for (int i = n - 1; i >= 0; --i) { if (mx < a[i]) { v.push_back(0); mx = a[i]; continue; } if (mx >= a[i]) { v.push_back(max(0, mx - a[i] + 1)); continue; } } for (int i = n - 1; i >= 0; --i) cout << v[i] << " "; } bool mtest = false; int main() { ios_base::sync_with_stdio(0); int TE = 1; if (mtest) cin >> TE; while (TE--) 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
n = int(input()) h = list(map(int, input().split())) m_h = 0 res = [] for h in h[::-1]: if m_h < h: res.append(0) m_h = h else: res.append(m_h - h + 1) print(" ".join(map(str, res[::-1])))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; void swapll(long long *a, long long *b) { long long tmp = *a; *a = *b; *b = tmp; } void swapc(char *a, char *b) { char tmp = *a; *a = *b; *b = tmp; } void solve() { long long n; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; ; vector<long long> greater_on_right(n); long long max_so_far = 0; for (long long i = n - 1; i >= 0; i--) { greater_on_right[i] = max_so_far; max_so_far = max(max_so_far, v[i]); } for (long long i = 0; i < n; i++) { v[i] = max(greater_on_right[i] - v[i] + 1, 0LL); } for (long long i = 0; i < v.size(); i++) cout << v[i] << (i + 1 < v.size() ? ' ' : '\n'); ; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 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
/******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { var Prelude_1 = __webpack_require__(1); var List = __webpack_require__(2); var n = parseInt(readline()); var xs = List.map(parseInt, readline().split(' ')); var result = []; var highest = 0; for (var i = xs.length - 1; i >= 0; i--) { result.push(Prelude_1.max(highest + 1, xs[i]) - xs[i]); highest = Prelude_1.max(highest, xs[i]); } result = List.reverse(result); print(result.join(" ")); /***/ }, /* 1 */ /***/ function(module, exports) { function min(a, b) { return a < b ? a : b; } exports.min = min; function max(a, b) { return a < b ? b : a; } exports.max = max; function curry(f) { return function (x) { return function (y) { return f(x, y); }; }; } exports.curry = curry; function uncurry(f) { return function (x, y) { return f(x)(y); }; } exports.uncurry = uncurry; function id(x) { return x; } exports.id = id; function constant(x) { return function (_) { return x; }; } exports.constant = constant; function flip(f) { return function (y) { return function (x) { return f(x)(y); }; }; } exports.flip = flip; function flip2(f) { return function (y, x) { return f(x, y); }; } exports.flip2 = flip2; function compose(g, f) { return function (x) { return g(f(x)); }; } exports.compose = compose; /***/ }, /* 2 */ /***/ function(module, exports, __webpack_require__) { var Prelude_1 = __webpack_require__(1); function add(xs, ys) { return xs.concat(ys); } exports.add = add; function head(xs) { return xs[0]; } exports.head = head; function last(xs) { return xs[xs.length - 1]; } exports.last = last; function tail(xs) { return xs.slice(1); } exports.tail = tail; function init(xs) { return xs.slice(0, xs.length - 1); } exports.init = init; function map(f, xs) { var result = new Array(xs.length); for (var i = 0; i < xs.length; i++) { result[i] = f(xs[i]); } return result; } exports.map = map; function reverse(xs) { return xs.slice().reverse(); } exports.reverse = reverse; function intersperse(x, xs) { if (xs.length == 0) { return []; } var result = new Array(xs.length + xs.length - 1); for (var i = 0; i + 1 < xs.length; i++) { result[i + i] = xs[i]; result[i + i + 1] = x; } result[result.length - 1] = xs[xs.length - 1]; return result; } exports.intersperse = intersperse; function intercalate(xs, xss) { return concat(intersperse(xs, xss)); } exports.intercalate = intercalate; function foldl(f, initial, xs) { var result = initial; for (var i = 0; i < xs.length; i++) { result = f(result, xs[i]); } return result; } exports.foldl = foldl; function foldr(f, initial, xs) { var result = initial; for (var i = xs.length - 1; i >= 0; i--) { result = f(xs[i], result); } return result; } exports.foldr = foldr; function concat(xss) { var total = sum(map(function (xs) { return xs.length; }, xss)); var result = new Array(total); var m = 0; for (var i = 0; i < xss.length; i++) { var xs = xss[i]; for (var j = 0; j < xs.length; j++) { result[m++] = xs[j]; } } return result; } exports.concat = concat; function sum(xs) { var result = 0; for (var i = 0; i < xs.length; i++) { result += xs[i]; } return result; } exports.sum = sum; function product(xs) { var result = 1; for (var i = 0; i < xs.length; i++) { result *= xs[i]; } return result; } exports.product = product; function maximum(xs) { var result = xs[0]; for (var i = 1; i < xs.length; i++) { if (result < xs[i]) result = xs[i]; } return result; } exports.maximum = maximum; function minimum(xs) { var result = xs[0]; for (var i = 1; i < xs.length; i++) { if (result > xs[i]) result = xs[i]; } return result; } exports.minimum = minimum; function replicate(n, x) { var result = new Array(n); for (var i = 0; i < result.length; i++) { result[i] = x; } return result; } exports.replicate = replicate; function take(n, xs) { return xs.slice(0, n); } exports.take = take; function drop(n, xs) { return xs.slice(n); } exports.drop = drop; function splitAt(n, xs) { return [take(n, xs), drop(n, xs)]; } exports.splitAt = splitAt; function takeWhile(f, xs) { for (var i = 0; i < xs.length; i++) { if (!f(xs[i])) { return xs.slice(0, i); } } return xs.slice(); } exports.takeWhile = takeWhile; function dropWhile(f, xs) { for (var i = 0; i < xs.length; i++) { if (!f(xs[i])) { return xs.slice(i); } } return []; } exports.dropWhile = dropWhile; function group(xs) { if (xs.length == 0) { return []; } var result = []; var last = [xs[0]]; for (var i = 1; i < xs.length; i++) { if (last[0] === xs[i]) { last.push(xs[i]); } else { result.push(last); last = [xs[i]]; } } result.push(last); return result; } exports.group = group; function filter(f, xs) { var result = []; for (var i = 0; i < xs.length; i++) { if (f(xs[i])) result.push(xs[i]); } return result; } exports.filter = filter; function zip(xs, ys) { var n = Prelude_1.min(xs.length, ys.length); var result = new Array(n); for (var i = 0; i < n; i++) { result[i] = [xs[i], ys[i]]; } return result; } exports.zip = zip; function unzip(xs) { var r1 = new Array(xs.length); var r2 = new Array(xs.length); for (var i = 0; i < xs.length; i++) { r1[i] = xs[i][0]; r2[i] = xs[i][1]; } return [r1, r2]; } exports.unzip = unzip; /***/ } /******/ ]);
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
//package Codeforces; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; /** * Created by mudit on 23/9/17. */ public class P13B581 { private static void solve() throws IOException{ Reader r = new Reader(); PrintWriter w = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); r.init(System.in); int n = r.ni(); int[] a = r.nia(n); int[] b = new int[n]; int max = a[n-1]; for (int i=n-2; i>=0; i--){ max = Math.max(max, a[i+1]); if(max>=a[i]) b[i]=max-a[i]+1; } for (int i=0; i<n; i++) w.print(b[i]+" "); w.flush(); } public static void main(String[] args) throws IOException{ solve(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static BigInteger big; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String rl() throws IOException{ String s = reader.readLine(); return s; } static int ni() throws IOException { return Integer.parseInt( next() ); } static double nd() throws IOException { return Double.parseDouble( next() ); } static long nl() throws IOException { return Long.parseLong( next() ); } static BigInteger nb() throws IOException{ return big = new BigInteger( next() ); } static int[] nia(int n) throws IOException{ int[] a = new int[n]; for(int i=0; i<n; i++) a[i]=ni(); return a; } static long[] nla(long n) throws IOException{ long[] a = new long[(int)n]; for(int i=0; i<n; i++) a[i]=nl(); return a; } static double[] nda(int n) throws IOException{ double[] a = new double[n]; for(int i=0; i<n; i++) a[i]=nd(); return a; } static char[] nca() throws IOException{ char[] a = next().toCharArray(); return a; } static BigInteger[] nba(int n) throws IOException{ BigInteger[] a = new BigInteger[n]; for(int i=0; i<n; i++) a[i]=nb(); return a; } static int[][] ni2(int m, int n) throws IOException{ int[][] a = new int[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ a[i][j]=ni(); } } return a; } static long[][] nl2(long m, long n) throws IOException{ long[][] a = new long[(int)m][(int)n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ a[i][j]=nl(); } } return a; } static double[][] nd2(int m, int n) throws IOException{ double[][] a = new double[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ a[i][j]=nd(); } } return a; } static BigInteger[][] nb2(int m, int n) throws IOException{ BigInteger[][] a = new BigInteger[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ a[i][j]=nb(); } } return a; } }
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
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = s.nextInt(); int max = arr[n-1]; arr[n-1] =0; for(int i=n-2;i>=0;i--) { if(arr[i]>max) { max = arr[i]; arr[i] =0; } else { arr[i] = max - arr[i] + 1; } } for(int i=0;i<n;i++) System.out.print(arr[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; template <class T> T power(T N, T P) { return (P == 0) ? 1 : N * power(N, P - 1); } int main() { long long int n; long long int ara[100100]; cin >> n; long long int boro = -1; long long int maxi[100100]; memset(maxi, -1, sizeof maxi); for (long long int i = 0; i < n; i++) { cin >> ara[i]; } for (long long int i = n - 1; i >= 0; i--) { boro = max(boro, ara[i]); maxi[i] = boro; } for (long long int i = 0; i < n; i++) { if (maxi[i] > maxi[i + 1]) printf("0 "); else printf("%lld ", maxi[i] - ara[i] + 1); } puts(""); }
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
//package solution; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solution implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); final boolean OJ = System.getProperty("ONLINE_JUDGE") != null; void init() throws FileNotFoundException { if (OJ) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } public void run() { try { init(); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } public static void main(String[] args) { new Thread(null, new Solution(), "", 256 * 1l << 20).start(); } void solve() throws IOException { int n; n = readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } int[] h = new int[n]; h[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; i--) { h[i] = Math.max(h[i + 1], a[i]); } for (int i = 0; i < n; i++) { if (i != n - 1) { out.print(Math.max(0, h[i + 1] + 1 - a[i]) + " "); } else { 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
#include <bits/stdc++.h> using namespace std; long long int n, a[100005], m, s, x, y, z; string k; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = n; i > 0; i--) { if (a[i] > m) z = 0; else z = 1; m = max(a[i], m); a[i] = m - a[i] + z; } for (int i = 1; i <= n; i++) cout << a[i] << " "; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> int main() { long long n, max, i; scanf("%lld", &n); long long a[n], b[n]; for (i = 0; i < n; i++) scanf("%lld", &a[i]); b[n - 1] = 0; max = a[n - 1]; for (i = n - 2; i >= 0; i--) { if (max >= a[i]) { b[i] = max - a[i] + 1; } else if (max < a[i]) { b[i] = 0; max = a[i]; } } for (i = 0; i < n; i++) printf("%lld ", 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
#include <bits/stdc++.h> using namespace std; int x; int main() { scanf("%i", &x); int *A1 = new int[x]; for (int i = 0; i < x; i++) scanf("%i", &A1[i]); int mx = A1[x - 1], sum = 0; vector<int> A2; A2.push_back(0); for (int i = x - 2; i > -1; i--) { A2.push_back(max(0, mx - A1[i] + 1)); mx = max(mx, A1[i]); } for (int i = A2.size() - 1; i > -1; i--) printf("%i ", A2[i]); return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, h, m, mx; vector<int> vp; cin >> n; m = n; while (n--) { cin >> h; vp.push_back(h); } mx = vp[m - 1]; vp[m - 1] = 0; for (int i = m - 2; i >= 0; i--) { if (vp[i] <= mx) vp[i] = mx - vp[i] + 1; else if (vp[i] > mx) { mx = vp[i]; vp[i] = 0; } } for (int i = 0; i < m; i++) cout << vp[i] << ' '; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * Built using CHelper plug-in * Actual solution is at the top * @author Tifuera */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) { h[i] = in.nextInt(); } int max = h[n - 1]; h[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (h[i] <= max) { h[i] = max + 1 - h[i]; } else { max = h[i]; h[i] = 0; } } for (int x : h) { out.print(x + " "); } } } class InputReader { private BufferedReader reader; private String[] currentArray; private int curPointer; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } public int nextInt() { if ((currentArray == null) || (curPointer >= currentArray.length)) { try { currentArray = reader.readLine().split(" "); } catch (IOException e) { throw new RuntimeException(e); } curPointer = 0; } return Integer.parseInt(currentArray[curPointer++]); } }
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())) v=0 S=[] for i in range(n-1,-1,-1) : if l[i]<=v : S.append(abs(l[i]-v)+1) else : S.append(0) if l[i]>v : v=l[i] S1=[] for i in range(n-1,-1,-1) : S1.append(str(S[i])) print(' '.join(S1))
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 Solution{ public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long[] arr = new long[n]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < arr.length; ++i){ arr[i] = Long.parseLong(st.nextToken()); } long max = arr[n-1]; long[] temp = new long[n]; for(int i = n-2; i >= 0 ; --i){ if(arr[i] > max)max=arr[i]; else if(arr[i] == max)temp[i] = 1; else temp[i] = max - arr[i]+1; } for(long i : temp){ System.out.print(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
if __name__ == '__main__': n = int(input()) l = list(map(int, input().split())) a = [] max1 = 0 for i in range (n-1,-1,-1): if l[i] > max1 : max1 = l[i] a.append(0) else: a.append((max1-l[i])+1) a.reverse() print(*a)
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Erasyl Abenov * * */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); try (PrintWriter out = new PrintWriter(outputStream)) { TaskB solver = new TaskB(); solver.solve(1, in, out); } } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long a[] = new long[n]; for(int i = 0; i < n; ++i){ a[i] = in .nextInt(); } long max = a[n - 1]; long b[] = new long[n]; for(int i = n - 2; i >= 0; --i){ long sum = 0; if(a[i] <= max){ // if(max + 1 - a[i] <= n){ sum = max + 1 - a[i]; // } }else{ max = a[i]; } b[i] = sum; } for(int i = 0; i < n; ++i){ out.print(b[i] + " "); } } } class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; public class bucky{ public static void main (String args[]) { Scanner input=new Scanner(System.in); HashMap<Integer, Integer> map= new HashMap<Integer, Integer>(); int n=input.nextInt(); Long nums[]=new Long[n]; Long valori[]=new Long[n]; Long max=Long.valueOf(-1); for (int i=0;i<n;i++) nums[i]=input.nextLong(); valori[n-1]=Long.valueOf(0); for(int i=n-2;i>=0;i--) if(nums[i+1]>valori[i+1]) valori[i]=nums[i+1]; else valori[i]=valori[i+1]; for(int i=0;i<n;i++) if(nums[i]>valori[i]) System.out.print("0 "); else {Long rez=valori[i]-nums[i]+1; System.out.print(rez+" ");} } }
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 a[100005]; int main() { int N; scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%ld", &a[i]); long maxi = INT_MIN; for (int i = N; i >= 1; --i) { if (a[i] > maxi) { maxi = a[i]; a[i] = 0; } else if (a[i] < maxi) { a[i] = maxi - a[i] + 1; } else { a[i] = 1; } } for (int i = 1; i <= N; ++i) printf("%ld ", a[i]); printf("\n"); }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, mx = 0; cin >> n; vector<long long> v(n), v2(n); if (n == 1) { cout << 0; return 0; } for (int i = 0; i < n; i++) { cin >> v[i]; } mx = v[n - 1]; v2.push_back(0); for (int i = n - 2; i >= 0; i--) { if (v[i] - mx == 0) { v2.push_back(1); } else if (v[i] - mx < 0) { v2.push_back(abs(v[i] - mx) + 1); } else v2.push_back(0); if (v[i] > mx) mx = v[i]; } reverse(v2.begin(), v2.end()); for (int i = 0; i < n; i++) cout << v2[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.*; import java.util.*; import java.math.*; public class Div2_322_B { public static void main(String[] args) throws IOException { BufferedInputStream bis = new BufferedInputStream(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(bis)); int n = Integer.parseInt(br.readLine().trim()); int [] floors = new int [n]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < floors.length; i++) { floors [i] = Integer.parseInt(st.nextToken()); } int max = floors[n-1]; floors [n-1] = 0; for (int i = n-2; i >= 0; i--) { int temp = floors [i]; floors [i] = Math.max(0, max - floors[i] + 1); max = Math.max(max, temp); } StringBuilder sb = new StringBuilder(""); sb.append(floors[0]); for(int i = 1;i < n;i++) sb.append(" "+floors[i]); System.out.println(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 i, j, a[100005], b[100005], n, s, sm; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sm = 0; for (i = n - 1; i >= 0; i--) { b[i] = sm; if (a[i] > sm) sm = a[i]; } for (i = 0; i < n; i++) { if (a[i] <= b[i]) cout << b[i] - a[i] + 1 << " "; else cout << 0 << " "; } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(raw_input()) r = raw_input() l = r.split(' ') l.reverse() L = [] highest = 0 for y in l: x = int(y) if x > highest: highest= x L.append('0') else: L.append(str(highest-x+1)) L.reverse() print ' '.join(L)
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class CF_322_B { public static void main(String[] args) throws IOException{ PrintWriter pw = new PrintWriter(System.out, true); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(input.readLine()); int[] N = new int[n]; boolean[] m = new boolean[n]; int[] max = new int[n]; String[] line = input.readLine().split(" "); for(int i = 0; i < n; i++){ N[i] = Integer.parseInt(line[i]); } int maxx = -1; for(int i = n - 1; i >= 0; i--){ if(N[i] > maxx){ max[i] = N[i]; m[i] = true; maxx = max[i]; }else{ max[i] = maxx; } } for(int i = 0; i < n; i++){ if(m[i]){ pw.print(0+" "); }else{ pw.print((max[i] - N[i] + 1)+" "); } } pw.println(); pw.close(); input.close(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) L = list(map(int, input().split())) R = ["" for _ in range(n)] R[n-1] = "0" m = L[n-1] for k in range(n-2,-1,-1): if m >= L[k]: R[k] = str(m-L[k]+1) else: R[k] = "0" m = max(L[k],m) print(" ".join(R))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1], i, g; for (i = 1; i <= n; ++i) cin >> a[i]; g = a[n]; for (i = n - 1; i > 0; --i) { if (a[i] > g) { g = a[i]; a[i] = 0; } else if (a[i] <= g) { a[i] = (g + 1 - a[i]); } } a[n] = 0; for (i = 1; i <= n; ++i) cout << 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
#include <bits/stdc++.h> using namespace std; inline double min(double a, double b) { if (a < b) return a; return b; } int n; int arr[100010]; int dp[100010]; int main() { cin >> n; for (int(i) = (0); (i) < (n); i++) cin >> arr[i]; dp[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { dp[i] = max(arr[i + 1], dp[i + 1]); } for (int i = 0; i < n; i++) { if (arr[i] > dp[i]) cout << 0 << " "; else cout << dp[i] + 1 - arr[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()) a=list(map(int,input().split())) b=(n-1) c=[] c.append(0) for i in range(n-2,-1,-1): if a[i]<=a[b]: c.append(a[b]-a[i]+1) else: b=i c.append(0) print(*c[::-1])
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e5 + 10; long long n, a[MAX]; long long Max[MAX]; int main() { cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; Max[n] = a[n]; for (long long i = n - 1; i >= 1; i--) { if (a[i] > Max[i + 1]) Max[i] = a[i]; else Max[i] = Max[i + 1]; } for (long long i = 1; i <= n; i++) { if (a[i] > Max[i + 1]) cout << 0 << " "; else cout << (Max[i + 1] + 1 - a[i]) << " "; } }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n, maxf; cin >> n; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; maxf = h[n - 1]; h[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (h[i] > maxf) { maxf = h[i]; h[i] = 0; } else h[i] = maxf - h[i] + 1; } for (int i = 0; i < n; i++) cout << h[i] << " "; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, i, c = 0; scanf("%i", &n); vector<int> b(n), a(n); for (i = 0; i < n; i++) scanf("%i", &b[i]); for (i = b.size() - 1; i >= 0; c = max(c, b[i--])) a[i] = max(0, c + 1 - b[i]); for (int i = 0; i < n; i++) printf("%i ", a[i]); }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int n, Max, a[100005], b[100005]; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { b[i] = max(0, Max - a[i] + 1); Max = max(Max, a[i]); } 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
import java.util.*; public class solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } int max=Integer.MIN_VALUE; int ans[]=new int[n]; for(int i=n-1;i>=0;i--) { if(a[i]>max) { max=a[i]; ans[i]=0; } else if(max==a[i]) { ans[i]=1; } else { ans[i]=max+1-a[i]; } } for(int i=0;i<n;i++) { System.out.print(ans[i]+" "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
N = int(input()) H = list(map(int, input().split())) maxh = H[-1] maxlst = [] for i in range(N): maxh = max(maxh, H[N - i - 1]) maxlst.append(maxh) maxlst.reverse() ans = [] for i in range(N): if i == N - 1: a = 0 else: maxv = maxlst[i + 1] if H[i] == maxv: a = 1 elif H[i] > maxv: a = 0 else: a = maxv - H[i] + 1 ans.append(a) print(" ".join(map(str, ans)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class CFLuxHouse { public static void main(String[] args)throws IOException { // TODO Auto-generated method stub BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); int noofHouses = Integer.parseInt(buff.readLine()); String[] floorOfHouses = buff.readLine().split(" "); int array[] = new int[noofHouses]; int floorVal[] = new int[noofHouses]; for(int i=0;i<noofHouses;i++){ array[i] = Integer.parseInt(floorOfHouses[i]); } int max = array[noofHouses-1]; for(int i=noofHouses-2;i>=0;i--){ if(max>=array[i]){ floorVal[i] = max - array[i]+1; } else{ max = array[i]; } } for(int i=0;i<noofHouses;i++){ System.out.print(floorVal[i]+" "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Scanner; public class B321 { public static void main(String[] args) { int n; Scanner in=new Scanner(System.in); n=in.nextInt(); int[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++){ a[i]=in.nextInt(); } int m=0; for (int i=n-1;i>-1;i--) { b[i] = Math.max(0, m - a[i] + 1); m = Math.max(m,a[i]); } for(int i=0;i<n;i++){ System.out.print(b[i]); System.out.print(" "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] h = new int[n]; for (int i = 0; i < h.length; i++) { h[i] = sc.nextInt(); } System.out.println(String.join(" ", Arrays.stream(solve(h)).mapToObj(String::valueOf).toArray(String[]::new))); sc.close(); } static int[] solve(int[] h) { int[] result = new int[h.length]; int max = -1; for (int i = result.length - 1; i >= 0; i--) { result[i] = Math.max(0, max + 1 - h[i]); max = Math.max(max, h[i]); } return result; } }
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 ar[n], ans[n]; for (int i = 0; i < n; i++) cin >> ar[i]; int maxi = 0; for (int i = n - 1; i >= 0; i--) { ans[i] = max(maxi - ar[i] + 1, 0); maxi = max(maxi, ar[i]); } 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.*; import java.io.*; import java.util.Scanner; public class main { public static void main (String [] args) throws Exception { new main(); } BufferedReader in; PrintWriter out; StringTokenizer st; public String next() throws Exception { //takes next word from input if (st == null || !st.hasMoreElements()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public main() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public void solve() throws Exception { int n = nextInt(); long A[] = new long[n]; long ans[] = new long[n]; for (int i = 0; i < n; ++i) { A[i] = nextInt(); } long max = -1000; for (int i = n - 1; i >= 0; --i) { if (A[i] > max) { max = A[i]; ans[i] = 0; } else if (A[i] <= max) { ans[i] = max - A[i] + 1; } } for (int i = 0; i < n; ++i) { out.print(ans[i] + " "); } } } //1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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 nn[100010]; bool bb[100010]; long long maxx[100010]; int main() { int n; while (scanf("%d", &n) != EOF) { memset(bb, 0, sizeof(bb)); memset(nn, 0, sizeof(nn)); memset(maxx, -1, sizeof(maxx)); for (int i = 0; i < n; i++) { scanf("%I64d", &nn[i]); } for (int i = n - 1; i >= 0; i--) { if (nn[i] > maxx[i + 1]) { maxx[i] = nn[i]; bb[i] = 1; } else maxx[i] = maxx[i + 1]; } for (int i = 0; i < n; i++) { if (i != 0) printf(" "); if (bb[i] == 1) printf("0"); else printf("%I64d", maxx[i] + 1 - nn[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
#!/usr/bin/python raw_input() houses = [int(i) for i in raw_input().strip().split(" ")] N = len(houses) maxh = 0 rs = [0] * N for i in xrange(N-1, -1, -1): # print i h = houses[i] if maxh < h: maxh = h r = 0 else: r = maxh + 1 - h rs[i] = str(r) print " ".join(rs)
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1]; a[n] = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> res; for (int i = n - 1; i >= 0; --i) { res.push_back(max(0, a[i + 1] - a[i] + 1)); a[i] = max(a[i], a[i + 1]); } for (int i = n - 1; i >= 0; --i) { cout << res[i] << " "; } }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int a[100020]; long long b[100020]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long max = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] <= max) b[i] = max - a[i] + 1; else max = a[i]; } 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 #-*- coding: utf-8 -*- def main_fun(): n = int(raw_input().strip()) line = raw_input().strip().split(' ') d = {n-1:0} for x in range(n-2,-1,-1): if int(line[x+1]) > d[x+1]: d[x] = int(line[x+1]) else: d[x] = d[x+1] for x in range(n): if int(line[x]) > d[x]: d[x] = 0 else: d[x] = d[x] - int(line[x]) + 1 print d[x], if __name__== '__main__': main_fun()
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=input() l=[int(i) for i in raw_input().split()] m=-1000000007 k=[] for i in range(n-1,-1,-1): x=l[i] if x>m: m=x k.append(0) else: k.append(m+1-x) for i in range(n): print k.pop(),
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.InputMismatchException; public class Main { class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } 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; } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } public void foo() throws IOException { //BufferedInputStream in = new BufferedInputStream(new FileInputStream("in.txt")); //System.setIn(in); MyScanner scan = new MyScanner(); int n = scan.nextInt(); int[] h = new int[n]; for(int i = 0;i < n;++i) { h[i] = scan.nextInt(); } int maxHeight = -1; for(int i = n - 1;i >= 0;--i) { if(h[i] > maxHeight) { maxHeight = h[i]; h[i] = 0; } else { h[i] = maxHeight - h[i] + 1; } } for(int i = 0;i < n;++i) { System.out.print(h[i] + " "); } System.out.println(); } public static void main(String[] args) throws IOException { new Main().foo(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; import java.io.*; import java.util.stream.*; public class b { private void solve() throws IOException { int n = nextInt(); Integer[] a = new Integer[n]; for(int i = 0; i < n; ++i) { a[i] = nextInt(); } int curMax = 0; for(int i = n - 1; i >= 0; --i) { int t = a[i]; if(a[i] <= curMax) { a[i] = (curMax - a[i]) + 1; } else { a[i] = 0; } curMax = Math.max(curMax, t); } System.out.println(String.join(" ", Arrays.asList(a).stream().map(v -> String.valueOf(v)).collect(Collectors.toList()))); } static void debug(Object...o) { System.err.println(Arrays.deepToString(o)); } BufferedReader br; StringTokenizer st; PrintWriter out; String next() throws IOException { while (st == null || !st.hasMoreTokens()) { String s = br.readLine(); if(s != null) { st = new StringTokenizer(s); } } if(st.hasMoreTokens()) { return st.nextToken(); } else { return null; } } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } private void run() throws IOException { //br = new BufferedReader(new FileReader("b.in")); br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //out = new PrintWriter("b.out"); solve(); out.close(); } public static void main(String[] args) throws IOException { new b().run(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long power(long long a, long long b) { long long ret = 1; while (b) { if (b % 2 == 1) ret = (ret * a) % ((int)(1e9) + 7); b /= 2; a = (a * a) % ((int)(1e9) + 7); } return ret; } int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int dp[n + 1]; dp[n] = 0; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) dp[i] = a[i]; else dp[i] = max(a[i], dp[i + 1]); } for (int i = 0; i < n; i++) printf("%d ", max(0, dp[i + 1] - a[i] + 1)); return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int sol[n]; fill(sol, sol + n, 0); int mx = a[n - 1]; sol[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (a[i] <= mx) { sol[i] = mx + 1 - a[i]; } mx = max(a[i], mx); } for (int i = 0; i < n; i++) { cout << sol[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
//Author: Mo Abjal, MJP Rohilkhand University Bareilly, UP, India 2018. /////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////********************////////////////////////////////////////////// /////////////////////////////////////////* SOLUTION *////////////////////////////////////////////// /////////////////////////////////////////********************////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.util.*; import java.io.*; import java.text.*; import java.math.*; public class Main{ public static StringTokenizer token = new StringTokenizer(""); public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter output = new PrintWriter(System.out); public static void main(String[] afzal) throws Exception { int N = ni(); Integer A[] = ai(N); Integer B[] = new Integer[N]; B[N-1] = A[N-1]; for(int i=N-2;i>=0;i--){ B[i] = Math.max(B[i+1],A[i]); } for(int i=0;i<N-1;i++){ if(A[i]<=B[i+1]){ output.print(B[i]-A[i]+1+" "); } else{ output.print(0+" "); } } output.println(0); output.close(); /*int N = ni(); for(int i=0;i<N;i++){ int count=0; int L = ni(); int v = ni(); int l = ni(); int r = ni(); if(1%v==0) count++; if(l%v==0) count++; if(r%v==0) count++; if(L%v==0) count++; count+=l/v; count+=(L-r)/v; int x = (L-(r-l+1))/v; output.println(count); } output.close();*/ /*int N = ni(); boolean flag = true; int x = N; while(x>0){ if(x%10==0 || x%10==1){ } else{ flag = false; break; } x/=10; } if(flag){ output.println("1\n"+N); output.close(); return; } String temp = ""; int count = 1; for(int i=0;count<=ps(N).length();i++){ temp+="1"; count++; } if(temp=="") temp="1"; int test = pi(temp); int ans=0; int A[] = new int[N+5]; int j=0; //output.println(test); while(N>0){ if(N>=test){ A[j] = test; N-=test; ans++; j++; //System.out.println(A[j]+" "); } else if(test%10==0 && (test/10)%10==1){ test-=9; } else if(test%10==1 && (test/10)%10==0){ test-=90; } else if(test%10==1 && (test/10)%10==1){ test--; } } output.println(ans); for(int i=0;A[i+1]!=0;i++){ output.print(A[i]+" "); } output.println(test); output.close();*/ } public static String pc(char c){ return Character.toString(c); } public static Integer pci(char c){ return pi(Character.toString(c)); } public static Integer pi(String str){ return Integer.parseInt(str); } public static Long pl(String str){ return Long.parseLong(str); } public static String ps(Integer N){ return Integer.toString(N); } public static char[] pcc(String str){ return str.toCharArray(); } public static Integer ni() throws IOException{ if(!token.hasMoreElements()){ token = new StringTokenizer(br.readLine()); } return Integer.parseInt(token.nextToken()); } public static Long nl() throws IOException{ if(!token.hasMoreElements()){ token = new StringTokenizer(br.readLine()); } return Long.parseLong(token.nextToken()); } public static Double nd() throws IOException{ if(!token.hasMoreElements()){ token = new StringTokenizer(br.readLine()); } return Double.parseDouble(token.nextToken()); } public static String ns() throws IOException{ return new StringTokenizer(br.readLine()).nextToken(); } public static Integer[] ai(int N) throws IOException{ Integer A[] = new Integer[N]; token = new StringTokenizer(br.readLine()); for(int i=0;i<A.length;i++){ A[i] = pi(token.nextToken()); } return A; } public static long[] al(int N) throws IOException{ long A[] = new long[N]; token = new StringTokenizer(br.readLine()); for(int i=0;i<N;i++){ A[i] = Long.parseLong(token.nextToken()); } return A; } public static char[][] acc(int N,int M) throws IOException{ char C[][] = new char[N][M]; for(int i=0;i<N;i++){ token = new StringTokenizer(br.readLine()); String s = token.nextToken(); for(int j=0;j<M;j++){ C[i][j] = s.charAt(j); } } return C; } public static String[] as(int N) throws IOException{ String S[] = new String[N]; token = new StringTokenizer(br.readLine()); for(int i=0;i<S.length;i++){ S[i] = token.nextToken(); } return S; } public static long fact(long N){ long fact = 1; for(int i=1;i<=N;i++){ fact = fact * i; } return fact; } public static BigInteger factB(int N){ BigInteger b = new BigInteger("1"); for(int i=1;i<=N;i++){ b = b.multiply(BigInteger.valueOf(i)); } return b; } public static long sod(long N){ long sum = 0; while(N>0){ long x = N%10; sum = sum + x; N = N/10; } return sum; } public static Integer cd(long N){ int count=0; while(N>0){ N = N/10; count++; } return count=0; } } class Pair implements Comparable{ int a,b; public Pair(int a,int b){ this.a = a; this.b = b; } public int compareTo(Object p){ Pair pa = (Pair)p; if(this.a==pa.a) return this.b - pa.b; return this.a - pa.a; } } //Copyright Reserved - Mo Abjal.
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
var numeric = readline(), floor = readline().split(' '), result = [0]; for (; floor.length; ) { var last_floor = +floor[floor.length - 1], prev_floor = +floor[floor.length - 2]; if (prev_floor > last_floor) { result.unshift(0); floor.splice(floor.length - 1, 1); } else { if (last_floor === prev_floor) { result.unshift(1); floor.splice(floor.length - 1, 1); } else { result.unshift( (last_floor - prev_floor) + 1 ); floor.splice(floor.length - 2, 1); } } } result.splice(0,1); print(result.join(' '));
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int a[n], b[n], max = 0; b[n - 1] = 0; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = n - 1; i >= 0; i--) { if (i == n - 1) max = a[i]; else { if (a[i] <= max) { b[i] = max - a[i] + 1; } else { max = a[i]; b[i] = 0; } } } for (long long int i = 0; i < n; i++) cout << b[i] << " "; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n, houses = int(raw_input()), map(int, raw_input().split()) m, l = houses[n-1], [0] for i in xrange(n-2, -1, -1): l.append(0 if houses[i] > m else m + 1 - houses[i]) m = max(m, houses[i]) print ' '.join(map(str, reversed(l)))
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.Scanner; public class B581 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int max = a[n-1]; int[] add = new int[n]; for (int i = n-2; i >= 0; i--) { if (a[i] <= max) { add[i] = max - a[i] + 1; } max = max>a[i] ? max : a[i]; } for (int i = 0; i < n; i++) { System.out.print(add[i] + " "); } System.out.println(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
input() a=map(int,raw_input().split()) r,m=[],0 for d in a[::-1]: r+=max(m+1-d,0), m=max(m,d) for d in r[::-1]: print d,
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().strip()) houses = map(int, raw_input().strip().split(' ')) answers = [-1] * n maxfloors = -1 for i in xrange(len(houses) - 1, -1, -1): answers[i] = max(maxfloors + 1 - houses[i], 0) if houses[i] > maxfloors: maxfloors = houses[i] print ' '.join(map(str, answers))
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 20; int a[MAXN], ans[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, maximum = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; ans[n - 1] = 0, maximum = max(maximum, a[n - 1]); for (int i = n - 2; i >= 0; i--) { maximum = max(maximum, a[i + 1]); if (a[i] <= maximum) ans[i] = maximum - a[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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { int a; cin >> a; v.push_back(a); } int Max = v.back(); v.back() = 0; for (int i = v.size() - 2; i >= 0; i--) { if (v[i] < Max) v[i] = ((Max - v[i]) + 1); else if (v[i] == Max) v[i] = 1; else if (v[i] > Max) { Max = v[i]; v[i] = 0; } } for (int i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other β€” the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≀ n ≀ 105) β€” the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≀ hi ≀ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int arr[100000 + 7], mx, d, ans[100000 + 7], n, i, j, k; while (scanf("%lld", &n) == 1) { for (i = 1; i <= n; i++) { scanf("%lld", &arr[i]); } mx = 0; for (i = n; i > 0; i--) { if (arr[i] > mx) { mx = arr[i]; ans[i] = 0; } else { d = mx - arr[i]; d++; ans[i] = d; } } for (i = 1; i < n; i++) { printf("%lld ", ans[i]); } printf("%lld\n", ans[i]); } return 0; }
CPP