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 | def trackIndexes(x):
global index
index += 1
return (x,index)
size = input(); index = 0
houses = map(lambda x: trackIndexes(int(x)),raw_input().split())
sortedHouses = sorted(houses,reverse = True); cache ,i= 0,0;
while i < size:
if houses[i][1] <= sortedHouses[cache][1]:
if houses[i][0] < sortedHouses[cache][0]:
print (sortedHouses[cache][0] - houses[i][0]) + 1
elif houses[i][0] == sortedHouses[cache][0] and i + 1 != size:
if sortedHouses[cache][0] == sortedHouses[cache+1][0] and houses[i][1] < sortedHouses[cache][1]:
print (sortedHouses[cache][0] - houses[i][0]) + 1
sortedHouses.pop(cache+1)
else:
print 0
sortedHouses.pop(cache)
else:
print 0
else:
i -= 1; sortedHouses.pop(cache)
i += 1
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
w = list(map(int,input().split()))
out = []
out.append(0)
ma = w[n-1]
for i in range(n-2,-1,-1):
if(w[i] < ma):
out.append((ma+1) - w[i])
elif(w[i] > ma):
out.append(0)
ma = w[i]
elif(w[i] == ma):
out.append(1)
for k in range(n-1,-1,-1):
print(out[k], end = " ") | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
int n;
int main() {
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int brr[n], max = arr[n - 1];
brr[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
if (max < arr[i + 1]) max = arr[i + 1];
if (arr[i] <= max) {
brr[i] = max - arr[i] + 1;
} else
brr[i] = 0;
}
for (int i = 0; i < n; i++) {
printf("%d ", brr[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, a[222222];
scanf("%d\n", &n);
pair<int, int> p;
vector<pair<int, int> > v, v1;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
p.first = x;
p.second = 0;
v.push_back(p);
}
reverse(v.begin(), v.end());
int maxi = v[0].first, pos = 0;
;
for (int i = 0; i < v.size(); i++) {
if (pos != i) {
if (maxi >= v[i].first) v[i].second = maxi - v[i].first + 1;
if (maxi < v[i].first) {
maxi = v[i].first;
pos = i;
}
}
}
reverse(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) printf("%d ", v[i].second);
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class DevelopingSkill
{
public static void main(String args[]) throws IOException
{
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
//System.out.println("Enter the number in 1 to 10^5");
int totalHouses = sc.nextInt();
//InputStreamReader ir = new InputStreamReader(System.in);
//BufferedReader br = new BufferedReader(ir);
//String floorsperhouse = br.readLine().trim();
//StringTokenizer st = new StringTokenizer(floorsperhouse);
//if(st.countTokens() == totalHouses)
//{
//int inparr[] = new int[totalHouses];
ArrayList<Integer> inparray = new ArrayList<Integer>(totalHouses);
for(int i=0;i<totalHouses;i++){
inparray.add(sc.nextInt());
}
//int outarr[] = new int[totalHouses];
int maxHt=inparray.get(totalHouses-1);
//ArrayList<Integer> outarray = new ArrayList<Integer>(totalHouses);
int out[] = new int[totalHouses];
out[totalHouses-1]=0;
for(int j=totalHouses-2;j>=0;j--)
{
int inp=inparray.get(j);
if(inp > maxHt)
{
maxHt=inp;
out[j]=0;
//out[j]=maxHt-inp+1;
}
else
{
out[j]=maxHt-inp+1;
}
}
for(int output=0;output<totalHouses;output++)
{
System.out.print(out[output] + " ");
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
l=list(map(int,input().split()))
if n==1:
print (0)
exit()
m=[-1]*n
maxx=l[-1]
m[-2]=maxx
for i in range(n-3,-1,-1):
maxx=max(maxx,m[i+1],l[i+1])
m[i]=maxx
ans=[]
# print (m)
for i in range(n):
if l[i]>m[i]:
ans.append(0)
else:
ans.append((m[i]-l[i]+1))
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;
template <typename T>
inline T sqr(T x) {
T x_ = (x);
return x_ * x_;
}
template <typename T>
inline T qbr(T x) {
T x_ = (x);
return x_ * x_ * x_;
}
template <typename T>
inline int sign(T x) {
T x_ = (x);
return ((x_ > T(0)) - (x_ < T(0)));
}
template <typename T>
inline T mod(T x, T m) {
T x_ = (x);
return (((x_) >= 0) ? ((x_) % (m)) : ((((x_) % (m)) + (m)) % (m)));
}
template <typename T>
inline T gcd(T a, T b) {
while (b) {
T t = a % b;
b = a;
a = t;
}
return a;
}
template <typename T>
inline T gcd_ex(T a, T b, T& x, T& y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
T x1, y1;
T d = gcd_ex(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return d;
}
template <typename T>
inline T lcm(T a, T b) {
return (a * (b / gcd(a, b)));
}
template <typename T>
inline T binpow(T x, T deg) {
T res = (T)1;
for (; deg; x *= x, deg >>= 1)
if (deg & 1) res *= x;
return res;
}
template <typename T>
inline T modpow(T x, T deg, T m) {
assert(deg >= (T)0);
T res = (T)(1);
for (; deg; x = (((x) % m) * ((x) % m)) % m, deg >>= 1)
if (deg & 1) res = (((res) % m) * ((x) % m)) % m;
return res;
}
template <typename T>
inline bool is_between(T POSITION, T LEFT, T RIGHT) {
return (LEFT < POSITION) && (POSITION < RIGHT);
}
template <typename T>
inline bool is_inside(T POSITION, T LEFT, T RIGHT) {
return (LEFT <= POSITION) && (POSITION <= RIGHT);
}
template <typename Collection, typename UnaryOperation>
void foreach (Collection& col, UnaryOperation op) {
for_each(begin(col), end(col), op);
}
template <typename Collection, typename UnaryOperation>
Collection fmap(Collection& col, UnaryOperation op) {
transform(begin(col), end(col), col.begin(), op);
return col;
}
template <typename Collection, typename binop>
Collection zip(Collection& fc, Collection& sc, binop op) {
transform(begin(fc), end(fc), sc.begin(), fc.begin(), op);
return fc;
}
template <typename Collection, typename Condition>
bool exists(Collection& col, Condition con) {
auto exist = find_if(begin(col), end(col), con);
return exist != col.end();
}
template <typename Collection, typename Predicate>
Collection filterNot(Collection& col, Predicate predicate) {
auto returnIterator = remove_if(begin(col), end(col), predicate);
col.erase(returnIterator, end(col));
return col;
}
template <class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& P) {
in >> P.first >> P.second;
return in;
}
template <class T>
istream& operator>>(istream& in, vector<T>& Col) {
for (auto& el : Col) in >> el;
return in;
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& Col) {
for (auto& el : Col) os << el << " ";
return os;
}
template <class T>
ostream& operator<<(ostream& os, set<T>& Col) {
for (auto& el : Col) os << el << " ";
return os;
}
template <class T1, class T2>
ostream& operator<<(ostream& os, map<T1, T2>& Col) {
for (auto& el : Col) os << el << " ";
return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
for (auto& row : v) {
for (auto& el : row) os << el << " ";
os << "\n";
}
return os;
}
int main() {
int n;
cin >> n;
vector<long long> h(n);
cin >> h;
vector<long long> suff(n, -1);
map<long long, vector<int>> pos;
suff[n - 1] = h[n - 1];
pos[h[n - 1]].push_back(n - 1);
for (int i = n - 2; i >= 0; i--) {
suff[i] = max(suff[i + 1], h[i]);
pos[suff[i]].push_back(i);
}
vector<long long> answ(n, 0);
answ[n - 1] = 0;
for (int i = 0; i < n - 1; i++)
answ[i] = h[i] == suff[i] ? pos[suff[i]][0] > i : abs(h[i] - suff[i]) + 1;
cout << answ << 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 | input()
n, result = [int(x) for x in input().split()], []
max = -1
for i in range(len(n) - 1, -1, -1):
if n[i] <= max:
result.append(max - n[i] + 1)
else:
max = n[i]
result.append(0)
for i in range(len(result) - 1, -1, -1):
print(result[i], end = ' ')
print() | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = map(int, input().split()[::-1])
m_h = 0
res = []
for h in h:
if m_h < h:
res.append('0')
m_h = h
else:
res.append(str(m_h - h + 1))
print(" ".join(res[::-1])) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
int arr[100000] = {0}, arr2[100000] = {0};
int main() {
int i, n, max;
while (~scanf("%d", &n)) {
memset(arr, 0, sizeof(arr));
memset(arr2, 0, sizeof(arr2));
for (i = 0; i < n; i++) scanf("%d", &arr[i]);
max = arr[n - 1];
for (i = n - 1; i >= 0; i--) {
if (i == n - 1)
arr2[i] = 0;
else if (arr[i] > max) {
max = arr[i];
arr2[i] = 0;
} else {
arr2[i] = max + 1 - arr[i];
}
}
for (i = 0; i < n; i++) printf("%d%c", arr2[i], i == n - 1 ? '\n' : ' ');
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, mx = 0, mil, l, limit;
scanf("%d", &n);
int ara[n], ara2[n];
for (i = 0; i < n; i++) scanf("%d", &ara[i]);
for (l = n - 1; l >= 0; l--) {
if (ara[l] > mx) {
mx = ara[l];
ara2[l] = 0;
} else {
mil = mx + 1 - ara[l];
ara2[l] = mil;
}
}
for (i = 0; i < n; i++) printf("%d ", ara2[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 MAX = 1e5 + 100;
int a[MAX], ma, ans[MAX];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = n - 1; i >= 0; i--)
ans[i] = max(0, ma - a[i] + 1), ma = max(ma, a[i]);
for (int i = 0; i < n; i++) cout << ans[i] << " ";
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int, input().split()))
mx = h[-1]
mh = []
for i in range(n-1, -1, -1):
if mx < h[i]:
mx = h[i]
mh.append(mx)
mh = list(reversed(mh))
for i in range(n):
res = 0
if i+1 < n and mh[i] == mh[i+1]:
res = mh[i]-h[i]+1
print(res, end=' ')
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int, input().split()))
max_height = 0
diff = n * [0]
for i in range(n).__reversed__():
if h[i] > max_height:
max_height = h[i]
diff[i] = 0
else:
diff[i] = max_height - h[i] + 1
print(*diff) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
a = list(map(int, input().split()))
add = [0]*n
m = a[n-1]
for i in range(n-2, -1, -1):
if a[i] > m:
add[i] = 0
m = a[i]
else:
add[i] = m - a[i] + 1
print(*add)
| 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 | number = raw_input()
houses = reversed(raw_input().split())
highest = 0
luxury = []
for house in houses:
if int(house) > highest:
highest = int(house)
luxury.append(0)
else:
luxury.append( highest - int(house) + 1 )
for aaa in reversed(luxury):
print aaa,
| 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.*;
import java.math.*;
public class Main1
{
static class Reader
{
private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;}
public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];}
public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;}
public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();}
public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;}
public int i(){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 double d() throws IOException {return Double.parseDouble(s()) ;}
public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
}
///////////////////////////////////////////////////////////////////////////////////////////
// RRRRRRRRR AAA HHH HHH IIIIIIIIIIIII LLL //
// RR RRR AAAAA HHH HHH IIIIIIIIIII LLL //
// RR RRR AAAAAAA HHH HHH III LLL //
// RR RRR AAA AAA HHHHHHHHHHH III LLL //
// RRRRRR AAA AAA HHHHHHHHHHH III LLL //
// RR RRR AAAAAAAAAAAAA HHH HHH III LLL //
// RR RRR AAA AAA HHH HHH IIIIIIIIIII LLLLLLLLLLLL //
// RR RRR AAA AAA HHH HHH IIIIIIIIIIIII LLLLLLLLLLLL //
///////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args)throws IOException
{
PrintWriter out= new PrintWriter(System.out);
Reader sc=new Reader();
int n=sc.i();
int arr[]=new int[n];
for(int i=0;i<n;i++)arr[i]=sc.i();
int store[]=new int[n];
int max=0;
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
for(int i=n-1;i>=0;i--)
{
max=Math.max(max,arr[i]);
if(hm.get(max)==null)
hm.put(max,i);
store[i]=max;
}
for(int i=0;i<n;i++)
{
int val=store[i]-arr[i];
if(val!=0)
out.print(val+1+" ");
else
{
if(hm.get(store[i])==i)
out.print(0+" ");
else
out.print(1+" ");
}
}
out.flush();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
int maxarr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
for(int i=n-1;i>0;i--)
maxarr[i-1]=Math.max(maxarr[i], arr[i]);
for(int i=0;i<n;i++)
{
if(arr[i]>maxarr[i])
System.out.print(0+" ");
else
System.out.print((maxarr[i]-arr[i]+1)+" ");
}
}} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.awt.Point;
import java.io.*;
import java.lang.Integer;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayDeque;
import static java.lang.Math.*;
public class Main {
final boolean ONLINE_JUDGE = !new File("input.txt").exists();
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
public static void main(String[] args) {
new Main().run();
// Sworn to fight and die
}
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
class LOL implements Comparable<LOL> {
int x;
int y;
public LOL(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(LOL o) {
if (o.x != x)
return (int) (o.x - x); // ---->
return (int) (o.y - y); // <----
}
}
public void solve() throws IOException {
int n = readInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = readInt();
}
int[] suffMax = new int[n];
suffMax[n - 1] = a[n - 1];
for (int i = n - 2; i >=0; i--) suffMax[i] = max(suffMax[i + 1], a[i]);
for (int i = 0; i < n; i++) {
int ans = suffMax[i] - a[i];
if (i < n - 1 && suffMax[i] == suffMax[i + 1]) ans++;
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;
const int INF = 1000000000;
const int mod = 1000000007;
const double eps = 0.0000000001;
void solution() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> dp(n + 1, 0);
dp[n] = a[n];
for (int i = n - 1; i >= 1; i--) {
dp[i] = max(dp[i + 1], a[i]);
}
for (int i = 1; i <= n - 1; i++) {
cout << max(dp[i + 1] - a[i] + 1, 0) << " ";
}
cout << 0 << "\n";
}
void include_file() {}
int main() {
include_file();
solution();
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import itertools
n = int(input())
h = [int(x) for x in input().split()] + [0]
max_tail = list(itertools.accumulate(reversed(h), max))
max_tail.pop()
max_tail.reverse()
for i in range(n):
print(max(max_tail[i] - h[i] + 1, 0), end = ' ')
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
nums = [int(i) for i in input().split()]
floorstoadd = []
largesttoright = nums[-1]
for i in range(len(nums)-2, -1, -1):
if nums[i] <= largesttoright:
newone = str(largesttoright - nums[i] + 1)
floorstoadd.append(newone[::-1])
else:
largesttoright = nums[i]
floorstoadd.append('0')
answerstring = " ".join(floorstoadd)
print(answerstring[::-1] + " 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;
const double EPS = 1e-9;
const double PI = acos(-1.0);
template <class T>
inline T _abs(T n) {
return ((n) < 0 ? -(n) : (n));
}
template <class T>
inline T _max(T a, T b) {
return (!((a) < (b)) ? (a) : (b));
}
template <class T>
inline T _min(T a, T b) {
return (((a) < (b)) ? (a) : (b));
}
template <class T>
inline T _swap(T& a, T& b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
template <class T>
inline T gcd(T a, T b) {
return (b) == 0 ? (a) : gcd((b), ((a) % (b)));
}
template <class T>
inline T lcm(T a, T b) {
return ((a) / gcd((a), (b)) * (b));
}
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
int bitOn(int N, int pos) { return N = N | (1 << pos); }
int bitOff(int N, int pos) { return N = N & ~(1 << pos); }
bool bitCheck(int N, int pos) { return (bool)(N & (1 << pos)); }
int ara[100005];
int res[100005];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &ara[i]);
}
res[n - 1] = 0;
int mm = ara[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (ara[i] > mm) {
mm = ara[i];
res[i] = 0;
continue;
}
res[i] = mm - ara[i] + 1;
}
for (int i = 0; i < n; i++) {
printf("%d ", res[i]);
}
printf("\n");
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | def luxhouse(n, h):
r = [0] * n
m = h[-1]
n -= 2
while n >= 0:
if m >= h[n]:
r[n] = m - h[n] + 1
else:
m = h[n]
n -= 1
return r
n = int(input())
h = raw_input().split(' ')
for i in range(len(h)):
h[i] = int(h[i])
for i in luxhouse(n, h):
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 | from collections import deque
n = input()
a = map(int, raw_input().split())
maxA = 0
res = deque()
for i in range(len(a)-1, -1, -1):
diff = maxA - a[i]
if diff < 0:
res.appendleft(str(0))
else:
res.appendleft(str(diff + 1))
if a[i] > maxA:
maxA = a[i]
print ' '.join(res)
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import math
import itertools
import collections
def getdict(n):
d = {}
if type(n) is list:
for i in n:
if i in d:
d[i] += 1
else:
d[i] = 1
else:
for i in range(n):
t = ii()
if t in d:
d[t] += 1
else:
d[t] = 1
return d
def cdiv(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a*b) // math.gcd(a, b)
def wr(arr): return ' '.join(map(str, arr))
def prime(n):
if n == 2:
return True
if n % 2 == 0 or n <= 1:
return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0:
return False
return True
def revn(n):
m = 0
while n > 0:
m = m * 10 + n % 10
n = n // 10
return m
n = ii()
a = li()
ans = [0]
inf = a[-1]
for i in range(n - 1):
if a[-i - 2] <= inf:
ans.append(inf - a[-i - 2] + 1)
else:
ans.append(0)
inf = a[-i - 2]
ans.reverse()
print(wr(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;
long long int mas[100001], ans[100001];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> mas[i];
}
long long int ma = 0;
for (int i = n - 1; i >= 0; i--) {
if (mas[i] > ma) {
ans[i] = 0;
ma = mas[i];
} else {
if (mas[i] == ma) {
ans[i] = 1;
} else {
ans[i] = ma - mas[i] + 1;
}
}
}
for (int i = 0; i < n; i++) {
cout << ans[i] << ' ';
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.IOException;
import java.util.InputMismatchException;
import java.util.TreeSet;
//package PACKAGE_NAME;
/**
* Created by gaponec on 28.09.15.
*/
public class B322 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0;i<n;i++){
arr[i] = sc.nextInt();
}
int[] arr2 = new int[n];
int max = 0;
arr2[n-1] = 0;
max = arr[n-1];
for(int i = n-2;i>=0;i--){
if(arr[i]>max){
arr2[i] = 0;
max = arr[i];
} else {
arr2[i] = Math.abs(arr[i]-max)+1;
}
}
for(int i: arr2){
System.out.print(i+" ");
}
}
public static class MyScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
houses = map(int, raw_input().split())
result = [0] * n
last = len(houses) - 2
maxHeight = houses[-1]
while last >= 0:
if houses[last] <= maxHeight:
result[last] = maxHeight - houses[last] + 1
maxHeight = max(maxHeight, houses[last])
last -= 1
print ' '.join(map(str, result)) | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x, c = 0, w;
cin >> x;
int a[x], b[x];
for (int i = 0; i < x; i++) cin >> a[i];
w = a[x - 1];
for (int i = x - 1; i >= 0; i--) {
if (a[i] == w) b[i] = 1;
if (a[i] > w) {
w = a[i];
b[i] = 0;
}
if (a[i] < w) b[i] = abs(w - a[i]) + 1;
}
b[x - 1] = 0;
for (int i = 0; i < x; i++) cout << b[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
mx=a[n-1]
for i in range(n-2,-1,-1):
ans[i]=max(0,mx-a[i]+1)
if a[i]>mx:
mx=a[i]
print(*ans) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.*;
import java.util.*;
public class LuxuriousHouses {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// My approach got tle error
/*
* static int getMax(ArrayList<Integer> arrayList, int currentIndex, int
* currentValue) {
*
* int max = currentValue; for (int i = currentIndex; i < arrayList.size(); i++)
* { currentValue = arrayList.get(i); if (currentValue > max) { max =
* currentValue; } } return max; }
*/
public static void main(String args[]) throws IOException {
FastReader s = new FastReader();
/*
* My approach got tle error ArrayList<Integer> arrayList = new ArrayList<>();
* for (int i = 0; i < n; i++) arrayList.add(scan.nextInt());
*
* for (int i = 0; i < n; i++) { int currentValue = arrayList.get(i); int max =
* getMax(arrayList, i, currentValue); if (currentValue == max) {
* System.out.println(0); } else { System.out.print((max - currentValue) + 1 +
* " "); }
*
* }
*/
// after watching solution
StringBuilder sb = new StringBuilder();
int n = s.nextInt(), i;
int[] a = new int[n];
int[] m = new int[n];
for (i = 0; i < n; i++)
a[i] = s.nextInt();
m[n - 1] = a[n - 1];
for (i = n - 2; i >= 0; i--)
m[i] = Math.max(m[i + 1], a[i + 1]);
for (i = 0; i < n - 1; i++) {
if (a[i] <= m[i])
sb.append(m[i] - a[i] + 1);
else
sb.append(0);
sb.append(" ");
}
sb.append(0);
System.out.print(sb);
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | # http://codeforces.com/problemset/problem/581/B
def calculate(numbers):
numbers = map(int, numbers.split())
result = []
current = 0
for i in numbers[::-1]:
if i <= current:
result.append(current - i + 1)
else:
result.append(0)
current = i
return ' '.join([str(i) for i in result[::-1]])
def main():
raw_input()
print(calculate(raw_input()))
if __name__ == '__main__':
main()
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, h[maxn], ans[maxn];
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
int _max = h[n];
for (int i = n - 1; i >= 1; i--) {
if (h[i] > _max)
ans[i] = 0;
else
ans[i] = _max - h[i] + 1;
_max = max(_max, h[i]);
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
int n, i, mx, h[100005], t[100005];
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &h[i]);
for (i = n; i >= 1; i--)
if (h[i] > mx)
mx = h[i];
else
t[i] = mx - h[i] + 1;
for (i = 1; i <= n; i++) printf("%d ", t[i]);
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Main
{
static FastScanner in = new FastScanner(System.in);
static StringBuilder sb = new StringBuilder();
static DecimalFormat df = new DecimalFormat();
public static void main(String[] args)
{
df.setMaximumFractionDigits(20);
df.setMinimumFractionDigits(20);
df.setRoundingMode(RoundingMode.DOWN);
// String formatted = df.format(0.2);
// Never sort array of primitive type
int N = in.nextInt();
int[] h = new int[N];
for (int i = 0; i < N; i++) {
h[i] = in.nextInt();
}
int[] mx = new int[N];
mx[N - 1] = h[N - 1];
for (int i = N - 2; i >= 0; i--) {
mx[i] = Math.max(h[i], mx[i + 1]);
}
for (int i = 0; i < N - 1; i++) {
int ans = Math.max(0, 1 + mx[i + 1] - h[i]);
sb.append(ans);
sb.append(' ');
}
sb.append(0);
sb.append('\n');
System.out.print(sb.toString());
}
static BigInteger b(long n) { return BigInteger.valueOf(n);}
static BigInteger b(String s) { return new BigInteger(s); }
static BigDecimal bd(double d) { return BigDecimal.valueOf(d);}
static BigDecimal bd(String s) { return new BigDecimal(s); }
}
class FastScanner
{
BufferedReader reader;
StringTokenizer tokenizer;
FastScanner (InputStream inputStream)
{
reader = new BufferedReader(new InputStreamReader(inputStream));
}
String nextToken()
{
while (tokenizer == null || ! tokenizer.hasMoreTokens())
{
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e){}
}
return tokenizer.nextToken();
}
boolean hasNext()
{
if (tokenizer == null || ! tokenizer.hasMoreTokens())
{
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e){ return false; }
}
return true;
}
int nextInt()
{
return Integer.parseInt(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 |
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
public class test {
public static void main (String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
OutputStream out = new BufferedOutputStream ( System.out );
int n = sc.nextInt();
int[] arr = new int[n];
int[] anss = new int[n];
int[] dh = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
dh[n-1] = 0;
anss[n-1]=0;
String ans = "";
for (int j = n - 2; j >= 0; j--) {
if (arr[j] <= arr[j+1]) {
dh[j] = arr[j+1] - arr[j] + 1;
arr[j] = arr[j+1];
anss[j] = dh[j];
}
else {
dh[j] = 0;
anss[j] = dh[j];
}
}
for(int i=0; i<n; i++){
//out.write((Integer.toString(anss[i]).concat(" ")).getBytes());
ans=ans.concat(Integer.toString(anss[i]).concat(" "));
if((i+1)%100==0){
out.write(ans.getBytes());
ans="";
}
if((i+1)%1000==0) out.flush();
}
out.write(ans.getBytes());
out.flush();
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long a[100010], b[100010];
int main() {
long long n, x, i = 0;
scanf("%lld", &n);
long long j = n - 1;
while (i < n) {
scanf("%lld", &a[i]);
i++;
}
b[j] = 0;
j--;
x = 0;
long long total = x + a[n - 1];
for (i = n - 2; i >= 0; i--) {
if (a[i] <= total && b[j + 1] != 0) {
b[j] = total - a[i];
j--;
} else if (a[i] <= total && b[j + 1] == 0) {
b[j] = total - a[i] + 1;
total = total + 1;
j--;
} else {
b[j] = 0;
total = a[i];
j--;
}
}
for (i = 0; i <= n - 1; i++) {
cout << b[i] << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
public class BF
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long a[]=new long[n];
long r[]=new long[n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
long m=0;
for(int i=n-1;i>=0;i--)
{
if(a[i]<=m)
r[i]=m-a[i]+1;
m=Math.max(m,a[i]);
}
for(int i=0;i<n;i++)
System.out.print(r[i]+" ");
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int mx = -100;
vector<int> ans;
for (int i = n - 1; i > -1; --i) {
if (arr[i] < mx)
ans.emplace_back(mx - arr[i] + 1);
else if (arr[i] == mx)
ans.emplace_back(1);
else
ans.emplace_back(0);
if (arr[i] > mx) mx = arr[i];
}
for (int i = n - 1; i > -1; --i) {
cout << ans[i] << ' ';
}
cout << '\n';
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.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 neuivn
*/
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[] elem = new int[N];
for (int i = 0; i < N; ++i) {
elem[N - i - 1] = in.nextInt();
}
int[] dp = new int[N];
for (int i = 1; i < N; ++i) {
dp[i] = Math.max(dp[i], dp[i - 1]);
dp[i] = Math.max(dp[i], elem[i - 1]);
}
int[] ret = new int[N];
for (int i = 0; i < N; ++i) {
if (elem[i] > dp[i]) {
ret[i] = 0;
} else if (elem[i] == dp[i]) {
ret[i] = 1;
} else {
ret[i] = Math.abs(elem[i] - (dp[i] + 1));
}
}
for (int j = N - 1; j >= 0; --j) {
out.print(ret[j] + " ");
}
out.println();
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int[] store = new int[n];
String[] in1 = in.readLine().split(" ");
for(int i = 0; i < n; i++){
store[i] = Integer.parseInt(in1[i]);
}
int[] ans = new int[n];
int max = store[n -1];
for(int i = n- 2; i >= 0; i--){
ans[i] = Math.max(0, max + 1 - store[i]);
max = Math.max(max, store[i]);
}
for(int i = 0; i < n; i++){
System.out.print(ans[i] + " ");
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
b = [0]*n
for i in range(n-2,-1,-1):
b[i] = max(a[i+1],b[i+1])
c = [max(b[i]+1-a[i],0) for i in range(n)]
c[-1] = 0
print(' '.join([str(i) for i in c]))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int a[100005];
stack<int> s;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", a + i);
int maxx = -1;
for (int i = n - 1; i >= 0; --i) {
if (maxx >= a[i])
s.push(maxx + 1 - a[i]);
else
s.push(0);
maxx = max(maxx, a[i]);
}
while (!s.empty()) {
printf("%d ", s.top());
s.pop();
}
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 | n = int(input())
a = list(map(int, input().split()))
f = [0] * (n + 1)
r = [0] * (n + 1)
for i in range(n - 1, -1, -1):
f[i] = f[i + 1]
if f[i] >= a[i]:
r[i] = f[i] - a[i] + 1
f[i] = max(f[i], a[i])
for i in range(n):
print(r[i], end=" ") | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.*;
import java.util.*;
public class Main {
private final static InputReader ir = new InputReader(System.in);
private final static OutputWriter ow = new OutputWriter(System.out);
private final static int INF = Integer.MAX_VALUE;
private final static int NINF = Integer.MIN_VALUE;
private final static double PI = Math.PI;
public static void main(String[] args) throws IOException {
try {
task();
} finally {
boolean exc = false;
try {
ir.close();
} catch (IOException e) {
e.printStackTrace();
exc = true;
}
try {
ow.close();
} catch (IOException e) {
e.printStackTrace();
exc = true;
}
if (exc) System.exit(1);
}
}
private static void task() throws IOException {
int n = ir.nextInt();
int[] sm = new int[n];
int[] a = new int[n];
a[0] = ir.nextInt();
if (n == 1) {
ow.print(0);
return;
}
for (int i = 1; i < n; ++i) {
a[i] = ir.nextInt();
}
sm[n - 1] = 0;
sm[n - 2] = a[n - 1];
for (int i = n - 3; i >= 0; --i) {
//ow.println(a[i + 1] + " " + sm[i + 2]);
sm[i] = max(a[i + 1], sm[i + 1]);
//ow.println(" ", sm);
}
for (int i = 0; i < n; ++i) {
ow.print(max(0, sm[i] - a[i] + 1) + " ");
}
}
private static int max(int... objects) {
if (objects.length == 0)
throw new IllegalArgumentException("objects.length == 0");
int max = objects[0];
for (int i = 1; i < objects.length; ++i) {
if (max < objects[i])
max = objects[i];
}
return max;
}
private static int min(int... objects) {
if (objects.length == 0)
throw new IllegalArgumentException("objects.length == 0");
int min = objects[0];
for (int i = 1; i < objects.length; ++i) {
if (min > objects[i])
min = objects[i];
}
return min;
}
private static long pow(int x, int n) {
if (x == 0) return 0;
if (x == 1 || n == 0) return 1;
if (x == 2) return x << (n - 1);
if (n == 1) return x;
long t = pow(x, n / 2);
if (n % 2 == 0) return t * t;
else return t * t * x;
}
private static double pow(double x, int n) {
if (x == 0.0) return 0;
if (x == 1.0 || n == 0) return 1;
if (n == 1) return x;
double t = pow(x, n / 2);
if (n % 2 == 0) return t * t;
else return t * t * x;
}
private static int abs(int a) {
return (a >= 0 ? a : -a);
}
}
class SQRTDecomposition {
private int[] array;
private int len;
private int[] max;
private int[] min;
public SQRTDecomposition(int[] arr, boolean useIt) {
if (useIt)
array = arr;
else {
array = new int[arr.length];
System.arraycopy(arr, 0, array, 0, arr.length);
}
len = (int) Math.sqrt(array.length) + 1;
max = new int[len];
min = new int[len];
for (int i = 0; i < array.length; ++i) {
max[i / len] = min[i / len] = array[i];
}
for (int i = 0; i < array.length; ++i) {
max[i / len] = Math.max(max[i / len], array[i]);
min[i / len] = Math.min(min[i / len], array[i]);
}
}
public int get(int i) {
if (i <= 0 || i >= array.length)
throw new IndexOutOfBoundsException("Index: " + i);
return array[i];
}
public int length() {
return array.length;
}
public int max(int l, int r) {
int cl = l / len, cr = r / len;
int m = array[l];
if (cl == cr) {
for (int i = l; i <= r; ++i) {
if (array[i] > m) m = array[i];
}
} else {
for (int i = l, end = (cl + 1) * len - 1; i <= end; ++i)
if (array[i] > m) m = array[i];
for (int i = cl + 1; i <= cr - 1; ++i)
if (max[i] > m) m = max[i];
for (int i = cr * len; i <= r; ++i)
if (array[i] > m) m = array[i];
}
return m;
}
public int min(int l, int r) {
int cl = l / len, cr = r / len;
int m = array[l];
if (cl == cr) {
for (int i = l; i <= r; ++i) {
if (array[i] < m) m = array[i];
}
} else {
for (int i = l, end = (cl + 1) * len - 1; i <= end; ++i)
if (array[i] < m) m = array[i];
for (int i = cl + 1; i <= cr - 1; ++i)
if (min[i] < m) m = min[i];
for (int i = cr * len; i <= r; ++i)
if (array[i] < m) m = array[i];
}
return m;
}
}
class InputReader implements AutoCloseable {
private final InputStream in;
private int capacity;
private byte[] buffer;
private int len = 0;
private int cur = 0;
public InputReader(InputStream stream) {
this(stream, 100_000);
}
public InputReader(InputStream stream, int capacity) {
this.in = stream;
this.capacity = capacity;
buffer = new byte[capacity];
}
private boolean update() {
if (cur >= len) {
try {
cur = 0;
len = in.read(buffer, 0, capacity);
if (len <= 0)
return false;
} catch (IOException e) {
throw new InputMismatchException();
}
}
return true;
}
private int read() {
if (update())
return buffer[cur++];
else return -1;
}
public boolean isEmpty() {
return !update();
}
private boolean isSpace(int c) {
return c == '\n' || c == '\t' || c == '\r' || c == ' ';
}
private boolean isEscape(int c) {
return c == '\n' || c == '\t' || c == '\r' || c == -1;
}
private int readSkipSpace() {
int c;
do {
c = read();
} while (isSpace(c));
return c;
}
public int nextInt() {
int c = readSkipSpace();
if (c < 0)
throw new InputMismatchException();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c == -1) break;
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
if (res < 0)
throw new InputMismatchException();
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
public int[] getAllInts() {
List<Integer> list = new LinkedList<>();
while (true) {
try {
int t = nextInt();
list.add(t);
} catch (InputMismatchException e) {
break;
}
}
int[] a = new int[list.size()];
Iterator<Integer> it = list.iterator();
for (int i = 0; it.hasNext(); ++i)
a[i] = it.next();
return a;
}
public int[] getIntArray1D(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) {
array[i] = nextInt();
}
return array;
}
public int[][] getIntArray2D(int n, int m) {
int[][] array = new int[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
array[i][j] = nextInt();
}
return array;
}
public long nextLong() {
int c = readSkipSpace();
if (c < 0)
throw new InputMismatchException();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c == -1) break;
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
if (res < 0)
throw new InputMismatchException();
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
public long[] getAllLongs() {
List<Long> list = new LinkedList<>();
while (true) {
try {
long t = nextLong();
list.add(t);
} catch (InputMismatchException e) {
break;
}
}
long[] a = new long[list.size()];
Iterator<Long> it = list.iterator();
for (int i = 0; it.hasNext(); ++i)
a[i] = it.next();
return a;
}
public long[] getLongArray1D(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) {
array[i] = nextLong();
}
return array;
}
public long[][] getLongArray2D(int n, int m) {
long[][] array = new long[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
array[i][j] = nextLong();
}
return array;
}
public int nextChar() {
int c = readSkipSpace();
if (c < 0)
throw new InputMismatchException();
return c;
}
public String nextLine(int initialCapacity) {
StringBuilder res = new StringBuilder(initialCapacity);
int c = readSkipSpace();
do {
res.append((char) (c));
c = read();
} while (!isEscape(c));
return res.toString();
}
@Override
public void close() throws IOException {
in.close();
}
}
class OutputWriter implements Flushable, AutoCloseable {
private byte[] buf;
private final int capacity;
private int count;
private OutputStream out;
public OutputWriter(OutputStream stream) {
this(stream, 10_000);
}
public OutputWriter(OutputStream stream, int capacity) {
if (capacity < 0)
throw new IllegalArgumentException("capacity < 0");
out = stream;
this.capacity = capacity;
if (capacity != 0) {
buf = new byte[capacity];
}
}
public void write(int b) throws IOException {
if (capacity != 0) {
if (count >= capacity)
flushBuffer();
buf[count++] = (byte) b;
} else {
out.write(b);
}
}
public void write(byte[] bytes, int off, int len) throws IOException {
if (capacity != 0) {
if (len >= capacity) {
flushBuffer();
out.write(bytes, off, len);
return;
}
if (len > capacity - count)
flushBuffer();
System.arraycopy(bytes, off, buf, count, len);
count += len;
} else out.write(bytes, off, len);
}
public void write(byte[] bytes) throws IOException {
write(bytes, 0, bytes.length);
}
public void println() throws IOException {
print("\n");
}
public void print(Object object) throws IOException {
write(String.valueOf(object).getBytes());
}
public void println(Object object) throws IOException {
print(object);
println();
}
public void print(int... ints) throws IOException {
print(" ", ints);
}
public void println(int... ints) throws IOException {
print(ints);
println();
}
public void println(String separator, int... ints) throws IOException {
print(separator, ints);
println();
}
public void print(String separator, int... ints) throws IOException {
if (ints.length == 0) throw new IllegalArgumentException("ints.length == 0");
for (int i = 0; i < ints.length; ++i) {
if (i != 0) print(separator);
print(ints[i]);
}
}
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
@Override
public void flush() throws IOException {
flushBuffer();
out.flush();
}
@Override
public void close() throws IOException {
flush();
out.close();
}
}
/*
class InputReader implements AutoCloseable {
private final InputStream in;
private int capacity;
private byte[] buffer;
private int len = 0;
private int cur = 0;
public InputReader(InputStream stream) {
this(stream, 100_000);
}
public InputReader(InputStream stream, int capacity) {
this.in = stream;
this.capacity = capacity;
buffer = new byte[capacity];
}
private int read() {
if (cur >= len) {
try {
cur = 0;
len = in.read(buffer, 0, capacity);
if (len <= 0)
return -1;
} catch (IOException e) {
throw new InputMismatchException();
}
}
return buffer[cur++];
}
private boolean isSpace(int c) {
return c == ' ' || isEscape(c);
}
private boolean isEscape(int c) {
return c == '\n' || c == '\t' || c == '\r' || c == -1;
}
private int readSkipSpace() {
int c;
do {
c = read();
} while (isSpace(c));
return c;
}
int nextInt() {
int c = readSkipSpace();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
if (res < 0)
throw new InputMismatchException();
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
public long nextLong() {
int c = readSkipSpace();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
if (res < 0)
throw new InputMismatchException();
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
int nextChar() {
return readSkipSpace();
}
String nextLine(int initialCapacity) {
StringBuilder res = new StringBuilder(initialCapacity);
int c = readSkipSpace();
do {
res.append((char) (c));
c = read();
} while (!isEscape(c));
return res.toString();
}
@Override
public void close() throws IOException {
in.close();
}
}
class OutputWriter implements Flushable, AutoCloseable {
private byte[] buf;
private int capacity;
private int count;
private OutputStream out;
public OutputWriter(OutputStream stream) {
out = stream;
capacity = 10_000;
buf = new byte[capacity];
}
public OutputWriter(OutputStream stream, int capacity) {
if (capacity <= 0)
throw new IllegalArgumentException("capacity <= 0");
out = stream;
this.capacity = capacity;
buf = new byte[capacity];
}
public void write(int b) throws IOException {
if (count >= buf.length)
flushBuffer();
buf[count++] = (byte) b;
}
public void write(byte[] bytes, int off, int len) throws IOException {
if (len >= buf.length) {
flushBuffer();
out.write(bytes, off, len);
return;
}
if (len > buf.length - count)
flushBuffer();
System.arraycopy(bytes, off, buf, count, len);
count += len;
}
public void write(byte[] bytes) throws IOException {
write(bytes, 0, bytes.length);
}
public void print(String str) throws IOException {
write(str.getBytes());
}
public void println() throws IOException {
print("\n");
}
public void print(Object object) throws IOException {
print(object.toString());
}
public void println(Object object) throws IOException {
print(object.toString());
println();
}
public void print(int... ints) throws IOException {
print(" ", ints);
}
public void println(int... ints) throws IOException {
print(ints);
println();
}
public void println(String separator, int... ints) throws IOException {
print(separator, ints);
println();
}
public void print(String separator, int... ints) throws IOException {
if (ints.length == 0) throw new IllegalArgumentException("ints.length == 0");
for (int i = 0; i < ints.length; ++i) {
if (i != 0) print(separator);
print(ints[i]);
}
}
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
@Override
public void flush() throws IOException {
flushBuffer();
out.flush();
}
@Override
public void close() throws IOException {
flush();
out.close();
}
}
*/
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | from collections import deque
input()
A, max_h = deque(), 0
for h in reversed(list(map(int, input().split()))):
if h > max_h:
max_h = h
A.appendleft(0)
else:
A.appendleft(max_h - h + 1)
print(*A) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int ara[100];
int main() {
int n;
cin >> n;
int ara[n];
int i;
for (i = 0; i < n; i++) {
cin >> ara[i];
}
int max = -1;
for (i = n - 1; i >= 0; i--) {
if (ara[i] > max) {
max = ara[i];
ara[i] = 0;
} else {
ara[i] = max - ara[i] + 1;
}
}
for (i = 0; i < n; i++) {
cout << ara[i] << ' ';
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
h=list(map(int,input().split()))
h.reverse()
s=[]
m=0
for i in h:
if i>m:
m=i
a=0
elif m >=i:
a=(m-i)+1
s+=[str(a)]
s.reverse()
print(' '.join(s))
| 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)%500==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 | raw_input()
h = map(int, raw_input().split(' '))
maxi = []
temp = h[len(h) - 1]
c_temp = 0
for i in xrange(len(h) - 1, -1, -1):
if temp != max(temp, h[i]):
c_temp = 0
temp = max(temp, h[i])
if temp == h[i]:
c_temp += 1
maxi.append((temp, c_temp))
# print temp, c_temp
# print maxi
for (i, j) in zip(xrange(len(maxi) - 1, -1, -1), xrange(len(h))):
if h[j] < maxi[i][0]:
print maxi[i][0] - h[j] + 1,
elif maxi[i][1] > 1:
print 1,
else:
print 0, | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
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; }
long long n, A[100005], tmp, dem, Ma[100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
tmp = 0;
for (int i = (1); i <= (n); ++i) {
cin >> A[i];
}
memset(Ma, 0, sizeof(Ma));
for (int i = (n); i >= (1); --i) Ma[i] = max(Ma[i + 1], A[i]);
for (int i = (1); i <= (n); ++i) {
if (A[i] > Ma[i + 1]) {
cout << 0 << " ";
continue;
}
if (A[i] == Ma[i + 1]) {
cout << 1 << " ";
continue;
}
if (A[i] < Ma[i + 1]) cout << Ma[i + 1] - A[i] + 1 << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Mani Kumar Adari ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
List<Integer> heights = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
heights.add(in.readInt());
}
List<Integer> answers = new ArrayList<>(n);
int maximumFloors = heights.get(heights.size() - 1);
answers.add(0);
for (int i = heights.size() - 2; i >= 0; i--) {
if (maximumFloors == heights.get(i))
answers.add(1);
else
answers.add(Math.max((maximumFloors - heights.get(i)) + 1, 0));
maximumFloors = Math.max(maximumFloors, heights.get(i));
}
Collections.reverse(answers);
for (Integer deltaFloors : answers)
out.print(deltaFloors + " ");
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void close() {
writer.close();
}
}
static class 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 readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
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 Ma {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] ArrIn= new int[N];
int[] ArrOut= new int[N];
int i=0;
for (int k=0; k<N; k++) {
ArrIn[k] = sc.nextInt();
}
int maxtmp=0;
for (int k=N-1; k>=0; k--){
if (ArrIn[k]>maxtmp){ // οΏ½οΏ½οΏ½ οΏ½οΏ½οΏ½οΏ½οΏ½?
maxtmp=ArrIn[k];
ArrIn[k]=0;
ArrOut[k]=0;
}
else {
ArrOut[k]=maxtmp-ArrIn[k]+1;
}
}
for (int k=0; k<N; k++){
System.out.print(ArrOut[k]+ " ");
}
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.*;
import java.util.StringTokenizer;
/**
* Created by Alvin on 5/20/2016.
*/
public class Codeforces_round_322_div_2_LuxuriousHouses {
public static void main(String[] args) {
FScanner input = new FScanner();
out = new PrintWriter(new BufferedOutputStream(System.out), true);
int[] houses = new int[input.nextInt()];
int[] biggest = new int[houses.length + 1];
for (int i = 0; i < houses.length; i++) {
houses[i] = input.nextInt();
}
biggest[houses.length - 1] = -1;
int previousBest = houses[houses.length - 1];
for (int i = houses.length - 2; i >= 0; i--) {
biggest[i] = previousBest;
previousBest = Math.max(previousBest, houses[i]);
}
for (int i = 0; i < houses.length; i++) {
if(houses[i] <= biggest[i]) {
out.print(biggest[i] - houses[i] + 1 + " ");
} else {
out.print(0 + " ");
}
}
out.close();
}
public static PrintWriter out;
public static class FScanner {
BufferedReader br;
StringTokenizer st;
public FScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
private String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int max;
max = a[n - 1];
int h[n];
h[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
if (max < a[i]) {
h[i] = 0;
} else {
h[i] = max - a[i] + 1;
}
if (max < a[i]) {
max = a[i];
}
}
for (int i = 0; i < n; i++) {
cout << h[i] << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class domik {
public static void main(String[] args) throws IOException {
Stack stek=new Stack();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
String[] input = reader.readLine().split(" ");
int max = 0;
int len=input.length;
for (int k = len - 1; k >= 0; k--) {
if (k == len - 1) {
max = Integer.parseInt(input[k]);
stek.push("0");
} else {
if (Integer.parseInt(input[k]) > max) {
max = Integer.parseInt(input[k]);
stek.push("0 ");
} else {
stek.push((max + 1 - Integer.parseInt(input[k]))+ " ");
}
}
}
for(int i=0;i<len;i++){
System.out.println(stek.pop());
}
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> nums;
for (int i = 0; i < n; i++) {
int nxt;
cin >> nxt;
nums.push_back(nxt);
}
int suffmax[100000];
suffmax[n - 1] = -999999;
for (int i = n - 2; i >= 0; i--) {
suffmax[i] = max(suffmax[i + 1], nums[i + 1]);
}
for (int i = 0; i < n; i++) {
cout << max(0, suffmax[i] - nums[i] + 1) << " ";
}
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.util.StringTokenizer;
public class LuxuriousHouses {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] houses = new int[n];
int[] maxHeight = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<n; i++) {
houses[i] = Integer.parseInt(st.nextToken());
}
for(int m=n-1; m>=0; m--) {
if(m == n-1) maxHeight[m] = houses[m];
else {
if(houses[m] > maxHeight[m+1]) {
maxHeight[m] = houses[m];
} else {
maxHeight[m] = maxHeight[m+1];
}
}
}
for(int j=0; j<n-1; j++) {
System.out.print((maxHeight[j+1]>=houses[j]?maxHeight[j+1]-houses[j]+1:0) + " ");
}
System.out.print(0);
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int house;
cin >> house;
int *floor = (int *)malloc(sizeof(int) * house);
int *added = (int *)malloc(sizeof(int) * house);
fill(added, added + house, 0);
for (int i = 0; i < house; i++) cin >> floor[i];
int max = floor[house - 1];
for (int i = house - 2; i >= 0; i--)
if (floor[i] <= max)
added[i] = max - floor[i] + 1;
else
max = floor[i];
for (int i = 0; i < house; i++) cout << added[i] << ' ';
cout << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
arr=list(map(int,input().split()))
temp=[None]*n
m=0
last=0
for i in range(n):
if last>m:
m=last
last=arr[n-1-i]
temp[n-1-i]=m
for i in range(len(arr)):
if arr[i]<temp[i]+1:
print(temp[i]+1-arr[i],end=' ')
else:
print(0,end=' ') | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.*;
import java.lang.reflect.Array;
import java.net.Inet4Address;
import java.nio.ByteBuffer;
import java.util.*;
public class test {
int max = 0;
long t[];
boolean visited[];
ArrayList<Integer> queue = new ArrayList<>();
public static void main(String args[]) throws IOException {
test obj = new test();
obj.start();
}
void start() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintStream out = System.out;
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
// int k=sc.nextInt();
int a[]=intArray(n,sc);
// Arrays.sort(a);
int max=a[n-1];
a[n-1]=0;
for(int i=n-2;i>=0;i--) {
int temp=a[i];
if (a[i]<=max)
a[i] = max-a[i] + 1;
else
a[i]=0;
if (max < temp)
max =temp;
}
println(a);
}
//<---------------------------------------------------------------------------------------------------------------->//
// to create and initialize Integer array
private int[] intArray(int a, Scanner sc) {
int temp[]=new int[a];
for(int i=0;i<a;i++) {
temp[i]=sc.nextInt();
}
return temp;
}
// to create and initialize Long Array
private long[] longArray(int a, Scanner sc) {
long temp[]=new long[a];
for(int i=0;i<a;i++) {
temp[i]=sc.nextLong();
}
return temp;
}
// displaying Map contents key--value
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
// Return arrayList of minimum factor of number
// 24= 2*2*2*3;
private ArrayList<Integer> minFactor(int a) {
ArrayList<Integer> obj=new ArrayList<>();
for(int i=2;i<=a;i++)
{
while(a!=0&&a%i==0)
{
obj.add(i);
a/=i;
}
}
return obj;
}
// check whether the number is prime or not
public static boolean isPrimeNumber(int i) {
int factors = 0;
int j = 1;
while(j <= i)
{
if(i % j == 0)
{
factors++;
}
j++;
}
return (factors == 2);
}
// creating and initializing arrayList
private void initArrayList(ArrayList<Integer>[] tempa) {
for(int i=0;i<tempa.length;i++)
tempa[i]=new ArrayList<>();
}
// return factorial contents as ArrayList
private ArrayList<Integer> factdigit(int i)
{
ArrayList<Integer> temp=new ArrayList<>();
for(int j=i;j>1;j--)
{
temp.add(j);
}
return temp;
}
private void println(int a[])
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();long max=0;
int[] a = new int[n];
long[] b = new long[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=n-1;i>=0;i--)
{
if(a[i]>max)b[i]=0;
else b[i]= max - a[i] +1;
if(a[i]>max)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 | input()
nums = [int(t) for t in input().split()]
max_ = nums[-1] - 1
for i in range(len(nums) - 1, -1, -1):
newVal = nums[i] if nums[i] > max_ else max_ + 1
max_ = max(max_, nums[i])
nums[i] = newVal - nums[i]
# print(nums)
for i, k in enumerate(nums):
print(k, end=' ' if i != (len(nums) - 1) else '\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 | n = input()
buildings = input().split(' ')
m = 0
result = []
for building in buildings[::-1]:
b = int(building)
x = m - b
if x < 0:
result.append('0')
m = b
else:
result.append(str(x + 1))
print(' '.join(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 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0; i<n; i++)
a[i] = sc.nextInt();
int max = a[n-1];
int[] ans = new int[n];
ans[n-1] = 0;
for(int i=n-2; i>=0; i--){
if(a[i]<=max){
ans[i] = max-a[i]+1;
}else{
max = a[i];
}
}
for(int i=0; i<n; i++)
System.out.print(ans[i] + " ");
System.out.println();
sc.close();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long a[200000], b[200000], n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) b[i] = max(a[i], b[i + 1]);
for (int i = 1; i <= n; i++)
if (b[i + 1] >= a[i])
cout << b[i + 1] - a[i] + 1 << " ";
else
cout << "0 ";
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=input()
a=map(int,raw_input().split())
h=0
for i in range(n-1,-1,-1):
x=max(0,h+1-a[i])
h=max(h,a[i])
a[i]=x
for x in a:
print x,
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n;
int h[100001];
int r[100001];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", h + i);
int maxis = 0;
for (int i = n - 1; i >= 0; i--) {
int k = maxis - h[i] + 1;
if (k >= 0)
r[i] = k;
else
r[i] = 0;
maxis = max(maxis, h[i]);
}
for (int i = 0; i < n; i++) {
printf("%d ", r[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;
bool flag;
void fast() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
string con(long long od) {
stringstream st;
st << od;
string pr;
st >> pr;
return pr;
}
int main() {
fast();
int n, mn = (INT_MIN);
cin >> n;
vector<int> v(n, 0);
vector<int> a(n, 0);
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = n - 1; i >= 0; i--) {
if (mn < v[i]) {
mn = v[i];
continue;
}
a[i] = mn - v[i] + 1;
}
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 | n = int(input())
a = list(map(int,input().split()))
L = [0] * n
mx = 0
for i in range(n-1, -1, -1):
if a[i] > mx: L[i] = 0
else: L[i] = mx + 1 - a[i]
mx = max(mx, a[i])
print(*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 | n = int(input())
h = list(map(int, input().split()))
h.reverse()
ans = [0]
m = h[0]
for i in range(1, n):
if h[i] > m:
ans.append(0)
m = h[i]
else:
ans.append(m-h[i] + 1)
ans.reverse()
print(*ans, sep=' ') | 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(raw_input())
A = map(int , raw_input().split())
l = len(A)
B = [0]
max_to_right = A[-1]
for i in range(2,len(A)+1):
max_to_right = max(max_to_right, A[-i+1])
B.append(max(0, max_to_right-A[-i]+1))
B.reverse()
for i in B:
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())
mat = list(map(int,input().split()))[::-1]
rmnd = 0
sat = []
for i in mat:
if rmnd >= i:
sat.append(rmnd - i + 1)
else:
sat.append(0)
rmnd = i
print(*sat[::-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 | _ = input()
a = [int(x) for x in input().split()]
mx = 0
ans = []
for x in reversed(a):
if x > mx:
ans.append(0)
mx = x
else:
ans.append(mx - x + 1)
ans = reversed(ans)
print(' '.join(str(x) for x in ans)) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
def main():
raw_input()
L = [ int(i) for i in raw_input().split(" ") ]
res = []
maxi = 0
for i in range(len(L)-1,-1,-1):
res.append( str( max(0,maxi+1-L[i])) )
maxi = max(L[i],maxi)
res.reverse()
print " ".join(res)
main() | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
h = map(int, raw_input().split()) + [0]
goal = [0 for i in xrange(n+1)]
for i in xrange(n-1, -1, -1):
goal[i] = max(goal[i+1], h[i+1]+1)
print " ".join([str(max(0, goal[i] - h[i])) for i in xrange(n)])
| 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 | mod = 10 ** 9 + 7
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n=ii()
l=il()
l1=[0]*n
mx=0
for i in range(n-1, -1, -1):
if l[i]<=mx:
l1[i]=mx-l[i]+1
mx=max(l[i],mx)
print(*l1) | 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>
int main(int argc, char const *argv[]) {
int n, i, max;
scanf("%d", &n);
int a[n];
int r[n];
for (i = 0; i < n; i++) scanf("%d", &a[i]);
r[n - 1] = 0;
max = a[n - 1];
for (i = n - 2; i >= 0; i--) {
if (a[i] <= max) {
r[i] = max - a[i] + 1;
} else {
r[i] = 0;
max = a[i];
}
}
for (i = 0; i < n; i++) printf("%d ", r[i]);
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | """
Codeforces Round #322 (Div. 2)
Problem 581 B. Luxurious Houses
@author yamaton
@date 2015-10-06
"""
import itertools as it
import functools
import operator
import collections
import math
import sys
def solve(xs):
max_height = 0
result = []
for x in reversed(xs):
if x > max_height:
result.append(0)
max_height = x
else:
additional = max_height - x + 1
result.append(additional)
return reversed(result)
def main():
n = int(input())
xs = [int(i) for i in input().strip().split()]
assert len(xs) == n
result = solve(xs)
print(' '.join(map(str, result)))
if __name__ == '__main__':
main()
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
int arr[123456];
int maxx[123456];
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
maxx[n] = 0;
for (int i = n - 1; i >= 0; i--) {
maxx[i] = max(maxx[i + 1], arr[i]);
}
for (int i = 0; i < n; i++) {
printf("%d ", maxx[i] - arr[i] + 1 - (arr[i] > maxx[i + 1]));
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<long long int> vec(n), ans(n);
for (int i = 0; n > i; i++) {
cin >> vec[i];
}
long long int maxi = vec[n - 1], sifir = 0;
ans[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
ans[i] = max(sifir, maxi - vec[i] + 1);
maxi = max(maxi, vec[i]);
}
for (int i = 0; n > i; i++) {
cout << ans[i] << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, b, mx = 0;
cin >> n;
int a[n + 1];
vector<int> t;
for (int k = 1; k <= n; k++) cin >> a[k];
for (int k = n; k >= 1; k--) {
t.push_back(max(0, mx - a[k] + 1));
mx = max(a[k], mx);
}
for (int k = t.size() - 1; k >= 0; k--) {
cout << t[k] << " ";
}
}
| 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, maxFloor = 0;
cin >> n;
int h[n];
int ans[n];
for (int i = 0; i < n; i++) {
cin >> h[i];
}
maxFloor = h[n - 1];
ans[n - 1] = 0;
for (int i = n - 2; i >= 0; i--) {
if (maxFloor < h[i]) {
maxFloor = h[i];
ans[i] = 0;
} else {
ans[i] = maxFloor - h[i] + 1;
}
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | //package CodeForces.Round322;
import java.util.Scanner;
/**
* Created by Ilya Sergeev on 28.09.2015.
*/
public class Belitn {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] mass = new int[n];
for (int i = 0; i < n; i++) {
mass[i] = sc.nextInt();
}
int[] boost = new int[n];
int max = 0;
for (int i = n-1; i >= 0; i--) {
if(max < mass[i]){
boost[i] = 0;
max = mass[i];
}
else if(max > mass[i]){
boost[i] = max - mass[i] + 1;
}
else boost[i] = 1;
}
for (int i = 0; i < boost.length; i++) {
System.out.print(boost[i]+" ");
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
long long a[200000], b[200000];
int main() {
ios_base::sync_with_stdio(0);
long long n, mx = 0, i, kol = 1;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = n; i >= 1; i--) {
b[kol] = max(0ll, mx - a[i] + 1);
mx = max(mx, a[i]);
kol++;
}
for (i = n; i >= 1; 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 t, a[100005], c[100005];
cin >> t;
for (int x = 0; x < t; x++) cin >> a[x];
c[t - 1] = 0;
int flag = a[t - 1];
for (int x = t - 2; x >= 0; x--) {
a[x] > flag ? c[x] = 0 : c[x] = flag - a[x] + 1;
flag = max(flag, a[x]);
}
for (int x = 0; x < t; x++) cout << c[x] << " ";
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.StringTokenizer;
public class Luxhouses
{
public static void main(String arg[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] stra;
StringBuilder sb=new StringBuilder();
int[] arr;
StringTokenizer st;
String str;
int t;
int g, i, j, n,max;
n = Integer.parseInt(br.readLine());
stra=br.readLine().split(" ");
arr=new int[n];
for(i=0;i<n;i++)
arr[i]=Integer.parseInt(stra[i]);
max=0;
for(i=n-1;i>=0;i--)
{
if(arr[i]>max)
{
stra[i]="0";
max=arr[i];
}
else
stra[i]=Integer.toString(max-arr[i]+1);
}
for(i=0;i<n;i++)
sb.append(stra[i]+" ");
//sb.append("\n");
System.out.print(sb.toString());
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
void maxe(long long int *array, long long int begin, long long int end,
long long int *max, long long int *index) {
long long int i, maxi = -9;
for (i = begin; i < end; i++) {
if (array[i] >= maxi) {
maxi = array[i];
*index = i;
}
}
*max = maxi;
}
int main() {
long long int x, y, z, i, t, j, arr[100002], max = 0, index;
cin >> t;
for (i = 0; i < t; i++) cin >> arr[i];
max = arr[t - 1];
arr[t - 1] = 0;
index = t - 1;
for (i = t - 2; i > -1; i--) {
if (max < arr[i]) {
max = arr[i];
index = i;
}
if (arr[i] == max) {
if (index == i)
arr[i] = 0;
else
arr[i] = 1;
} else
arr[i] = max - arr[i] + 1;
}
for (i = 0; i < t; 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 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, _max;
vector<long long> h, lux;
cin >> n;
h.resize(n);
lux.resize(n);
for (long long i = 0; i < n; i++) cin >> h[i];
lux[n - 1] = 0;
_max = n - 1;
for (long long i = n - 2; i >= 0; i--) {
if (h[i] <= h[_max])
lux[i] = h[_max] - h[i] + 1;
else
lux[i] = 0;
if (h[i] > h[_max]) _max = i;
};
for (long long i = 0; i < n - 1; i++) cout << lux[i] << " ";
cout << lux[n - 1] << 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;
const double pi = 2 * acos(0.0);
const int OO = 0x3f3f3f3f;
const int N = 1e5 + 5;
int arr[N], n;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
int mx = -1;
vector<int> v;
for (int i = n - 1; i >= 0; i--) {
v.push_back(max(mx - arr[i] + 1, 0));
mx = max(mx, arr[i]);
}
for (int i = v.size() - 1; i >= 0; i--) cout << v[i] << ' ';
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
l=list(map(int,input().split()))
maxh=0
a=[]
for i in range(n-1,-1,-1):
a.append((max(0,maxh+1-l[i])))
if l[i]>maxh:
maxh=l[i]
for i in range(n-1,-1,-1):
print(a[i],end=" ") | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
n= int(input())
a= [int(i) for i in input().split()]
pref= [0]*n
from collections import defaultdict
d = defaultdict(int)
pref[-1] = a[-1]
d[pref[-1]]=n-1
for i in range(n-2,-1,-1):
pref[i] = max(pref[i+1],a[i])
if d[pref[i]]==0:
d[pref[i]]=i
out = [0]*n
for i in range(n):
if a[i]==pref[i] and i==d[a[i]]:
continue
elif a[i]==pref[i]:
out[i] = 1
else:
out[i] = pref[i]-a[i]+1
print(*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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, arr[1 << 18];
cin >> n;
for (i = 0; i < n; i++) cin >> arr[i];
vector<int> v1;
int max1 = -1;
for (i = n - 1; i >= 0; i--) {
if (max1 == -1 || max1 < arr[i])
v1.push_back(0);
else
v1.push_back(max1 - arr[i] + 1);
max1 = max(max1, arr[i]);
}
for (i = n - 1; i >= 0; i--) cout << v1[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, c = 0;
int zero = 0;
vector<int> v;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = n - 1; i >= 0; i--) {
if (arr[i] > c) {
v.push_back(zero);
c = arr[i];
} else
v.push_back(c - arr[i] + 1);
}
reverse(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, i, j, ma;
cin >> n;
int arr[n], b[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
}
ma = arr[n - 1];
b[n - 1] = 0;
for (i = n - 2; i >= 0; i--) {
if (arr[i] <= ma) {
b[i] = (ma - arr[i]) + 1;
} else {
b[i] = 0;
ma = arr[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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, i, j;
int max = INT_MIN;
cin >> n;
int a[n];
int b[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (j = n - 1; j >= 0; j--) {
if (a[j] <= max) {
b[j] = max + 1 - a[j];
} else {
b[j] = 0;
}
if (max < a[j]) {
max = a[j];
}
}
for (i = 0; i < n; i++) {
cout << b[i] << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
const int MX = 100005;
using namespace std;
int main() {
int n;
cin >> n;
long long int ar[n + 5];
long long int maxi[n + 5];
for (int i = 0; i <= n - 1; i++) cin >> ar[i];
maxi[n - 1] = ar[n - 1];
for (int i = n - 2; i >= 0; i--) {
maxi[i] = ((ar[i]) > (maxi[i + 1]) ? (ar[i]) : (maxi[i + 1]));
}
long long int ans[n + 5];
map<int, int> m;
for (int i = n - 1; i >= 0; i--) {
if (maxi[i] == ar[i]) {
if (m[ar[i]] == 0) {
ans[i] = 0;
m[ar[i]]++;
} else {
ans[i] = 1;
}
} else {
ans[i] = maxi[i] - ar[i] + 1;
}
}
for (int i = 0; i <= n - 1; i++) cout << ans[i] << " ";
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
public class LuxuriousHouses {
static int K;
static int[] intarray;
static Scanner scan;
static int[] results;
static int max;
public static void main(String[] args) {
scan = new Scanner(System.in);
K = scan.nextInt();
intarray = new int[K];
results = new int[K];
results[K - 1] = 0;
for (int i = 0; i < K; i++)
intarray[i] = scan.nextInt();
max = intarray[K - 1];
for (int i = K - 2; i >= 0; i--) {
if (intarray[i] > max) {
max = intarray[i];
results[i] = 0;
continue;
}
results[i] = max + 1 - intarray[i];
}
for (int numb : results)
System.out.print(numb + " ");
}
}
| 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.lang.reflect.Array;
import java.util.*;
public class CodeForces {
public static void main(String[] args)throws IOException {
//MyScanner sc = new MyScanner(new FileReader("input.txt"));
//PrintWriter out = new PrintWriter("output.txt");
PrintWriter out = new PrintWriter(System.out);
MyScanner sc = new MyScanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; ++i)
a[i] = sc.nextInt();
int[] maxH = new int[n];
for(int i = n - 1; i > 0; --i)
maxH[i - 1] = Math.max(a[i], maxH[i]);
for(int i = 0; i < n - 1; ++i)
if(a[i] > maxH[i])
out.format("%d ", 0);
else
out.format("%d ", maxH[i] - a[i] + 1);
out.println(0);
out.flush();
out.close();
}
static long gcd(long a, long b) { return b==0 ? a : gcd(b, a % b); }
static long lcm(long a, long b) { return (a / gcd(a, b)) * b; }
static class MyScanner
{
StringTokenizer st;
BufferedReader br;
public MyScanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public MyScanner(FileReader s) throws FileNotFoundException { br = new BufferedReader(s);}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException { return Double.parseDouble(next()); }
public boolean ready() throws IOException {return br.ready();}
}
} | JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.