Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long p[n], q[n];
for (long long i = 0; i < n; i++) {
cin >> p[i];
}
long long m = p[n - 1];
for (long long i = n - 2; i >= 0; i--) {
if (p[i] <= m) {
q[i] = (m - p[i]) + 1;
} else {
q[i] = 0;
m = p[i];
}
}
q[n - 1] = 0;
for (long long i = 0; i < n; i++) {
cout << q[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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class LuxuriousHouses {
void solve() {
int n = in.nextInt();
int[] H = new int[n];
for (int i = 0; i < n; i++) H[i] = in.nextInt();
int[] res = new int[n];
int max = H[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (H[i] > max) {
max = H[i];
} else {
res[i] = (max + 1) - H[i];
}
}
for (int i = 0; i < n; i++) {
if (i > 0) out.print(' ');
out.print(res[i]);
}
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new LuxuriousHouses().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, *a;
cin >> n;
a = new int[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--) {
mx = max(mx, a[i + 1]);
v.push_back(max(mx - a[i] + 1, 0));
}
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i] << ' ';
}
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import math
def fact(n):
ans = 1
for i in range(2, n+1):
ans*= i
return ans
def comb(n, c):
return fact(n)//(fact(n-c)*c)
n = int(input())
nums= list(map(int, input().split()))
m= 0
ans = []
for i in range(n-1, -1, -1):
if(nums[i] <= m):
ans.append(str(m-nums[i]+1))
else:
ans.append('0')
m =max(m, nums[i])
ans = ans[::-1]
print(' '.join(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 | #!/bin/python
from sys import stdin
n=int(stdin.readline().strip())
arr=map(int,stdin.readline().strip().split())
max=arr[n-1]+1
ans=[0]*n
for i in range(n-2,-1,-1):
if arr[i]<max:
ans[i]=max-arr[i]
else:
ans[i]=0
max=arr[i]+1
for i in range(n):
print ans[i], | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n= int(input())
mas = list(map(int,input().split(" ")))
submas=[0]*(n)
ma=0
for i in range(n-1,-1,-1):
submas[i]=str(max(ma-mas[i]+1,0))
if (ma<mas[i]):
ma =mas[i]
print(" ".join(submas))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const long long MAXN = (long long)((1e5) + 100);
long long cuberoot(long long x) {
long long lo = 1, hi = min(2000000ll, x);
while (hi - lo > 1) {
long long mid = (lo + hi) / 2;
if (mid * mid * mid < x) {
lo = mid;
} else
hi = mid;
}
if (hi * hi * hi <= x)
return hi;
else
return lo;
}
const long long dx[4] = {-1, 1, 0, 0};
const long long dy[4] = {0, 0, -1, 1};
long long XX[] = {-1, -1, -1, 0, 0, 1, 1, 1};
long long YY[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const long long N = (long long)(1 * 1e6 + 10);
bool comp(pair<long long, pair<long long, long long>> p1,
pair<long long, pair<long long, long long>> p2) {
if (p1.first > p2.first)
return true;
else if (p1.second == p2.second) {
if (p1.second.first < p2.second.first) return true;
}
return false;
}
long long fact(long long n);
long long ncr(long long n, long long r) {
return fact(n) / (fact(r) * fact(n - r));
}
long long fact(long long n) {
long long res = 1;
for (long long i = 2; i <= n; i++) res = res * i;
return res % 1000000007;
}
const long long a = 1000000000;
long long nCr(long long n, long long r) {
long long fac1 = 1, fac2 = 1, fac;
for (long long i = r; i >= 1; i--, n--) {
fac1 = fac1 * n;
if (fac1 % i == 0)
fac1 = fac1 / i;
else
fac2 = fac2 * i;
}
fac = fac1 / fac2;
return fac % a;
}
const long long m = 1000000007;
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
signed main() {
long long n;
cin >> n;
long long arr[n + 1];
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
long long mx = 0;
vector<long long> v;
for (long long i = n - 1; i >= 0; i--) {
if (i == n - 1) {
v.push_back(0);
mx = arr[i];
} else {
long long ans = 0;
if (mx >= arr[i])
ans = max(mx, arr[i]) - arr[i] + 1;
else
ans = max(mx, arr[i]) - arr[i];
v.push_back(ans);
mx = max(mx, arr[i]);
}
}
for (long long i = n - 1; i >= 0; i--) cout << v[i] << " ";
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
a = map(int, raw_input().split())
out = [0]*n
m = 0
for i in range(n) :
e = a[n-1-i]
if e <= m :
out[n-1-i] = m - e + 1
else :
m = e
for o in out:
print o, | 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 | __author__ = 'yarsanich'
n = int(input())
a = list(map(int,input().split()))
b = [0 for i in range(n)]
m = 0
for i in range(n-1, -1, -1):
b[i] = max(0, m - a[i] + 1)
m = max(m, a[i])
print(*b) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String line = br.readLine();
String[] split = line.split(" ");
int[] list = new int[n];
for (int i = 0; i < n; i++) {
list[i] = Integer.parseInt(split[i]);
}
solve(list);
}
private static void solve(int[] ints) {
Integer higher = 0;
int[] ret = new int[ints.length];
for (int i = ints.length - 1; i > -1; i--) {
Integer current = ints[i];
if (current > higher) {
ret[i] = 0;
} else {
ret[i] = higher - current + 1;
}
if (current > higher) {
higher = current;
}
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < ret.length; i++) {
result.append(ret[i] + " ");
}
System.out.println(result.toString().trim());
}
}
| 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.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.InputMismatchException;
public class Main
{
class MyScanner
{
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
BufferedInputStream bis = new BufferedInputStream(System.in);
public int read()
{
if (-1 == numChars)
{
throw new InputMismatchException();
}
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = bis.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
{
return -1;
}
}
return buf[curChar++];
}
public int nextInt()
{
int c = read();
while (isSpaceChar(c))
{
c = read();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
{
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c)
{
return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
}
}
public void foo() throws IOException
{
//BufferedInputStream in = new BufferedInputStream(new FileInputStream("in.txt"));
//System.setIn(in);
MyScanner scan = new MyScanner();
int n = scan.nextInt();
int[] h = new int[n];
for(int i = 0;i < n;++i)
{
h[i] = scan.nextInt();
}
int maxHeight = -1;
int[] ans = new int[n];
for(int i = n - 1;i >= 0;--i)
{
if(h[i] <= maxHeight)
{
ans[i] = maxHeight - h[i] + 1;
}
maxHeight = Math.max(maxHeight, h[i]);
}
for(int i = 0;i < n;++i)
{
System.out.print(ans[i] + " ");
}
System.out.println();
}
public static void main(String[] args) throws IOException
{
new Main().foo();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* Created by tawsif on 9/29/15.
*
* @Time 12:07 PM
*/
public class LuxuriousHouses322B {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
PrintWriter out;
long timeBegin, timeEnd;
public void runIO() throws IOException {
timeBegin = System.currentTimeMillis();
InputStream inputStream;
OutputStream outputStream;
if (ONLINE_JUDGE) {
inputStream = System.in;
Reader.init(inputStream);
outputStream = System.out;
out = new PrintWriter(outputStream);
} else {
inputStream = new FileInputStream("input.txt");
Reader.init(inputStream);
out = new PrintWriter("output.txt");
}
solve();
out.flush();
out.close();
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
private void solve() throws IOException {
int n = Reader.nextInt();
long [] houses = new long[n];
long [] answer = new long[n];
for (int i = 0; i < n; i++) {
houses[i] = Reader.nextLong();
}
long maxHouse = 0 ;
for (int i = n-1 ; i >= 0 ; i--) {
answer[i] = Math.max(0, maxHouse + 1 - houses[i]) ;
if(houses[i] > maxHouse) maxHouse = houses[i];
}
for (int i = 0; i < n; i++) {
out.print(answer[i] + " ");
}
out.println();
}
public static void main(String[] args) throws IOException {
new LuxuriousHouses322B().runIO();
}
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
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.*;
import java.util.*;
public class B {
public static void main(String[]args)throws IOException{
BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(x.readLine());
StringTokenizer st=new StringTokenizer(x.readLine());
int[]arr=new int[n];
for (int i=0; i<n; i++){
arr[i]=Integer.parseInt(st.nextToken());
}
int curmax=-1;
int[]ans=new int[n];
for (int i=n-1; i>=0; i--){
if (arr[i]<=curmax){
ans[i]=curmax-arr[i]+1;
}else{
curmax=arr[i];
}
}
for (int i=0; i<n; i++){
System.out.print(ans[i]+" ");
}
System.out.println();
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | i = int(input())
l = list(reversed(list(map(int,input().split()))))
m = 0
t = []
for x in l:
if x > m:
t.append(0)
else:
t.append((m+1) - x)
if x > m:
m = x
print(' '.join([str(elem) for elem in list(reversed(t))])) | 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 isP(long int hj) {
long int op;
for (op = 2; op <= sqrt(hj); op++) {
if (hj % op == 0) return 0;
}
return 1;
}
void swap(long long int *p, long long int *q) {
long long int tmp = *p;
*p = *q;
*q = tmp;
}
int mind(long long int p) {
int mindd = 10;
while (p > 0) {
if (p % 10 < mindd) mindd = p % 10;
p /= 10;
}
return mindd;
}
int maxd(long long int p) {
int maxdd = -1;
while (p > 0) {
if (p % 10 > maxdd) maxdd = p % 10;
p /= 10;
}
return maxdd;
}
string fdi(int hi) {
switch (hi) {
case 0:
return "zero";
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "six";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
}
return "";
}
long long int bd(long long int mk, long long int nk) {
long long int i;
for (i = min(mk, nk); i >= 1; i--) {
if (mk % i == 0 && nk % i == 0) return i;
}
return 0;
}
int dsm(long long int pkk) {
if (pkk < 0) pkk *= -1;
while (pkk > 0) {
if (pkk % 10 == 8) return 1;
pkk /= 10;
}
return 0;
}
void seving(int ar[], long long int n) {
long long int i, j;
for (i = 3; i < n; i += 2) ar[i] = 1;
ar[2] = 1;
for (i = 2; i < n; i++) {
if (ar[i] == 1) {
for (j = i * i; j < n; j += i) ar[j] = 0;
}
}
}
int tellme(int a, int b, int c) {
if (c < a + b)
return 1;
else if (a + b == c)
return 2;
else
return 0;
}
int vow(string ch) {
int i;
for (i = 0; i < ch.size(); i++) {
if (ch[i] >= 48 && ch[i] <= 57) return 1;
}
return 0;
}
int rk(char ch1, char ch2) {
char str[9] = {'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
int i, j;
for (i = 0; i < 9; i++) {
if (str[i] == ch1) break;
}
for (j = 0; j < 9; j++) {
if (str[j] == ch2) break;
}
if (i > j)
return 1;
else
return 0;
}
long long int gcd(long long int a, long long int b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
int isT(long long int b) {
long long int i, ct;
ct = 0;
for (i = 2; i <= (b - 1); i++) {
if (b % i == 0) ct++;
if (ct > 1) return 0;
}
if (ct == 1)
return 1;
else
return 0;
}
int restalp(string st) {
long int i, lt;
lt = st.size();
for (i = 1; i < lt; i++) {
if (st[i] >= 65 && st[i] <= 90) return 1;
}
return 0;
}
long long int rerun(long long int i, long long int a, long long int k) {
if (i == k)
return a;
else
return rerun(i + 1, a + mind(a) * maxd(a), k);
}
int bitt(long long int a) {
long int ct;
ct = 0;
while (a > 0) {
if (a & 1) ct++;
a = a >> 1;
}
return ct;
}
int kim(long long int a, long long int b) {
while (a > 0) {
if (a == 1)
break;
else if (a % 2 == 0)
a /= 2;
else if (a % 3 == 0)
a /= 3;
else
return 0;
}
while (b > 0) {
if (b == 1)
return 1;
else if (b % 2 == 0)
b /= 2;
else if (b % 3 == 0)
b /= 3;
else
return 0;
}
return 0;
}
int smdg(int as, int bs) {
int sm2 = 0;
while (as > 0) {
sm2 += as % bs;
as /= bs;
}
return sm2;
}
int main() {
long long int a, b, c, d, p, q, g1, g2, b1, b2, d2, a1, a2, i, l, lt, n, k, j,
r, col, h, f, maxl, minr, maxI, m, mov, prev, t, curr, sm, eng, ct, x,
prevI, num, den, s, y, max;
cin >> n;
long long int A[n];
for (i = 0; i < n; i++) {
cin >> A[i];
}
max = -1;
for (i = n - 1, j = 0; j < n; j++, i--) {
p = A[i];
if (A[i] > max)
A[i] = 0;
else
A[i] = max + 1 - A[i];
if (p > max) max = p;
}
for (i = 0; i < n; i++) cout << A[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;
public class B_581 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(
System.in)));
OutputStream out = new BufferedOutputStream ( System.out );
int n = sc.nextInt();
long maxHi = Long.MIN_VALUE;
long[] houses = new long[n];
long[] so = new long[n];
so[n-1] =0;
for(int i=0; i<n ; i++)
{
houses[i] = sc.nextLong();
}
for(int i=n-2; i>=0 ; i--) {
maxHi = Math.max(maxHi, houses[i+1]);
so[i] = Math.max(0, (maxHi -houses[i]) +1);
}
for(int i=0; i<n ; i++)
out.write((so[i]+" ").getBytes());
out.flush();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = [int(i) for i in input().split()] + [0]
v = [0 for i in h]
for p in range(n,-1,-1):
if p == n: v[p] = h[p]
else: v[p] = max(v[p+1], h[p])
print(' '.join(["%s" % max(0, v[i+1]+1-h[i]) for i in range(n)])) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100002], m[100002], i, j, ans[100002];
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
m[n - 1] = 0;
ans[n - 1] = 0;
for (i = n - 2; i >= 0; i--) {
m[i] = max(m[i + 1], a[i + 1]);
ans[i] = max(0, m[i] - a[i] + 1);
}
for (i = 0; i < n; i++) {
printf("%d ", ans[i]);
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
h = map(int, raw_input().split())
maxvec = [ 0 for i in xrange(n)]
for i in xrange(n-2,-1,-1):
maxvec[i]=max(maxvec[i+1],h[i+1])
newh=[max(maxvec[i]-h[i]+1,0) for i in xrange(n)]
for i in newh:
print i,
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.util.*;
import java.io.*;
public class temp {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
int[] ans = new int[n];
int max = arr[n-1];
for (int i = n-2; i >=0; i--)
{
if(arr[i]>max)
{
max = arr[i];
}
else
{
if(arr[i]<max)
{
ans[i] = (max+1)-arr[i];
}
else
{
ans[i] = 1;
}
}
}
for(int i : ans)
System.out.print(i+" ");
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
const int MAX = 1e7 + 1;
double pi = 3.1415926535897932384626433832795;
const long long inf = 1e18;
long long mod = 1e6;
long long powr(long long a, long long b) {
if (b == 0) return 1LL;
long long x = powr(a, b / 2);
x = (x * x) % mod;
if (b % 2) return ((a % mod) * x) % mod;
return x;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int jump(int h, int *H, int index) {
if (h > H[index])
return H[index];
else
return h;
}
int main() {
int N;
cin >> N;
long long a[N];
for (int i = 0; i < N; i++) cin >> a[i];
long long b[N], mx = a[N - 1];
b[N - 1] = 0;
for (int i = N - 2; i >= 0; i--) {
if (a[i] > mx)
b[i] = 0;
else
b[i] = mx - a[i] + 1;
mx = max(mx, a[i]);
}
for (int i = 0; i < N; i++) cout << b[i] << " ";
cout << "\n";
;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
import java.io.*;
public class solution {
public static void merge(long arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
public static void sort(long arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int a[] = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
a[i] = Integer.parseInt(st.nextToken());
}
int max = a[n-1];
int ans = 0;
int b[] = new int[n];
b[n-1] = 0;
for(int i=n-2;i>=0;i--){
ans = Math.max(0,max+1-a[i]);
b[i] = ans;
max = Math.max(max,a[i]);
}
for(int i=0;i<n;i++){
System.out.print(b[i] + " ");
}
}
static class sort implements Comparator<ArrayList<Integer>> {
@Override
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
int c = o1.get(0).compareTo(o2.get(0));
return 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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
public class C {
static Random random;
private static void mySort(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
Arrays.sort(s);
}
public static void main(String[] args) {
random = new Random(543534151132L + System.currentTimeMillis());
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int nb=in.nextInt();
int[] T=new int[nb];
for(int i=0;i<nb;i++)T[i]=in.nextInt();
int[] A=new int[nb];
int max=T[nb-1];
A[nb-1]=0;
for(int i=nb-2;i>=0;i--) {
if(T[i]<=max) {
A[i]=max-T[i]+1;
}else {
max=T[i];
A[i]=0;
}
}
for(int i=0;i<nb;i++)out.print(A[i]+" ");
out.close();
}
public static int gcd(int a, int b)
{
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public static long gcd(long a, long b)
{
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[] a = new int[n];
int[] max = new int[n];
boolean[] visited = new boolean[n];
for (int i = 0; i < n; i++) {
a[i] = cin.nextInt();
}
max[n - 1] = a[n - 1];
visited[n - 1] = true;
for (int i = n - 2; i >= 0; i--) {
if (a[i] > max[i + 1]) {
max[i] = a[i];
visited[i] = true;
} else {
max[i] = max[i + 1];
}
}
for (int i = 0; i < n; i++) {
if (visited[i]) {
System.out.print("0 ");
} else {
System.out.print(Math.max(0, max[i] - a[i] + 1) + " ");
}
}
cin.close();
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
l = list(map(int , input().split()))
a = list(range(n+1)); y = 0
for i in range(n-1 , -1 , -1):
y = max(y , l[i])
a[i] = y
a[n] = 0
for i in range(n):
if (l[i] == a[i]) & (l[i] != a[i+1]):
print('0 ' , end = '')
else:
print(a[i]-l[i]+1 , end = '')
print(' ' , end = '')
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.util.Scanner;
public class LuxuriousHouse {
public static void main(String asd[])throws Exception
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int a[]=new int[n];
int b[]=new int[n];
int max=Integer.MIN_VALUE;int k=0;
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
}
max=a[n-1];
for(int i=n-2;i>=0;i--)
{
if(a[i]<=max)
{
b[i]=(max-a[i])+1;
}
else
max=a[i];
}
for(int i=0;i<n;i++)
System.out.print(b[i]+" ");
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.*;
import java.util.*;
public class div_2_322_b {
public static void main(String args[]){
FScanner in = new FScanner();
PrintWriter out = new PrintWriter(System.out);
//int t = in.nextInt();
//while(t-->0) { }
int n=in.nextInt();
int a[]=in.readArray(n);
int arr[]=new int[n];
int max=0;
for(int i=n-1;i>=0;i--)
{
if(a[i]>max)
max=a[i];
else
{
arr[i]=max+1-a[i];
}
}
arr[n-1]=0;
for(int i=0;i<n;i++)
{
out.print(arr[i]+" ");
}
out.close();
}
static class FScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sb = new StringTokenizer("");
String next(){
while(!sb.hasMoreTokens()){
try{
sb = new StringTokenizer(br.readLine());
} catch(IOException e){ }
}
return sb.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
float nextFloat(){
return Float.parseFloat(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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class LuxuriousHouses {
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
Reader in = new Reader();
int n = in.readInt();
int[] a = in.readIntArray(n);
int[] ans = new int[n];
int max = a[n - 1];
ans[n - 1] = 0;
for(int i = n - 1; i > 0; i--){
if(max > a[i - 1]) ans[i - 1] = max - a[i - 1] + 1;
else{
if(max == a[i - 1]) ans[i - 1] = 1;
else{
ans[i - 1] = 0;
max = a[i - 1];
}
}
}
for(int i : ans) out.print(i + " ");
in.close();
out.close();
}
static PrintWriter out;
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String read() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int readInt() {
return Integer.parseInt(read());
}
long readLong() {
return Long.parseLong(read());
}
double readDouble() {
return Double.parseDouble(read());
}
char readChar() {
return read().charAt(0);
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = readInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = readLong();
return a;
}
double[] readDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = readDouble();
return a;
}
String[] readStringArray(int n) {
String[] s = new String[n];
for (int i = 0; i < n; i++)
s[i] = read();
return s;
}
char[] readCharArray(int n) {
char[] ch = new char[n];
for (int i = 0; i < n; i++)
ch[i] = readChar();
return ch;
}
String readLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | input()
h = list(map(int,input().split()))
m = h[-1]
n = []
for x in h[::-1]:
n.append(str(max(0,m-x)))
m = max(m,x+1)
print (' '.join(n[::-1]))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int total;
cin >> total;
int *input = new int[total];
int *save = new int[total];
for (int i = 0; i < total; i++) cin >> input[i];
save[total - 1] = 0;
int highest = input[total - 1];
for (int i = total - 2; i > -1; i--) {
if (input[i] > highest) {
highest = input[i];
save[i] = 0;
} else {
save[i] = highest - input[i] + 1;
}
}
for (int i = 0; i < total; i++) cout << save[i] << ' ';
cout << endl;
delete[] save;
delete[] input;
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=[int(i) for i in input().split()]
l.reverse()
n1=1
ans=[0]
mx=l[0]
if n>1:
if l[0]<l[1]:
ans.append(0)
else:
ans.append(l[0]-l[1]+1)
while n1<n-1:
num1=l[n1]
num2=l[n1+1]
mx=max(num1,l[0],mx)
if num2<=mx:
ans.append(mx-num2+1)
else:
ans.append(0)
n1+=1
ans.reverse()
print(*ans)
else:
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.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
*
* @author karanjobanputra
*/
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);
CF322B solver = new CF322B();
solver.solve(1, in, out);
out.close();
}
static class CF322B {
public void solve(int testNumber, InputReader sc, PrintWriter pw) {
int n = sc.nextInt();
long arr[] = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
long max = arr[arr.length - 1];
long required[] = new long[n];
required[arr.length - 1] = 0;
for (int i = arr.length - 2; i >= 0; i--) {
if (arr[i] > max) {
max = arr[i];
required[i] = 0;
} else {
required[i] = max - arr[i] + 1;
}
}
for (long l : required) {
pw.print(l + " ");
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.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 nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int maxx = 0;
for (int i = n - 1; i >= 0; i--) {
int jada = maxx - a[i] + 1;
if (maxx < a[i]) {
maxx = a[i];
}
a[i] = max(0, jada);
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class LuxuriousHouses {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
int ans[] = new int[n];
int max = 0;
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
max = arr[n-1]+1;
for(int i=n-2;i>=0;i--){
if(arr[i] >= max){
ans[i] = 0;
max = arr[i]+1;
}
else
ans[i] = 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())
t = list(map(int,input().split()))[::-1]
f=[0]
u=t[0]
for k in range(1,n):
if t[k]>u:
f.append(0)
if t[k]>u:
u=t[k]
else:
s = u-t[k]+1
f.append(s)
if t[k]>u:
u=t[k]
print(*f[::-1])
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
l=list(map(int,input().split()))
max1=0
arr1=[]
for i in range (n-1,-1, -1):
if l[i] >max1:
max1=l[i]
arr1.append(0)
elif l[i]==max1:
arr1.append(1)
else:
arr1.append(max1-l[i]+1)
arr1=arr1[::-1]
print(*arr1) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int max = v[v.size() - 1];
vector<int> res;
res.push_back(0);
for (int i = v.size() - 2; i >= 0; i--) {
if (max >= v[i]) {
res.push_back(max - v[i] + 1);
} else {
res.push_back(0);
max = v[i];
}
}
reverse(res.begin(), res.end());
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
buildings = list(map(int, input().split(' ')))
m = 0
result = []
for i in range(len(buildings) - 1, -1, -1):
b = buildings[i]
x = m - b
if x < 0:
result.append(0)
m = b
else:
result.append(x + 1)
print(' '.join(map(str, result[::-1]))) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int maxn = 100010;
int n;
int h[maxn];
int ans[maxn];
void solve() {
scanf("%d", &n);
for (int i = (0); i < (n); i++) scanf("%d", h + i);
int hmax = 0;
for (int i = (n)-1; i >= (0); i--) {
if (h[i] > hmax) {
ans[i] = 0;
} else {
ans[i] = hmax + 1 - h[i];
}
hmax = max(hmax, h[i]);
}
for (int i = (0); i < (n); i++) printf("%d ", ans[i]);
}
int main() {
solve();
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import sys
input=sys.stdin.buffer.readline
n=int(input())
arr=list(map(int,input().split()))
right_max=arr[n-1]
ans=[0]
for i in range(n-2,-1,-1):
if arr[i]>right_max:
ans.append(0)
right_max=arr[i]
else:
ans.append(right_max-arr[i]+1)
ans=ans[::-1]
for x in ans:
print(x,end=' ')
#unknown_2433 | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[100001], maxi[100001];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
maxi[i] = max(maxi[i + 1], a[i]);
}
for (int i = 0; i < n; i++) {
cout << max(maxi[i + 1] - a[i] + 1, 0) << ' ';
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class _0909LuxuriousHouses {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
long[] arr = new long[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextLong();
}
long max=arr[n-1];
arr[n-1]=0;
for(int i=n-2;i>=0;i--) {
long temp=Math.max(0,max+1-arr[i]);
max=Math.max(max, arr[i]);
arr[i]=temp;
}
//print
for(int i=0;i<n;i++) {
System.out.print(arr[i]+" ");
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
n = int(input())
A = [int(i) for i in input().split()]
start = time.time()
A = [ A[n-i-1] for i in range(n)]
ans = [ 0 for i in range(n) ]
m = 0
for i in range(n):
if A[i] > m:
m = A[i]
else:
ans[i] = 1+m-A[i]
for i in range(n):
print(ans[n-1-i], end=' ')
print()
finish = time.time()
#print(finish - start)
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[100000];
int b[100000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int max = a[n - 1];
b[0] = 0;
for (int i = n - 2; i >= 0; i--) {
if (a[i] <= max) {
b[i] = max - a[i] + 1;
} else {
b[i] = 0;
max = a[i];
}
}
for (int i = 0; i < n - 1; i++) {
cout << b[i] << " ";
}
cout << b[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>
using namespace std;
int main() {
vector<int> v(100000), a(100000);
int max = -1, c;
cin >> c;
for (int i = 0; i < c; i++) {
cin >> v[i];
}
a[c - 1] = 0;
max = v[c - 1];
for (int i = c - 2; i >= 0; i--) {
if (v[i] > max) {
max = v[i];
a[i] = 0;
} else {
a[i] = max - v[i] + 1;
}
}
for (int i = 0; i < c; i++) {
cout << " " << a[i];
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, a[100005], maxx[100005];
int main(void) {
int i, j;
while (~scanf("%d", &n)) {
for (i = 0; i < n; i++) scanf("%d", &a[i]);
maxx[n - 1] = 0;
for (i = n - 2; i >= 0; i--) maxx[i] = max(maxx[i + 1], a[i + 1]);
for (i = 0; i < n; i++) {
if (a[i] > maxx[i])
printf("0 ");
else
printf("%d ", maxx[i] + 1 - a[i]);
}
puts("");
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
public class Main {
public static void main(String[] args) throws NumberFormatException,
IOException {Solve solve = new Solve();solve.solve();}
}
class Solve{
void solve() throws NumberFormatException, IOException{
ContestScanner in = new ContestScanner();
Writer out = new Writer();
int n = in.nextInt();
int max = 0;
StringBuilder sb = new StringBuilder();
int[] h = new int[n];
for(int i=0; i<n; i++){
h[i] = in.nextInt();
}
int[] res = new int[n];
for(int i=n-1; i>=0; i--){
if(h[i] > max){
max = h[i];
}else{
res[i] = max+1-h[i];
}
}
for(int i=0; i<n-1; i++){
System.out.print(res[i]+" ");
}
System.out.println(res[n-1]);
}
}
class MultiSet<T> extends HashMap<T, Integer>{
@Override
public Integer get(Object key){return containsKey(key)?super.get(key):0;}
public void add(T key,int v){put(key,get(key)+v);}
public void add(T key){put(key,get(key)+1);}
}
class Timer{
long time;
public void set(){time = System.currentTimeMillis();}
public long stop(){return System.currentTimeMillis()-time;}
}
class Writer extends PrintWriter{
public Writer(String filename) throws IOException
{super(new BufferedWriter(new FileWriter(filename)));}
public Writer() throws IOException{super(System.out);}
}
class ContestScanner {
private BufferedReader reader;
private String[] line;
private int idx;
public ContestScanner() throws FileNotFoundException
{reader = new BufferedReader(new InputStreamReader(System.in));}
public ContestScanner(String filename) throws FileNotFoundException
{reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));}
public String nextToken() throws IOException {
if (line == null || line.length <= idx) {
line = reader.readLine().trim().split(" ");
idx = 0;
}
return line[idx++];
}
public String readLine() throws IOException{return reader.readLine();}
public long nextLong() throws IOException, NumberFormatException
{return Long.parseLong(nextToken());}
public int nextInt() throws NumberFormatException, IOException
{return (int) nextLong();}
public double nextDouble() throws NumberFormatException, IOException
{return Double.parseDouble(nextToken());}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
int h[100000], max[100001];
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
max[n] = h[n - 1] - 1;
for (int i = n - 1; i >= 0; --i) {
if (h[i] > max[i + 1])
max[i] = h[i];
else
max[i] = max[i + 1];
}
for (int i = 0; i < n; ++i) {
if (max[i + 1] >= h[i])
cout << max[i + 1] - h[i] + 1 << ' ';
else
cout << 0 << ' ';
}
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, y = 0;
cin >> n;
long long a[n], b[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
b[n - 1] = 0;
y = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (y == a[i]) {
b[i] = 1;
} else {
y = max(y, a[i]);
if (a[i] >= y) {
b[i] = 0;
} else
b[i] = y - a[i] + 1;
}
}
for (long long i = 0; i < n; i++) {
cout << b[i] << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #!/usr/bin/python
from collections import deque
def ir():
return int(raw_input())
def ia():
line = raw_input()
line = line.split()
return map(int, line)
# e + x = m + 1
# x = m + 1 - e
n = ir()
a = ia(); a.reverse()
m = 0; ans = []
for e in a:
ans.append(max(m + 1 - e, 0))
if e > m: m = e
ans.reverse()
ans = map(str, ans)
print ' '.join(ans)
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long int maxt(long long int x, long long int y) { return (x > y) ? x : y; }
int main() {
long long int n;
cin >> n;
long long int arr[n], i;
for (i = 0; i < n; ++i) cin >> arr[i];
long long int ans[n];
ans[n - 1] = arr[n - 1];
for (i = n - 2; i >= 0; i--) {
ans[i] = maxt(ans[i + 1], arr[i]);
}
for (i = 0; i < n - 1; ++i) {
if (ans[i + 1] < arr[i])
cout << "0 ";
else
cout << ans[i] - arr[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=input().split()
s=[]
for i in range(n):
l[i]=int(l[i])
s.append(0)
i=0
m=l[n-1]
while(i<n):
k=n-i-1
if(m>=l[k] and i!=0):
s[k]=m-l[k]+1
else:
m=l[k]
i+=1
for i in range(n):
print(s[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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int h[n];
for (int i = 0; i < n; i++) {
cin >> h[i];
}
long long int b[n];
long long int max = 0;
for (int j = 0; j < n; j++) {
if (j == 0) {
max = h[n - j - 1];
b[n - j - 1] = max - 1;
} else {
if (max > h[n - j - 1]) {
b[n - j - 1] = max;
} else if (max < h[n - j - 1]) {
max = h[n - j - 1];
b[n - j - 1] = max - 1;
} else if (max == h[n - j - 1]) {
b[n - j - 1] = max;
}
}
}
for (int k = 0; k < n; k++) {
if (h[k] == b[k]) {
cout << "1" << endl;
} else if (b[k] > h[k]) {
cout << (b[k] - h[k] + 1) << endl;
} else if ((h[k] - b[k]) > 0) {
cout << "0" << 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 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scan.nextInt();
}
int[] temp = new int[n];
temp[n-1]=0;
int max = array[n-1];
for(int i=n-2; i>=0; i--){
if(array[i]>max){
max = array[i];
temp[i]=0;
}
else
temp[i] = max-array[i]+1;
}
for(int x:temp){
System.out.print(x+" ");
}
scan.close();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
import java.io.*;
import java.lang.*;
public class Luxury
{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class pair implements Comparable<pair>
{
Integer x, y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
public int compareTo(pair o) {
return x.compareTo(o.x);
}
}
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt();
int[] a = new int[n];
int[] ans = new int[n];
a = in.nextIntArray(n);
int max = a[n-1];
ans[n-1] = 0;
for(int i=n-2;i>=0;i--)
{
if(a[i]>max)
{
max = a[i];
ans[i] = 0;
}
else
{
ans[i] = max - a[i]+1;
}
}
for(int i=0;i<n;i++)
pw.print(ans[i] + " ");
pw.flush();
pw.close();
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
h=list(map(int,input().split()))
l1=[0]*n; mx=0; ans=""
for i in range(n-1,-1,-1):
mx=max(mx,h[i])
l1[i]=mx
for i in range(n-1):
if h[i]>l1[i+1]: ans+="0 "
else: ans+=f"{l1[i]-h[i]+1} "
ans+='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.*;
public class Test {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] arr = new int[sc.nextInt()];
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextInt();
}
sc.close();
int[] max = new int[arr.length];
for(int i = max.length - 2; i >= 0; i--){
max[i] = Math.max(max[i + 1], arr[i + 1]);
}
for(int i = 0; i < arr.length; i++){
System.out.print((max[i] - arr[i] < 0 ? 0 : max[i] - arr[i] + 1) + " ");
}
System.out.println();
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, i, j, a[100000], k, x, b[100000];
int main() {
cin >> n;
j = n - 2;
b[n - 1] = 0;
for (i = 0; i < n; i++) cin >> a[i];
x = a[n - 1];
for (i = n - 2; i >= 0; i--) {
if (a[i] <= x) {
b[j] = x - a[i] + 1;
j--;
} else {
b[j] = 0;
j--;
x = a[i];
}
}
for (i = 0; i < n; i++) cout << b[i] << " ";
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
long long int a[n], suf[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
suf[n - 1] = 0;
for (i = n - 2; i >= 0; i--) {
suf[i] = max(a[i + 1], suf[i + 1]);
}
for (i = 0; i < n; i++) {
cout << max(suf[i] - a[i] + 1, 0LL) << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=input()
l=map(int,raw_input().split())
x=[0]*n
maxi=l[-1]
for i in xrange(n-2,-1,-1):
if l[i]>maxi:
maxi=l[i]
else:
x[i]=maxi-l[i]+1
for i in x:
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 | n = int(input())
a = list(map(int, input().split()))
a.reverse()
mx = 0
out = [0] * n
for i in range(n):
out[i] = max(mx + 1, a[i]) - a[i]
mx = max(a[i], mx)
out.reverse()
print(' '.join(map(str, out)))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | N = int(input())
A = list(map(int, input().split()))
B = A[:]
for i in range(N-2, -1, -1):
if(B[i] < B[i+1]):
B[i] = B[i+1]
for i in range(0, N - 1):
if(A[i] > B[i+1]):
print("0",end=" ")
else:
# print(A[i], end = "----")
print(B[i+1] + 1 - A[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 | #include <bits/stdc++.h>
using namespace std;
double pi = acos(-1);
const int MXN = 1e5 + 5;
const long long mod = 1e9 + 7;
void printv(vector<long long> v, long long n) {
for (long long i = 0; i < n; i++) {
cout << v[i] << " ";
}
}
vector<long long> inputv(vector<long long> v, long long n) {
for (long long i = 0; i < n; i++) {
long long y;
cin >> y;
v.push_back(y);
}
return v;
}
void solve() {
int n;
cin >> n;
vector<long long> v, res;
v = inputv(v, n);
long long m = 0;
for (int i = n - 1; i >= 0; i--) {
if (v[i] <= m) {
res.push_back(m - v[i] + 1);
} else {
m = v[i];
res.push_back(0);
}
}
reverse(res.begin(), res.end());
printv(res, (int)res.size());
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
a = list(map(int, input().split()))
ans = [0] * n
x = a[-1]
for i in range(n-2, -1, -1):
ans[i] = max(0, x-a[i]+1)
x = max(a[i], x)
print(*ans) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int num[100010];
int cop[100010];
int ans[100010];
map<int, int> pic;
int cmp(int a, int b) { return a > b; }
int main() {
int n, flagc = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &num[i]);
pic[num[i]]++;
cop[i] = num[i];
}
sort(cop, cop + n, cmp);
for (int i = 0; i < n; i++) {
if (!pic[cop[flagc]]) {
for (int j = flagc; j < n; j++) {
if (pic[cop[flagc = j]]) break;
}
}
if (pic[cop[flagc]]) {
if (cop[flagc] == num[i] && pic[cop[flagc]] == 1)
ans[i] = cop[flagc] - num[i];
else
ans[i] = cop[flagc] - num[i] + 1;
pic[num[i]]--;
}
}
for (int i = 0; i < n; i++) {
printf(i == 0 ? "%d" : " %d", ans[i]);
}
printf("\n");
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n, m, v = int(input()), 0, []
for x in map(int, reversed(input().split())):
v.append(max(0, m - x + 1))
m = max(m, x)
print(' '.join(map(str, reversed(v)))) | 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 | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
/**
*
* @author MyPC
*/
public class _581B_LuxuriousHouses {
public static void main(String args[]){
int n;
int[] a, max;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
// n = 100000;
a = new int[100000];
max = new int[100001];
for (int i = 0; i < n; i++){
a[i] = sc.nextInt();
// a[i] = i;
}
max[n] = 0;
for(int i = n-1; i >= 0; i--){
if (a[i] > max[i+1])
max[i] = a[i];
else
max[i] = max[i+1];
}
for(int i = 0; i < n; i++){
if (max[i+1] - a[i] >= 0)
System.out.print(max[i+1] - a[i] + 1 + " ");
else
System.out.print("0 ");
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | def f(l):
n = len(l)
rl = [0]*n
rm = l[-1]
for i in range(n-2,-1,-1):
rl[i] = max(rm+1-l[i],0)
if l[i]>rm:
rm = l[i]
return rl
_ = input() #1e5
l = list(map(int,input().split()))
print(*f(l))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int a[maxn], b[maxn], c[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
b[n + 1] = 0;
for (int i = (n); i >= (1); i--) {
if (a[i] > b[i + 1]) {
b[i] = a[i];
} else {
b[i] = b[i + 1];
}
}
for (int i = (n); i >= (1); i--) {
if (a[i] == b[i]) {
if (b[i] > b[i + 1])
c[i] = 0;
else
c[i] = 1;
} else
c[i] = b[i] - a[i] + 1;
}
for (int i = (1); i <= (n); i++) printf("%d ", c[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())
house = list(map(int,input().split(' ')))
dos = [0]*n
maximum = house[n-1]
for i in range(n-2,-1,-1):
dos[i]=max(0,maximum-house[i]+1)
if house[i]>maximum:
maximum=house[i]
print(*dos)
| 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()))
maxr = [0 for i in range(n)]
result = [0 for i in range(n)]
for i in range(n-2, -1, -1):
maxr[i] = max(maxr[i+1], a[i+1])
for i in range(n):
result[i] = maxr[i] - a[i] + 1
if result[i] < 0:
result[i] = 0
print(' '.join(str(r) for r in result))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, num[100002], i, j, ans[100002], max;
cin >> n;
for (i = 1; i <= n; i++) scanf("%d", &num[i]);
ans[n] = 0;
max = 0;
for (i = n; i >= 1; i--) {
if (max <= num[i]) {
max = num[i];
ans[i] = 0;
++max;
} else
ans[i] = max - num[i];
}
for (i = 1; i <= n; i++)
if (i != n)
printf("%d ", ans[i]);
else
printf("%d\n", 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;
const int maxn = 1e6 + 1;
long long n, i, j, k, m;
long long dem = 0, tong = 0, x, y, z, l, r;
char k1, k2;
long long a[maxn], b[maxn], c[maxn], h[maxn];
long long d[maxn];
string s, s1, s2;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = n; i >= 1; i--) {
b[i] = max(a[i], b[i + 1]);
}
for (i = 1; i <= n; i++) {
if (b[i] != a[i] || (b[i] == a[i] && a[i] == b[i + 1]))
c[i] = b[i] - a[i] + 1;
cout << c[i] << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
static const int MAXN = 1e5 + 10;
int n;
int data[MAXN];
int tail[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", data + i);
}
for (int i = n; i > 0; --i) {
tail[i] = max(tail[i + 1], data[i + 1]);
}
for (int i = 1; i <= n; ++i) {
printf("%d ", max(tail[i] + 1 - data[i], 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, X, Answer = int(input()), list(map(int, input().split())), [0]
Max = X[-1]
for i in X[:N - 1:][::-1]:
Answer.append(max(0, Max - i + 1))
Max = max(Max, i)
print(*Answer[::-1])
# Come together for getting better !!!!
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
public class 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+anss[i]+" ";
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];
for (int i = 0; i < n; ++i) cin >> Arr[i];
int mFl = -1;
for (int i = n - 1; i >= 0; --i) {
if (Arr[i] > mFl) {
mFl = Arr[i];
Arr[i] = 0;
} else
Arr[i] = (mFl + 1) - Arr[i];
}
for (int i = 0; i < n; ++i) cout << Arr[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 | r=lambda:map(int,raw_input().split())
n=input()
h=r()
M=0
a=[]
for i in reversed(h):
a.append(max(0,(M+1)-i))
M=max(M,i)
print ' '.join(map(str,reversed(a))) | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
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];
for (int i = 0; i < n; ++i)
a[i] = in.nextInt();
int[] res = new int[n];
int curMax = -1;
for (int i = n - 1; i >= 0; --i) {
if (a[i] > curMax) {
res[i] = 0;
curMax = a[i];
} else res[i] = curMax - a[i] + 1;
}
for (int i = 0; i < n; ++i)
out.print(res[i] + " ");
out.println();
}
}
static class InputReader {
BufferedReader in;
StringTokenizer st;
public InputReader(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
eat("");
}
public int nextInt() {
return Integer.parseInt(next());
}
public String next() {
while (!st.hasMoreTokens())
eat(nextLine());
return st.nextToken();
}
public String nextLine() {
try {
return in.readLine();
} catch (IOException e) {
throw new InputMismatchException();
}
}
public void eat(String str) {
if (str == null) throw new InputMismatchException();
st = new StringTokenizer(str);
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Bat-Orgil
*/
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[] h = new int[n];
for (int i = 0; i < n; i++) {
h[i] = in.nextInt();
}
int[] max = new int[n];
max[n - 1] = h[n - 1];
for (int i = n - 2; i >= 0; i--) {
max[i] = Math.max(h[i], max[i + 1]);
}
for (int i = 0; i < (n - 1); i++) {
if (h[i] > max[i + 1]) {
out.print("0 ");
} else {
out.print((max[i + 1] - h[i] + 1) + " ");
}
}
out.print(0);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int arr_min(int arr[], int a, int b) {
int min = arr[0];
for (int i = a; i != b; i++) {
if (min > arr[i]) min = arr[i];
}
return min;
}
int arr_max(int arr[], int a, int b) {
int max = arr[0];
for (int i = a; i != b; i++) {
if (max < arr[i]) max = arr[i];
}
return max;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t-- > 0) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int max = INT_MIN;
vector<int> v;
for (int i = n - 1; i >= 0; i--) {
if (arr[i] > max) {
v.push_back(0);
max = arr[i];
} else {
v.push_back(max - arr[i] + 1);
}
}
for (int i = v.size() - 1; i >= 0; i--) {
cout << v[i] << " ";
}
cout << endl;
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int h[] = new int[n+2];
for(int i = 0 ; i < n; i++) {
h[i] = in.nextInt();
}
int max[] = new int[n+2];
max[n] = 0;
for(int i = n-1; i >= 0; i--) {
max[i] = Math.max(max[i+1], h[i]);
}
for(int i = 0; i < n; i++) {
System.out.print(Math.max(0, max[i+1] - h[i] + 1) + " ");
}
System.out.println();
in.close();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.awt.Point;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
import static java.lang.Math.*;
import java.util.Map;
/**
*
* @author Mojtaba
*/
public class Main {
public static void main(String[] args) throws IOException {
MyScanner in = new MyScanner(System.in);
PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
StringBuilder sb = new StringBuilder("");
int n = in.nextInt();
int[] a = in.nextIntegerArray(n);
int max = Integer.MIN_VALUE;
ArrayList<Integer> h = new ArrayList<>();
for (int i = a.length - 1; i >= 0; i--) {
if (a[i] > max) {
max = a[i];
h.add(0);
} else {
h.add(max - a[i] + 1);
}
}
Collections.reverse(h);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < h.size(); i++) {
builder.append(h.get(i)).append(" ");
}
sb.append(builder.toString().trim());
//System.out.println(sb.toString().trim());
writer.println(sb.toString().trim());
writer.flush();
in.close();
}
static void sort(int[] a, int bp) {
Random rand = new Random();
for (int i = a.length - 1; i > 0; i--) {
int index = rand.nextInt(i + 1);
// Simple swap
int temp = a[index];
a[index] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
}
class MyScanner {
BufferedReader reader;
StringTokenizer tokenizer;
public MyScanner(InputStream stream) {
this.reader = new BufferedReader(new InputStreamReader(stream));
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public int[] nextIntegerArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextLong();
}
return a;
}
public int nextInt(int radix) throws IOException {
return Integer.parseInt(next(), radix);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public long nextLong(int radix) throws IOException {
return Long.parseLong(next(), radix);
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigInteger nextBigInteger(int radix) throws IOException {
return new BigInteger(next(), radix);
}
public String next() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
return this.next();
}
return tokenizer.nextToken();
}
public void close() throws IOException {
this.reader.close();
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n), x(n);
for (int i = 0; i < n; i++) cin >> v[i];
int mx = 0;
for (int i = n - 1; i >= 0; i--) {
if (v[i] > mx) {
x[i] = 0;
mx = v[i];
} else if (v[i] < mx)
x[i] = mx + 1 - v[i];
else
x[i] = 1;
}
for (int i = 0; i < n; i++) cout << x[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(raw_input())
hs = map(int, raw_input().split(' '))
max_h = 0
a = []
for h in hs[::-1]:
a.append(0 if h > max_h else max_h + 1 - h)
if max_h < h:
max_h = h
for aa in a[::-1]:
print aa, | 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())
houses = list(reversed(list(map(int, input().split()))))
ans = [0]*(n)
highestToTheRight = houses[0]
for i in range(1,n):
if houses[i] > highestToTheRight:
highestToTheRight = houses[i]
ans[i] = 0
else:
ans[i] = highestToTheRight - houses[i] + 1
print(*reversed(ans)) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
A = list(map(int, input().split()))
etaj = [0] * n
l = len(A)
ma = A[-1]
for i in range(-2, -l-1, -1):
if A[i] > ma:
ma = A[i]
etaj[i] = 0
"""
ma = A[i]
if A[i] <= A[i+1]:
etaj[i] = etaj[i+1] + A[i+1] - A[i]
elif (A[i] > A[i+1]) and (etaj[i+1] == 0):
etaj[i] = 0
elif (A[i] > A[i+1]) and (etaj[i+1] > 0):
etaj[i] = A[i+1] + etaj[i+1] - A[i]
"""
elif A[i] == ma:
etaj[i] = 1
else:
etaj[i] = ma - A[i] + 1
for elem in etaj: print(elem, end = ' ')
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #!/usr/bin/env python
#
# http://codeforces.com/problemset/problem/581/B
try:
n = int(raw_input())
h = raw_input().split()
for i in range(0, n):
h[i] = int(h[i])
t = 0
for i in reversed(range(0, n)):
if h[i] > t:
t = h[i]
h[i] = 0
else:
h[i] = t + 1 - h[i]
print(" ".join([str(h[i]) for i in range(0, n)]))
except IOError:
pass
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class contest_1_2 {
public static void main(String[] args) {
int n;
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
int[] array=new int[n];
int[] sol=new int[n];
for(int i=0;i<n;i++)
{
array[i]=cin.nextInt();
}
int max=array[n-1];
for(int i=n-2;i>=0;i--){
if(array[i]>max)
{
max=array[i];
continue;}
else{
sol[i]=max-array[i]+1;
}
}
for(int i=0;i<n;i++){
System.out.print(sol[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())
houses = [int(x) for x in input().split(' ')]
max_sizes = [0] * n
current_max = 0
for i in reversed(range(n)):
max_sizes[i] = current_max
if houses[i] > current_max:
current_max = houses[i]
for i in range(n):
floors_added = max_sizes[i] - houses[i] + 1
if floors_added < 0:
floors_added = 0
if i < n - 1:
print(floors_added, end=' ')
else:
print(floors_added) | 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;
using namespace std::chrono;
template <typename T1, typename T2>
void mapped(map<T1, T2> m) {
for (auto ite = m.begin(); ite != m.end(); ++ite) {
cout << "'" << ite->first << "'"
<< " : " << ite->second << endl;
}
}
template <typename T1, typename T2>
void mappedArr(map<T1, vector<T2>> m) {
for (auto ity = m.begin(); ity != m.end(); ++ity) {
cout << "'" << ity->first << "'"
<< " : "
<< "[";
for (int i = 0; i < (int)ity->second.size(); ++i) {
(i == (int)ity->second.size() - 1) ? cout << ity->second[i]
: cout << ity->second[i] << ",";
}
cout << "]" << endl;
}
}
string int_to_string(int x) {
stringstream ss;
ss << x;
string ni = ss.str();
return ni;
}
int char_to_int(char c) {
int n = (int)c - 48;
return n;
}
int string_to_int(string x) {
int n;
stringstream s(x);
s >> n;
return n;
}
char upperCase(char a) {
int d = (int)a - 97 + 65;
char c = (char)d;
return c;
}
char lowerCase(char a) {
int d = (int)a - 65 + 97;
char c = (char)d;
return c;
}
bool isInt(char a) {
int d = (int)a - 48;
if (d >= 0 && d <= 9) {
return true;
}
return false;
}
template <typename T1, typename T2>
void ArrPair(vector<pair<T1, T2>> vp) {
for (int i = 0; i < (int)vp.size(); ++i) {
cout << "(" << vp[i].first << "," << vp[i].second << ")" << endl;
}
}
const int MOD = 1e9 + 7;
const long long INF = 1e18L + 5;
unordered_map<int, int> um;
unordered_set<int> us;
set<int> s;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
int maxm = v[n - 1] - 1;
vector<int> ans;
for (int i = n - 1; i >= 0; --i) {
int w = v[i] - maxm;
if (w > 0) {
ans.push_back(0);
} else {
int k = abs(w) + 1;
ans.push_back(k);
}
maxm = max(maxm, v[i]);
}
for (int i = (int)ans.size() - 1; i >= 0; --i) {
cout << ans[i] << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MX = (int)1e6 + 17;
const int MOD = (int)1e9 + 7;
const long long oo = (long long)1e18 + 7;
const int INF = (int)999999999;
const int N = (int)1e5 + 17;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};
inline long long IN() {
long long x = 0, ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
inline void OUT(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10)
OUT(x / 10), putchar(x % 10 + '0');
else
putchar(x + '0');
}
int n, mx;
int a[N], dp[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) dp[i] = max(dp[i + 1], a[i]);
for (int i = 1; i <= n; i++) {
int x = dp[i + 1];
if (a[i] > x)
cout << 0 << ' ';
else
cout << x - 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 | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n = I()
a = LI()
m = -1
r = []
for c in a[::-1]:
r.append(max(0, m+1-c))
if m < c:
m = c
return ' '.join(map(str, r[::-1]))
print(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.*;
import java.util.*;
/**
* Created by bobyk on 22/08/15.
*/
public class Main {
public static void main(String[] args) throws IOException {
new Main().Run();
}
InputReader reader;
PrintWriter pw;
StringTokenizer stok;
private void Run() throws IOException {
reader = new InputReader(System.in);
pw = new PrintWriter(new OutputStreamWriter(System.out));
solve();
pw.flush();
pw.close();
}
private void solve() throws IOException {
int n = reader.nextInt();
int[] a = new int[n+1];
for (int i = 0; i < n; i++){
a[i] = reader.nextInt();
}
int[] mx = new int[n+1];
mx[n-1] = 0;
for (int i = n - 2; i >=0; i--){
mx[i] = Math.max(mx[i + 1], a[i + 1]);
}
for (int i = 0; i < n; i++){
if (a[i] <= mx[i]){
pw.print((mx[i] - a[i] + 1) + " ");
}
else pw.print(0 + " ");
}
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
public String nextLine() {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
int a[100010];
int b[100010];
int main() {
int n;
scanf("%d", &n);
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int max1 = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
b[i] = max1 - a[i] + 1;
if (b[i] < 0) b[i] = 0;
if (max1 < a[i]) max1 = a[i];
}
for (int i = 0; i < n - 1; i++) printf("%d ", b[i]);
printf("%d\n", b[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 | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws FileNotFoundException {
/*Read and Write*/
//Scanner in=new Scanner(new FileReader("testA.txt"));
//FileWriter fw=new FileWriter("outputTest.txt");
//BufferedWriter bw=new BufferedWriter(fw);
Scanner in=new Scanner(System.in);
int N=in.nextInt();
int[] A=new int[N];
for (int i=0;i<N;i++) A[i]=in.nextInt();
int[] out=new int[N];
int max=A[N-1];
for (int i=N-2;i>=0;i--){
out[i]=Math.max(0, max-A[i]+1);
max=Math.max(max, A[i]);
}
for (int i=0;i<out.length;i++){System.out.print(out[i]+" ");}
/*Write Epilogue
//test Write part
bw.write("asd is art\n");
bw.close();
*/
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
int data[maxn], answer[maxn] = {0};
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &data[i]);
int high = data[n - 1];
for (int i = n - 2; i >= 0; --i) {
if (data[i] > high)
high = data[i];
else
answer[i] = high - data[i] + 1;
}
printf("%d", answer[0]);
for (int i = 1; i < n; ++i) printf(" %d", answer[i]);
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long int a[100006], max;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
max = a[n - 1];
a[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
if (a[i] <= max) {
a[i] = max - a[i] + 1;
} else {
max = a[i];
a[i] = 0;
}
}
for (int i = 0; i < n; i++) {
printf("%lld", a[i]);
if (i != n - 1) printf(" ");
}
printf("\n");
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class Program {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner (System.in);
int n = Integer.parseInt(sc.nextLine());
int [] kq = new int [n];
String [] tmp = sc.nextLine().split(" ");
int max = Integer.parseInt(tmp[n - 1]) - 1;
int [] DL = new int [n];
for (int i = n - 1; i >= 0; i --){
DL [i] = Integer.parseInt(tmp[i]);
if (DL[i] > max + 1){
kq[i] = 0;
}
else {
kq[i] = max + 1 - DL[i];
}
max = Math.max(max, DL[i]);
}
for (int i = 0; i < n - 1; i ++){
System.out.print(kq[i] + " ");
}
System.out.println(kq[n - 1]);
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void pl(long long a) { printf("%lld\n", a); }
void pll(long long a, long long b) { printf("%lld %lld\n", a, b); }
void plll(long long a, long long b, long long c) {
printf("%lld %lld %lld ", a, b, c);
}
void sss(string s) { cout << s, printf("\n"); }
long long string_to_ll(string s) {
stringstream ss;
ss << s;
long long n;
ss >> n;
return n;
}
string ll_to_string(long long n) {
stringstream ss;
ss << n;
string s;
ss >> s;
return s;
}
long long n, i, j, ara[100005], mx;
int main() {
scanf("%lld", &n);
;
for (long long i = (long long)0; i < (long long)n; i++)
scanf("%lld", &ara[i]);
;
for (i = n - 1; i >= 0; i--) {
if (ara[i] > mx) {
mx = ara[i];
ara[i] = 0;
} else
ara[i] = mx - ara[i] + 1;
mx = max(mx, ara[i]);
}
for (long long i = (long long)0; i < (long long)n; i++)
cout << ara[i], printf(" ");
printf("\n");
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.