output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long arr[n + 1];
memset(arr, 0, sizeof(arr));
int pi = n, a;
for (auto i = 1; i <= n; i += 1) {
cin >> a;
arr[a] = 1;
if (a == pi) {
while (arr[a] == 1) {
cout << a << " ";
a--;
pi = a;
}
cout << "\n";
} else
cout << "\n";
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long arr[n + 1];
memset(arr, 0, sizeof(arr));
int pi = n, a;
for (auto i = 1; i <= n; i += 1) {
cin >> a;
arr[a] = 1;
if (a == pi) {
while (arr[a] == 1) {
cout << a << " ";
a--;
pi = a;
}
cout << "\n";
} else
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 9;
set<int> ist;
map<string, int> msi;
map<string, string> mss;
map<int, string> mis;
map<int, int> mii;
pair<int, int> pii;
vector<int> v;
vector<pair<int, int> > vv;
int cc[] = {1, -1, 0, 0};
int rr[] = {0, 0, 1, -1};
int dp[100009] = {};
void BACKSLASH() {
int t;
int n;
cin >> n;
int m = n, x;
for (int i = 0; i < n; i++) {
cin >> x;
dp[x] = 1;
while (dp[m] == 1) {
cout << m << " ";
m--;
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
BACKSLASH();
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 9;
set<int> ist;
map<string, int> msi;
map<string, string> mss;
map<int, string> mis;
map<int, int> mii;
pair<int, int> pii;
vector<int> v;
vector<pair<int, int> > vv;
int cc[] = {1, -1, 0, 0};
int rr[] = {0, 0, 1, -1};
int dp[100009] = {};
void BACKSLASH() {
int t;
int n;
cin >> n;
int m = n, x;
for (int i = 0; i < n; i++) {
cin >> x;
dp[x] = 1;
while (dp[m] == 1) {
cout << m << " ";
m--;
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
BACKSLASH();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
long num, curr = n;
vector<bool> s(n, false);
for (int i = 0; i < n; i++) {
cin >> num;
if (num == curr) {
cout << num << " ";
curr--;
while (1) {
if (s[curr]) {
cout << curr << " ";
s[curr] = false;
curr--;
} else
break;
}
cout << endl;
} else {
cout << endl;
s[num] = true;
}
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
long num, curr = n;
vector<bool> s(n, false);
for (int i = 0; i < n; i++) {
cin >> num;
if (num == curr) {
cout << num << " ";
curr--;
while (1) {
if (s[curr]) {
cout << curr << " ";
s[curr] = false;
curr--;
} else
break;
}
cout << endl;
} else {
cout << endl;
s[num] = true;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a[n + 1];
memset(a, 0, sizeof(a));
while (n) {
int x;
cin >> x;
a[x] = 1;
while (a[n]) {
cout << n-- << " ";
}
cout << endl;
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a[n + 1];
memset(a, 0, sizeof(a));
while (n) {
int x;
cin >> x;
a[x] = 1;
while (a[n]) {
cout << n-- << " ";
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> pq;
int main() {
int n, a;
scanf("%d", &n);
int b = n;
for (int i = 0; i < n; i++) {
scanf("%d", &a);
pq.push(a);
while (!pq.empty() && pq.top() == b) {
printf("%d ", pq.top());
pq.pop();
b--;
}
printf("\n");
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> pq;
int main() {
int n, a;
scanf("%d", &n);
int b = n;
for (int i = 0; i < n; i++) {
scanf("%d", &a);
pq.push(a);
while (!pq.empty() && pq.top() == b) {
printf("%d ", pq.top());
pq.pop();
b--;
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j;
cin >> n;
int a[n + 1], u[n + 1], place = n, pos = 1;
memset(u, 0, sizeof(u));
for (i = 1; i <= n; i++) {
cin >> a[i];
u[a[i]] = i;
}
for (i = 1; i <= n; i++) {
while (u[place] <= i && place > 0) {
cout << place << " ";
place--;
}
cout << endl;
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j;
cin >> n;
int a[n + 1], u[n + 1], place = n, pos = 1;
memset(u, 0, sizeof(u));
for (i = 1; i <= n; i++) {
cin >> a[i];
u[a[i]] = i;
}
for (i = 1; i <= n; i++) {
while (u[place] <= i && place > 0) {
cout << place << " ";
place--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n, m;
scanf("%d", &n);
int z = n;
for (int i = 0; i < n; i++) {
scanf("%d", &m);
a[m]++;
while (z > 0 && a[z]) printf("%d ", z--);
printf("\n");
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n, m;
scanf("%d", &n);
int z = n;
for (int i = 0; i < n; i++) {
scanf("%d", &m);
a[m]++;
while (z > 0 && a[z]) printf("%d ", z--);
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
(ios_base::sync_with_stdio(false), cin.tie(NULL));
int n;
cin >> n;
vector<int> c(n);
for (int i = 0; i < (int)(n); ++i) cin >> c[i];
vector<bool> fallen(n + 1);
int cur = n;
for (int i = 0; i < (int)(n); ++i) {
fallen[c[i]] = true;
if (c[i] == cur) {
while (fallen[cur]) cout << cur-- << " ";
cout << endl;
} else
cout << endl;
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
(ios_base::sync_with_stdio(false), cin.tie(NULL));
int n;
cin >> n;
vector<int> c(n);
for (int i = 0; i < (int)(n); ++i) cin >> c[i];
vector<bool> fallen(n + 1);
int cur = n;
for (int i = 0; i < (int)(n); ++i) {
fallen[c[i]] = true;
if (c[i] == cur) {
while (fallen[cur]) cout << cur-- << " ";
cout << endl;
} else
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ar[n + 1];
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
priority_queue<int> v;
int prev = n;
int c = 0;
for (int i = 1; i <= n; i++) {
v.push(ar[i]);
while (v.top() == prev) {
cout << v.top() << " ";
prev--;
v.pop();
}
cout << endl;
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ar[n + 1];
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
priority_queue<int> v;
int prev = n;
int c = 0;
for (int i = 1; i <= n; i++) {
v.push(ar[i]);
while (v.top() == prev) {
cout << v.top() << " ";
prev--;
v.pop();
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
int v[100001];
int main() {
int i, n, t;
std::cin >> n;
for (i = 1; i <= n; ++i) {
std::cin >> t;
v[t] = 1;
if (v[t + 1] == 2 || t == n) {
std::cout << t;
v[t] = 2;
for (--t; v[t]; --t) {
std::cout << " " << t;
v[t] = 2;
}
}
std::cout << "\n";
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
int v[100001];
int main() {
int i, n, t;
std::cin >> n;
for (i = 1; i <= n; ++i) {
std::cin >> t;
v[t] = 1;
if (v[t + 1] == 2 || t == n) {
std::cout << t;
v[t] = 2;
for (--t; v[t]; --t) {
std::cout << " " << t;
v[t] = 2;
}
}
std::cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int a[N] = {0};
int main() {
int m, n;
cin >> n;
m = n;
for (int i = 0; i < n; i++) {
int p;
cin >> p;
a[p] = 1;
while (a[m] == 1) {
cout << m << " ";
m--;
}
cout << endl;
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int a[N] = {0};
int main() {
int m, n;
cin >> n;
m = n;
for (int i = 0; i < n; i++) {
int p;
cin >> p;
a[p] = 1;
while (a[m] == 1) {
cout << m << " ";
m--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int z[n + 1];
memset(z, 0, sizeof z);
int f = n;
vector<int> v;
for (int i = 0; i < n; i++) {
int y;
cin >> y;
v.push_back(y);
}
for (int i = 0; i < n; i++) {
if (f == v[i]) {
z[v[i]] = 2;
f--;
cout << v[i];
while (z[f] == 1) {
cout << " " << f;
z[f] = 2;
f--;
}
cout << endl;
} else {
cout << endl;
z[v[i]] = 1;
}
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int z[n + 1];
memset(z, 0, sizeof z);
int f = n;
vector<int> v;
for (int i = 0; i < n; i++) {
int y;
cin >> y;
v.push_back(y);
}
for (int i = 0; i < n; i++) {
if (f == v[i]) {
z[v[i]] = 2;
f--;
cout << v[i];
while (z[f] == 1) {
cout << " " << f;
z[f] = 2;
f--;
}
cout << endl;
} else {
cout << endl;
z[v[i]] = 1;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> pq;
int main() {
int arr[100005], i, j, n, top;
cin >> n;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
int tmp = n;
for (i = 0; i < tmp; i++) {
pq.push(arr[i]);
top = pq.top();
while (n == top) {
cout << top << ' ';
pq.pop();
n--;
top = pq.top();
}
cout << endl;
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> pq;
int main() {
int arr[100005], i, j, n, top;
cin >> n;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
int tmp = n;
for (i = 0; i < tmp; i++) {
pq.push(arr[i]);
top = pq.top();
while (n == top) {
cout << top << ' ';
pq.pop();
n--;
top = pq.top();
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
int store_ar[500000];
int main() {
int stack_ar[500000], n, i, pivot;
scanf("%d", &n);
pivot = n;
for (i = 0; i < n; ++i) {
scanf("%d", &stack_ar[i]);
}
for (i = 0; i < n; ++i) {
store_ar[stack_ar[i]] = stack_ar[i];
if (stack_ar[i] == pivot) {
again:
printf("%d ", pivot);
--pivot;
if (store_ar[pivot] != 0)
goto again;
else
printf("\n");
} else {
printf("\n");
}
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
int store_ar[500000];
int main() {
int stack_ar[500000], n, i, pivot;
scanf("%d", &n);
pivot = n;
for (i = 0; i < n; ++i) {
scanf("%d", &stack_ar[i]);
}
for (i = 0; i < n; ++i) {
store_ar[stack_ar[i]] = stack_ar[i];
if (stack_ar[i] == pivot) {
again:
printf("%d ", pivot);
--pivot;
if (store_ar[pivot] != 0)
goto again;
else
printf("\n");
} else {
printf("\n");
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int size;
int curmax;
cin >> size;
curmax = size;
vector<int> arr;
arr.reserve(size);
vector<int> temp_arr(size, 0);
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
for (int i = 0; i < size; i++) {
temp_arr[arr[i] - 1] = arr[i];
string print_str = "";
while (temp_arr[curmax - 1] != 0) {
string cur_string = to_string(curmax);
print_str.append(cur_string);
print_str.append(" ");
curmax--;
if (curmax == 0) {
cout << print_str;
return 0;
}
}
print_str.append("\n");
cout << print_str;
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int size;
int curmax;
cin >> size;
curmax = size;
vector<int> arr;
arr.reserve(size);
vector<int> temp_arr(size, 0);
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
for (int i = 0; i < size; i++) {
temp_arr[arr[i] - 1] = arr[i];
string print_str = "";
while (temp_arr[curmax - 1] != 0) {
string cur_string = to_string(curmax);
print_str.append(cur_string);
print_str.append(" ");
curmax--;
if (curmax == 0) {
cout << print_str;
return 0;
}
}
print_str.append("\n");
cout << print_str;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace ::std;
const int maxn = 1e5 + 10;
int n, ptr, vis[maxn];
void solve() {
scanf("%d", &n);
ptr = n;
for (int i = 0; i < n; i++) {
int tmp;
scanf("%d", &tmp);
vis[tmp] = 1;
if (tmp == ptr) {
while (vis[ptr]) {
printf("%d ", ptr);
ptr--;
}
}
puts("");
}
}
int main() {
solve();
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace ::std;
const int maxn = 1e5 + 10;
int n, ptr, vis[maxn];
void solve() {
scanf("%d", &n);
ptr = n;
for (int i = 0; i < n; i++) {
int tmp;
scanf("%d", &tmp);
vis[tmp] = 1;
if (tmp == ptr) {
while (vis[ptr]) {
printf("%d ", ptr);
ptr--;
}
}
puts("");
}
}
int main() {
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, x, m;
priority_queue<int> s;
int main() {
scanf("%d", &n);
m = n;
for (int i = 0; i < int(n); i++) {
scanf("%d", &x);
if (x < m) {
s.push(x);
cout << " ";
} else if (x == m) {
cout << x << " ";
m--;
while (!s.empty() && m == s.top()) {
cout << s.top() << " ";
s.pop();
m--;
}
}
cout << endl;
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, x, m;
priority_queue<int> s;
int main() {
scanf("%d", &n);
m = n;
for (int i = 0; i < int(n); i++) {
scanf("%d", &x);
if (x < m) {
s.push(x);
cout << " ";
} else if (x == m) {
cout << x << " ";
m--;
while (!s.empty() && m == s.top()) {
cout << s.top() << " ";
s.pop();
m--;
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
void solve() {
long long n;
cin >> n;
long long ptr = n;
long long ct[n + 1];
memset(ct, 0, sizeof(ct));
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
ct[x] = 1;
while (ptr > 0 && ct[ptr] == 1) {
cout << ptr << " ";
ptr--;
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using namespace std;
void solve() {
long long n;
cin >> n;
long long ptr = n;
long long ct[n + 1];
memset(ct, 0, sizeof(ct));
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
ct[x] = 1;
while (ptr > 0 && ct[ptr] == 1) {
cout << ptr << " ";
ptr--;
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005];
int main() {
int n;
while ((scanf("%d", &n)) != EOF) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int t = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[a[i]] = 1;
for (; b[t] == 1; t--) printf("%d ", t);
printf("\n");
}
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005];
int main() {
int n;
while ((scanf("%d", &n)) != EOF) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int t = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[a[i]] = 1;
for (; b[t] == 1; t--) printf("%d ", t);
printf("\n");
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int bigger = n;
vector<bool> snacks(n + 1, false);
while (n--) {
int snack;
cin >> snack;
snacks[snack] = true;
if (snack == bigger) {
for (bigger; bigger > 0; bigger -= 1) {
if (snacks[bigger]) {
cout << bigger;
if (bigger - 1 != 0 && snacks[bigger - 1]) {
cout << " ";
}
} else {
cout << endl;
break;
}
}
} else {
cout << endl;
}
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int bigger = n;
vector<bool> snacks(n + 1, false);
while (n--) {
int snack;
cin >> snack;
snacks[snack] = true;
if (snack == bigger) {
for (bigger; bigger > 0; bigger -= 1) {
if (snacks[bigger]) {
cout << bigger;
if (bigger - 1 != 0 && snacks[bigger - 1]) {
cout << " ";
}
} else {
cout << endl;
break;
}
}
} else {
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
vector<long long> Sieve(long long n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (long long i = 2; i * i <= n; i++) {
if (prime[i]) {
for (long long j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
vector<long long> primes;
for (long long i = 2; i <= n; i++) {
if (prime[i]) primes.push_back(i);
}
return primes;
}
map<long long, long long> primeFactors(long long n) {
map<long long, long long> factors;
while (n % 2 == 0) {
n /= 2;
factors[2]++;
}
for (long long i = 3; i <= sqrt(n); i += 2) {
long long count = 0;
while (n % i == 0) {
factors[i]++;
n /= i;
}
}
if (n > 2) factors[n]++;
return factors;
}
long long n, i, a[100001], x, t;
int32_t main() {
c_p_c();
cin >> n;
t = n;
for (i = 0; i < n; i++) {
cin >> x;
a[x]++;
while (a[t]) {
cout << t << " ";
t--;
}
cout << '\n';
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
vector<long long> Sieve(long long n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (long long i = 2; i * i <= n; i++) {
if (prime[i]) {
for (long long j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
vector<long long> primes;
for (long long i = 2; i <= n; i++) {
if (prime[i]) primes.push_back(i);
}
return primes;
}
map<long long, long long> primeFactors(long long n) {
map<long long, long long> factors;
while (n % 2 == 0) {
n /= 2;
factors[2]++;
}
for (long long i = 3; i <= sqrt(n); i += 2) {
long long count = 0;
while (n % i == 0) {
factors[i]++;
n /= i;
}
}
if (n > 2) factors[n]++;
return factors;
}
long long n, i, a[100001], x, t;
int32_t main() {
c_p_c();
cin >> n;
t = n;
for (i = 0; i < n; i++) {
cin >> x;
a[x]++;
while (a[t]) {
cout << t << " ";
t--;
}
cout << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
long long int c = 1;
vector<long long int> v(n + 1);
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
v[x] = c;
c++;
}
long long int x = 1;
for (long long int i = 1; i < v[n]; i++) {
cout << endl;
x++;
}
cout << n << " ";
for (long long int i = n - 1; i >= 1; i--) {
if (v[i] < v[i + 1]) {
cout << i << " ";
} else {
for (long long int j = x; j < v[i]; j++) {
cout << endl;
x++;
}
cout << i << " ";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t = 1;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
long long int c = 1;
vector<long long int> v(n + 1);
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
v[x] = c;
c++;
}
long long int x = 1;
for (long long int i = 1; i < v[n]; i++) {
cout << endl;
x++;
}
cout << n << " ";
for (long long int i = n - 1; i >= 1; i--) {
if (v[i] < v[i + 1]) {
cout << i << " ";
} else {
for (long long int j = x; j < v[i]; j++) {
cout << endl;
x++;
}
cout << i << " ";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x[100002] = {0}, j, t;
cin >> n;
t = n;
for (int i = 0; i < n; i++) {
cin >> j;
x[j] = 1;
while (t >= 1 && x[t] == 1) {
cout << t << " ";
t--;
}
cout << endl;
}
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x[100002] = {0}, j, t;
cin >> n;
t = n;
for (int i = 0; i < n; i++) {
cin >> j;
x[j] = 1;
while (t >= 1 && x[t] == 1) {
cout << t << " ";
t--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int> q;
int x, i = n;
while (n--) {
cin >> x;
q.push(x);
while (true) {
if (!(q.empty()) && q.top() == i) {
cout << i << " ";
i--;
q.pop();
} else {
cout << endl;
break;
}
}
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int> q;
int x, i = n;
while (n--) {
cin >> x;
q.push(x);
while (true) {
if (!(q.empty()) && q.top() == i) {
cout << i << " ";
i--;
q.pop();
} else {
cout << endl;
break;
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string a(n + 1, 0);
for (int i = 0, x, j = n; i < n; i++) {
cin >> x;
a[x] = 1;
if (x == j) {
for (; a[j]; j--) cout << j << ' ';
cout << '\n';
} else {
cout << ' ' << '\n';
}
}
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string a(n + 1, 0);
for (int i = 0, x, j = n; i < n; i++) {
cin >> x;
a[x] = 1;
if (x == j) {
for (; a[j]; j--) cout << j << ' ';
cout << '\n';
} else {
cout << ' ' << '\n';
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
while (t--) {
int n, ele;
cin >> n;
map<int, bool> number;
int current = n;
for (int i = 0; i < n; i++) {
cin >> ele;
number[ele] = true;
while (number.find(current) != number.end()) {
cout << current << " ";
current--;
}
cout << '\n';
}
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
while (t--) {
int n, ele;
cin >> n;
map<int, bool> number;
int current = n;
for (int i = 0; i < n; i++) {
cin >> ele;
number[ele] = true;
while (number.find(current) != number.end()) {
cout << current << " ";
current--;
}
cout << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100010], mark[100010];
vector<int> prev;
set<int> s;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int mx = n;
for (int i = 1; i <= n; i++) {
mark[a[i]] = 1;
while (mark[mx]) {
cout << mx << " ";
mx--;
}
cout << endl;
}
}
| ### Prompt
Develop a solution in CPP to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100010], mark[100010];
vector<int> prev;
set<int> s;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int mx = n;
for (int i = 1; i <= n; i++) {
mark[a[i]] = 1;
while (mark[mx]) {
cout << mx << " ";
mx--;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
const long long oo = (long long)1e18;
using namespace std;
int n;
bool f[100005];
int main() {
ios::sync_with_stdio(false);
cin >> n;
int bg = n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
f[x] = true;
while (bg > 0 && f[bg]) {
cout << bg << " ";
bg--;
}
cout << "\n";
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
const long long oo = (long long)1e18;
using namespace std;
int n;
bool f[100005];
int main() {
ios::sync_with_stdio(false);
cin >> n;
int bg = n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
f[x] = true;
while (bg > 0 && f[bg]) {
cout << bg << " ";
bg--;
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 10000000;
const int MOD = 1000000007;
int n, m, k, c = 1, q, d;
set<char> se;
vector<char> v, u, w;
int a[100005];
int ind[100005];
map<int, int> mp;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ind[i] = i;
mp[a[i]] = i;
}
auto it = mp.rbegin();
vector<pair<int, int> > v;
int mx = n;
for (auto x : mp) {
v.push_back({x.first, x.second});
}
reverse(v.begin(), v.end());
int w = 1, last = -1;
for (int i = 0; i < n; i++) {
while (w < v[i].second) {
w++;
cout << endl;
}
if (w == last) cout << " ";
if (w >= v[i].second) {
last = w;
cout << v[i].first;
}
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 10000000;
const int MOD = 1000000007;
int n, m, k, c = 1, q, d;
set<char> se;
vector<char> v, u, w;
int a[100005];
int ind[100005];
map<int, int> mp;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ind[i] = i;
mp[a[i]] = i;
}
auto it = mp.rbegin();
vector<pair<int, int> > v;
int mx = n;
for (auto x : mp) {
v.push_back({x.first, x.second});
}
reverse(v.begin(), v.end());
int w = 1, last = -1;
for (int i = 0; i < n; i++) {
while (w < v[i].second) {
w++;
cout << endl;
}
if (w == last) cout << " ";
if (w >= v[i].second) {
last = w;
cout << v[i].first;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = (1e5 + 9);
long long inv(long long a, long long p, long long c) {
long long temp = 1;
while (p != 0) {
if (p % 2 == 1) temp = (temp * a) % c;
a = ((long long)a * a) % c;
p = p / 2;
}
return temp;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int ar[N];
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> ar[i];
int coun[N];
int ch = n;
memset(coun, (0), sizeof(coun));
for (int i = 1; i <= n; i++) {
coun[ar[i]] = 1;
while (coun[ch] == 1) {
cout << ch << " ";
ch--;
}
cout << endl;
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = (1e5 + 9);
long long inv(long long a, long long p, long long c) {
long long temp = 1;
while (p != 0) {
if (p % 2 == 1) temp = (temp * a) % c;
a = ((long long)a * a) % c;
p = p / 2;
}
return temp;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int ar[N];
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> ar[i];
int coun[N];
int ch = n;
memset(coun, (0), sizeof(coun));
for (int i = 1; i <= n; i++) {
coun[ar[i]] = 1;
while (coun[ch] == 1) {
cout << ch << " ";
ch--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, x, poz;
bool v[100001];
int main() {
cin >> n;
poz = n;
for (int i = 1; i <= n; i++) {
cin >> x;
v[x] = 1;
while (v[poz] == 1) cout << poz << ' ', poz--;
cout << '\n';
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, x, poz;
bool v[100001];
int main() {
cin >> n;
poz = n;
for (int i = 1; i <= n; i++) {
cin >> x;
v[x] = 1;
while (v[poz] == 1) cout << poz << ' ', poz--;
cout << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100000], b[400000] = {0}, x, i, j, k = 0;
cin >> n;
x = n;
for (i = 0; i < x; i++) {
cin >> a[i];
b[a[i]]++;
if (a[i] == n) {
for (j = n; j > 0; j--) {
if (b[j] == 1) {
cout << j << " ";
k++;
}
if (b[j] == 0) {
cout << endl;
break;
}
}
} else
cout << endl;
n = n - k;
k = 0;
}
}
| ### Prompt
Generate a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100000], b[400000] = {0}, x, i, j, k = 0;
cin >> n;
x = n;
for (i = 0; i < x; i++) {
cin >> a[i];
b[a[i]]++;
if (a[i] == n) {
for (j = n; j > 0; j--) {
if (b[j] == 1) {
cout << j << " ";
k++;
}
if (b[j] == 0) {
cout << endl;
break;
}
}
} else
cout << endl;
n = n - k;
k = 0;
}
}
``` |
#include <bits/stdc++.h>
int a[100005], n, x;
int main() {
for (scanf("%d", &n); n;) {
scanf("%d", &x), a[x] = 1;
while (a[n]) printf("%d ", n--);
puts("");
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
int a[100005], n, x;
int main() {
for (scanf("%d", &n); n;) {
scanf("%d", &x), a[x] = 1;
while (a[n]) printf("%d ", n--);
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
long long a[n];
map<long long, long long> m;
long long d = n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
m[a[i]] = 1;
while (m[d]) {
cout << d << " ";
d--;
}
cout << "\n";
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
long long a[n];
map<long long, long long> m;
long long d = n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
m[a[i]] = 1;
while (m[d]) {
cout << d << " ";
d--;
}
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
long long j = n;
long long i = 0;
vector<bool> vis(n + 1, false);
while (j > 0 && i < n) {
vis[a[i]] = true;
while (j > 0 && vis[j] == true) {
cout << j << " ";
vis[j] = false;
j--;
}
cout << endl;
i++;
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
long long j = n;
long long i = 0;
vector<bool> vis(n + 1, false);
while (j > 0 && i < n) {
vis[a[i]] = true;
while (j > 0 && vis[j] == true) {
cout << j << " ";
vis[j] = false;
j--;
}
cout << endl;
i++;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long num = n, number;
map<long long, long long> m;
for (int i = 0; i < n; i++) {
cin >> number;
m[number]++;
while (m[num] == 1) {
cout << num-- << " ";
}
cout << "\n";
}
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long num = n, number;
map<long long, long long> m;
for (int i = 0; i < n; i++) {
cin >> number;
m[number]++;
while (m[num] == 1) {
cout << num-- << " ";
}
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, val;
cin >> n;
int TargetNum{n};
array<bool, 1000001> has{false};
for (int i = 0; i < n; ++i) {
cin >> val;
has[val] = true;
while (has[TargetNum] == true) {
cout << TargetNum-- << " ";
}
cout << endl;
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, val;
cin >> n;
int TargetNum{n};
array<bool, 1000001> has{false};
for (int i = 0; i < n; ++i) {
cin >> val;
has[val] = true;
while (has[TargetNum] == true) {
cout << TargetNum-- << " ";
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
map<long long int, long long int> m;
long long int nn = n;
for (long long int i = 0; i < nn; i++) {
if (a[i] == n) {
cout << n << " ";
while (1) {
if (m[n - 1]) {
cout << n - 1 << " ";
n--;
} else
break;
}
n--;
cout << endl;
} else {
cout << endl;
m[a[i]]++;
}
}
}
| ### Prompt
Create a solution in CPP for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
map<long long int, long long int> m;
long long int nn = n;
for (long long int i = 0; i < nn; i++) {
if (a[i] == n) {
cout << n << " ";
while (1) {
if (m[n - 1]) {
cout << n - 1 << " ";
n--;
} else
break;
}
n--;
cout << endl;
} else {
cout << endl;
m[a[i]]++;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, d;
cin >> n;
unordered_set<int> s;
int mx = n;
int c = 0;
for (int i = 0; i < n; i++) {
cin >> d;
if (d == mx) {
for (int i = 0; i < c; i++) cout << "\n";
cout << mx;
c = 0;
int k = mx - 1;
while (s.find(k) != s.end()) {
cout << " " << k;
s.erase(k);
k--;
}
cout << "\n";
mx = k;
} else {
s.insert(d);
c++;
}
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, d;
cin >> n;
unordered_set<int> s;
int mx = n;
int c = 0;
for (int i = 0; i < n; i++) {
cin >> d;
if (d == mx) {
for (int i = 0; i < c; i++) cout << "\n";
cout << mx;
c = 0;
int k = mx - 1;
while (s.find(k) != s.end()) {
cout << " " << k;
s.erase(k);
k--;
}
cout << "\n";
mx = k;
} else {
s.insert(d);
c++;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100000];
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
int pos(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[x] = 1;
while (a[pos]) cout << pos-- << ' ';
cout << endl;
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100000];
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
int pos(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[x] = 1;
while (a[pos]) cout << pos-- << ' ';
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, mx, x;
bool used[100001];
int main() {
cin >> n;
mx = n;
for (int i = 0; i < n; i++) {
cin >> x;
if (!used[x]) used[x] = true;
if (used[mx]) {
while (used[mx]) {
cout << mx << ' ';
mx--;
}
}
cout << endl;
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, mx, x;
bool used[100001];
int main() {
cin >> n;
mx = n;
for (int i = 0; i < n; i++) {
cin >> x;
if (!used[x]) used[x] = true;
if (used[mx]) {
while (used[mx]) {
cout << mx << ' ';
mx--;
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, a[1000000], x, r;
int main() {
cin >> n;
r = n;
for (int i = 0; i < n; i++) {
cin >> x;
if (x == r) {
cout << x << " ";
r--;
while (a[r] == 1) {
cout << r << " ";
r--;
}
cout << endl;
} else {
a[x] = 1;
cout << endl;
}
}
}
| ### Prompt
Create a solution in cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, a[1000000], x, r;
int main() {
cin >> n;
r = n;
for (int i = 0; i < n; i++) {
cin >> x;
if (x == r) {
cout << x << " ";
r--;
while (a[r] == 1) {
cout << r << " ";
r--;
}
cout << endl;
} else {
a[x] = 1;
cout << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[112345];
bool there[112345];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
int cur = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
there[a[i]] = true;
while (there[cur]) {
cout << cur << " ";
cur--;
}
cout << "\n";
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int a[112345];
bool there[112345];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
int cur = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
there[a[i]] = true;
while (there[cur]) {
cout << cur << " ";
cur--;
}
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x;
cin >> n;
long long m = n, a[100005];
for (long long i = 0; i < n; i++) {
cin >> x;
a[x] = 1;
while (a[m] == 1) {
cout << m << " ";
m--;
}
cout << "\n";
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x;
cin >> n;
long long m = n, a[100005];
for (long long i = 0; i < n; i++) {
cin >> x;
a[x] = 1;
while (a[m] == 1) {
cout << m << " ";
m--;
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
;
long long a, c;
cin >> a;
c = a;
vector<long long> v(a);
priority_queue<long long> p;
for (int i = 0; i < a; i++) cin >> v[i];
for (int i = 0; i < a; i++) {
if (v[i] == c) {
cout << v[i] << " ";
c--;
while (((int)((p).size())) > 0 && c == p.top()) {
long long x = p.top();
cout << x << " ";
c--;
p.pop();
}
cout << endl;
} else {
cout << endl;
p.push(v[i]);
}
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
;
long long a, c;
cin >> a;
c = a;
vector<long long> v(a);
priority_queue<long long> p;
for (int i = 0; i < a; i++) cin >> v[i];
for (int i = 0; i < a; i++) {
if (v[i] == c) {
cout << v[i] << " ";
c--;
while (((int)((p).size())) > 0 && c == p.top()) {
long long x = p.top();
cout << x << " ";
c--;
p.pop();
}
cout << endl;
} else {
cout << endl;
p.push(v[i]);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int> s;
int order = n;
int tmp;
for (int i = 0; i < n; ++i) {
cin >> tmp;
s.push(tmp);
while (!s.empty() && s.top() == order) {
cout << s.top() << " ";
s.pop();
order--;
}
cout << '\n';
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int> s;
int order = n;
int tmp;
for (int i = 0; i < n; ++i) {
cin >> tmp;
s.push(tmp);
while (!s.empty() && s.top() == order) {
cout << s.top() << " ";
s.pop();
order--;
}
cout << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, has[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1, t, cur = n; i <= n; ++i) {
scanf("%d", &t);
has[t] = 1;
while (has[cur]) {
printf("%d ", cur);
--cur;
}
puts("");
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, has[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1, t, cur = n; i <= n; ++i) {
scanf("%d", &t);
has[t] = 1;
while (has[cur]) {
printf("%d ", cur);
--cur;
}
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int stk[n + 1];
memset(stk, 0, sizeof(stk));
int nxt = n;
for (int i = 0; i < n; i++) {
int elm;
cin >> elm;
stk[elm] = 1;
if (elm == nxt) {
for (int j = nxt; j > 0; j--) {
if (stk[j] == 1) {
cout << j << " ";
nxt--;
} else
break;
}
}
cout << endl;
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int stk[n + 1];
memset(stk, 0, sizeof(stk));
int nxt = n;
for (int i = 0; i < n; i++) {
int elm;
cin >> elm;
stk[elm] = 1;
if (elm == nxt) {
for (int j = nxt; j > 0; j--) {
if (stk[j] == 1) {
cout << j << " ";
nxt--;
} else
break;
}
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000000 + 100];
pair<int, int> a[1000000 + 100];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].first), a[i].second = i;
sort(a + 1, a + n + 1);
reverse(a + 1, a + n + 1);
int l = 1, r = 1;
while (r <= n) {
if (a[r].second <= a[l].second)
v[a[l].second].push_back(a[r].first), r++;
else
l = r;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < v[i].size(); j++) printf("%d ", v[i][j]);
cout << endl;
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000000 + 100];
pair<int, int> a[1000000 + 100];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].first), a[i].second = i;
sort(a + 1, a + n + 1);
reverse(a + 1, a + n + 1);
int l = 1, r = 1;
while (r <= n) {
if (a[r].second <= a[l].second)
v[a[l].second].push_back(a[r].first), r++;
else
l = r;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < v[i].size(); j++) printf("%d ", v[i][j]);
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool arr[100001];
int current = 0;
void print(int x, int current2) {
if (x == current2)
;
{
for (int i = current2; i > 0; i--) {
if (arr[i]) {
cout << i << " ";
arr[i] = 0;
current = i - 1;
} else {
break;
}
}
}
}
int main() {
int n;
cin >> n;
current = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr[x] = 1;
print(x, current);
cout << endl;
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool arr[100001];
int current = 0;
void print(int x, int current2) {
if (x == current2)
;
{
for (int i = current2; i > 0; i--) {
if (arr[i]) {
cout << i << " ";
arr[i] = 0;
current = i - 1;
} else {
break;
}
}
}
}
int main() {
int n;
cin >> n;
current = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr[x] = 1;
print(x, current);
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
void print(int allb[], int i, int* k) {
int j, x = 0;
for (j = (*k); j > 0; j--) {
if (allb[j - 1] == (*k)) {
printf("%d ", allb[j - 1]);
(*k)--;
print(allb, i, k);
break;
} else
break;
}
}
int main() {
int a, n, i, j, k, all[100000], allb[100000], b;
for (i = 0; i < 100000; i++) {
all[i] = 0;
}
for (i = 0; i < 100000; i++) {
allb[i] = 0;
}
scanf("%d", &n);
k = n;
for (i = 0; i < n; i++) {
scanf("%d", &all[i]);
allb[all[i] - 1] = all[i];
if (all[i] == k) {
printf("%d ", all[i]);
k--;
print(allb, i, &k);
}
printf("\n");
}
}
| ### Prompt
Create a solution in cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
void print(int allb[], int i, int* k) {
int j, x = 0;
for (j = (*k); j > 0; j--) {
if (allb[j - 1] == (*k)) {
printf("%d ", allb[j - 1]);
(*k)--;
print(allb, i, k);
break;
} else
break;
}
}
int main() {
int a, n, i, j, k, all[100000], allb[100000], b;
for (i = 0; i < 100000; i++) {
all[i] = 0;
}
for (i = 0; i < 100000; i++) {
allb[i] = 0;
}
scanf("%d", &n);
k = n;
for (i = 0; i < n; i++) {
scanf("%d", &all[i]);
allb[all[i] - 1] = all[i];
if (all[i] == k) {
printf("%d ", all[i]);
k--;
print(allb, i, &k);
}
printf("\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
priority_queue<int> q;
int ara[n];
int seen[n];
memset(seen, 0, n);
for (i = 0; i < n; i++) {
cin >> ara[i];
q.push(ara[i]);
}
i = 0;
while (!q.empty()) {
if (seen[q.top()] == 1) {
cout << q.top() << " ";
} else {
while (i < n) {
if (ara[i] == q.top()) {
seen[ara[i]] = 1;
cout << ara[i] << " ";
break;
} else {
seen[ara[i]] = 1;
cout << endl;
}
i++;
}
}
q.pop();
}
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
priority_queue<int> q;
int ara[n];
int seen[n];
memset(seen, 0, n);
for (i = 0; i < n; i++) {
cin >> ara[i];
q.push(ara[i]);
}
i = 0;
while (!q.empty()) {
if (seen[q.top()] == 1) {
cout << q.top() << " ";
} else {
while (i < n) {
if (ara[i] == q.top()) {
seen[ara[i]] = 1;
cout << ara[i] << " ";
break;
} else {
seen[ara[i]] = 1;
cout << endl;
}
i++;
}
}
q.pop();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long a[100008], b[100009];
map<long long, long long> mp;
map<long long, long long> mp1;
int main() {
cin.sync_with_stdio(false);
long long i, j, k, l, c, v, bbbb[1000], aa[1009], bb[1000], n, m, r, t;
long long x1, x2, y1, y2, x3, x4, x5, x6;
string s, s1, s2, s3, ss[10000];
while (cin >> n) {
k = n;
for (i = 0; i < n; i++) {
cin >> m;
if (m == k) {
a[m] = 1;
for (j = k;; j--) {
if (a[j] == 1) {
cout << j << " ";
k = j;
} else {
cout << endl;
k = j;
break;
}
}
} else {
a[m] = 1;
cout << endl;
}
}
}
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long a[100008], b[100009];
map<long long, long long> mp;
map<long long, long long> mp1;
int main() {
cin.sync_with_stdio(false);
long long i, j, k, l, c, v, bbbb[1000], aa[1009], bb[1000], n, m, r, t;
long long x1, x2, y1, y2, x3, x4, x5, x6;
string s, s1, s2, s3, ss[10000];
while (cin >> n) {
k = n;
for (i = 0; i < n; i++) {
cin >> m;
if (m == k) {
a[m] = 1;
for (j = k;; j--) {
if (a[j] == 1) {
cout << j << " ";
k = j;
} else {
cout << endl;
k = j;
break;
}
}
} else {
a[m] = 1;
cout << endl;
}
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
queue<int> snacks;
priority_queue<int> notvalid;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
snacks.push(x);
}
x = n;
for (int i = 0; i < n; i++) {
if (snacks.front() == x) {
cout << snacks.front();
snacks.pop();
x--;
while (!notvalid.empty() && notvalid.top() == x) {
cout << " " << x;
x--;
notvalid.pop();
}
cout << endl;
} else {
cout << endl;
notvalid.push(snacks.front());
snacks.pop();
}
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
queue<int> snacks;
priority_queue<int> notvalid;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
snacks.push(x);
}
x = n;
for (int i = 0; i < n; i++) {
if (snacks.front() == x) {
cout << snacks.front();
snacks.pop();
x--;
while (!notvalid.empty() && notvalid.top() == x) {
cout << " " << x;
x--;
notvalid.pop();
}
cout << endl;
} else {
cout << endl;
notvalid.push(snacks.front());
snacks.pop();
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
vector<bool> fallen(n, 0);
for (int i = 0; i < n; i++) cin >> v[i];
int j = 0;
int fall = 0;
for (int i = 0; i < n; i++) {
if (v[i] == n - j && !fallen[n - j - 1]) {
fallen[n - j - 1] = 1;
cout << n - j;
j++;
while (n - j - 1 >= 0 && fallen[n - j - 1]) {
cout << " " << n - j;
j++;
}
cout << endl;
} else if (v[i] != n - j) {
cout << endl;
fallen[v[i] - 1] = 1;
}
}
return 0;
}
| ### Prompt
In CPP, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
vector<bool> fallen(n, 0);
for (int i = 0; i < n; i++) cin >> v[i];
int j = 0;
int fall = 0;
for (int i = 0; i < n; i++) {
if (v[i] == n - j && !fallen[n - j - 1]) {
fallen[n - j - 1] = 1;
cout << n - j;
j++;
while (n - j - 1 >= 0 && fallen[n - j - 1]) {
cout << " " << n - j;
j++;
}
cout << endl;
} else if (v[i] != n - j) {
cout << endl;
fallen[v[i] - 1] = 1;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool e[100005];
int n, t;
vector<vector<int> > o;
int main() {
cin >> n;
int lr = n;
int lf = -1;
for (int i = 0; i < n; i++) {
vector<int> ss;
cin >> t;
e[t] = true;
if (t > lf) lf = t;
if (lf == lr) {
for (int j = lr; j >= 1; j--) {
if (e[j]) {
ss.push_back(j);
} else {
lr = j;
lf = -1;
break;
}
}
}
o.push_back(ss);
}
for (int i = 0; i < o.size(); i++) {
if (o[i].size() == 0) {
cout << " ";
} else {
for (int j = 0; j < o[i].size(); j++) {
cout << o[i][j] << " ";
}
}
cout << '\n';
}
}
| ### Prompt
In Cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool e[100005];
int n, t;
vector<vector<int> > o;
int main() {
cin >> n;
int lr = n;
int lf = -1;
for (int i = 0; i < n; i++) {
vector<int> ss;
cin >> t;
e[t] = true;
if (t > lf) lf = t;
if (lf == lr) {
for (int j = lr; j >= 1; j--) {
if (e[j]) {
ss.push_back(j);
} else {
lr = j;
lf = -1;
break;
}
}
}
o.push_back(ss);
}
for (int i = 0; i < o.size(); i++) {
if (o[i].size() == 0) {
cout << " ";
} else {
for (int j = 0; j < o[i].size(); j++) {
cout << o[i][j] << " ";
}
}
cout << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, x, mx;
priority_queue<int> q;
int main() {
cin >> n;
mx = n;
while (n--) {
cin >> x;
q.push(x);
while (!q.empty() && q.top() == mx) {
cout << q.top() << ' ';
q.pop();
--mx;
}
cout << endl;
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, x, mx;
priority_queue<int> q;
int main() {
cin >> n;
mx = n;
while (n--) {
cin >> x;
q.push(x);
while (!q.empty() && q.top() == mx) {
cout << q.top() << ' ';
q.pop();
--mx;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[100001], n, temp, arr1[100002], arr2[100010];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
memset(arr1, 0, sizeof(arr1));
int j = 0, n1 = n;
for (int i = 0; i < n; i++) {
if (arr[i] != n1) {
arr1[arr[i]] = 1;
cout << endl;
} else {
cout << arr[i] << " ";
n1 = n1 - 1;
for (j = n1; j >= 1; j--) {
if (arr1[j] == 1) {
n1 = n1 - 1;
cout << j << " ";
} else {
cout << endl;
break;
}
}
}
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[100001], n, temp, arr1[100002], arr2[100010];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
memset(arr1, 0, sizeof(arr1));
int j = 0, n1 = n;
for (int i = 0; i < n; i++) {
if (arr[i] != n1) {
arr1[arr[i]] = 1;
cout << endl;
} else {
cout << arr[i] << " ";
n1 = n1 - 1;
for (j = n1; j >= 1; j--) {
if (arr1[j] == 1) {
n1 = n1 - 1;
cout << j << " ";
} else {
cout << endl;
break;
}
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, count = 1;
cin >> n;
vector<pair<int, int> > snacks;
for (i = 0; i < n; i++) {
int size;
cin >> size;
snacks.push_back(make_pair(size, i + 1));
}
sort(snacks.begin(), snacks.end());
for (j = 1; j < snacks[n - 1].second; j++) {
cout << endl;
count++;
}
cout << snacks[n - 1].first << " ";
for (i = n - 2; i >= 0; i--) {
int temp = count;
for (j = 1; j <= snacks[i].second - temp; j++) {
cout << endl;
count++;
}
cout << snacks[i].first << " ";
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, count = 1;
cin >> n;
vector<pair<int, int> > snacks;
for (i = 0; i < n; i++) {
int size;
cin >> size;
snacks.push_back(make_pair(size, i + 1));
}
sort(snacks.begin(), snacks.end());
for (j = 1; j < snacks[n - 1].second; j++) {
cout << endl;
count++;
}
cout << snacks[n - 1].first << " ";
for (i = n - 2; i >= 0; i--) {
int temp = count;
for (j = 1; j <= snacks[i].second - temp; j++) {
cout << endl;
count++;
}
cout << snacks[i].first << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::set<int> s;
int n;
std::cin >> n;
int placed = 0;
for (int i = 0; i < n; i++) {
int x;
std::cin >> x;
s.insert(x);
while (s.count(n - placed)) {
std::cout << n - placed << " ";
s.erase(n - placed);
placed++;
}
std::cout << std::endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::set<int> s;
int n;
std::cin >> n;
int placed = 0;
for (int i = 0; i < n; i++) {
int x;
std::cin >> x;
s.insert(x);
while (s.count(n - placed)) {
std::cout << n - placed << " ";
s.erase(n - placed);
placed++;
}
std::cout << std::endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int PI = 3.14159265;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long a[100001] = {0};
int main() {
fast();
long long x, m, n;
cin >> n;
m = n;
while (n--) {
cin >> x;
a[x]++;
while (a[m]) {
cout << m << " ";
m--;
}
cout << endl;
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int PI = 3.14159265;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long a[100001] = {0};
int main() {
fast();
long long x, m, n;
cin >> n;
m = n;
while (n--) {
cin >> x;
a[x]++;
while (a[m]) {
cout << m << " ";
m--;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n, k, a[1000000], m, cnt, ans, x, y;
long long c[1000000];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
long long mn = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
c[a[i]]++;
if (a[i] == mn) {
while (c[mn] != 0) {
cout << mn << ' ';
mn--;
}
}
cout << "\n";
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, k, a[1000000], m, cnt, ans, x, y;
long long c[1000000];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
long long mn = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
c[a[i]]++;
if (a[i] == mn) {
while (c[mn] != 0) {
cout << mn << ' ';
mn--;
}
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 100020;
int a[N] = {0};
int main() {
int n;
cin >> n;
int m = n;
for (int i = 0; i < n; i++) {
int p;
cin >> p;
a[p] = 1;
while (a[m] == 1) {
cout << m << " ";
m--;
}
cout << endl;
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100020;
int a[N] = {0};
int main() {
int n;
cin >> n;
int m = n;
for (int i = 0; i < n; i++) {
int p;
cin >> p;
a[p] = 1;
while (a[m] == 1) {
cout << m << " ";
m--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N;
int main() {
scanf("%d\n", &N);
int snacks[N], seen[N + 1];
memset(seen, 0, sizeof(seen));
for (int i = 0; i < N; i++) {
scanf("%d", &snacks[i]);
}
int next = N;
for (int i = 0; i < N; i++) {
seen[snacks[i]] = snacks[i];
while (seen[next]) {
printf("%d ", next);
next--;
}
printf("\n");
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N;
int main() {
scanf("%d\n", &N);
int snacks[N], seen[N + 1];
memset(seen, 0, sizeof(seen));
for (int i = 0; i < N; i++) {
scanf("%d", &snacks[i]);
}
int next = N;
for (int i = 0; i < N; i++) {
seen[snacks[i]] = snacks[i];
while (seen[next]) {
printf("%d ", next);
next--;
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, a;
cin >> n;
bool b[n];
for (int i = 0; i < n; i++) {
b[i] = 0;
}
int x = n;
for (int i = 0; i < n; i++) {
cin >> a;
b[a - 1] = 1;
while (b[x - 1]) {
cout << x << " ";
x--;
}
cout << '\n';
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, a;
cin >> n;
bool b[n];
for (int i = 0; i < n; i++) {
b[i] = 0;
}
int x = n;
for (int i = 0; i < n; i++) {
cin >> a;
b[a - 1] = 1;
while (b[x - 1]) {
cout << x << " ";
x--;
}
cout << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, dj, num, a[100001] = {0};
cin >> n;
dj = n;
for (i = 1; i <= dj; i++) {
cin >> num;
a[num] = 1;
if (a[n] == 1) {
cout << n;
n--;
}
while (a[n] == 1) {
cout << ' ' << n;
n--;
}
cout << endl;
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, dj, num, a[100001] = {0};
cin >> n;
dj = n;
for (i = 1; i <= dj; i++) {
cin >> num;
a[num] = 1;
if (a[n] == 1) {
cout << n;
n--;
}
while (a[n] == 1) {
cout << ' ' << n;
n--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005];
int main() {
int n;
(scanf("%d", &n)) != EOF;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int t = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[a[i]] = 1;
for (; b[t] == 1; t--) printf("%d ", t);
printf("\n");
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005];
int main() {
int n;
(scanf("%d", &n)) != EOF;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int t = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[a[i]] = 1;
for (; b[t] == 1; t--) printf("%d ", t);
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
priority_queue<int> pq;
int main() {
cin >> n;
int mx = n;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
pq.push(t);
while (!pq.empty() && pq.top() == mx) {
cout << pq.top() << " ";
pq.pop();
mx--;
}
cout << "\n";
}
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
priority_queue<int> pq;
int main() {
cin >> n;
int mx = n;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
pq.push(t);
while (!pq.empty() && pq.top() == mx) {
cout << pq.top() << " ";
pq.pop();
mx--;
}
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, a[100001], i, fix[100001], x;
int main() {
cin >> n;
x = n;
for (i = 0; i < n; i++) {
cin >> a[i];
fix[a[i]] = 12;
while (fix[x] == 12) {
cout << x << " ";
x--;
}
cout << endl;
}
}
| ### Prompt
Create a solution in CPP for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, a[100001], i, fix[100001], x;
int main() {
cin >> n;
x = n;
for (i = 0; i < n; i++) {
cin >> a[i];
fix[a[i]] = 12;
while (fix[x] == 12) {
cout << x << " ";
x--;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
const long long int oo = (long long int)1e18 + 10;
const int inf = 0x3f3f3f3f;
const double PHI = ((sqrt(5) + 1) / 2);
const double PI = acos(-1.0);
using namespace std;
long long int CEIL(long long int, long long int);
long long int FLOOR(long long int a, long long int b) {
if (b < 0)
return FLOOR(-a, -b);
else if (a < 0)
return -1 * CEIL(-a, b);
else
return a / b;
}
long long int CEIL(long long int a, long long int b) {
if (b < 0)
return CEIL(-a, -b);
else if (a < 0)
return -1 * FLOOR(-a, b);
else
return (a + b - 1) / b;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int exgcd(long long int a, long long int b, long long int &x,
long long int &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
long long int xx, yy, g = exgcd(b % a, a, xx, yy);
x = yy - (b / a) * xx;
y = xx;
return g;
}
int bin1(long long int x) {
int r = 0;
while (x) {
r++;
x &= x - 1;
}
return r;
}
long long int power(long long int b, long long int e) {
if (e == 0) return 1;
if (e % 2 == 0) return power(b * b, e / 2);
return b * power(b * b, e / 2);
}
double powerf(double b, int e) {
if (e == 0) return 1;
if (e % 2 == 0)
return powerf(b * b, e / 2);
else {
if (e > 0)
return b * powerf(b * b, e / 2);
else
return powerf(b * b, e / 2) / b;
}
}
long long int modpow(long long int b, long long int e, long long int m) {
long long int r = 1;
b = b % m;
while (e > 0) {
if (e % 2 == 1) r = (r * b) % m;
e /= 2;
b = (b * b) % m;
}
return r;
}
long long int modinverse(long long int a, long long int mod) {
return modpow(a, mod - 2, mod);
}
long long int modinverse2(long long int a, long long int m) {
long long int x, y;
exgcd(a, m, x, y);
while (x < 0) x += m;
return x;
}
long long int nCrmod(long long int n, long long int r, long long int m) {
if (r > n) r = n - r;
long long int res = 1;
for (long long int i = r; i >= 1; i--) {
res = (res * (n - i + 1)) % m;
res = (res * modinverse(i, m));
}
return res;
}
long long int nCr(long long int n, long long int r) {
if (r > n) r = n - r;
long long int res = 1;
for (long long int i = r; i >= 1; i--) {
res *= (n - i + 1);
res /= i;
}
return res;
}
long long int totient(long long int n) {
long long int res = n, p;
for (p = 2; p * p <= n; p++) {
if (n % p == 0) {
while (n % p == 0) n /= p;
res -= res / p;
}
}
if (n > 1) res -= res / n;
return res;
}
bool isprime(long long int x) {
if (x == 1) return false;
for (long long int i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
bool isvowel(char c) {
if (isupper(c)) c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
bool istriangle(long long int a, long long int b, long long int c) {
return (a + b > c) && (a + c > b) && (b + c > a);
}
long long int stringmod(string s, long long int mod) {
long long int res = 0;
for (unsigned int i = 0; i < s.size(); i++)
res = (res * 10 + s[i] - '0') % mod;
return res;
}
long long int powinfact(long long int n, long long int p) {
long long int res = 0, pw = p;
while (n >= pw) {
res += (n / pw);
pw *= p;
}
return res;
}
vector<vector<long long int> > matrixmultiply(vector<vector<long long int> > A,
vector<vector<long long int> > B,
long long int n) {
vector<vector<long long int> > C(2, vector<long long int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]);
return C;
}
vector<vector<long long int> > modmatrixmultiply(
vector<vector<long long int> > A, vector<vector<long long int> > B,
long long int n, long long int mod) {
vector<vector<long long int> > C(2, vector<long long int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
return C;
}
vector<vector<long long int> > powermatrix(vector<vector<long long int> > A,
long long int exp, long long int n) {
if (exp == 1) return A;
if (exp % 2) return matrixmultiply(A, powermatrix(A, exp - 1, n), n);
vector<vector<long long int> > X = powermatrix(A, exp / 2, n);
return matrixmultiply(X, X, n);
}
vector<vector<long long int> > modpowmatrix(vector<vector<long long int> > A,
long long int exp, long long int n,
long long int mod) {
if (exp == 1) return A;
if (exp % 2)
return modmatrixmultiply(A, modpowmatrix(A, exp - 1, n, mod), n, mod);
vector<vector<long long int> > X = modpowmatrix(A, exp / 2, n, mod);
return modmatrixmultiply(X, X, n, mod);
}
long long int merge(long long int a[], long long int l, long long int m,
long long int r) {
long long int i = l, j = m + 1, k = 0, inv_cnt = 0, temp[r - l + 1];
while (i <= m && j <= r) {
if (a[i] <= a[j])
temp[k++] = a[i++];
else {
temp[k++] = a[j++];
inv_cnt += (m - i + 1);
}
}
while (i <= m) temp[k++] = a[i++];
while (j <= r) temp[k++] = a[j++];
for (i = l; i <= r; i++) a[i] = temp[i - l];
return inv_cnt;
}
long long int mergesort(long long int a[], long long int l, long long int r) {
long long int inv_count = 0;
if (l < r) {
long long int m = (l + r) / 2;
inv_count += mergesort(a, l, m);
inv_count += mergesort(a, m + 1, r);
inv_count += merge(a, l, m, r);
}
return inv_count;
}
double dist(long long int x1, long long int y1, long long int x2,
long long int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void pre() {}
void gcj() {
long long int t;
cin >> t;
for (long long int c = 1; c <= t; c++) {
cout << "Case #" << c << ": " << endl;
}
}
set<long long int> s;
void judge() {
long long int n, curr, x;
cin >> n;
curr = n;
for (long long int i = 0; i < n; i++) {
cin >> x;
s.insert(x);
while (s.find(curr) != s.end()) {
cout << curr << " ";
curr--;
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
pre();
long long int tt = 1;
while (tt--) {
judge();
}
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
const long long int oo = (long long int)1e18 + 10;
const int inf = 0x3f3f3f3f;
const double PHI = ((sqrt(5) + 1) / 2);
const double PI = acos(-1.0);
using namespace std;
long long int CEIL(long long int, long long int);
long long int FLOOR(long long int a, long long int b) {
if (b < 0)
return FLOOR(-a, -b);
else if (a < 0)
return -1 * CEIL(-a, b);
else
return a / b;
}
long long int CEIL(long long int a, long long int b) {
if (b < 0)
return CEIL(-a, -b);
else if (a < 0)
return -1 * FLOOR(-a, b);
else
return (a + b - 1) / b;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int exgcd(long long int a, long long int b, long long int &x,
long long int &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
long long int xx, yy, g = exgcd(b % a, a, xx, yy);
x = yy - (b / a) * xx;
y = xx;
return g;
}
int bin1(long long int x) {
int r = 0;
while (x) {
r++;
x &= x - 1;
}
return r;
}
long long int power(long long int b, long long int e) {
if (e == 0) return 1;
if (e % 2 == 0) return power(b * b, e / 2);
return b * power(b * b, e / 2);
}
double powerf(double b, int e) {
if (e == 0) return 1;
if (e % 2 == 0)
return powerf(b * b, e / 2);
else {
if (e > 0)
return b * powerf(b * b, e / 2);
else
return powerf(b * b, e / 2) / b;
}
}
long long int modpow(long long int b, long long int e, long long int m) {
long long int r = 1;
b = b % m;
while (e > 0) {
if (e % 2 == 1) r = (r * b) % m;
e /= 2;
b = (b * b) % m;
}
return r;
}
long long int modinverse(long long int a, long long int mod) {
return modpow(a, mod - 2, mod);
}
long long int modinverse2(long long int a, long long int m) {
long long int x, y;
exgcd(a, m, x, y);
while (x < 0) x += m;
return x;
}
long long int nCrmod(long long int n, long long int r, long long int m) {
if (r > n) r = n - r;
long long int res = 1;
for (long long int i = r; i >= 1; i--) {
res = (res * (n - i + 1)) % m;
res = (res * modinverse(i, m));
}
return res;
}
long long int nCr(long long int n, long long int r) {
if (r > n) r = n - r;
long long int res = 1;
for (long long int i = r; i >= 1; i--) {
res *= (n - i + 1);
res /= i;
}
return res;
}
long long int totient(long long int n) {
long long int res = n, p;
for (p = 2; p * p <= n; p++) {
if (n % p == 0) {
while (n % p == 0) n /= p;
res -= res / p;
}
}
if (n > 1) res -= res / n;
return res;
}
bool isprime(long long int x) {
if (x == 1) return false;
for (long long int i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
bool isvowel(char c) {
if (isupper(c)) c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
bool istriangle(long long int a, long long int b, long long int c) {
return (a + b > c) && (a + c > b) && (b + c > a);
}
long long int stringmod(string s, long long int mod) {
long long int res = 0;
for (unsigned int i = 0; i < s.size(); i++)
res = (res * 10 + s[i] - '0') % mod;
return res;
}
long long int powinfact(long long int n, long long int p) {
long long int res = 0, pw = p;
while (n >= pw) {
res += (n / pw);
pw *= p;
}
return res;
}
vector<vector<long long int> > matrixmultiply(vector<vector<long long int> > A,
vector<vector<long long int> > B,
long long int n) {
vector<vector<long long int> > C(2, vector<long long int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]);
return C;
}
vector<vector<long long int> > modmatrixmultiply(
vector<vector<long long int> > A, vector<vector<long long int> > B,
long long int n, long long int mod) {
vector<vector<long long int> > C(2, vector<long long int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
return C;
}
vector<vector<long long int> > powermatrix(vector<vector<long long int> > A,
long long int exp, long long int n) {
if (exp == 1) return A;
if (exp % 2) return matrixmultiply(A, powermatrix(A, exp - 1, n), n);
vector<vector<long long int> > X = powermatrix(A, exp / 2, n);
return matrixmultiply(X, X, n);
}
vector<vector<long long int> > modpowmatrix(vector<vector<long long int> > A,
long long int exp, long long int n,
long long int mod) {
if (exp == 1) return A;
if (exp % 2)
return modmatrixmultiply(A, modpowmatrix(A, exp - 1, n, mod), n, mod);
vector<vector<long long int> > X = modpowmatrix(A, exp / 2, n, mod);
return modmatrixmultiply(X, X, n, mod);
}
long long int merge(long long int a[], long long int l, long long int m,
long long int r) {
long long int i = l, j = m + 1, k = 0, inv_cnt = 0, temp[r - l + 1];
while (i <= m && j <= r) {
if (a[i] <= a[j])
temp[k++] = a[i++];
else {
temp[k++] = a[j++];
inv_cnt += (m - i + 1);
}
}
while (i <= m) temp[k++] = a[i++];
while (j <= r) temp[k++] = a[j++];
for (i = l; i <= r; i++) a[i] = temp[i - l];
return inv_cnt;
}
long long int mergesort(long long int a[], long long int l, long long int r) {
long long int inv_count = 0;
if (l < r) {
long long int m = (l + r) / 2;
inv_count += mergesort(a, l, m);
inv_count += mergesort(a, m + 1, r);
inv_count += merge(a, l, m, r);
}
return inv_count;
}
double dist(long long int x1, long long int y1, long long int x2,
long long int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void pre() {}
void gcj() {
long long int t;
cin >> t;
for (long long int c = 1; c <= t; c++) {
cout << "Case #" << c << ": " << endl;
}
}
set<long long int> s;
void judge() {
long long int n, curr, x;
cin >> n;
curr = n;
for (long long int i = 0; i < n; i++) {
cin >> x;
s.insert(x);
while (s.find(curr) != s.end()) {
cout << curr << " ";
curr--;
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
pre();
long long int tt = 1;
while (tt--) {
judge();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
int n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
v.push_back({-t, i});
}
sort(v.begin(), v.end());
int j = 0;
for (int i = 0; i < n; i++) {
int t = 0;
while (i >= v[j].second && j < n) {
if (t == 1) cout << " ";
cout << n - j;
t = 1;
j++;
}
cout << '\n';
}
}
| ### Prompt
Please create a solution in CPP to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
int n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
v.push_back({-t, i});
}
sort(v.begin(), v.end());
int j = 0;
for (int i = 0; i < n; i++) {
int t = 0;
while (i >= v[j].second && j < n) {
if (t == 1) cout << " ";
cout << n - j;
t = 1;
j++;
}
cout << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, temp;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> temp;
v.push_back(temp);
}
int lookfor = n;
unordered_set<int> u;
for (int i = 0; i < v.size(); i++) {
if (v[i] == lookfor) {
cout << v[i];
lookfor--;
while (u.find(lookfor) != u.end()) {
cout << " " << lookfor;
lookfor--;
}
cout << endl;
} else {
u.insert(v[i]);
cout << endl;
}
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, temp;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> temp;
v.push_back(temp);
}
int lookfor = n;
unordered_set<int> u;
for (int i = 0; i < v.size(); i++) {
if (v[i] == lookfor) {
cout << v[i];
lookfor--;
while (u.find(lookfor) != u.end()) {
cout << " " << lookfor;
lookfor--;
}
cout << endl;
} else {
u.insert(v[i]);
cout << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int qq = 1e5 + 10;
int n, m;
vector<int> v;
int vis[qq];
int main() {
scanf("%d", &n);
int maxn = n;
int x;
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
vis[x] = 1;
while (vis[maxn]) {
printf("%d ", maxn);
maxn--;
}
printf("\n");
}
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int qq = 1e5 + 10;
int n, m;
vector<int> v;
int vis[qq];
int main() {
scanf("%d", &n);
int maxn = n;
int x;
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
vis[x] = 1;
while (vis[maxn]) {
printf("%d ", maxn);
maxn--;
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
long long int k = n;
set<long long int> s;
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
if (x == k) {
cout << x << " ";
k--;
while (k) {
if (s.find(k) != s.end()) {
cout << k << " ";
k--;
} else
break;
}
} else {
cout << " ";
s.insert(x);
}
cout << endl;
}
}
| ### Prompt
Please create a solution in CPP to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
long long int k = n;
set<long long int> s;
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
if (x == k) {
cout << x << " ";
k--;
while (k) {
if (s.find(k) != s.end()) {
cout << k << " ";
k--;
} else
break;
}
} else {
cout << " ";
s.insert(x);
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> arr(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
arr[i] = x;
}
set<int, greater<int>> s;
int current = n;
for (int i = 0; i < n; ++i) {
s.insert(arr[i]);
bool not_first = false;
while (s.find(current) != s.end()) {
if (not_first) {
cout << " ";
}
cout << *s.begin();
s.erase(current);
current--;
not_first = true;
}
cout << "\n";
}
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> arr(n);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
arr[i] = x;
}
set<int, greater<int>> s;
int current = n;
for (int i = 0; i < n; ++i) {
s.insert(arr[i]);
bool not_first = false;
while (s.find(current) != s.end()) {
if (not_first) {
cout << " ";
}
cout << *s.begin();
s.erase(current);
current--;
not_first = true;
}
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
bool compare(T x, U y) {
return (abs(x - y) <= 1e-9);
}
const int MOD = 1e9 + 7;
void solve() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
vector<int> v;
cin >> n;
int ini = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
vector<int> freq(n + 5);
for (int i = 0; i < n; i++) {
if (ini == v[i]) {
cout << ini << " ";
ini--;
while (ini > 0 && freq[ini]) {
cout << ini << " ";
freq[ini] = 0;
ini--;
}
cout << "\n";
} else {
freq[v[i]] = 1;
cout << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
cout << "\n";
}
}
| ### Prompt
Create a solution in Cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
bool compare(T x, U y) {
return (abs(x - y) <= 1e-9);
}
const int MOD = 1e9 + 7;
void solve() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
vector<int> v;
cin >> n;
int ini = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
vector<int> freq(n + 5);
for (int i = 0; i < n; i++) {
if (ini == v[i]) {
cout << ini << " ";
ini--;
while (ini > 0 && freq[ini]) {
cout << ini << " ";
freq[ini] = 0;
ini--;
}
cout << "\n";
} else {
freq[v[i]] = 1;
cout << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, k = 0;
set<int> s;
cin >> n;
int n0 = n;
while (n0--) {
cin >> x;
s.insert(x);
for (auto i = s.rbegin(); i != s.rend(); i++) {
if (*i == n) {
cout << *i << ' ';
n--;
k++;
} else
break;
}
s.erase(prev(s.end(), k), s.end());
k = 0;
cout << endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, k = 0;
set<int> s;
cin >> n;
int n0 = n;
while (n0--) {
cin >> x;
s.insert(x);
for (auto i = s.rbegin(); i != s.rend(); i++) {
if (*i == n) {
cout << *i << ' ';
n--;
k++;
} else
break;
}
s.erase(prev(s.end(), k), s.end());
k = 0;
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int* p = (int*)malloc(n * sizeof(int) + 1);
bool* vis = (bool*)malloc(n * sizeof(bool) + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
int helper = n;
for (int i = 1; i <= n; i++) {
vis[p[i]] = true;
while ((vis[helper]) && (helper > 0)) cout << helper-- << " ";
cout << "\n";
}
free(p);
free(vis);
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int* p = (int*)malloc(n * sizeof(int) + 1);
bool* vis = (bool*)malloc(n * sizeof(bool) + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
int helper = n;
for (int i = 1; i <= n; i++) {
vis[p[i]] = true;
while ((vis[helper]) && (helper > 0)) cout << helper-- << " ";
cout << "\n";
}
free(p);
free(vis);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int idx[100001];
vector<int> nums;
vector<vector<int> > ans(100001);
set<int> ss;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int t;
scanf("%d", &t);
idx[t] = i + 1;
nums.push_back(t);
ss.insert(i + 1);
}
int cur = 0;
int last = n;
for (int i = n - 1; i >= 0; i--) {
while (ss.size() && *ss.begin() <= nums[i]) {
ans[i + 1].push_back(*ss.begin());
ss.erase(ss.begin());
}
}
for (int i = 1; i <= n; i++) {
sort(ans[i].rbegin(), ans[i].rend());
for (int j = 0; j < ans[i].size(); j++) {
if (j != 0)
printf(" %d", ans[i][j]);
else
printf("%d", ans[i][j]);
}
printf("\n");
}
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int idx[100001];
vector<int> nums;
vector<vector<int> > ans(100001);
set<int> ss;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int t;
scanf("%d", &t);
idx[t] = i + 1;
nums.push_back(t);
ss.insert(i + 1);
}
int cur = 0;
int last = n;
for (int i = n - 1; i >= 0; i--) {
while (ss.size() && *ss.begin() <= nums[i]) {
ans[i + 1].push_back(*ss.begin());
ss.erase(ss.begin());
}
}
for (int i = 1; i <= n; i++) {
sort(ans[i].rbegin(), ans[i].rend());
for (int j = 0; j < ans[i].size(); j++) {
if (j != 0)
printf(" %d", ans[i][j]);
else
printf("%d", ans[i][j]);
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
unordered_set<int> b;
cin >> n;
int prev = n;
int x;
while (n--) {
cin >> x;
b.insert(x);
while (b.find(prev) != b.end()) {
cout << prev << " ";
prev--;
}
cout << "\n";
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
unordered_set<int> b;
cin >> n;
int prev = n;
int x;
while (n--) {
cin >> x;
b.insert(x);
while (b.find(prev) != b.end()) {
cout << prev << " ";
prev--;
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n;
bool hash[n + 1];
memset(hash, false, sizeof(hash));
int c = n;
for (int i = 0; i < n; i++) {
cin >> x;
hash[x] = true;
while (hash[c] && c > 0) {
cout << c << " ";
c = c - 1;
}
cout << endl;
}
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n;
bool hash[n + 1];
memset(hash, false, sizeof(hash));
int c = n;
for (int i = 0; i < n; i++) {
cin >> x;
hash[x] = true;
while (hash[c] && c > 0) {
cout << c << " ";
c = c - 1;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
set<int> s;
int pos = n;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
if (a == pos) {
cout << pos << " ";
pos--;
while (!s.empty() && *(--s.end()) == pos) {
cout << pos << " ";
pos--;
s.erase(--s.end());
}
cout << "\n";
} else
s.insert(a), cout << "\n";
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
set<int> s;
int pos = n;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
if (a == pos) {
cout << pos << " ";
pos--;
while (!s.empty() && *(--s.end()) == pos) {
cout << pos << " ";
pos--;
s.erase(--s.end());
}
cout << "\n";
} else
s.insert(a), cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
stack<int> q;
set<int> st;
int n;
cin >> n;
int t = n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a == t) {
cout << a;
t--;
if (!st.empty()) {
while (1) {
if (st.find(t) != st.end()) {
cout << " " << t;
t--;
} else {
break;
}
}
}
cout << "\n";
} else {
cout << "\n";
st.insert(a);
}
}
return 0;
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
stack<int> q;
set<int> st;
int n;
cin >> n;
int t = n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a == t) {
cout << a;
t--;
if (!st.empty()) {
while (1) {
if (st.find(t) != st.end()) {
cout << " " << t;
t--;
} else {
break;
}
}
}
cout << "\n";
} else {
cout << "\n";
st.insert(a);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int MAXN = 1e5 + 5;
const int MAXM = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 2e9;
int n, m, k, x, y, a, b, c, d, l, r, w, res, val, cnt, ans;
string s;
int sn[MAXN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int nxt = n;
for (int i = 1; i <= n; i++) {
cin >> a;
sn[a] = 1;
if (a == nxt) {
int j = nxt;
for (; sn[j] == 1; j--) {
cout << j << ' ';
}
nxt = j;
}
cout << '\n';
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int MAXN = 1e5 + 5;
const int MAXM = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 2e9;
int n, m, k, x, y, a, b, c, d, l, r, w, res, val, cnt, ans;
string s;
int sn[MAXN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int nxt = n;
for (int i = 1; i <= n; i++) {
cin >> a;
sn[a] = 1;
if (a == nxt) {
int j = nxt;
for (; sn[j] == 1; j--) {
cout << j << ' ';
}
nxt = j;
}
cout << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
vector<bool> f(n, false);
long long now = n - 1;
for (long long i = 0; i < n; i++) {
f[a[i]] = true;
if (f[now]) {
for (long long j = now; j >= 0; j--) {
if (f[j] == false) {
now = j;
break;
}
if (j != now) cout << " ";
cout << j + 1;
}
}
cout << endl;
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
vector<bool> f(n, false);
long long now = n - 1;
for (long long i = 0; i < n; i++) {
f[a[i]] = true;
if (f[now]) {
for (long long j = now; j >= 0; j--) {
if (f[j] == false) {
now = j;
break;
}
if (j != now) cout << " ";
cout << j + 1;
}
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
int cur = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = cur; j < a[i].second; j++) cout << '\n';
cur = max(a[i].second, cur);
if (a[i].second <= cur) cout << a[i].first << " ";
}
}
| ### Prompt
Please formulate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
int cur = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = cur; j < a[i].second; j++) cout << '\n';
cur = max(a[i].second, cur);
if (a[i].second <= cur) cout << a[i].first << " ";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, a[100000];
priority_queue<int> lst;
priority_queue<int> we;
cin >> n;
for (i = 0; i < n; i++) {
int l;
cin >> l;
a[i] = l;
we.push(l);
}
for (i = 0; i < n; i++) {
if (a[i] != we.top()) {
lst.push(a[i]);
if (i != n - 1) cout << "\n";
} else {
lst.push(a[i]);
while (!lst.empty()) {
if (lst.top() == we.top()) {
cout << lst.top() << " ";
lst.pop();
we.pop();
} else
break;
}
if (i != n - 1) cout << "\n";
}
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, a[100000];
priority_queue<int> lst;
priority_queue<int> we;
cin >> n;
for (i = 0; i < n; i++) {
int l;
cin >> l;
a[i] = l;
we.push(l);
}
for (i = 0; i < n; i++) {
if (a[i] != we.top()) {
lst.push(a[i]);
if (i != n - 1) cout << "\n";
} else {
lst.push(a[i]);
while (!lst.empty()) {
if (lst.top() == we.top()) {
cout << lst.top() << " ";
lst.pop();
we.pop();
} else
break;
}
if (i != n - 1) cout << "\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ara[100001];
memset(ara, 0, sizeof(ara));
int ara2[n];
for (int i = 0; i < n; i++) {
cin >> ara2[i];
}
int s = n;
for (int i = 0; i < n; i++) {
if (ara2[i] == s) {
ara[ara2[i]] = 1;
while (ara[ara2[i]]) {
cout << ara2[i]-- << " ";
s--;
}
cout << endl;
} else {
ara[ara2[i]] = 1;
cout << endl;
}
}
return 0;
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ara[100001];
memset(ara, 0, sizeof(ara));
int ara2[n];
for (int i = 0; i < n; i++) {
cin >> ara2[i];
}
int s = n;
for (int i = 0; i < n; i++) {
if (ara2[i] == s) {
ara[ara2[i]] = 1;
while (ara[ara2[i]]) {
cout << ara2[i]-- << " ";
s--;
}
cout << endl;
} else {
ara[ara2[i]] = 1;
cout << endl;
}
}
return 0;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long N = 1e5;
int main() {
int x, curr, n;
cin >> n;
curr = n;
vector<bool> excists(n + 1);
for (int i = 0; i < n; ++i) {
cin >> x;
excists[x] = true;
while (excists[curr]) cout << curr-- << " ";
cout << "\n";
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long N = 1e5;
int main() {
int x, curr, n;
cin >> n;
curr = n;
vector<bool> excists(n + 1);
for (int i = 0; i < n; ++i) {
cin >> x;
excists[x] = true;
while (excists[curr]) cout << curr-- << " ";
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
}
priority_queue<int> que;
int main() {
int n, lst, x;
read(n);
lst = n;
for (int i = 0; i < n; i++) {
read(x);
que.push(x);
while (lst == que.top()) {
printf("%d ", que.top());
que.pop();
lst--;
}
puts("");
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
}
priority_queue<int> que;
int main() {
int n, lst, x;
read(n);
lst = n;
for (int i = 0; i < n; i++) {
read(x);
que.push(x);
while (lst == que.top()) {
printf("%d ", que.top());
que.pop();
lst--;
}
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
bool vis[100010];
int main() {
scanf("%d", &n);
int x;
int now = n;
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
vis[x] = 1;
if (vis[now]) {
printf("%d", now);
--now;
while (vis[now]) {
printf(" %d", now);
--now;
}
}
puts("");
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
bool vis[100010];
int main() {
scanf("%d", &n);
int x;
int now = n;
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
vis[x] = 1;
if (vis[now]) {
printf("%d", now);
--now;
while (vis[now]) {
printf(" %d", now);
--now;
}
}
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100005], n;
vector<int> ve[100005];
bool used[100005];
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int lst = n, s = 0;
for (int i = 0; i < n; i++) {
used[a[i]] = true;
while (used[lst]) {
ve[i].push_back(lst);
s = i;
lst--;
}
}
for (int i = 0; i <= s; i++) {
for (int j = 0; j < ve[i].size(); j++) {
cout << ve[i][j] << ' ';
}
cout << endl;
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int a[100005], n;
vector<int> ve[100005];
bool used[100005];
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int lst = n, s = 0;
for (int i = 0; i < n; i++) {
used[a[i]] = true;
while (used[lst]) {
ve[i].push_back(lst);
s = i;
lst--;
}
}
for (int i = 0; i <= s; i++) {
for (int j = 0; j < ve[i].size(); j++) {
cout << ve[i][j] << ' ';
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
priority_queue<int> q;
int bottom = n + 1;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (bottom - x == 1) {
cout << x << " ";
bottom = x;
while (!q.empty()) {
int may_be = q.top();
if (bottom - may_be == 1) {
cout << may_be << " ";
bottom = may_be;
q.pop();
} else
break;
}
cout << '\n';
} else {
q.push(x);
cout << '\n';
}
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
priority_queue<int> q;
int bottom = n + 1;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (bottom - x == 1) {
cout << x << " ";
bottom = x;
while (!q.empty()) {
int may_be = q.top();
if (bottom - may_be == 1) {
cout << may_be << " ";
bottom = may_be;
q.pop();
} else
break;
}
cout << '\n';
} else {
q.push(x);
cout << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, x, mx, a[N];
int main() {
cin >> n;
mx = n;
while (n--) {
cin >> x;
a[x] = 1;
while (a[mx]) {
cout << mx << ' ';
--mx;
}
cout << endl;
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, x, mx, a[N];
int main() {
cin >> n;
mx = n;
while (n--) {
cin >> x;
a[x] = 1;
while (a[mx]) {
cout << mx << ' ';
--mx;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int a[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
a[i] = 0;
}
int num = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[x] = 1;
if (a[num] == 1) {
while (a[num]) {
cout << num;
if (a[num - 1]) {
cout << " ";
} else {
cout << "\n";
}
num--;
}
} else {
cout << "\n";
}
}
return 0;
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int a[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
a[i] = 0;
}
int num = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[x] = 1;
if (a[num] == 1) {
while (a[num]) {
cout << num;
if (a[num - 1]) {
cout << " ";
} else {
cout << "\n";
}
num--;
}
} else {
cout << "\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
map<int, int> check;
long long n, i, flag, j;
cin >> n;
flag = n;
long long a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
check[a[i]] = 1;
for (j = flag; j >= 0; j--) {
if (check[j] == 1)
cout << j << " ";
else
break;
}
cout << endl;
flag = j;
}
}
| ### Prompt
Generate a CPP solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
map<int, int> check;
long long n, i, flag, j;
cin >> n;
flag = n;
long long a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
check[a[i]] = 1;
for (j = flag; j >= 0; j--) {
if (check[j] == 1)
cout << j << " ";
else
break;
}
cout << endl;
flag = j;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
long long a = n;
vector<long long> vec(n);
fill_n(vec.begin(), n, 0);
long long b;
for (long long i = 0; i < n; i++) {
cin >> b;
vec[b - 1] = 1;
if (b == a) {
cout << b << " ";
a--;
while (vec[a - 1] == 1 && a > 0) {
cout << a << " ";
a--;
}
cout << endl;
} else {
cout << endl;
}
}
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
long long a = n;
vector<long long> vec(n);
fill_n(vec.begin(), n, 0);
long long b;
for (long long i = 0; i < n; i++) {
cin >> b;
vec[b - 1] = 1;
if (b == a) {
cout << b << " ";
a--;
while (vec[a - 1] == 1 && a > 0) {
cout << a << " ";
a--;
}
cout << endl;
} else {
cout << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
vi v(t);
for (int i = 0; i < t; i++) {
cin >> v[i];
}
int m = t;
priority_queue<int> qi;
for (int i = 0; i < t; i++) {
if (v[i] == m) {
m--;
cout << v[i] << " ";
while (!qi.empty() && (qi.top() == m)) {
m--;
cout << qi.top() << " ";
qi.pop();
}
} else
qi.push(v[i]);
cout << "\n";
}
cerr << "\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
<< endl;
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
vi v(t);
for (int i = 0; i < t; i++) {
cin >> v[i];
}
int m = t;
priority_queue<int> qi;
for (int i = 0; i < t; i++) {
if (v[i] == m) {
m--;
cout << v[i] << " ";
while (!qi.empty() && (qi.top() == m)) {
m--;
cout << qi.top() << " ";
qi.pop();
}
} else
qi.push(v[i]);
cout << "\n";
}
cerr << "\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
<< endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
set<int> s;
int x = n;
for (long long i = 0; i < n; i++) {
int k;
cin >> k;
s.insert(k);
while (s.find(x) != s.end()) {
cout << x << " ";
s.erase(x);
x--;
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
{ solve(); }
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
set<int> s;
int x = n;
for (long long i = 0; i < n; i++) {
int k;
cin >> k;
s.insert(k);
while (s.find(x) != s.end()) {
cout << x << " ";
s.erase(x);
x--;
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
{ solve(); }
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s[200000], m, a;
cin >> n;
m = n;
for (int i = 0; i < n; i++) {
cin >> a;
s[a] = 1;
if (a == m) {
while (s[m]) {
cout << m << ' ';
m--;
}
cout << endl;
} else
cout << endl;
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.
Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower.
<image>
However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.
Write a program that models the behavior of Ankh-Morpork residents.
Input
The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks.
The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
Output
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
Examples
Input
3
3 1 2
Output
3
2 1
Input
5
4 5 1 2 3
Output
5 4
3 2 1
Note
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s[200000], m, a;
cin >> n;
m = n;
for (int i = 0; i < n; i++) {
cin >> a;
s[a] = 1;
if (a == m) {
while (s[m]) {
cout << m << ' ';
m--;
}
cout << endl;
} else
cout << endl;
}
return 0;
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.