Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.*; import java.util.Stack; import java.util.StringTokenizer; public class Main { static BufferedReader bufferedReader; static PrintWriter printWriter; static StringTokenizer stringTokenizer; static void solve() throws Exception { int houses = nextInt(); int max; int[] floors = new int[houses + 5]; Stack<Integer> stack = new Stack<Integer>(); stack.push(0); for(int i = 0; i < houses; i++) floors[i] = nextInt(); max = floors[houses - 1]; for(int i = houses - 2; i >= 0; i--) { if(floors[i] < max) { stack.push(max - floors[i] + 1); } else if(floors[i] == max) { stack.push(1); } else { stack.push(0); max = floors[i]; } } while (!stack.empty()) { printWriter.print(stack.pop() + " "); } printWriter.flush(); } public static void main(String[] args) { try { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); printWriter = new PrintWriter(new BufferedOutputStream(System.out)); solve(); bufferedReader.close(); printWriter.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } return stringTokenizer.nextToken(); } static long nextLong() throws Exception { return Long.parseLong( next() ); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100009], b[100009], i, flag = 0; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; for (i = n - 1; i >= 0; i--) { if (a[i] > flag) b[i] = -1; else b[i] = max(a[i], flag); flag = max(flag, a[i]); } for (i = 0; i < n; i++) { if (i != n - 1) { if (b[i] == -1) cout << "0" << " "; else cout << b[i] - a[i] + 1 << " "; } else { if (b[i] == -1) cout << "0"; else cout << b[i] - a[i] + 1; } } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { size_t n; cin >> n; vector<int> houses(n); for (size_t i = 0; i < n; ++i) { cin >> houses[i]; } vector<int> res(n); int max_elem = 0; for (size_t i = 0; i < n; ++i) { size_t j = n - 1 - i; if (houses[j] > max_elem) { max_elem = houses[j]; res[j] = 0; } else { res[j] = max_elem - houses[j] + 1; } } 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
n = int(input()); li = list(map(int, input().split(" "))) lu = [] k = 0 for i in range(n-1, -1, -1): lu.append(max(0, k+1-li[i])) k = max(k, li[i]) lu.reverse() print(' '.join(map(str, lu)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; void solve() { int n, m = 0; cin >> n; vector<pair<int, int>> a(n); for (auto &u : a) cin >> u.first; a[n - 1].second = 0; for (int i = n - 2; i >= 0; i--) { m = max(m, a[i + 1].first); a[i].second = max(m, a[i + 1].first); } for (auto &u : a) { cout << (u.first > u.second ? 0 : u.second - u.first + 1) << " "; } } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); solve(); }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Scanner; public class codeforces581B { public static void main(String[] args) { Scanner s=new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n=s.nextInt(),i; int[] a = new int[n]; int[] m = new int[n]; for(i=0;i<n;i++) a[i]=s.nextInt(); m[n-1]=a[n-1]; for(i=n-2;i>=0;i--) m[i]=Math.max(m[i+1],a[i+1]); for(i=0;i<n-1;i++) { if(a[i]<=m[i]) sb.append(m[i]-a[i]+1); else sb.append(0); sb.append(" "); } sb.append(0); System.out.print(sb); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#~ import io #~ import sys #~ import time #~ start = time.clock() #~ test = '''5 #~ 1 2 3 1 2''' #~ test = '''4 #~ 3 2 1 4''' #~ sys.stdin = io.StringIO(test) n = int(input()) floors = list(map(int, input().split())) #~ print(floors) ma = floors[:] max_so_far = float("-inf") for i in range(n-1,-1,-1): ma[i] = max(0,1+max_so_far-floors[i]) max_so_far = max(max_so_far,floors[i]) print(" ".join(map(str,ma)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const double PI = acos(-1.0); const int MOD = 1000 * 1000 * 1000 + 7; const int INF = 2000 * 1000 * 1000; const int MAXN = 100010; template <typename T> inline T sqr(T n) { return n * n; } int n; int a[MAXN], ans[MAXN], suff; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = n; i > 0; i--) { suff = max(suff, a[i + 1]); ans[i] = max(0, suff + 1 - a[i]); } for (int i = 1; i <= n; i++) { printf("%d ", ans[i]); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; long long a[100100], n, b[100100], mx = -1e10; set<pair<int, int> > ss; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; b[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; i--) { b[i] = max(b[i + 1], a[i]); } for (int i = 0; i < n - 1; i++) { a[i] > b[i + 1] ? cout << 0 << ' ' : cout << b[i + 1] - a[i] + 1 << ' '; } cout << 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(raw_input()) l = map(int, raw_input().split()) m = [0]*n max_value = 0 for i in xrange(n-1, -1,-1): max_value = max(max_value, l[i]) m[i] = max_value for i in xrange(n-1): ans = m[i+1] - l[i] if ans < 0: ans = 0 else: ans += 1 print ans, print 0
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n=int(raw_input()) l=map(int,raw_input().split()) ans=[0] cnt,chk=l[-1],1 for i in l[-2::-1]: if i<=cnt: ans.append(cnt-i+1) else: cnt=i ans.append(0) print ' '.join(map(str,ans[::-1]))
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; public class test { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; int[] anss = new int[n]; int[] dh = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); dh[n-1] = 0; anss[n-1]=0; String ans = ""; for (int j = n - 2; j >= 0; j--) { if (arr[j] <= arr[j+1]) { dh[j] = arr[j+1] - arr[j] + 1; arr[j] = arr[j+1]; anss[j] = dh[j]; } else { dh[j] = 0; anss[j] = dh[j]; } } ans=""; for(int i=0; i<n; i++){ //System.out.print(anss[i]+" "); ans=ans.concat(Integer.toString(anss[i]).concat(" ")); if((i+1)%3==0){ System.out.print(ans); ans=""; } } System.out.print(ans); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; int max_r; int b[n]; fill(b, b + n, 0); for (int i = 0; i < n; i++) { cin >> arr[i]; } max_r = arr[n - 1]; for (int i = n - 2; i >= 0; i--) { if (max_r < arr[i]) { max_r = arr[i]; } else b[i] = max_r - arr[i] + 1; } 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
n = int(raw_input()) inp = raw_input() inp =inp.split() houses = [int(i) for i in inp] added=[0 for i in inp] max=0 for i in range(n-1,-1,-1): if houses[i]<=max: added[i]= max - houses[i]+1 if houses[i]>max: max=houses[i] for i in added: print i,
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int a[N], b[N]; int main() { int n; while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int mmax = a[n]; b[n] = 0; for (int i = n - 1; i > 0; i--) { if (a[i] == mmax) b[i] = 1; else if (a[i] > mmax) b[i] = 0, mmax = a[i]; else b[i] = mmax - a[i] + 1; } for (int i = 1; i < n; i++) { printf("%d ", b[i]); } printf("%d\n", b[n]); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) h = list(map(int, input().split())) cur = 0 l = [0] * n for i in range(n - 1, -1, -1): l[i] = max(0, cur + 1 - h[i]) cur = max(cur, h[i]) print (" ".join(map(str, l)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int b, d, n, m, i, j, v[2100000], f[100001], maxi, a, ul, nrq = 1, nrr = 1, newq, newr, ls, ld; char s[100005], z; vector<int> c; double r, t, q, ff, pri; bool cmm(int a, int b) { if (a > b) return 1; return 0; } int main() { cin >> n; for (i = 1; i <= n; i++) cin >> v[i]; for (i = n; i; i--) { a = v[i]; if (v[i] <= maxi) v[i] = maxi - v[i] + 1; else v[i] = 0; maxi = max(maxi, a); } for (i = 1; i <= n; 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
#!/usr/bin/python if __name__ == '__main__': size = int(raw_input().strip()) arr = [int(x) for x in raw_input().strip().split(" ")] max_num = -1 op = [] for i in reversed(arr): if i == max_num: op.append(1) elif i > max_num: op.append(0) max_num = i else: op.append(max_num-i+1) for x in reversed(op): print str(x),
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const long long Nmax = 1000000000; int n; vector<long long> a; vector<long long> tree; void add(int x, int i, int l, int r, int pos) { if (i < l || i > r) return; if (l == r) { tree[pos] = x; return; } int mid = (l + r) / 2; add(x, i, l, mid, pos * 2); add(x, i, mid + 1, r, pos * 2 + 1); tree[pos] = max(tree[pos * 2], tree[pos * 2 + 1]); } long long get_max(int u, int v, int l, int r, int pos) { if (v < l || r < u) return -999999999999999999; if (u <= l && r <= v) return tree[pos]; int mid = (l + r) / 2; return max(get_max(u, v, l, mid, pos * 2), get_max(u, v, mid + 1, r, pos * 2 + 1)); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; tree.assign(4 * n + 1, 0); a.assign(n + 1, 0); for (int i = 1; i <= n; ++i) { cin >> a[i]; add(a[i], i, 1, n, 1); } for (int i = 1; i < n; ++i) { long long v = get_max(i + 1, n, 1, n, 1); if (a[i] > v) cout << 0 << ' '; else cout << v + 1 - a[i] << ' '; } 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; const int MXN = 1e5 + 3; long long n, sum, mx = -MXN, id; int main() { long long a[MXN]; pair<int, int> b[MXN]; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = n; i >= 1; i--) { if (a[i] > mx) { mx = a[i]; id = i; } b[i].first = id; b[i].second = mx; } for (int i = 1; i <= n; i++) { if (i != b[i].first) { if (a[i] < b[i].second) { cout << b[i].second - a[i] + 1 << " "; } if (a[i] == b[i].second) { cout << 1 << " "; } } if (i == b[i].first) { 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; string Y = "YES"; string N = "NO"; string yy = "Yes"; string nn = "No"; int main() { long long n; while (cin >> n) { long long ar[n]; for (long long i = 0; i < n; i++) { cin >> ar[i]; } long long mx = 0; vector<long long> v(n); for (long long i = n - 1; i >= 0; i--) { if (ar[i] > mx) { v[i] = 0; mx = max(mx, ar[i]); } else { v[i] = mx + 1 - ar[i]; } } for (long long i = 0; i < n; i++) cout << v[i] << " "; cout << "\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
T = int(input()) st = raw_input().split() st = list(map(int,st)) mx = 0 a = [0]*T for i in range(T-1,-1,-1): a[i] = max(0,mx + 1 - st[i]) mx = max(mx,st[i]) for i in range(0,T): print a[i],
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n; cin >> n; int a[n + 1], ans[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; int max = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > max) ans[i] = 0, max = a[i]; else if (a[i] < max) ans[i] = max - a[i] + 1; else ans[i] = 1; } ans[n - 1] = 0; 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
n = int(input()) a = list(map(int, input().split())) _max = max(a) b = [0] * n b[n - 1] = 0 c = a[n - 1] for i in range(n - 2, -1, -1): if(a[i] > c): b[i] = 0 c = a[i] else: c = max(c, a[i]) b[i] = c - a[i] + 1 for i in range(n): print(b[i])
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
from sys import * inp = lambda : stdin.readline() def main(): n = int(inp()) l = [int(i) for i in inp().split()] maxl,maxe = [0 for i in range(n)],0 for i in range(n-1,-1,-1): maxe = max(maxe, l[i]) maxl[i] = maxe for i in range(n-1): print(max(0,maxl[i+1]-l[i]+1),end=" ") print(0) if __name__ == "__main__": main()
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Codeforces { StringTokenizer tok; BufferedReader in; PrintWriter out; final boolean OJ = System.getProperty("ONLINE_JUDGE") != null; String filename = null; void init() { try { if (OJ) { if (filename == null) { 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"); } } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } catch (Exception e) { throw new RuntimeException(e); } } String readString() { try { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } catch (Throwable t) { throw new RuntimeException(t); } } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } public static void main(String[] args) { new Codeforces().run(); } void run() { init(); long time = System.currentTimeMillis(); solve(); System.err.println(System.currentTimeMillis() - time); out.close(); } void solve() { int n = readInt(); int[] a = new int[n]; for (int i=0;i<n;i++) { a[i] = readInt(); } int max = 0; int[] ans = new int[n]; ans[n - 1] = 0; max = a[n - 1]; for (int i=n-2;i>=0;i--) { ans[i] = Math.max(0, max + 1 - a[i]); max = Math.max(max,a[i]); } for (int i=0;i<n;i++) out.print(ans[i] + " "); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] hs = new int[n]; for (int i = 0; i < n; i++) hs[i] = sc.nextInt(); sc.close(); long[] max = new long[n + 1]; for (int i = n - 1; i >= 0; i--) max[i] = Math.max(hs[i], max[i + 1]); for (int i = 0; i < n; i++) { System.out.print(max[i + 1] >= hs[i] ? max[i + 1] - hs[i] + 1 : 0); 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
/** * * @author Faruk */ import java.util.Arrays; import java.util.Scanner; import java.util.HashSet; import java.util.HashMap; import java.util.ArrayList; public class tmp { public static void main(String [] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int [] arr = new int[n]; int [] maxafter = new int[n+1]; int max = -1; for(int i=0; i<n; i++) arr[i] = scan.nextInt(); for(int i=n-1; i>=0; i--){ if(arr[i]>maxafter[i+1]) maxafter[i] = arr[i]; else maxafter[i] = maxafter[i+1]; } for(int i=1; i<n+1; i++){ max = maxafter[i]+1-arr[i-1]; System.out.print((max >= 0 ? max : "0") + " "); } System.out.println(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long i, mi, n, a[100005], b[100005]; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; mi = a[n - 1]; b[n - 1] = 0; for (i = n - 2; i >= 0; i -= 1) { if (a[i] > mi) { b[i] = 0; mi = a[i]; } else if (a[i] == mi) { b[i] = 1; mi = a[i]; } else b[i] = mi - a[i] + 1; } for (i = 0; i < n; i++) cout << b[i] << " "; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; int ans[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int max = 0; for (int i = n - 1; i >= 0; i--) { int flag = 0; if (arr[i] > max) { max = arr[i]; flag = 1; } if (max == arr[i] && flag) ans[i] = 0; else if (arr[i] <= max) ans[i] = max + 1 - arr[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.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * * @author Shreyas */ public class Height { /** * @param args the command line arguments */ public static void main(String[] args) { InputReader in =new InputReader(System.in); int n=in.nextInt(); int[] a=new int[n]; int b[]=new int[n]; for(int i=0;i<n;i++){ a[i]=in.nextInt(); } b[n-1]=a[n-1]; for(int i=n-2;i>=0;i--){ if(b[i+1]<a[i]){ b[i]=a[i]; } else b[i]=b[i+1]; } for(int i=0;i<n;i++){ if(i+1<n&&b[i]<=b[i+1]) System.out.print(b[i+1]-a[i]+1+" "); else System.out.print(0+" "); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine() { String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInputReader in = new FastInputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, FastInputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int[] max = new int[n]; max[n - 1] = 0; for (int i = n - 2; i >= 0; i--) { max[i] = Math.max(max[i + 1], a[i + 1]); } for (int i = 0; i < n; i++) { if (a[i] <= max[i]) { out.print((max[i] - a[i] + 1) + " "); } else { out.print(0 + " "); } } } } static class FastInputReader { static InputStream is; static private byte[] buffer; static private int lenbuf; static private int ptrbuf; public FastInputReader(InputStream stream) { is = stream; buffer = new byte[1024]; lenbuf = 0; ptrbuf = 0; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return buffer[ptrbuf++]; } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } }
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 = [ int(x) for x in input().split() ] + [ 0 ] s = [ 0 for x in range(n) ] m = a[-1] for i in range(n): m = max(m , a[n - i]) s[n - i - 1] = m res = '' for i in range(n): res += str(max(s[i] - a[i] + 1, 0)) + ' ' print(res[:-1])
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; public class P322_2B implements Runnable { int nextInt(StreamTokenizer in) throws Exception { in.nextToken(); return (int) in.nval; } StreamTokenizer in; private int nextInt() throws Exception { in.nextToken(); return (int) in.nval; } @Override public void run() { try { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } int[] r = new int[n]; r[n - 1] = 0; int max = a[n - 1]; for (int i = n - 2; i >= 0; i--) { r[i] = Math.max(max + 1 - a[i], 0); max = Math.max(max, a[i]); } PrintWriter out = new PrintWriter(System.out); for (int i = 0; i < n; i++) { out.print(r[i] + " "); } out.println(); out.close(); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) { new Thread(new P322_2B()).start(); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#!/usr/bin/env python n = input() a = map(int,raw_input().split()) b = [None] * n b[n-1] = 0 i = n-2 tmp = 0 while i >= 0: tmp = max(tmp, a[i+1]) b[i] = max(0, tmp+1-a[i]) i-=1 for x in range(0,n) : print b[x],
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int a[N], n, b[N], maxn; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; b[n] = 0; maxn = a[n]; for (int i = n - 1; i >= 1; i--) { if (a[i] > maxn) { b[i] = 0; maxn = a[i]; } else { b[i] = maxn - a[i] + 1; } } for (int i = 1; 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 sys import string from collections import Counter, defaultdict from math import fsum, sqrt, gcd, ceil, factorial from itertools import combinations, permutations # input = sys.stdin.readline flush = lambda: sys.stdout.flush comb = lambda x, y: (factorial(x) // factorial(y)) // factorial(x - y) # inputs # ip = lambda : input().rstrip() ip = lambda: input() ii = lambda: int(input()) r = lambda: map(int, input().split()) rr = lambda: list(r()) n = ii() arr = rr() arr += [arr[-1] - 1] x = 0 ans = [] for i in range(n, -1, -1): x = max(x, arr[i]) if i: ans.append(max(0, x - arr[i - 1] + 1)) print(*ans[::-1])
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; void YES(bool t = 1) { cout << YESNO[t] << '\n'; } void Yes(bool t = 1) { cout << YesNo[t] << '\n'; } void yes(bool t = 1) { cout << yesno[t] << '\n'; } template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); } template <class T> void scan(vector<T> &); template <class T> void scan(vector<T> &a) { for (auto &i : a) scan(i); } template <class T> void scan(T &a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; IN(n); vector<int> h(n); IN(h); vector<int> suf(n + 1); suf[n] = 0; for (int i = n - 1; i >= 0; --i) { suf[i] = max(suf[i + 1], h[i]); } vector<int> ans(n); for (int i = 0; i < n; ++i) { if (suf[i + 1] >= h[i]) { ans[i] = suf[i + 1] - h[i] + 1; } else { ans[i] = 0; } } for (int i = 0; i < n; ++i) { cout << ans[i] << " \n"[i == n - 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> int main() { int n, i, j; int max = INT_MIN; scanf("%d", &n); int a[n]; int b[n]; for (i = 0; i < n; ++i) { scanf("%d", &a[i]); } for (j = n - 1; j >= 0; --j) { if (a[j] <= max) { b[j] = max + 1 - a[j]; } else { b[j] = 0; } if (max < a[j]) { max = a[j]; } } for (i = 0; i < n; ++i) { printf("%d\n", b[i]); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1], maxx[n + 1]; for (int i = 1; i < n + 1; ++i) cin >> a[i]; maxx[n] = a[n]; for (int i = n - 1; i > 0; --i) maxx[i] = max(maxx[i + 1], a[i]); for (int i = 1; i < n; ++i) if (maxx[i + 1] >= a[i]) cout << maxx[i + 1] - a[i] + 1 << ' '; else cout << "0 "; cout << "0"; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n; int h[maxn], r[maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &h[i]); } memset(r, 0, sizeof(r)); for (int i = n; i >= 1; --i) { if (i == n) continue; r[i] = max(h[i + 1], r[i + 1]); } for (int i = 1; i <= n; ++i) { if (h[i] > r[i]) { printf("0 "); } else { printf("%d ", r[i] - h[i] + 1); } } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; void Rd(int &res) { char c; res = 0; while (c = getchar(), !isdigit(c)) ; do { res = (res << 3) + (res << 1) + (c ^ 48); } while (c = getchar(), isdigit(c)); } int A[100005], B[100005]; int main() { int n, i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) { Rd(A[i]); B[i] = A[i]; } A[n + 1] = 0; for (i = n - 1; i >= 1; i--) A[i] = max(A[i], A[i + 1]); for (i = 1; i <= n; i++) { int t = A[i + 1] + 1 - B[i]; if (t < 0) t = 0; printf("%d ", t); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int mx, n, n1, k, a[100000], b[100000]; int main() { cin >> n; mx = 0; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { b[i] = max(0, (mx - a[i] + 1)); mx = max(mx, a[i]); } for (int i = 0; i < n; i++) cout << b[i] << " "; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.*; import java.util.*; import java.math.*; public class B { public static void main(String args[]) { try { InputReader in = new InputReader(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n = in.readInt(); long har[] = new long[n]; for (int i = 0; i < n; i++) { har[i] = in.readLong(); } long maxr[] = new long[n]; maxr[n - 1] = har[n - 1]; for (int i = n - 1; i >= 0; i--) { maxr[i] = har[i]; maxr[i] = Math.max(maxr[i], (i + 1 < n ? maxr[i + 1] : 0)); } for (int i = 0; i < n; i++) { int fid = i; long ans = - har[fid] + ((fid + 1 < n) ? maxr[fid + 1] : 0) + 1; if(ans < 0) ans = 0; out.write(Long.toString(ans)+ " "); } out.close(); } catch (Exception e) { e.printStackTrace(); } } } class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int length = readInt(); if (length < 0) return null; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) bytes[i] = (byte) read(); try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { return new String(bytes); } } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return readString(); } public boolean readBoolean() { return readInt() == 1; } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import sun.font.FontRunIterator; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; /** * Created by baba_beda on 9/22/15. */ public class B { public static void main(String[] args) { new B().run(); } Scanner in; void run() { try { in = new Scanner(System.in); solve(); } catch (Exception e) { e.printStackTrace(); } } void solve() { int n = in.nextInt(); int[] houses = new int[n]; for (int i = 0; i < n; i++) { houses[i] = in.nextInt(); } int max = 0; int[] ans = new int[n]; for (int i = n - 1; i >= 0; i--) { if (houses[i] <= max) { ans[i] = (max - houses[i] + 1); } max = Math.max(max, houses[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
#include <bits/stdc++.h> using namespace std; int main() { long long int a[100009], i, j, k, l, m, n, b[100009]; while (cin >> n) { for (i = 0; i < n; i++) { cin >> a[i]; } m = a[n - 1]; b[n - 1] = 0; k = 0; for (i = n - 2; i >= 0; i--) { if (m - a[i] == 0) { b[i] = 1; } else if (m - a[i] > 0) { b[i] = (m - a[i]) + 1; } else { m = a[i]; b[i] = 0; } } for (i = 0; i < n; i++) cout << b[i] << " "; cout << endl; } }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n=int(input()) v = list(map(int, input().split())) v1 = [0]*n m = 0 for i in range(n): if v[-i-1]>m: m=v[-i-1] else: v1[-i-1]=m-v[-i-1]+1 print(' '.join(map(str,v1)))
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; /** * Created by Microsoft on 18/10/2015. */ public class algo_contest1_q5 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); int input []=new int[n]; int repeats[]=new int[n]; String ins[]=br.readLine().split(" "); for(int i=0;i<n;i++){ input[i]=Integer.parseInt(ins[i]); } int help[]=new int[n]; help[n-1]=input[n-1]; for(int i=n-2;i>-1;i--){ if(input[i]>help[i+1]) help[i]=input[i]; else if (input[i]==help[i+1]){ repeats[i]=repeats[i+1]+1; help[i]=help[i+1]; } else{ help[i]=help[i+1]; repeats[i]=repeats[i+1]; }; } // for (int i=0;i<n;i++){ // System.out.print(repeats[i]+" "); // } // System.out.println(); for(int i=0;i<n;i++){ if(help[i]==input[i]&&repeats[i]==0) System.out.print(0+" "); else if(help[i]==input[i]&&repeats[i]>0){ System.out.print(1+" "); // repeats[i+1]--; } else { System.out.print(help[i]-input[i]+1+" "); } } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Arrays; import java.util.Scanner; public class BigRecursion { 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[] b = new int[n]; b[n-1] = 0; int 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]; } } Arrays.stream(b).forEach(c->System.out.print(c+" ")); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n=int(input()) lst=list(map(int,input().split())) res,mx=[0],lst[-1] for i in range(n-2,-1,-1): x=lst[i] if x<=mx:res.append(mx-x+1) else:res.append(0);mx=x res.reverse() 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
n = int(input()) h = list(map(int, input().split())) + [0] h_max = 0 res = [0] * n for i in range(n, 0, -1): h_max = max(h_max, h[i] + 1) res[i - 1] = max(h_max - h[i - 1], 0) print(" ".join(map(str, res)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int p, n; cin >> n; int a[100001] = {}, dp[100001], h[100001] = {}; for (int i = 0; i < n; ++i) { cin >> a[i]; } dp[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; --i) { if (a[i] == dp[i + 1]) h[i] = 1; dp[i] = max(dp[i + 1], a[i]); } for (int i = 0; i < n - 1; ++i) { if (dp[i] == a[i]) { if (h[i] == 1) cout << "1 "; else cout << "0 "; } else cout << dp[i] - a[i] + 1 << " "; } cout << 0; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n=int(input()) l=list(map(int,input().split())) s=[0]*n a=[0]*n s[-1]=l[-1] for i in range(1,n): if l[n-i-1]<=s[n-i]: a[n-i-1]=s[n-i]-l[n-i-1]+1 s[n-i-1]=max(l[n-i-1],s[n-i]) print(' '.join(map(str,a)))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; long double PI = acosl(-1); bool compare_int(int a, int b) { return (a > b); } bool compare_string(string a, string b) { return a.size() < b.size(); } bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long int fact(long long int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int main() { int n, x; cin >> n; vector<int> a; vector<int> v; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } reverse(a.begin(), a.end()); v.push_back(0); int m = a[0]; for (int i = 1; i < n; i++) { if (a[i] <= m) v.push_back((m - a[i] + 1)); else { m = a[i]; v.push_back(0); } } reverse(v.begin(), v.end()); for (int i = 0; i < n; 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
import java.util.Scanner; public class B322 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int max=0; int ans[]= new int [n]; for(int i=arr.length-1;i>=0;i--){ if(arr[i]<=max){ ans[i]=max-arr[i]+1; }else{ ans[i]=0; max=arr[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 = [int(x) for x in input().split()] tmp = [] for i in range (n): tmp.append(0) tmp[n - 1] = h[n - 1] for i in range (n - 2, -1, -1): if h[i] > tmp[i + 1]: tmp[i] = h[i] else: tmp[i] = tmp[i + 1] for i in range (n - 1): if h[i] == tmp[i + 1]: print(1, end = " ") elif h[i] == tmp[i]: print(0, end=" ") else: print(tmp[i] + 1 - h[i], end = " ") print(0)
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws java.lang.Exception { Reader pm =new Reader(); int n = pm.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] = pm.nextInt(); } ArrayList<Integer> al = new ArrayList<>(); al.add(0); int max = a[n-1]; for(int i=n-2;i>=0;i--){ if(a[i] > max){ max = a[i]; al.add(0); } else { al.add(max - a[i] + 1); } } for(int i=al.size()-1;i>=0;i--) System.out.print(al.get(i)+" "); System.out.println(); } 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(); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(raw_input()) houses = [int(x) for x in raw_input().split()] m = 0 ans = [0] * n for i in xrange(n-1, -1, -1): if houses[i] <= m: ans[i] = m - houses[i] + 1 else: ans[i] = 0 m = max(houses[i], m) print " ".join([str(x) for x in ans])
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n=int(input()) a=list(map(int,input().split())) a.reverse() c=[] c.append(0) maxi=a[0] for i in range(len(a)-1): if(a[i+1]>maxi): c.append(0) maxi=a[i+1] elif(a[i+1]<=maxi): c.append(abs(1+maxi-a[i+1])) c.reverse() print(*c)
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
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.pop(); } else { if (last_floor === prev_floor) { result.unshift(1); floor.pop(); } 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
n = int(input()) a = list(map(int, input().split())) b = [0] * n ma = a[-1] for i in range(n - 2, -1, -1): if a[i] <= ma: b[i] = ma - a[i] + 1 else: ma = a[i] string_ints = [str(int) for int in b] print(" ".join(string_ints))
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; long long arr[100005], arr2[100005]; map<long long, int> cnt; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; cnt[arr[i]]++; arr2[i] = arr[i]; } for (int i = n - 2; i >= 0; i--) { arr2[i] = max(arr2[i], arr2[i + 1]); } for (int i = 0; i < n; i++) { if (arr[i] == arr2[i] and arr2[i] != arr2[i + 1]) cout << 0 << " "; else cout << arr2[i] - arr[i] + 1 << " "; } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = int(input()) a = [int(x) for x in input().split()] m = [0] * (n + 1) m[n] = - 10 ** 9 - 7 for i in range(n-1,0,-1): m[i] = max(m[i+1], a[i]) for i in range(n): print(max(a[i], m[i+1] + 1) - a[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
n = int(input()) a = list(map(int, input().split())) ans = [] m = 0 for i in range(n - 1, -1, -1): ans.append(max(0, (m + 1) - a[i])) m = max(m, a[i]) ans.reverse() for i in ans: print(i, end = ' ')
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n = input("") hString = raw_input("") h = hString.split(" ") h = map(int, h) result = ["0"] prevMax = n - 1 for i in range(n-2, -1, -1): if h[prevMax] < h[i]: result.append("0") prevMax = i else: result.append(str(h[prevMax] - h[i] + 1)) print " ".join(result[::-1])
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int tmp = 0, index; for (int i = n - 1; i >= 0; i--) { if (tmp < arr[i]) { tmp = arr[i]; index = i; } if (i == index) { arr[i] = 0; } else { arr[i] = max(0, (tmp + 1) - arr[i]); } } for (int i = 0; i < n; i++) { cout << arr[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 _581B_Luxurious_Houses { public static void main(String[] args){ Scanner leer = new Scanner(System.in); int n = leer.nextInt(); long h[]= new long[n]; for (int i = 0; i < n; i++){ h[i]= leer.nextLong(); } long T[]= new long[n+1]; boolean P[]= new boolean[n]; for (int i = n-1; i >=0; i--){ T[i]=T[i+1]; if(h[i]>T[i]){T[i]=h[i]; P[i]=true; } } for (int i = 0; i < n; i++) { if(!P[i])T[i]++; System.out.print(T[i]-h[i]+" "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); int n; cin >> n; int a[n]; vector<int> v; for (int i = 0; i < n; i++) cin >> a[i]; v.push_back(0); int m = a[n - 1]; for (int i = n - 2; i >= 0; i--) { if (a[i] > m) { v.push_back(0); m = a[i]; } else v.push_back(m + 1 - a[i]); } for (int i = 0; i < n; i++) cout << v[n - 1 - 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 python3 # 20150929 # 11:10PM- def main(): n = int(input()) a = list(map(int, input().split())) b = [0]*len(a) m = a[-1] for i in range(len(a)-2,-1,-1): if a[i] > m: m = a[i] else: b[i] = m - a[i] + 1 print(' '.join(map(str,b))) if __name__ == '__main__': main()
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import array def main(): n = int(input()) hs = input().split() maxi = 0 p = array.array('Q', n * [0]) for i in range(n - 1, -1, -1): hi = int(hs[i]) p[i] = max(0, maxi - hi + 1) maxi = max(maxi, hi) for i in range(n - 1): print(p[i], '', end='') print(p[n - 1]) if __name__ == "__main__": main()
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; import java.io.*; public class Luxurious_Houses { public static void main(String args[]) throws Exception { BufferedReader f=new BufferedReader(new InputStreamReader(System.in)); int runs=Integer.parseInt(f.readLine()); StringTokenizer st=new StringTokenizer(f.readLine()); int[] arr=new int[runs]; for(int x=0;x<runs;x++) arr[x]=Integer.parseInt(st.nextToken()); int max=arr[runs-1]-1; ArrayList<Integer> list=new ArrayList<Integer>(); for(int x=runs-1;x>=0;x--) { list.add(Math.max(0,max-arr[x]+1)); max=Math.max(max,arr[x]); } for(int x=runs-1;x>=0;x--) if(x==0) System.out.println(list.get(0)); else System.out.print(list.get(x)+" "); } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
# -*- coding: utf-8 -*- import sys,math,heapq,itertools as it,fractions,re,bisect,collections as coll n = int(raw_input()) h = map(int, raw_input().split()) mx = [0] * n for i in xrange(n - 2, -1, -1): mx[i] = max(mx[i + 1], h[i + 1]) ans = [max(0, mx[i] - h[i] + 1) for i in xrange(n)] print " ".join(map(str, ans))
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; public class testP02 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] h=new int[n]; for(int i=0;i<n;i++) h[i]=sc.nextInt(); int[] a=new int[n]; int[] ans=new int[n]; a[n-1]=h[n-1]; for(int i=n-2;i>=0;i--){ if(a[i+1]>=h[i]) ans[i]=a[i+1]-h[i]+1; else ans[i]=0; a[i]=Math.max(a[i+1], h[i]); } for(int i=0;i<n;i++){ System.out.print(ans[i]+" "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int n, h[100008], mayor, resp[100008], mayores[100008]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &h[i]); } mayor = -1; for (int j = n - 1; j >= 0; j--) { mayores[j] = mayor; if (h[j] > mayor) { mayor = h[j]; } } for (int i = 0; i < n; ++i) { if (mayores[i] >= h[i]) { resp[i] = mayores[i] - h[i] + 1; } } for (int i = 0; i < n; ++i) { if (i == 0) { printf("%d", resp[i]); } else { printf(" %d", resp[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.Locale; import java.util.StringTokenizer; public class Main implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new Main(), "", 128 * (1L << 20)).start(); } void init() throws FileNotFoundException { Locale.setDefault(Locale.US); /*if(!ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else { in = new BufferedReader(new FileReader("gazmyas.in")); out = new PrintWriter("gazmyas.out"); }*/ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } public void run() { try { timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } String readString(String s) throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine(), s + "\n \t"); } catch (Exception e) { return null; } } return tok.nextToken(); } double readDouble() throws IOException { return Double.parseDouble(readString()); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } int readInt(String s) throws IOException { return Integer.parseInt(readString(s)); } boolean isPrime(int a) throws IOException { int del = 0; if (a == 2) { return true; } for (int i = 2; i < Math.sqrt(a) + 1; i++) { if (a % i == 0) { del = i; break; } } if (del > 0) { return false; } else return true; } public long factorial(int num) throws IOException { long res = 1; for (int i = 2; i <= num; i++) { res *= i; } return res; } public int[] readIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } return a; } class pair implements Comparable<pair>{ int val, count; @Override public int compareTo(pair pair) { return Integer.compare(val, pair.val); } } void solve() throws Exception{ int n=readInt(); int[] max = new int[n]; int[] indmax = new int[n]; int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i]=readInt(); } max[n-1]=a[n-1]; indmax[n-1]=n-1; for(int i=n-2;i>=0;i--){ if(a[i]>max[i+1]){ max[i]=a[i]; indmax[i]=i; }else{ max[i]=max[i+1]; indmax[i]=indmax[i+1]; } } for (int i = 0; i < n; i++) { if(a[i]==max[i] && indmax[i]==i){ out.print(0+" "); }else{ out.print(max[i]-a[i]+1+" "); } } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n=input() a=map(int,raw_input().split()) rs=[0]*n mx=0 for i in xrange(n-1,-1,-1): rs[i]=max(mx+1,a[i]) mx=max(mx,a[i]) for i in xrange(n): print rs[i]-a[i],
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; long long n, arr[100005], Max, ans[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (long long i = (long long)(1); i <= (long long)(n); i++) cin >> arr[i]; Max = arr[n]; ans[n] = 0; for (long long i = (long long)(n - 1); i >= (long long)(1); i--) { if (arr[i] <= Max) ans[i] = Max - arr[i] + 1; else { Max = arr[i]; ans[i] = 0; } } for (long long i = (long long)(1); i <= (long long)(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
#include <bits/stdc++.h> using namespace std; long long h[100010], a[100010]; int main() { int n; while (cin >> n) { memset(a, 0, sizeof(a)); for (int i = 0; i < n; ++i) cin >> h[i]; a[n - 1] = 0; if (n > 1) a[n - 2] = h[n - 1]; for (int i = n - 3; i >= 0; --i) { a[i] = max(a[i + 1], h[i + 1]); } bool f = false; for (int i = 0; i < n; ++i) { if (a[i] >= h[i]) { if (!f) { cout << a[i] - h[i] + 1; f = true; } else cout << " " << a[i] - h[i] + 1; } else { if (!f) { cout << "0"; f = true; } else cout << " 0"; } } cout << endl; } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; bool compare(int a, int b) { return a > b; } class Compare { public: bool operator()(int a, int b) { return a > b; } }; int mod(int a, int b) { return ((a % b) + b) % b; } int a[100009], n, suffix[100009]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); suffix[n - 1] = n - 1; for (int i = n - 2; i >= 0; i--) { if (a[suffix[i + 1]] >= a[i]) suffix[i] = suffix[i + 1]; else suffix[i] = i; } for (int i = 0; i < n; i++) { if (suffix[i] == i) printf("0 "); else printf("%d ", a[suffix[i]] - a[i] + 1); } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, a[N]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int mx = a[n - 1]; vector<int> v; v.push_back(0); for (int i = n - 2; i >= 0; i--) { if (a[i] > mx) { v.push_back(0); mx = max(mx, a[i]); } else { v.push_back(abs(mx - a[i]) + 1); mx = max(mx, a[i]); } } reverse(v.begin(), v.end()); for (auto i : v) { cout << i << " "; } }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.*; import java.util.*; public class test { int INF = (int)1e9; long MOD = 1000000007; void solve(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++) { a[i] = in.nextInt(); } int[] m = new int[n+1]; int[] rs = new int[n]; for(int i=n-1; i>=0; i--) { m[i] = Math.max(a[i], m[i+1]); if(m[i+1]>=a[i]) { rs[i] = m[i+1] - a[i] + 1; } } for(int i=0; i<n; i++){ out.print(rs[i]+" "); } out.println(); } public static void main(String[] args) throws IOException { if(args.length>0 && args[0].equalsIgnoreCase("d")) { DEBUG_FLAG = true; } InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); int t = 1;//in.nextInt(); long start = System.nanoTime(); while(t-->0) { new test().solve(in, out); } long end = System.nanoTime(); debug("\nTime: " + (end-start)/1e6 + " \n\n"); out.close(); } static boolean DEBUG_FLAG = false; static void debug(String s) { if(DEBUG_FLAG) System.out.print(s); } public static void show(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { static BufferedReader br; static StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } 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.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ 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(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] A = new int[n], B = new int[n]; for (int i = 0; i < n; ++i) A[i] = in.nextInt(); int max = -1; for (int i = n - 1; i >= 0; --i) { B[i] = Math.max(0, max - A[i] + 1); max = Math.max(max, A[i]); } for (int p : B) out.print(p + " "); out.println(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; long long int pwr(long long int a, long long int b, long long int mod) { a %= mod; if (a < 0) a += mod; long long int ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b /= 2; } return ans; } long long int gcd(long long int a, long long int b) { while (b) { long long int temp = a; a = b; b = temp % b; } return a; } int main() { int n, i, j, h[100005], r[100005]; cin >> n; for (i = 0; i < n; i++) cin >> h[i]; r[n - 1] = h[n - 1]; for (i = n - 2; i >= 0; i--) { r[i] = max(r[i + 1], h[i + 1]); } for (i = 0; i < n - 1; i++) { cout << max(0, r[i] - h[i] + 1) << " "; } cout << 0; return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int[] ans=new int[n]; ans[n-1]=0; int temp=0; for(int i=n-2;i>=0;i--){ temp=Math.max(temp,arr[i+1]); if(temp>=arr[i]){ ans[i]=temp-arr[i]+1; }else{ ans[i]=0; } } for(int i=0;i<n;i++){ System.out.print(ans[i]+" "); } } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long i, n, a, max = -1; cin >> n; vector<int> v, m; while (n--) { cin >> a; v.push_back(a); } for (i = v.size() - 1; i >= 0; i--) { if (v[i] > max) max = v[i]; m.push_back(max); } for (i = 0; i < v.size() - 1; i++) { if (v[i] <= m[v.size() - 2 - i]) cout << m[v.size() - 2 - i] - v[i] + 1; else cout << 0; cout << " "; } cout << 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
# your code goes here n = int(raw_input()) h = map(int, raw_input().split()) l=[0]*n max = 0 arr = h[::-1] # print h, arr for i in range(n): if arr[i] <= max: l[i] = max - arr[i] + 1 else: max = arr[i] # print max l = l[::-1] print ' '.join(str(x) for x in 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
n = int(input()) h = [i for i in map(int, input().split())] h = h[::-1] ans = [] maxH = h[0] for i in range(1, len(h)): if maxH - h[i] + 1 < 0: ans.append(0) else: ans.append(maxH - h[i] + 1) maxH = max(maxH, h[i]) ans = ans[::-1] ans.append(0) print(*ans)
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.util.Scanner; public class probACon322 { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = 0; n = sc.nextInt(); int[] houses = new int[n]; int[] diff = new int[n]; for (int i = 0 ; i < houses.length ; i ++){ houses[i] = sc.nextInt(); } int prev = houses[n-1]; // StringBuilder res = new StringBuilder("0"); diff[n-1] = 0; for (int i = n-2 ; i >= 0 ;i--){ if (prev >= houses[i]){ // res.insert(0, " " ); // res.insert(0, prev-houses[i]+1 ); diff[i] = prev-houses[i]+1; }else { // res.insert(0, "0 " ); diff[i] = 0; prev = houses[i]; } } for (int i = 0 ; i < houses.length ; i ++){ System.out.println(diff[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 = input() s = map(int,raw_input().split()) a = s[-1] b = ['0'] for i in xrange(n-2,-1,-1): if s[i]==a: b.append('1') elif s[i]>a: b.append('0') else: b.append(str(a+1-s[i])) a = max(a,s[i]) print ' '.join(b[::-1])
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
from math import * from Queue import * from sys import * from datetime import * n = int(raw_input()) h = map(int, raw_input().split()) m = [0 for i in range(n)] m[n-1] = h[n-1] for i in range(n-2, -1, -1): m[i] = max(m[i+1], h[i]) res = [] for i in range(n-1): res.append(max(m[i+1]-h[i]+1,0)) res.append(0) print(' '.join(map(str, res)))
PYTHON
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String args[]) throws IOException { BufferedReader in; File f = new File("entrada.txt"); StringBuilder out = new StringBuilder(); if (f.exists()) { in = new BufferedReader(new FileReader(f)); } else { in = new BufferedReader(new InputStreamReader(System.in)); } int n = Integer.parseInt(in.readLine()); int arr[] = readInts(in.readLine()); long res[] = new long[arr.length]; long max = arr[res.length - 1]; res[res.length - 1] = 0; for (int i = n - 2; i >= 0; i--) { if (arr[i] <= max) { res[i] = max - arr[i] + 1; } max = Math.max(max, arr[i]); } for (int i = 0; i < n; i++) { if (i > 0) out.append(" "); out.append(res[i]); } System.out.print(out); } public static int[] readInts(String cad) { StringTokenizer token = new StringTokenizer(cad, " "); int i = 0; int arr[] = new int[token.countTokens()]; while (token.hasMoreTokens()) arr[i++] = Integer.parseInt(token.nextToken()); return arr; } }
JAVA
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int a[n], r[n]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); int mx = a[n - 1]; r[n - 1] = 0; for (int i = n - 2; i > -1; i--) { if (mx >= a[i]) r[i] = mx - a[i] + 1; else mx = a[i], r[i] = 0; } for (int i = 0; i < n; i++) printf("%d ", r[i]); return puts(""), 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; vector<int> v(n), res(n); for (int i = 0; i < n; i++) cin >> v[i]; reverse(v.begin(), v.end()); int mx = v[0]; res[0] = 0; for (int i = 1; i < n; i++) { if (v[i] > mx) { mx = v[i]; res[i] = 0; } else { res[i] = mx - v[i] + 1; } } reverse(res.begin(), res.end()); for (int i = 0; i < n; i++) { cout << res[i] << " "; } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, max; cin >> n; long long int a[n], b[n + 1]; for (i = 0; i < n; i++) cin >> a[i]; max = a[n - 1]; for (i = n - 1; i >= 0; i--) { if (a[i] >= max) { b[i] = a[i]; max = a[i]; } else b[i] = b[i + 1]; } b[n] = 0; for (i = 0; i < n; i++) { if (a[i] == b[i + 1]) cout << 1 << " "; else if ((a[i] - b[i]) == 0) cout << 0 << " "; else cout << b[i] - a[i] + 1 << " "; } return 0; }
CPP
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
n=int(input()) a=list(map(int,input().split())) x=a[n-1] b=[0]*n for i in range(n-2,-1,-1): b[i]=max(0,x-a[i]+1) if a[i]>x:x=a[i] print(*b)
PYTHON3
581_B. Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row. Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same. The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task. Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added). Input The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. Output Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one. Examples Input 5 1 2 3 1 2 Output 3 2 0 2 0 Input 4 3 2 1 4 Output 2 3 4 0
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] ary = IOUtils.readIntArray(in, n); int max = 0; int[] result = new int[n]; for (int i = ary.length - 1; i >= 0; i--) { if (ary[i] <= max) result[i] = max - ary[i] + 1; max = Math.max(max, ary[i]); } for (int i : result) out.print(i + " "); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void close() { writer.close(); } } static class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.File; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author zodiacLeo */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { String[] p = {"1101", "1100", "0011", "1111", "0000", "0100", "1011", "0001", "0100", "0010", "1101", "0110", "1001"}; public void solve(int testNumber, FastScanner in, FastPrinter out) { // test(); int n = in.nextInt(); if (n <= 2) { out.println(n); return; } String t = in.next(); char[] s = t.toCharArray(); int res = 1; int prev = s[0] - '0'; for (int i = 1; i < n; i++) { int now = s[i] - '0'; if (prev != now) { res++; } prev = now; } for (int i = 0; i < p.length; i++) { if (t.contains(p[i])) { out.println(Math.min(n, res + 2)); return; } } int p1 = t.indexOf("11"); int p2 = t.lastIndexOf("11"); if (p1 >= 0 && p2 >= 0 && p1 < p2) { out.println(Math.min(n, res + 2)); return; } p1 = t.indexOf("00"); p2 = t.lastIndexOf("00"); if (p1 >= 0 && p2 >= 0 && p1 < p2) { out.println(Math.min(n, res + 2)); return; } p1 = t.indexOf("11"); p2 = t.lastIndexOf("00"); if (p1 >= 0 && p2 >= 0 && p1 < p2) { out.println(Math.min(n, res + 2)); return; } p1 = t.indexOf("00"); p2 = t.lastIndexOf("11"); if (p1 >= 0 && p2 >= 0 && p1 < p2) { out.println(Math.min(n, res + 2)); return; } if (s[0] == s[1] || s[n - 1] == s[n - 2]) { out.println(Math.min(n, res + 1)); return; } out.println(res); } } static class FastScanner { public BufferedReader br; public StringTokenizer st; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String next() { while (st == null || !st.hasMoreElements()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.*; public class A603 { public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); int n = input.nextInt(); int res = 1; char[] s = input.next().toCharArray(); for(int i= 1; i<n; i++) if(s[i] != s[i-1]) res++; out.println(Math.min(res+2, n)); out.close(); } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
input() a = input() s = [] for c in a: if not s or s[-1][0] != c: s.append([c, 1]) else: s[-1][1] += 1 s2 = sorted(s, key=lambda x: x[1]) delta = 0 if s2[-1][1] >= 3 or len(s2) > 1 and s2[-2][1] >= 2: delta = 2 elif s2[-1][1] >= 2: delta = 1 print(len(s) + delta)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.math.*; import java.util.*; /** * * @author Saju * */ public class Main { private static int dx[] = { 1, 0, -1, 0 }; private static int dy[] = { 0, -1, 0, 1 }; private static final long INF = Long.MAX_VALUE; private static final int INT_INF = Integer.MAX_VALUE; private static final long NEG_INF = Long.MIN_VALUE; private static final int NEG_INT_INF = Integer.MIN_VALUE; private static final double EPSILON = 1e-10; private static final long MAX = (long) 1e12; private static final long MOD = 100000007; private static final int MAXN = 300005; private static final int MAXA = 2000007; private static final int MAXLOG = 22; private static final double PI = Math.acos(-1); public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); // Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); // InputReader in = new InputReader(new FileInputStream("src/test.in")); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("src/test.out"))); /* */ int n = in.nextInt(); String str = in.next(); char first = str.charAt(0); int cnt = 1; for(int i = 1; i < n; i++) { if(str.charAt(i) != first) { first = str.charAt(i); cnt++; } } out.println(min(n, cnt + 2)); in.close(); out.flush(); out.close(); System.exit(0); } /* * return the number of elements in list that are less than or equal to the val */ private static long upperBound(List<Long> list, long val) { int start = 0; int len = list.size(); int end = len - 1; int mid = 0; while (true) { if (start > end) { break; } mid = (start + end) / 2; long v = list.get(mid); if (v == val) { start = mid; while(start < end) { mid = (start + end) / 2; if(list.get(mid) == val) { if(mid + 1 < len && list.get(mid + 1) == val) { start = mid + 1; } else { return mid + 1; } } else { end = mid - 1; } } return start + 1; } if (v > val) { end = mid - 1; } else { start = mid + 1; } } if (list.get(mid) < val) { return mid + 1; } return mid; } private static boolean isPalindrome(String str) { StringBuilder sb = new StringBuilder(); sb.append(str); String str1 = sb.reverse().toString(); return str.equals(str1); } private static String getBinaryStr(long n, int j) { String str = Long.toBinaryString(n); int k = str.length(); for (int i = 1; i <= j - k; i++) { str = "0" + str; } return str; } private static long modInverse(long r) { return bigMod(r, MOD - 2, MOD); } private static long bigMod(long n, long k, long m) { long ans = 1; while (k > 0) { if ((k & 1) == 1) { ans = (ans * n) % m; } n = (n * n) % m; k >>= 1; } return ans; } private static long ceil(long n, long x) { long div = n / x; if(div * x != n) { div++; } return div; } private static int ceil(int n, int x) { int div = n / x; if(div * x != n) { div++; } return div; } private static int abs(int x) { if (x < 0) { return -x; } return x; } private static double abs(double x) { if (x < 0) { return -x; } return x; } private static long abs(long x) { if(x < 0) { return -x; } return x; } private static int lcm(int a, int b) { return (a * b) / gcd(a, b); } private static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } private static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } private static int log(long x, int base) { return (int) (Math.log(x) / Math.log(base)); } private static long min(long a, long b) { if (a < b) { return a; } return b; } private static int min(int a, int b) { if (a < b) { return a; } return b; } private static long max(long a, long b) { if (a < b) { return b; } return a; } private static int max(int a, int b) { if (a < b) { return b; } return a; } private static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { return null; } return tokenizer.nextToken(); } public String nextLine() { String line = null; try { tokenizer = null; line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public boolean hasNext() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public int[] nextIntArr(int n) { int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArr(int n) { long arr[] = new long[n]; for(int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[] nextIntArr1(int n) { int arr[] = new int[n + 1]; for(int i = 1; i <= n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArr1(int n) { long arr[] = new long[n + 1]; for(int i = 1; i <= n; i++) { arr[i] = nextLong(); } return arr; } public void close() { try { if(reader != null) { reader.close(); } } catch(Exception e) { } } } }
JAVA