output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
int max;
int n;
cin >> n;
max = n;
bool arr[100000]{false};
int in;
for (int i = 0; i < n; ++i) {
cin >> in;
if (in != max) {
arr[in - 1] = true;
cout << endl;
} else {
cout << max << ' ';
--max;
while (max != 0 && arr[max - 1]) {
cout << max << ' ';
--max;
}
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 max;
int n;
cin >> n;
max = n;
bool arr[100000]{false};
int in;
for (int i = 0; i < n; ++i) {
cin >> in;
if (in != max) {
arr[in - 1] = true;
cout << endl;
} else {
cout << max << ' ';
--max;
while (max != 0 && arr[max - 1]) {
cout << max << ' ';
--max;
}
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int> pq;
int A[n], S[n];
for (int i = 0; i < n; i++) {
cin >> S[i];
}
for (int i = n, j = 0; i >= 1; i--, j++) A[j] = i;
int index = 0;
for (int i = 0; i < n; i++) {
pq.push(S[i]);
if (A[index] == S[i]) {
while (!pq.empty() && A[index] == pq.top()) {
cout << pq.top() << " ";
pq.pop();
if (pq.top() != A[index] && !pq.empty()) {
if (pq.top() == A[index + 1] && index < n) index += 1;
}
}
index++;
cout << "\n";
} else {
cout << "\n";
}
}
if (!pq.empty()) {
while (!pq.empty() && A[index] == pq.top()) {
cout << pq.top() << " ";
pq.pop();
if (pq.top() != A[index] && index < n) {
if (pq.top() == A[index + 1]) index += 1;
}
}
index++;
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 n;
cin >> n;
priority_queue<int> pq;
int A[n], S[n];
for (int i = 0; i < n; i++) {
cin >> S[i];
}
for (int i = n, j = 0; i >= 1; i--, j++) A[j] = i;
int index = 0;
for (int i = 0; i < n; i++) {
pq.push(S[i]);
if (A[index] == S[i]) {
while (!pq.empty() && A[index] == pq.top()) {
cout << pq.top() << " ";
pq.pop();
if (pq.top() != A[index] && !pq.empty()) {
if (pq.top() == A[index + 1] && index < n) index += 1;
}
}
index++;
cout << "\n";
} else {
cout << "\n";
}
}
if (!pq.empty()) {
while (!pq.empty() && A[index] == pq.top()) {
cout << pq.top() << " ";
pq.pop();
if (pq.top() != A[index] && index < n) {
if (pq.top() == A[index + 1]) index += 1;
}
}
index++;
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void start(istream& str) {}
bool* b;
string solve(istream& cin) {
int n;
cin >> n;
int* ord = new int[n];
for (int i = 0; i < n; i++) cin >> ord[i];
int m = n;
int last = 0;
string res = "";
b = new bool[n + 1];
for (int i = 0; i < n + 1; i++) {
b[i] = false;
}
for (int i = 0; i < n; i++) {
b[ord[i]] = true;
if (ord[i] == m) {
for (int j = m; b[j]; j--) {
cout << to_string(j) + " ";
m--;
}
cout << "\n";
} else {
cout << "\n";
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
bool debug = false;
if (debug) {
ifstream infile;
infile.open("input.txt");
start(infile);
while (!infile.eof()) {
cout << solve(infile) << endl;
}
infile.close();
cin.get();
} else {
start(cin);
cout << solve(cin) << 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;
void start(istream& str) {}
bool* b;
string solve(istream& cin) {
int n;
cin >> n;
int* ord = new int[n];
for (int i = 0; i < n; i++) cin >> ord[i];
int m = n;
int last = 0;
string res = "";
b = new bool[n + 1];
for (int i = 0; i < n + 1; i++) {
b[i] = false;
}
for (int i = 0; i < n; i++) {
b[ord[i]] = true;
if (ord[i] == m) {
for (int j = m; b[j]; j--) {
cout << to_string(j) + " ";
m--;
}
cout << "\n";
} else {
cout << "\n";
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
bool debug = false;
if (debug) {
ifstream infile;
infile.open("input.txt");
start(infile);
while (!infile.eof()) {
cout << solve(infile) << endl;
}
infile.close();
cin.get();
} else {
start(cin);
cout << solve(cin) << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int main() {
int n;
int a[100010];
bool visited[100010];
memset(visited, 0, sizeof visited);
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int ptr = n;
for (int i = 0; i < n; i++) {
visited[a[i]] = 1;
while (ptr > 0 && visited[ptr]) cout << ptr << " ", ptr--;
cout << endl;
}
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;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int main() {
int n;
int a[100010];
bool visited[100010];
memset(visited, 0, sizeof visited);
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int ptr = n;
for (int i = 0; i < n; i++) {
visited[a[i]] = 1;
while (ptr > 0 && visited[ptr]) cout << ptr << " ", ptr--;
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int, greater<int>> s;
vector<int> v;
int n, num;
cin >> n;
int max = n;
while (n--) {
cin >> num;
int count = 0, count1 = 0;
int first = 0, second = 0;
s.insert(num);
if (num == max) {
cout << num << " ";
max = num - 1;
for (auto it = s.begin(); it != s.end(); it++, count++) {
first = second;
second = *it;
if (second == first - 1 && count >= 1) {
cout << second << " ";
v.push_back(second);
max = second - 1;
count1++;
} else if (count >= 1) {
break;
}
}
cout << endl;
auto ir = s.begin();
s.erase(ir);
for (int i = 0; i < count1; i++) {
auto it = s.find(v[i]);
if (it != s.end()) {
s.erase(it);
}
}
v.clear();
} else {
cout << endl;
}
}
s.clear();
}
| ### 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() {
set<int, greater<int>> s;
vector<int> v;
int n, num;
cin >> n;
int max = n;
while (n--) {
cin >> num;
int count = 0, count1 = 0;
int first = 0, second = 0;
s.insert(num);
if (num == max) {
cout << num << " ";
max = num - 1;
for (auto it = s.begin(); it != s.end(); it++, count++) {
first = second;
second = *it;
if (second == first - 1 && count >= 1) {
cout << second << " ";
v.push_back(second);
max = second - 1;
count1++;
} else if (count >= 1) {
break;
}
}
cout << endl;
auto ir = s.begin();
s.erase(ir);
for (int i = 0; i < count1; i++) {
auto it = s.find(v[i]);
if (it != s.end()) {
s.erase(it);
}
}
v.clear();
} else {
cout << endl;
}
}
s.clear();
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, a, b;
cin >> n;
b = n;
set<int> v;
for (long long i = 0; i < n; i++) {
cin >> a;
v.insert(a);
if (a == b) {
cout << b << " ";
v.erase(b);
b--;
while (true) {
if (v.find(b) != v.end()) {
cout << b << " ";
v.erase(b);
b--;
} else {
break;
}
}
cout << endl;
} else
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() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, a, b;
cin >> n;
b = n;
set<int> v;
for (long long i = 0; i < n; i++) {
cin >> a;
v.insert(a);
if (a == b) {
cout << b << " ";
v.erase(b);
b--;
while (true) {
if (v.find(b) != v.end()) {
cout << b << " ";
v.erase(b);
b--;
} else {
break;
}
}
cout << endl;
} else
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 100007;
int n;
int a[MAXN];
bool f[MAXN];
void init();
void input();
void work();
int main() {
init();
input();
work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
}
void work() {
int now = n;
memset(f, false, sizeof(f));
for (int i = 1; i <= n; ++i) {
f[a[i]] = true;
while (f[now]) cout << now-- << " ";
cout << endl;
}
}
| ### 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;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 100007;
int n;
int a[MAXN];
bool f[MAXN];
void init();
void input();
void work();
int main() {
init();
input();
work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
}
void work() {
int now = n;
memset(f, false, sizeof(f));
for (int i = 1; i <= n; ++i) {
f[a[i]] = true;
while (f[now]) cout << now-- << " ";
cout << endl;
}
}
``` |
#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 << 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;
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 << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
priority_queue<int> torre;
int golosinas_totales, golosinas, golosina_esperada;
cin >> golosinas_totales;
golosina_esperada = golosinas_totales;
for (int i = 0; i < golosinas_totales; i++) {
cin >> golosinas;
if (golosinas == golosina_esperada) {
torre.push(golosinas);
} else {
torre.push(golosinas);
cout << endl;
}
if (!torre.empty() && torre.top() == golosina_esperada) {
while (!torre.empty() && torre.top() == golosina_esperada) {
cout << torre.top() << " ";
torre.pop();
golosina_esperada = golosina_esperada - 1;
}
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() {
priority_queue<int> torre;
int golosinas_totales, golosinas, golosina_esperada;
cin >> golosinas_totales;
golosina_esperada = golosinas_totales;
for (int i = 0; i < golosinas_totales; i++) {
cin >> golosinas;
if (golosinas == golosina_esperada) {
torre.push(golosinas);
} else {
torre.push(golosinas);
cout << endl;
}
if (!torre.empty() && torre.top() == golosina_esperada) {
while (!torre.empty() && torre.top() == golosina_esperada) {
cout << torre.top() << " ";
torre.pop();
golosina_esperada = golosina_esperada - 1;
}
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a;
cin >> a;
int b = a;
set<int, greater<int>> k;
int i;
for (i = 0; i < a; i++) {
int c;
cin >> c;
if (c == b) {
cout << c << " ";
b--;
long long f = 0;
if (k.find(b) != k.end()) {
for (auto it = k.find(b); it != k.end(); ++it) {
if (*it == b) {
cout << *it << " ";
b--;
f++;
} else
break;
}
}
cout << "\n";
} else {
cout << "\n";
k.insert(c);
}
}
}
| ### 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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a;
cin >> a;
int b = a;
set<int, greater<int>> k;
int i;
for (i = 0; i < a; i++) {
int c;
cin >> c;
if (c == b) {
cout << c << " ";
b--;
long long f = 0;
if (k.find(b) != k.end()) {
for (auto it = k.find(b); it != k.end(); ++it) {
if (*it == b) {
cout << *it << " ";
b--;
f++;
} else
break;
}
}
cout << "\n";
} else {
cout << "\n";
k.insert(c);
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
set<int, greater<int> > S;
int y = n;
for (int i = n; i > 0; --i) {
int x;
cin >> x;
S.insert(x);
for (set<int, greater<int> >::iterator ix = S.begin();
ix != S.end() && *ix == y;) {
cout << y << " ";
set<int, greater<int> >::iterator t = ++ix;
S.erase(--ix);
ix = t;
--y;
}
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 main() {
int n;
cin >> n;
set<int, greater<int> > S;
int y = n;
for (int i = n; i > 0; --i) {
int x;
cin >> x;
S.insert(x);
for (set<int, greater<int> >::iterator ix = S.begin();
ix != S.end() && *ix == y;) {
cout << y << " ";
set<int, greater<int> >::iterator t = ++ix;
S.erase(--ix);
ix = t;
--y;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int A[N], C[N], bit[N];
map<int, int> B;
void update(int x) {
while (x < N) {
bit[x]++;
x += x & (-x);
}
}
int query(int a) {
int s = 0;
while (a) {
s += bit[a];
a -= a & (-a);
}
return s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> A[i], B[A[i]];
int num = n;
for (auto &it : B) {
it.second = --num;
C[num] = it.first;
}
int done = -1;
for (int i = 0; i < n; i++) {
bit[B[A[i]]]++;
while (bit[done + 1]) {
cout << C[done + 1] << " ";
done++;
}
cout << endl;
}
}
| ### 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;
const int N = 1e5 + 10;
int A[N], C[N], bit[N];
map<int, int> B;
void update(int x) {
while (x < N) {
bit[x]++;
x += x & (-x);
}
}
int query(int a) {
int s = 0;
while (a) {
s += bit[a];
a -= a & (-a);
}
return s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> A[i], B[A[i]];
int num = n;
for (auto &it : B) {
it.second = --num;
C[num] = it.first;
}
int done = -1;
for (int i = 0; i < n; i++) {
bit[B[A[i]]]++;
while (bit[done + 1]) {
cout << C[done + 1] << " ";
done++;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void make_unique(vector<T>& vec) {
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
const long long mod = 1000000007;
const long double PI = 3.141592653589793;
bool isprime(long long n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n = 0, m = 0, i = 0, j = 0, k = 0, cnt = 0, ans = 0, sum = 0,
flag = 0, pos = 0, ind = 0, mn = 0, mx = 0, res = 0;
cin >> n;
vector<long long> v(n);
for (i = 0; i < n; i++) {
cin >> v[i];
}
m = n;
priority_queue<long long> q;
for (i = 0; i < n; i++) {
q.push(v[i]);
while (q.top() == m) {
cout << q.top() << " ";
q.pop();
m--;
}
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>
using namespace std;
template <typename T>
void make_unique(vector<T>& vec) {
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
const long long mod = 1000000007;
const long double PI = 3.141592653589793;
bool isprime(long long n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n = 0, m = 0, i = 0, j = 0, k = 0, cnt = 0, ans = 0, sum = 0,
flag = 0, pos = 0, ind = 0, mn = 0, mx = 0, res = 0;
cin >> n;
vector<long long> v(n);
for (i = 0; i < n; i++) {
cin >> v[i];
}
m = n;
priority_queue<long long> q;
for (i = 0; i < n; i++) {
q.push(v[i]);
while (q.top() == m) {
cout << q.top() << " ";
q.pop();
m--;
}
cout << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
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
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 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;
int main() {
long long n, a;
cin >> n;
map<long long, int> m;
map<long long, int>::iterator it;
long long z = n;
for (long long i = 0; i < n; i++) {
cin >> a;
if (a == z) {
z--;
cout << a << " ";
while (!m.empty()) {
it = m.find(z);
if (it != m.end()) {
cout << z << " ";
z--;
m.erase(it);
} else
break;
}
} else {
m[a]++;
}
if ((i + 1) != n) cout << endl;
}
map<long long, int>::reverse_iterator it1;
for (it1 = m.rbegin(); it1 != m.rend(); it1++) cout << it1->first << " ";
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, a;
cin >> n;
map<long long, int> m;
map<long long, int>::iterator it;
long long z = n;
for (long long i = 0; i < n; i++) {
cin >> a;
if (a == z) {
z--;
cout << a << " ";
while (!m.empty()) {
it = m.find(z);
if (it != m.end()) {
cout << z << " ";
z--;
m.erase(it);
} else
break;
}
} else {
m[a]++;
}
if ((i + 1) != n) cout << endl;
}
map<long long, int>::reverse_iterator it1;
for (it1 = m.rbegin(); it1 != m.rend(); it1++) cout << it1->first << " ";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n, x;
scanf("%d", &n);
int nxt = n;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
a[x] = 1;
while (nxt > 0 && a[nxt]) {
printf("%d ", nxt);
nxt--;
}
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 a[100005];
int main() {
int n, x;
scanf("%d", &n);
int nxt = n;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
a[x] = 1;
while (nxt > 0 && a[nxt]) {
printf("%d ", nxt);
nxt--;
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, a[100004] = {0}, i, j, x;
int main() {
cin >> n;
for (i = 0, j = n; i < n; i++) {
cin >> x;
a[x] = 1;
if (a[j] == 1) {
while (j >= 1 && a[j--] == 1) {
cout << j + 1 << " ";
}
j++;
}
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 n, a[100004] = {0}, i, j, x;
int main() {
cin >> n;
for (i = 0, j = n; i < n; i++) {
cin >> x;
a[x] = 1;
if (a[j] == 1) {
while (j >= 1 && a[j--] == 1) {
cout << j + 1 << " ";
}
j++;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int M = 100002;
int a[M];
bool b[M];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) scanf("%d", a + i);
int m = n;
for (int i = 0; i < n; ++i) {
int x = a[i];
b[x] = 1;
while (x == m && b[x]) {
printf("%d ", x);
--m, --x;
}
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 M = 100002;
int a[M];
bool b[M];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) scanf("%d", a + i);
int m = n;
for (int i = 0; i < n; ++i) {
int x = a[i];
b[x] = 1;
while (x == m && b[x]) {
printf("%d ", x);
--m, --x;
}
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, a[100000], k;
bool b[100005];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
k = n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
b[a[i]] = true;
while (b[k]) {
cout << k << " ";
k--;
}
cout << endl;
}
}
| ### 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, a[100000], k;
bool b[100005];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
k = n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
b[a[i]] = true;
while (b[k]) {
cout << k << " ";
k--;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, m, a[100005] = {};
cin >> n;
m = n;
while (n--) {
cin >> x;
a[x] = 1;
while (a[m]) {
cout << m << ' ';
m--;
}
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, m, a[100005] = {};
cin >> n;
m = n;
while (n--) {
cin >> x;
a[x] = 1;
while (a[m]) {
cout << m << ' ';
m--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long stll(string mystr) {
stringstream convert(mystr);
long long x;
convert >> x;
return x;
}
vector<long long> v(100010);
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
long long t, te, j;
t = n;
for (long long i = 0; i < n; i += 1) {
cin >> te;
v[te] = 1;
if (v[t]) {
while (v[t]) {
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;
long long stll(string mystr) {
stringstream convert(mystr);
long long x;
convert >> x;
return x;
}
vector<long long> v(100010);
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
long long t, te, j;
t = n;
for (long long i = 0; i < n; i += 1) {
cin >> te;
v[te] = 1;
if (v[t]) {
while (v[t]) {
cout << t << " ";
t--;
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int x = n;
int a[n];
priority_queue<int> pq, pq2;
for (int i = 0; i < n; i++) {
cin >> a[i];
pq.push(a[i]);
}
for (int i = 0; i < n; i++) {
if (a[i] == x) {
cout << a[i];
pq.pop();
x--;
for (int j = 0; j < pq2.size(); j++) {
if (pq2.top() == x) {
cout << " " << pq2.top();
pq2.pop();
--x;
j--;
}
}
cout << endl;
} else {
cout << endl;
pq2.push(a[i]);
}
}
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;
cin >> n;
int x = n;
int a[n];
priority_queue<int> pq, pq2;
for (int i = 0; i < n; i++) {
cin >> a[i];
pq.push(a[i]);
}
for (int i = 0; i < n; i++) {
if (a[i] == x) {
cout << a[i];
pq.pop();
x--;
for (int j = 0; j < pq2.size(); j++) {
if (pq2.top() == x) {
cout << " " << pq2.top();
pq2.pop();
--x;
j--;
}
}
cout << endl;
} else {
cout << endl;
pq2.push(a[i]);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100002], b[100004];
int main() {
int n;
while (~scanf("%d", &n)) {
memset(b, 0, sizeof(b));
int p = n;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[a[i]] = 1;
if (a[i] == p) {
printf("%d", a[i]);
p--;
while (b[p]) {
printf(" %d", p);
p--;
}
printf("\n");
} else
printf("\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 a[100002], b[100004];
int main() {
int n;
while (~scanf("%d", &n)) {
memset(b, 0, sizeof(b));
int p = n;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[a[i]] = 1;
if (a[i] == p) {
printf("%d", a[i]);
p--;
while (b[p]) {
printf(" %d", p);
p--;
}
printf("\n");
} else
printf("\n");
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
int N;
cin >> N;
vector<int> myVector(N);
vector<int>::iterator it;
unordered_set<int> mySet;
unordered_set<int>::const_iterator got;
int myExpecValue = N;
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
if (temp == myExpecValue) {
cout << myExpecValue << " ";
do {
myExpecValue--;
got = mySet.find(myExpecValue);
if (got != mySet.end()) {
cout << *got << " ";
}
} while (got != mySet.end());
cout << endl;
} else {
cout << endl;
mySet.insert(temp);
}
}
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 argc, const char* argv[]) {
int N;
cin >> N;
vector<int> myVector(N);
vector<int>::iterator it;
unordered_set<int> mySet;
unordered_set<int>::const_iterator got;
int myExpecValue = N;
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
if (temp == myExpecValue) {
cout << myExpecValue << " ";
do {
myExpecValue--;
got = mySet.find(myExpecValue);
if (got != mySet.end()) {
cout << *got << " ";
}
} while (got != mySet.end());
cout << endl;
} else {
cout << endl;
mySet.insert(temp);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
map<int, bool> m;
int a[1234567], i, max1;
int main() {
int n;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
if (max1 < a[i]) max1 = a[i];
}
for (i = 0; i < n; i++) {
if (max1 != a[i]) {
cout << endl;
m[a[i]] = true;
} else {
m[a[i]] = true;
while (m[max1] == true) {
cout << max1 << " ";
max1--;
}
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;
map<int, bool> m;
int a[1234567], i, max1;
int main() {
int n;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
if (max1 < a[i]) max1 = a[i];
}
for (i = 0; i < n; i++) {
if (max1 != a[i]) {
cout << endl;
m[a[i]] = true;
} else {
m[a[i]] = true;
while (m[max1] == true) {
cout << max1 << " ";
max1--;
}
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0;
cin >> n;
bool arr[n];
for (int i = 0; i < n; ++i) {
arr[i] = false;
}
int arrow = n - 1;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
arr[tmp - 1] = true;
while (arr[arrow]) {
cout << 1 + arrow-- << " ";
}
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 n = 0;
cin >> n;
bool arr[n];
for (int i = 0; i < n; ++i) {
arr[i] = false;
}
int arrow = n - 1;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
arr[tmp - 1] = true;
while (arr[arrow]) {
cout << 1 + arrow-- << " ";
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> check(n + 1);
int max_el = n;
for (int i = 0; i < n; i++) {
check[a[i]] = 1;
if (a[i] == max_el) {
while (check[max_el]) {
cout << max_el << " ";
max_el -= 1;
}
}
if (i != n - 1) cout << endl;
}
}
| ### 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;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> check(n + 1);
int max_el = n;
for (int i = 0; i < n; i++) {
check[a[i]] = 1;
if (a[i] == max_el) {
while (check[max_el]) {
cout << max_el << " ";
max_el -= 1;
}
}
if (i != n - 1) cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int j = n;
int visited[100010] = {0};
for (int i = 0; i < n; i++) {
if (a[i] == j) {
visited[j] = 1;
while (1) {
if (visited[j]) {
cout << j << " ";
j--;
} else
break;
}
cout << endl;
} else {
visited[a[i]] = 1;
cout << endl;
}
}
if (j > 0) {
for (int k = j; k > 0; k--) cout << k << " ";
}
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() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int j = n;
int visited[100010] = {0};
for (int i = 0; i < n; i++) {
if (a[i] == j) {
visited[j] = 1;
while (1) {
if (visited[j]) {
cout << j << " ";
j--;
} else
break;
}
cout << endl;
} else {
visited[a[i]] = 1;
cout << endl;
}
}
if (j > 0) {
for (int k = j; k > 0; k--) cout << k << " ";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int cnt[200009];
map<int, int> m;
void slv() {
int n;
cin >> n;
int a[n + 4];
for (int i = 0; i < n; i++) cin >> a[i];
int mx = n;
priority_queue<int> pq;
for (int i = 0; i < n; i++) {
if (a[i] == mx) {
cout << a[i] << " ";
mx--;
while (mx > 0 && !pq.empty()) {
int u = pq.top();
if (mx == u) {
cout << u << " ";
mx--;
pq.pop();
} else
break;
}
cout << endl;
} else {
cout << endl;
pq.push(a[i]);
}
}
}
int main() {
int t;
t = 1;
for (int cs = 1; cs <= t; cs++) {
slv();
}
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 cnt[200009];
map<int, int> m;
void slv() {
int n;
cin >> n;
int a[n + 4];
for (int i = 0; i < n; i++) cin >> a[i];
int mx = n;
priority_queue<int> pq;
for (int i = 0; i < n; i++) {
if (a[i] == mx) {
cout << a[i] << " ";
mx--;
while (mx > 0 && !pq.empty()) {
int u = pq.top();
if (mx == u) {
cout << u << " ";
mx--;
pq.pop();
} else
break;
}
cout << endl;
} else {
cout << endl;
pq.push(a[i]);
}
}
}
int main() {
int t;
t = 1;
for (int cs = 1; cs <= t; cs++) {
slv();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[100005] = {0};
bool b[100005] = {0};
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int c = n;
for (int i = 0; i < n; i++) {
b[a[i]] = true;
while (b[c] && c > 0) {
printf("%d ", c--);
}
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 main() {
int a[100005] = {0};
bool b[100005] = {0};
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int c = n;
for (int i = 0; i < n; i++) {
b[a[i]] = true;
while (b[c] && c > 0) {
printf("%d ", c--);
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, a, cur;
priority_queue<int> pq;
int main() {
scanf("%d", &n);
cur = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
pq.push(a);
while (!pq.empty() && pq.top() == cur) {
printf("%d ", cur);
pq.pop();
cur--;
}
puts("");
}
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, a, cur;
priority_queue<int> pq;
int main() {
scanf("%d", &n);
cur = n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
pq.push(a);
while (!pq.empty() && pq.top() == cur) {
printf("%d ", cur);
pq.pop();
cur--;
}
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
int main() {
long i, n, a[100030] = {0}, k, m;
scanf("%d", &n);
k = n;
for (i = 0; i < n; i++) {
scanf("%d", &m);
a[m] = 1;
while (a[k] == 1) {
printf("%d ", k);
k--;
}
printf("\n");
}
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 main() {
long i, n, a[100030] = {0}, k, m;
scanf("%d", &n);
k = n;
for (i = 0; i < n; i++) {
scanf("%d", &m);
a[m] = 1;
while (a[k] == 1) {
printf("%d ", k);
k--;
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100001];
int main() {
cin >> n;
m = n;
while (m--) {
int x;
cin >> x;
a[x] = 1;
while (a[n]) {
cout << n << " ";
n--;
}
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 n, m, a[100001];
int main() {
cin >> n;
m = n;
while (m--) {
int x;
cin >> x;
a[x] = 1;
while (a[n]) {
cout << n << " ";
n--;
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
static const double pi = acos(-1);
long long answer{0};
char tempc;
long long templ;
string temps;
const long long mod = 998244353;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
long long maxi = n;
priority_queue<long long> p;
for (long long i = 1; i <= n; i++) {
cin >> templ;
p.push(templ);
if (templ == maxi) {
long long g = p.size();
for (long long j = 1; j <= g; j++) {
if (p.top() == maxi) {
maxi--;
cout << p.top() << ' ';
p.pop();
} else {
break;
}
}
}
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;
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
static const double pi = acos(-1);
long long answer{0};
char tempc;
long long templ;
string temps;
const long long mod = 998244353;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
long long maxi = n;
priority_queue<long long> p;
for (long long i = 1; i <= n; i++) {
cin >> templ;
p.push(templ);
if (templ == maxi) {
long long g = p.size();
for (long long j = 1; j <= g; j++) {
if (p.top() == maxi) {
maxi--;
cout << p.top() << ' ';
p.pop();
} else {
break;
}
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long arr[100000];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
long long c = n;
priority_queue<long long> pq;
for (int i = 0; i < n; i++) {
pq.push(arr[i]);
while (pq.top() == c) {
cout << pq.top() << " ";
pq.pop();
c--;
}
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 main() {
long long n;
cin >> n;
long long arr[100000];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
long long c = n;
priority_queue<long long> pq;
for (int i = 0; i < n; i++) {
pq.push(arr[i]);
while (pq.top() == c) {
cout << pq.top() << " ";
pq.pop();
c--;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr1(n + 1);
bool scan = true;
int key = n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
arr1[x]++;
if (x == key)
while (arr1[key]) {
cout << key << " ";
key--;
}
cout << endl;
}
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;
cin >> n;
vector<int> arr1(n + 1);
bool scan = true;
int key = n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
arr1[x]++;
if (x == key)
while (arr1[key]) {
cout << key << " ";
key--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
bool arr[100005] = {false};
cin >> n;
int c = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == c) {
cout << x << " ";
c--;
while (arr[c]) {
cout << c << " ";
c--;
}
} else {
arr[x] = true;
}
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;
bool arr[100005] = {false};
cin >> n;
int c = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == c) {
cout << x << " ";
c--;
while (arr[c]) {
cout << c << " ";
c--;
}
} else {
arr[x] = true;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 10;
int n, a, cnt, s[maxN];
int main() {
cin >> n;
cnt = n;
for (int i = 1; i <= n; i++) {
cin >> a;
s[a]++;
while (s[cnt] > 0) {
cout << cnt-- << ' ';
}
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 maxN = 100 * 1000 + 10;
int n, a, cnt, s[maxN];
int main() {
cin >> n;
cnt = n;
for (int i = 1; i <= n; i++) {
cin >> a;
s[a]++;
while (s[cnt] > 0) {
cout << cnt-- << ' ';
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int Max = 100000;
int days[Max];
int main() {
int n, temp, x;
cin >> n;
temp = n;
for (int i = 0; i < n; i++) {
cin >> x;
days[x - 1] = 1;
if (x == temp) {
while (days[temp - 1] == 1 && temp) cout << temp-- << " ";
}
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 Max = 100000;
int days[Max];
int main() {
int n, temp, x;
cin >> n;
temp = n;
for (int i = 0; i < n; i++) {
cin >> x;
days[x - 1] = 1;
if (x == temp) {
while (days[temp - 1] == 1 && temp) cout << temp-- << " ";
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int n = 10000020;
int a[n] = {0};
int main() {
int k;
cin >> k;
int m = k;
for (int i = 0; i < k; i++) {
int p;
cin >> p;
a[p] = 1;
while (a[m] == 1) {
cout << m << " ";
m--;
}
cout << endl;
}
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 n = 10000020;
int a[n] = {0};
int main() {
int k;
cin >> k;
int m = k;
for (int i = 0; i < k; 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;
bool oh[100005];
int main() {
int n;
scanf("%d", &n);
int x;
int ctr = n;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
oh[x] = true;
while (ctr > 0 && oh[ctr]) {
printf("%d ", ctr);
ctr--;
}
printf("\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;
bool oh[100005];
int main() {
int n;
scanf("%d", &n);
int x;
int ctr = n;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
oh[x] = true;
while (ctr > 0 && oh[ctr]) {
printf("%d ", ctr);
ctr--;
}
printf("\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
bool used[100005];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int u = n;
for (int i = 0; i < n; i++) {
if (a[i] == u) {
used[a[i]] = true;
int temp = a[i];
while (used[temp] == true) {
cout << temp;
if (used[temp - 1] == true) {
cout << ' ';
}
temp--;
}
u = temp;
if (u) cout << endl;
} else {
used[a[i]] = true;
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;
int a[100005];
bool used[100005];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int u = n;
for (int i = 0; i < n; i++) {
if (a[i] == u) {
used[a[i]] = true;
int temp = a[i];
while (used[temp] == true) {
cout << temp;
if (used[temp - 1] == true) {
cout << ' ';
}
temp--;
}
u = temp;
if (u) cout << endl;
} else {
used[a[i]] = true;
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100009];
bool mark[100009];
memset(mark, false, sizeof(mark));
cin >> n;
int c = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mark[a[i]] = true;
while (mark[c]) {
cout << c << ' ';
c--;
}
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, a[100009];
bool mark[100009];
memset(mark, false, sizeof(mark));
cin >> n;
int c = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mark[a[i]] = true;
while (mark[c]) {
cout << c << ' ';
c--;
}
cout << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[100001] = {0};
int com[100001] = {0};
int main() {
int n, end;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
com[arr[i]] = i;
}
end = n;
for (int i = 1; i <= n; i++) {
if (arr[i] == end) {
while (com[end] <= i && end >= 1) {
cout << end << " ";
end--;
}
}
cout << endl;
}
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 arr[100001] = {0};
int com[100001] = {0};
int main() {
int n, end;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
com[arr[i]] = i;
}
end = n;
for (int i = 1; i <= n; i++) {
if (arr[i] == end) {
while (com[end] <= i && end >= 1) {
cout << end << " ";
end--;
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[200005];
int b[200005];
int n, max, i;
while (cin >> n) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (i = 0; i < n; i++) {
cin >> a[i];
}
max = n;
for (i = 0; i < n; i++) {
b[a[i]] = 1;
while (b[max]) {
cout << max << " ";
max--;
}
cout << endl;
}
}
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 a[200005];
int b[200005];
int n, max, i;
while (cin >> n) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (i = 0; i < n; i++) {
cin >> a[i];
}
max = n;
for (i = 0; i < n; i++) {
b[a[i]] = 1;
while (b[max]) {
cout << max << " ";
max--;
}
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
inline void normal(long long& a) {
a %= 1000000007;
(a < 0) && (a += 1000000007);
}
inline long long modMul(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long modPow(long long b, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1;
}
return r;
}
inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }
inline long long modDiv(long long a, long long b) {
return modMul(a, modInverse(b));
}
inline long long mod(long long a, long long m) {
if (a < 0)
return m - (abs(a) % m);
else if (a < m)
return a;
else
return (a % m);
}
template <class A, class B>
bool cmin(A& a, B b) {
return a > b && (a = b, true);
}
template <class A, class B>
bool cmax(A& a, B b) {
return a < b && (a = b, true);
}
mt19937_64 rang(
chrono::high_resolution_clock::now().time_since_epoch().count());
long long GCD(long long a, long long b) {
if (b == 0) {
return a;
}
return GCD(b, a % b);
}
long long Reveser(long long n) {
long long x = n, d, r = 0;
while (x > 0) {
d = x % 10;
r = r * 10 + d;
x /= 10;
}
return r;
}
void solve() {
int n;
cin >> n;
int check = n;
priority_queue<int> q;
while (n--) {
int x;
cin >> x;
q.push(x);
while (!q.empty() && q.top() == check) {
check--;
cout << q.top() << " ";
q.pop();
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
| ### 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;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
inline void normal(long long& a) {
a %= 1000000007;
(a < 0) && (a += 1000000007);
}
inline long long modMul(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long modPow(long long b, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1;
}
return r;
}
inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }
inline long long modDiv(long long a, long long b) {
return modMul(a, modInverse(b));
}
inline long long mod(long long a, long long m) {
if (a < 0)
return m - (abs(a) % m);
else if (a < m)
return a;
else
return (a % m);
}
template <class A, class B>
bool cmin(A& a, B b) {
return a > b && (a = b, true);
}
template <class A, class B>
bool cmax(A& a, B b) {
return a < b && (a = b, true);
}
mt19937_64 rang(
chrono::high_resolution_clock::now().time_since_epoch().count());
long long GCD(long long a, long long b) {
if (b == 0) {
return a;
}
return GCD(b, a % b);
}
long long Reveser(long long n) {
long long x = n, d, r = 0;
while (x > 0) {
d = x % 10;
r = r * 10 + d;
x /= 10;
}
return r;
}
void solve() {
int n;
cin >> n;
int check = n;
priority_queue<int> q;
while (n--) {
int x;
cin >> x;
q.push(x);
while (!q.empty() && q.top() == check) {
check--;
cout << q.top() << " ";
q.pop();
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
void printnl(int x) {
for (int i = 0; i < x; i++) cout << "\n";
}
int main() {
int n;
cin >> n;
int x[n], tmp;
for (int i = 0; i < n; i++) {
cin >> tmp;
x[tmp - 1] = i;
}
tmp = x[n - 1];
printnl(x[n - 1]);
cout << n << " ";
for (int i = n - 2; i >= 0; i--) {
if (x[i + 1] > x[i]) {
cout << i + 1 << " ";
} else {
printnl(x[i] - tmp);
if (tmp < x[i]) tmp = x[i];
cout << i + 1 << " ";
}
}
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;
void printnl(int x) {
for (int i = 0; i < x; i++) cout << "\n";
}
int main() {
int n;
cin >> n;
int x[n], tmp;
for (int i = 0; i < n; i++) {
cin >> tmp;
x[tmp - 1] = i;
}
tmp = x[n - 1];
printnl(x[n - 1]);
cout << n << " ";
for (int i = n - 2; i >= 0; i--) {
if (x[i + 1] > x[i]) {
cout << i + 1 << " ";
} else {
printnl(x[i] - tmp);
if (tmp < x[i]) tmp = x[i];
cout << i + 1 << " ";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
int main() {
long n;
scanf("%ld", &n);
std::vector<bool> a(n + 1);
long next = n;
for (long p = 0; p < n; p++) {
long x;
scanf("%ld", &x);
a[x] = 1;
while (a[next] == 1) {
printf("%ld ", next);
--next;
}
puts("");
}
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>
int main() {
long n;
scanf("%ld", &n);
std::vector<bool> a(n + 1);
long next = n;
for (long p = 0; p < n; p++) {
long x;
scanf("%ld", &x);
a[x] = 1;
while (a[next] == 1) {
printf("%ld ", next);
--next;
}
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int n;
cin >> n;
vector<int> arr1(n + 1);
bool scan = true;
int key = n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
arr1[x]++;
if (x == key)
while (arr1[key]) {
cout << key << " ";
key--;
}
cout << "\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 main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int n;
cin >> n;
vector<int> arr1(n + 1);
bool scan = true;
int key = n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
arr1[x]++;
if (x == key)
while (arr1[key]) {
cout << key << " ";
key--;
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n + 1];
vector<int> arr1(n + 1);
bool scan = true;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int key = n;
for (int i = 1; i <= n; i++) {
arr1[arr[i]]++;
if (arr[i] == key)
while (arr1[key]) {
cout << key << " ";
key--;
}
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;
cin >> n;
int arr[n + 1];
vector<int> arr1(n + 1);
bool scan = true;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int key = n;
for (int i = 1; i <= n; i++) {
arr1[arr[i]]++;
if (arr[i] == key)
while (arr1[key]) {
cout << key << " ";
key--;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 1e6 + 10, inf = 2e9 + 10, mod = 1e9 + 7;
const long long INF = 1e18 + 10;
int a[N];
int flag[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int en = n;
for (int i = 1; i <= n; i++) {
flag[a[i]] = 1;
while (flag[en] == 1) {
printf("%d ", en);
en--;
}
printf("\n");
}
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 = 1e5 + 10, M = 1e6 + 10, inf = 2e9 + 10, mod = 1e9 + 7;
const long long INF = 1e18 + 10;
int a[N];
int flag[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int en = n;
for (int i = 1; i <= n; i++) {
flag[a[i]] = 1;
while (flag[en] == 1) {
printf("%d ", en);
en--;
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
int a[100005], b[100005];
int main() {
int n, i, j, p;
scanf("%d", &n);
j = n;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (i = 1; j != 0; i++) {
p = a[i];
if ((b[j] == 0 && j == a[i]) || b[j] != 0) {
if (b[j] != 0) i--;
printf("%d ", j);
j--;
if (b[j] == 0) printf("\n");
} else {
printf("\n");
b[a[i]] = a[i];
}
}
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>
int a[100005], b[100005];
int main() {
int n, i, j, p;
scanf("%d", &n);
j = n;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (i = 1; j != 0; i++) {
p = a[i];
if ((b[j] == 0 && j == a[i]) || b[j] != 0) {
if (b[j] != 0) i--;
printf("%d ", j);
j--;
if (b[j] == 0) printf("\n");
} else {
printf("\n");
b[a[i]] = a[i];
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
priority_queue<int> pq;
int n;
cin >> n;
int maxm = n;
int snacks;
for (int i = 0; i < n; i++) {
cin >> snacks;
if (snacks >= maxm) {
cout << snacks << " ";
maxm--;
while (!pq.empty()) {
if (pq.top() >= maxm) {
cout << pq.top() << " ";
pq.pop();
} else
break;
maxm--;
}
cout << "\n";
} else {
cout << "\n";
pq.push(snacks);
}
}
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() {
priority_queue<int> pq;
int n;
cin >> n;
int maxm = n;
int snacks;
for (int i = 0; i < n; i++) {
cin >> snacks;
if (snacks >= maxm) {
cout << snacks << " ";
maxm--;
while (!pq.empty()) {
if (pq.top() >= maxm) {
cout << pq.top() << " ";
pq.pop();
} else
break;
maxm--;
}
cout << "\n";
} else {
cout << "\n";
pq.push(snacks);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int n, i, k, l, m, x;
vector<long int> a;
set<long int> b;
set<long int>::iterator it2, it1;
vector<long int>::iterator it;
cin >> n;
m = n;
for (i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
for (it = a.begin(); it != a.end(); it++) {
if ((*it) == m) {
b.insert((*it));
for (it1 = b.end(); it1 != b.begin();) {
if (it1 == b.end()) {
it1--;
continue;
} else if ((*it1) == m) {
cout << (*it1) << ' ';
it2 = it1;
it1--;
b.erase(*it2);
--m;
} else {
break;
}
}
if (it1 == b.begin()) {
if ((*it1) == m) {
cout << (*it1) << ' ';
it2 = it1;
it1--;
b.erase(*it2);
--m;
}
}
cout << '\n';
} else {
b.insert((*it));
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>
using namespace std;
int main() {
long int n, i, k, l, m, x;
vector<long int> a;
set<long int> b;
set<long int>::iterator it2, it1;
vector<long int>::iterator it;
cin >> n;
m = n;
for (i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
for (it = a.begin(); it != a.end(); it++) {
if ((*it) == m) {
b.insert((*it));
for (it1 = b.end(); it1 != b.begin();) {
if (it1 == b.end()) {
it1--;
continue;
} else if ((*it1) == m) {
cout << (*it1) << ' ';
it2 = it1;
it1--;
b.erase(*it2);
--m;
} else {
break;
}
}
if (it1 == b.begin()) {
if ((*it1) == m) {
cout << (*it1) << ' ';
it2 = it1;
it1--;
b.erase(*it2);
--m;
}
}
cout << '\n';
} else {
b.insert((*it));
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);
;
int n;
cin >> n;
int arr[n + 1];
vector<int> arr1(n + 1);
bool scan = true;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int key = n;
for (int i = 1; i <= n; i++) {
if (arr[i] != key) {
cout << "\n";
arr1[arr[i]]++;
} else {
cout << arr[i] << " ";
key--;
while (key != 0) {
if (arr1[key] != 0) {
cout << key-- << " ";
} else
break;
}
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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int n;
cin >> n;
int arr[n + 1];
vector<int> arr1(n + 1);
bool scan = true;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int key = n;
for (int i = 1; i <= n; i++) {
if (arr[i] != key) {
cout << "\n";
arr1[arr[i]]++;
} else {
cout << arr[i] << " ";
key--;
while (key != 0) {
if (arr1[key] != 0) {
cout << key-- << " ";
} else
break;
}
cout << "\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> q;
int n, f, c;
int main() {
scanf("%d", &n);
f = n;
for (int i = 0; i < n; i++) {
scanf("%d", &c);
q.push(c);
while (q.top() == f) {
printf("%d ", f);
q.pop();
f--;
}
if (f != 0) printf("\n");
}
}
| ### 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;
priority_queue<int> q;
int n, f, c;
int main() {
scanf("%d", &n);
f = n;
for (int i = 0; i < n; i++) {
scanf("%d", &c);
q.push(c);
while (q.top() == f) {
printf("%d ", f);
q.pop();
f--;
}
if (f != 0) printf("\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int exp(long long int t, long long int x) {
if (x == 0) return 1;
if (x == 1) return t;
if (x % 2 == 1) return (t * exp((t * t) % mod, x / 2)) % mod;
if (x % 2 == 0) return exp((t * t) % mod, x / 2);
}
long long int gcd(long long int x, long long int y) {
return x % y == 0 ? y : gcd(y, x % y);
}
long long int lcm(long long int x, long long int y) {
return x * (y / gcd(x, y));
}
long long int bsum(long long int u, long long int b) {
return u < b ? u : bsum(u / b, b) + u % b;
}
long long int prival(long long int u, long long int p) {
int cn = 0;
while (u % p == 0) {
cn++;
u = u / p;
}
return cn;
}
bool isprime(long long int x) {
for (long long int i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
void YAY(void) { cout << "YES" << endl; }
void NAY(void) { cout << "NO" << endl; }
int n;
int a[111111];
int cur;
int main(void) {
cin >> n;
cur = n;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
a[x] = 1;
while (a[cur]) {
printf("%d ", cur);
cur--;
}
printf("\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 int mod = 1e9 + 7;
long long int exp(long long int t, long long int x) {
if (x == 0) return 1;
if (x == 1) return t;
if (x % 2 == 1) return (t * exp((t * t) % mod, x / 2)) % mod;
if (x % 2 == 0) return exp((t * t) % mod, x / 2);
}
long long int gcd(long long int x, long long int y) {
return x % y == 0 ? y : gcd(y, x % y);
}
long long int lcm(long long int x, long long int y) {
return x * (y / gcd(x, y));
}
long long int bsum(long long int u, long long int b) {
return u < b ? u : bsum(u / b, b) + u % b;
}
long long int prival(long long int u, long long int p) {
int cn = 0;
while (u % p == 0) {
cn++;
u = u / p;
}
return cn;
}
bool isprime(long long int x) {
for (long long int i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
void YAY(void) { cout << "YES" << endl; }
void NAY(void) { cout << "NO" << endl; }
int n;
int a[111111];
int cur;
int main(void) {
cin >> n;
cur = n;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
a[x] = 1;
while (a[cur]) {
printf("%d ", cur);
cur--;
}
printf("\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
set<int> sizes;
cin >> n;
int placeholder;
while (n > 0) {
cin >> placeholder;
sizes.insert(placeholder);
while (sizes.find(n) != sizes.end()) {
cout << n << " ";
n--;
}
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() {
int n;
set<int> sizes;
cin >> n;
int placeholder;
while (n > 0) {
cin >> placeholder;
sizes.insert(placeholder);
while (sizes.find(n) != sizes.end()) {
cout << n << " ";
n--;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> d(n);
int i = n - 1;
for (int j = 0; j < n; j++) {
int val;
cin >> val;
d[val - 1] = val;
while (i >= 0 && d[i] != 0) {
cout << d[i] << " ";
i--;
}
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;
cin >> n;
vector<int> d(n);
int i = n - 1;
for (int j = 0; j < n; j++) {
int val;
cin >> val;
d[val - 1] = val;
while (i >= 0 && d[i] != 0) {
cout << d[i] << " ";
i--;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int arr[N];
int main() {
int n;
memset(arr, 0, sizeof arr);
cin >> n;
int maxx = n;
int num;
for (int i = 0; i < n; i++) {
cin >> num;
arr[num] = -1;
while (arr[maxx] == -1) {
cout << maxx << " ";
maxx -= 1;
}
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 = 1e5 + 5;
int arr[N];
int main() {
int n;
memset(arr, 0, sizeof arr);
cin >> n;
int maxx = n;
int num;
for (int i = 0; i < n; i++) {
cin >> num;
arr[num] = -1;
while (arr[maxx] == -1) {
cout << maxx << " ";
maxx -= 1;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int a[100009], dict[1000009];
int main() {
int n, i, j, mark;
cin >> n;
mark = n;
for (i = 0; i < n; i++) {
cin >> a[i];
dict[a[i]] = 1;
if (a[i] == mark) {
for (j = a[i]; j >= 1; j--) {
if (dict[j])
cout << j << " ";
else
break;
}
mark = j;
cout << endl;
} else
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 a[100009], dict[1000009];
int main() {
int n, i, j, mark;
cin >> n;
mark = n;
for (i = 0; i < n; i++) {
cin >> a[i];
dict[a[i]] = 1;
if (a[i] == mark) {
for (j = a[i]; j >= 1; j--) {
if (dict[j])
cout << j << " ";
else
break;
}
mark = j;
cout << endl;
} else
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int mx = 0;
vector<int> q(n), q1;
for (auto &i : q) {
cin >> i;
q1.push_back(i);
}
for (auto &i : q)
if (i > mx) mx = i;
vector<bool> used, used1;
used.resize(n + 1);
used1.resize(n + 1);
int f;
for (int i = 0; i < n; ++i) {
used[q1[i]] = 1;
if (q1[i] == mx) {
f = q1[i];
while (used[f] && f > 0) {
cout << f << " ";
used1[f] = 1;
--f;
}
} else {
if (used1[q1[i] + 1]) {
f = q1[i];
while (used[f] && f > 0) {
cout << f << " ";
used1[f] = 1;
--f;
}
}
}
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 main() {
int n;
cin >> n;
int mx = 0;
vector<int> q(n), q1;
for (auto &i : q) {
cin >> i;
q1.push_back(i);
}
for (auto &i : q)
if (i > mx) mx = i;
vector<bool> used, used1;
used.resize(n + 1);
used1.resize(n + 1);
int f;
for (int i = 0; i < n; ++i) {
used[q1[i]] = 1;
if (q1[i] == mx) {
f = q1[i];
while (used[f] && f > 0) {
cout << f << " ";
used1[f] = 1;
--f;
}
} else {
if (used1[q1[i] + 1]) {
f = q1[i];
while (used[f] && f > 0) {
cout << f << " ";
used1[f] = 1;
--f;
}
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
int arr[N];
int all[N];
int cur;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", arr + i);
all[i] = 0;
}
cur = n;
for (int i = 1; i <= n; ++i) {
all[arr[i]] = 1;
while (all[cur]) {
printf("%d ", cur);
--cur;
}
printf("\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;
const int N = 1e5 + 5;
int n;
int arr[N];
int all[N];
int cur;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", arr + i);
all[i] = 0;
}
cur = n;
for (int i = 1; i <= n; ++i) {
all[arr[i]] = 1;
while (all[cur]) {
printf("%d ", cur);
--cur;
}
printf("\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
int t;
int vis[200000];
int main() {
cin >> n;
int tn = n;
int p = n;
while (n--) {
cin >> t;
vis[t] = 1;
for (int i = 0; vis[p]; i = 1) {
if (i) putchar(' ');
printf("%d", p);
p--;
}
puts("");
}
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 n;
int t;
int vis[200000];
int main() {
cin >> n;
int tn = n;
int p = n;
while (n--) {
cin >> t;
vis[t] = 1;
for (int i = 0; vis[p]; i = 1) {
if (i) putchar(' ');
printf("%d", p);
p--;
}
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int vis[100005] = {0};
int main() {
int n, x;
cin >> n;
int p = n;
for (int i = 1; i <= n; i++) {
cin >> x;
vis[x] = 1;
if (x == p) {
for (int j = x; j >= 0 && vis[j]; j--) {
if (vis[j] == 1) {
cout << j << " ";
p--;
}
}
}
cout << "\n";
}
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 vis[100005] = {0};
int main() {
int n, x;
cin >> n;
int p = n;
for (int i = 1; i <= n; i++) {
cin >> x;
vis[x] = 1;
if (x == p) {
for (int j = x; j >= 0 && vis[j]; j--) {
if (vis[j] == 1) {
cout << j << " ";
p--;
}
}
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
t = 1;
while (t--) {
long long n, i, j, x;
set<long long> st;
cin >> n;
long long arr[n];
long long search = n;
for (i = 0; i < n; i++) {
cin >> x;
if (x == search) {
cout << x << " ";
for (j = x - 1; j > 0; j--) {
if (st.count(j)) {
cout << j << " ";
st.erase(j);
} else {
search = j;
break;
}
}
cout << '\n';
} else {
st.insert(x);
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() {
long long t;
t = 1;
while (t--) {
long long n, i, j, x;
set<long long> st;
cin >> n;
long long arr[n];
long long search = n;
for (i = 0; i < n; i++) {
cin >> x;
if (x == search) {
cout << x << " ";
for (j = x - 1; j > 0; j--) {
if (st.count(j)) {
cout << j << " ";
st.erase(j);
} else {
search = j;
break;
}
}
cout << '\n';
} else {
st.insert(x);
cout << '\n';
}
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
struct ele {
long long a, b;
};
bool comp(ele &a, ele &b) { return a.b > b.b; }
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void solve() {
long long n;
cin >> n;
vector<ele> a(n);
for (int i = 0; i < n; i++) {
ele x;
x.a = i;
cin >> x.b;
a[i] = x;
}
sort(a.begin(), a.end(), comp);
int print = 0, k = 0;
while (print < n and k < n) {
while (print < a[k].a) {
cout << endl;
print++;
}
cout << a[k].b << " ";
int j = k;
k++;
while (k < n and a[k].a < a[j].a) {
cout << a[k].b << " ";
k++;
}
cout << endl;
print++;
}
}
int main() {
long long t = 1;
for (long long i = 1; i <= t; i++) solve();
}
| ### 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;
struct ele {
long long a, b;
};
bool comp(ele &a, ele &b) { return a.b > b.b; }
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void solve() {
long long n;
cin >> n;
vector<ele> a(n);
for (int i = 0; i < n; i++) {
ele x;
x.a = i;
cin >> x.b;
a[i] = x;
}
sort(a.begin(), a.end(), comp);
int print = 0, k = 0;
while (print < n and k < n) {
while (print < a[k].a) {
cout << endl;
print++;
}
cout << a[k].b << " ";
int j = k;
k++;
while (k < n and a[k].a < a[j].a) {
cout << a[k].b << " ";
k++;
}
cout << endl;
print++;
}
}
int main() {
long long t = 1;
for (long long i = 1; i <= t; i++) solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
bool a[100001];
int st = n;
for (int i = 1; i <= n; i++) a[i] = false;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
a[k] = true;
while (a[st]) {
cout << st << " ";
st = st - 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;
cin >> n;
bool a[100001];
int st = n;
for (int i = 1; i <= n; i++) a[i] = false;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
a[k] = true;
while (a[st]) {
cout << st << " ";
st = st - 1;
}
cout << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n;
bool a[100001];
bool uyg(int x) {
if (x == n || a[x + 1] == 1) {
a[x] = 1;
return 1;
}
return 0;
}
int main() {
int q;
vector<int> v;
priority_queue<int> pq;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &q);
v.push_back(q);
}
for (int i = 0; i < n; i++) {
if (uyg(v[i])) {
printf("%d ", v[i]);
if (!pq.empty())
while (uyg(pq.top())) {
printf("%d ", pq.top());
pq.pop();
if (pq.empty()) break;
}
if (i != n - 1) printf("\n");
} else {
printf("\n");
pq.push(v[i]);
}
}
}
| ### 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 n;
bool a[100001];
bool uyg(int x) {
if (x == n || a[x + 1] == 1) {
a[x] = 1;
return 1;
}
return 0;
}
int main() {
int q;
vector<int> v;
priority_queue<int> pq;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &q);
v.push_back(q);
}
for (int i = 0; i < n; i++) {
if (uyg(v[i])) {
printf("%d ", v[i]);
if (!pq.empty())
while (uyg(pq.top())) {
printf("%d ", pq.top());
pq.pop();
if (pq.empty()) break;
}
if (i != n - 1) printf("\n");
} else {
printf("\n");
pq.push(v[i]);
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int N = int(1e5) + 10;
bool vis[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> v;
int last = n;
for (int i = (int)0; i < (int)n; i++) {
int x;
cin >> x;
vis[x] = 1;
if (x == last) {
while (vis[last]) {
cout << last << " ";
last--;
}
}
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;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int N = int(1e5) + 10;
bool vis[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> v;
int last = n;
for (int i = (int)0; i < (int)n; i++) {
int x;
cin >> x;
vis[x] = 1;
if (x == last) {
while (vis[last]) {
cout << last << " ";
last--;
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int arr[100100];
int vis[100100];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int ctr = n;
for (int i = 0; i < n; i++) {
if (arr[i] == ctr) {
vis[arr[i]] = 1;
while (true) {
if (vis[ctr] == 1) {
cout << ctr << " ";
ctr--;
} else {
break;
}
}
cout << endl;
} else {
cout << endl;
vis[arr[i]] = 1;
}
}
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 arr[100100];
int vis[100100];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int ctr = n;
for (int i = 0; i < n; i++) {
if (arr[i] == ctr) {
vis[arr[i]] = 1;
while (true) {
if (vis[ctr] == 1) {
cout << ctr << " ";
ctr--;
} else {
break;
}
}
cout << endl;
} else {
cout << endl;
vis[arr[i]] = 1;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, h, test;
priority_queue<int> PQ;
int main() {
scanf("%d", &n);
h = n;
for (int i = 0; i < n; i++) {
scanf("%d", &test);
PQ.push(test);
if (PQ.top() == h) {
while (PQ.top() == h) {
printf("%d ", PQ.top());
h--;
PQ.pop();
}
printf("\n");
} else {
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;
int n, h, test;
priority_queue<int> PQ;
int main() {
scanf("%d", &n);
h = n;
for (int i = 0; i < n; i++) {
scanf("%d", &test);
PQ.push(test);
if (PQ.top() == h) {
while (PQ.top() == h) {
printf("%d ", PQ.top());
h--;
PQ.pop();
}
printf("\n");
} else {
printf("\n");
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long pow(int a, int b, long long MOD) {
long long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y);
if (x > MOD) x %= MOD;
}
y = (y * y);
if (y > MOD) y %= MOD;
b /= 2;
}
return x;
}
long long InverseEuler(int n, long long MOD) { return pow(n, MOD - 2, MOD); }
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
int main() {
int n;
cin >> n;
int a[100002];
map<int, int> m;
for (int i = 0; i < n; i++) cin >> a[i];
int t = n;
for (int i = 0; i < n; i++) {
if (a[i] == t) {
cout << t << " ";
t--;
while (m[t] != 0) {
if (m[t] == 1) cout << t << " ";
t--;
}
cout << endl;
} else {
m[a[i]] = 1;
cout << endl;
}
}
}
| ### 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;
long long pow(int a, int b, long long MOD) {
long long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y);
if (x > MOD) x %= MOD;
}
y = (y * y);
if (y > MOD) y %= MOD;
b /= 2;
}
return x;
}
long long InverseEuler(int n, long long MOD) { return pow(n, MOD - 2, MOD); }
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
int main() {
int n;
cin >> n;
int a[100002];
map<int, int> m;
for (int i = 0; i < n; i++) cin >> a[i];
int t = n;
for (int i = 0; i < n; i++) {
if (a[i] == t) {
cout << t << " ";
t--;
while (m[t] != 0) {
if (m[t] == 1) cout << t << " ";
t--;
}
cout << endl;
} else {
m[a[i]] = 1;
cout << endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
map<long long, bool> mp;
long long n;
cin >> n;
long long k = n;
for (int i = 1; i <= n; i++) {
long long a;
cin >> a;
mp[a] = 1;
while (mp[k] == 1) {
cout << k << " ";
k--;
}
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;
int main() {
map<long long, bool> mp;
long long n;
cin >> n;
long long k = n;
for (int i = 1; i <= n; i++) {
long long a;
cin >> a;
mp[a] = 1;
while (mp[k] == 1) {
cout << k << " ";
k--;
}
cout << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n = 0, ordr = 0, tmpS = 0;
int main() {
cin >> n;
ordr = n;
std::vector<bool> temp(n, false);
std::vector<int> inp(n, 0);
for (int i = 0; i < n; i++) {
cin >> inp[i];
}
for (int i = 0; i < n; i++) {
if (inp[i] == ordr) {
cout << ordr << " ";
ordr--;
while (temp[ordr] == true) {
cout << ordr << " ", ordr--;
}
} else {
temp[inp[i]] = true;
}
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 n = 0, ordr = 0, tmpS = 0;
int main() {
cin >> n;
ordr = n;
std::vector<bool> temp(n, false);
std::vector<int> inp(n, 0);
for (int i = 0; i < n; i++) {
cin >> inp[i];
}
for (int i = 0; i < n; i++) {
if (inp[i] == ordr) {
cout << ordr << " ";
ordr--;
while (temp[ordr] == true) {
cout << ordr << " ", ordr--;
}
} else {
temp[inp[i]] = true;
}
cout << "\n";
}
return 0;
}
``` |
#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 + 1 == 1)) {
m--;
cout << qi.top() << " ";
qi.pop();
}
if (i < t - 1) cout << "\n";
} else {
cout << "\n";
qi.push(v[i]);
}
}
cerr << "\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
<< 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;
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 + 1 == 1)) {
m--;
cout << qi.top() << " ";
qi.pop();
}
if (i < t - 1) cout << "\n";
} else {
cout << "\n";
qi.push(v[i]);
}
}
cerr << "\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
<< endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, k;
cin >> n;
int arr[n];
map<int, int> m;
int x = n;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
for (i = 0; i < n; i++) {
if (arr[i] == x) {
cout << x << " ";
x--;
while (m[x]) {
cout << x << " ";
x--;
}
}
m[arr[i]] = 1;
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 n, i, j, k;
cin >> n;
int arr[n];
map<int, int> m;
int x = n;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
for (i = 0; i < n; i++) {
if (arr[i] == x) {
cout << x << " ";
x--;
while (m[x]) {
cout << x << " ";
x--;
}
}
m[arr[i]] = 1;
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long unsigned int countDivisors(long long unsigned int n) {
long long unsigned int cnt = 0;
for (long long unsigned int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
++cnt;
else
cnt += 2;
}
}
return cnt;
}
int Search(string pat, string txt) {
int n = txt.size();
int m = pat.size();
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (txt[i + j] != pat[j]) {
break;
}
}
if (j == m) {
return i;
}
}
return -1;
}
int findFirst(const std::vector<long long int> &array, int key, int low,
int high) {
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (key == array[mid]) {
ans = mid;
high = mid - 1;
} else if (key > array[mid])
low = mid + 1;
else
high = mid - 1;
}
return ans;
}
int findLast(const std::vector<long long int> &array, int key, int low,
int high) {
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (key == array[mid]) {
ans = mid;
low = mid + 1;
} else if (key > array[mid])
low = mid + 1;
else
high = mid - 1;
}
return ans;
}
int d, x, y;
void extendedEuclid(int a, int b) {
if (b == 0) {
d = 1;
x = 1;
y = 0;
} else {
extendedEuclid(b, a % b);
int temp = x;
x = y;
y = temp - (a / b) * y;
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
for (int tc = 0; tc < t; tc++) {
int n;
cin >> n;
vector<int> arr(n), brr(n, 0), v;
for (int i1 = 0; i1 < n; i1++) cin >> arr[i1];
int pos = 0;
for (int i = 0; i < n; i++) {
int indx = n - arr[i];
brr[indx] = 1;
while (brr[pos] == 1 && pos < n) {
cout << (n - pos) << " ";
pos++;
}
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;
long long unsigned int countDivisors(long long unsigned int n) {
long long unsigned int cnt = 0;
for (long long unsigned int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
++cnt;
else
cnt += 2;
}
}
return cnt;
}
int Search(string pat, string txt) {
int n = txt.size();
int m = pat.size();
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (txt[i + j] != pat[j]) {
break;
}
}
if (j == m) {
return i;
}
}
return -1;
}
int findFirst(const std::vector<long long int> &array, int key, int low,
int high) {
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (key == array[mid]) {
ans = mid;
high = mid - 1;
} else if (key > array[mid])
low = mid + 1;
else
high = mid - 1;
}
return ans;
}
int findLast(const std::vector<long long int> &array, int key, int low,
int high) {
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (key == array[mid]) {
ans = mid;
low = mid + 1;
} else if (key > array[mid])
low = mid + 1;
else
high = mid - 1;
}
return ans;
}
int d, x, y;
void extendedEuclid(int a, int b) {
if (b == 0) {
d = 1;
x = 1;
y = 0;
} else {
extendedEuclid(b, a % b);
int temp = x;
x = y;
y = temp - (a / b) * y;
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
for (int tc = 0; tc < t; tc++) {
int n;
cin >> n;
vector<int> arr(n), brr(n, 0), v;
for (int i1 = 0; i1 < n; i1++) cin >> arr[i1];
int pos = 0;
for (int i = 0; i < n; i++) {
int indx = n - arr[i];
brr[indx] = 1;
while (brr[pos] == 1 && pos < n) {
cout << (n - pos) << " ";
pos++;
}
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void solve() {
int n;
cin >> n;
priority_queue<int> q;
int curr = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
q.push(x);
if (x == curr) {
while (!q.empty() && q.top() == curr) {
curr--;
cout << q.top() << ' ';
q.pop();
}
cout << '\n';
} else {
cout << '\n';
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
int t = 1;
while (t--) {
solve();
}
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 dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void solve() {
int n;
cin >> n;
priority_queue<int> q;
int curr = n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
q.push(x);
if (x == curr) {
while (!q.empty() && q.top() == curr) {
curr--;
cout << q.top() << ' ';
q.pop();
}
cout << '\n';
} else {
cout << '\n';
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
int t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n + 1];
vector<int> arr1(n + 1);
bool scan = true;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int key = n;
for (int i = 1; i <= n; i++) {
if (arr[i] != key) {
cout << endl;
arr1[arr[i]]++;
} else {
cout << arr[i] << " ";
key--;
while (key != 0) {
if (arr1[key] != 0) {
cout << key-- << " ";
} else
break;
}
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;
int main() {
int n;
cin >> n;
int arr[n + 1];
vector<int> arr1(n + 1);
bool scan = true;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int key = n;
for (int i = 1; i <= n; i++) {
if (arr[i] != key) {
cout << endl;
arr1[arr[i]]++;
} else {
cout << arr[i] << " ";
key--;
while (key != 0) {
if (arr1[key] != 0) {
cout << key-- << " ";
} else
break;
}
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
int n, maxx, vis[100005], w, a[100005], c[100005];
int main() {
scanf("%d", &n);
maxx = n;
w = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (maxx == a[i]) {
c[w] = a[i];
w++;
for (int j = a[i] - 1; j > 0; j--) {
if (vis[j] == 1) {
c[w] = j;
w++;
vis[j] = 0;
} else
break;
}
printf("%d", c[0]);
for (int j = 1; j < w; j++) {
printf(" %d", c[j]);
}
printf("\n");
maxx -= w;
w = 0;
} else {
vis[a[i]] = 1;
w = 0;
printf("\n");
}
}
}
| ### 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>
int n, maxx, vis[100005], w, a[100005], c[100005];
int main() {
scanf("%d", &n);
maxx = n;
w = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (maxx == a[i]) {
c[w] = a[i];
w++;
for (int j = a[i] - 1; j > 0; j--) {
if (vis[j] == 1) {
c[w] = j;
w++;
vis[j] = 0;
} else
break;
}
printf("%d", c[0]);
for (int j = 1; j < w; j++) {
printf(" %d", c[j]);
}
printf("\n");
maxx -= w;
w = 0;
} else {
vis[a[i]] = 1;
w = 0;
printf("\n");
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool bysec(const pair<int, int>& a, const pair<int, int>& b) {
return (a.second > b.second);
}
long long ans = 0;
void solve() {
int n, x, k;
cin >> n;
k = n;
priority_queue<int> q;
for (int i = 0; i < n; ++i) {
cin >> x;
q.push(x);
while (q.size() && q.top() == k) {
cout << q.top() << ' ';
--k;
q.pop();
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.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;
bool bysec(const pair<int, int>& a, const pair<int, int>& b) {
return (a.second > b.second);
}
long long ans = 0;
void solve() {
int n, x, k;
cin >> n;
k = n;
priority_queue<int> q;
for (int i = 0; i < n; ++i) {
cin >> x;
q.push(x);
while (q.size() && q.top() == k) {
cout << q.top() << ' ';
--k;
q.pop();
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int t[n], x = n;
bool t2[n];
for (int i = 0; i < n; i++) t2[i] = false;
for (int i = 0; i < n; i++) {
cin >> t[i];
t2[t[i] - 1] = true;
while (t2[x - 1] == true) {
cout << x << " ";
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;
int main() {
int n;
cin >> n;
int t[n], x = n;
bool t2[n];
for (int i = 0; i < n; i++) t2[i] = false;
for (int i = 0; i < n; i++) {
cin >> t[i];
t2[t[i] - 1] = true;
while (t2[x - 1] == true) {
cout << x << " ";
x--;
}
cout << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long NLINF = 0xf7f7f7f7f7f7f7f7;
const int INF = 0x3f3f3f3f, NINF = 0xf7f7f7f7;
const int MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;
const int N = 1e5 + 10;
int n, a[N], mex = -1;
int main() {
scanf("%d", &n);
int cur = n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
set<int> acc;
for (int i = 1; i <= n; i++) {
bool ok = 0;
acc.insert(a[i]);
while (acc.find(cur) != acc.end()) ok = 1, printf("%d ", cur--);
printf("\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;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long NLINF = 0xf7f7f7f7f7f7f7f7;
const int INF = 0x3f3f3f3f, NINF = 0xf7f7f7f7;
const int MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;
const int N = 1e5 + 10;
int n, a[N], mex = -1;
int main() {
scanf("%d", &n);
int cur = n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
set<int> acc;
for (int i = 1; i <= n; i++) {
bool ok = 0;
acc.insert(a[i]);
while (acc.find(cur) != acc.end()) ok = 1, printf("%d ", cur--);
printf("\n");
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int ra() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
priority_queue<int> q;
int main() {
int n = ra(), t = n;
for (int i = 1; i <= n; i++) {
int x = ra();
q.push(x);
while (!q.empty()) {
int y = q.top();
if (y == t) {
printf("%d ", y);
q.pop();
t--;
} else
break;
}
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;
inline int ra() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
priority_queue<int> q;
int main() {
int n = ra(), t = n;
for (int i = 1; i <= n; i++) {
int x = ra();
q.push(x);
while (!q.empty()) {
int y = q.top();
if (y == t) {
printf("%d ", y);
q.pop();
t--;
} else
break;
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const long long MAX_N = 1e5 + 10;
const long long mod = 1e9 + 7;
using namespace std;
bool used[MAX_N];
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
fill(used + 1, used + n + 1, false);
int mx = n;
int no;
for (int i = 1; i <= n; i++) {
cin >> no;
used[no] = true;
if (used[mx]) {
cout << no;
mx--;
while (used[mx]) {
cout << " " << mx;
mx--;
}
}
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>
const int INF = 0x3f3f3f3f;
const long long MAX_N = 1e5 + 10;
const long long mod = 1e9 + 7;
using namespace std;
bool used[MAX_N];
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
fill(used + 1, used + n + 1, false);
int mx = n;
int no;
for (int i = 1; i <= n; i++) {
cin >> no;
used[no] = true;
if (used[mx]) {
cout << no;
mx--;
while (used[mx]) {
cout << " " << mx;
mx--;
}
}
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
bool used[100010] = {0};
int a[100010] = {0};
int m = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
used[a[i]] = 1;
if (a[i] == m) {
while (used[m]) {
cout << m << " ";
m--;
}
cout << endl;
} else {
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 main() {
int n;
cin >> n;
bool used[100010] = {0};
int a[100010] = {0};
int m = n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
used[a[i]] = 1;
if (a[i] == m) {
while (used[m]) {
cout << m << " ";
m--;
}
cout << endl;
} else {
cout << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int* nums = new int[n];
int* occured = new int[n]{0};
for (int i = 1; i <= n; ++i) cin >> nums[i];
int x = n;
for (int i = 1; i <= n; ++i) {
if (nums[i] < x) {
cout << " " << endl;
occured[nums[i]] = 1;
} else {
occured[nums[i]] = 1;
for (int j = nums[i]; j > 0; j--) {
if (occured[j] == 1) {
cout << j << " ";
x--;
} else {
cout << endl;
break;
}
}
}
}
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* nums = new int[n];
int* occured = new int[n]{0};
for (int i = 1; i <= n; ++i) cin >> nums[i];
int x = n;
for (int i = 1; i <= n; ++i) {
if (nums[i] < x) {
cout << " " << endl;
occured[nums[i]] = 1;
} else {
occured[nums[i]] = 1;
for (int j = nums[i]; j > 0; j--) {
if (occured[j] == 1) {
cout << j << " ";
x--;
} else {
cout << endl;
break;
}
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, i, j, k, l;
cin >> n;
int b[n + 1];
int a[n];
memset(b, 0, sizeof b);
for (i = 0; i < n; i++) {
cin >> a[i];
}
k = n;
for (i = 0; i < n; i++) {
b[a[i]] = 1;
if (a[i] == k) {
cout << a[i] << " ";
b[a[i]] = 1;
k--;
while (1) {
if (b[k] == 0) {
cout << endl;
break;
}
if (b[k] == 1) {
cout << k << " ";
k--;
}
}
} else
cout << endl;
}
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(0);
cin.tie(0);
int n, i, j, k, l;
cin >> n;
int b[n + 1];
int a[n];
memset(b, 0, sizeof b);
for (i = 0; i < n; i++) {
cin >> a[i];
}
k = n;
for (i = 0; i < n; i++) {
b[a[i]] = 1;
if (a[i] == k) {
cout << a[i] << " ";
b[a[i]] = 1;
k--;
while (1) {
if (b[k] == 0) {
cout << endl;
break;
}
if (b[k] == 1) {
cout << k << " ";
k--;
}
}
} else
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, j, m;
cin >> n;
m = n;
priority_queue<int> pq;
for (int i = 0; i < n; i++) {
cin >> j;
pq.push(j);
while (!pq.empty() && pq.top() == m) {
cout << m << " ";
m--;
pq.pop();
}
if (i != n - 1) cout << '\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 main() {
int n, j, m;
cin >> n;
m = n;
priority_queue<int> pq;
for (int i = 0; i < n; i++) {
cin >> j;
pq.push(j);
while (!pq.empty() && pq.top() == m) {
cout << m << " ";
m--;
pq.pop();
}
if (i != n - 1) cout << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
bool bs[MAX_N + 1];
int main() {
int n;
scanf("%d", &n);
int pt = n;
for (int i = 0; i < n; i++) {
int ai;
scanf("%d", &ai);
bs[ai] = true;
for (bool cont = false; pt > 0 && bs[pt]; pt--, cont = true) {
if (cont) putchar(' ');
printf("%d", pt);
}
putchar('\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 MAX_N = 100000;
bool bs[MAX_N + 1];
int main() {
int n;
scanf("%d", &n);
int pt = n;
for (int i = 0; i < n; i++) {
int ai;
scanf("%d", &ai);
bs[ai] = true;
for (bool cont = false; pt > 0 && bs[pt]; pt--, cont = true) {
if (cont) putchar(' ');
printf("%d", pt);
}
putchar('\n');
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int n, a[100001], x, t;
int main() {
scanf("%d", &n);
t = n;
while (n--) {
scanf("%d", &x);
a[x] = 1;
while (a[t]) printf("%d ", t--);
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;
int n, a[100001], x, t;
int main() {
scanf("%d", &n);
t = n;
while (n--) {
scanf("%d", &x);
a[x] = 1;
while (a[t]) printf("%d ", t--);
puts("");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int l = n;
map<int, int> cks;
while (n--) {
int x;
scanf("%d", &x);
cks[x]++;
while (cks.find(l) != cks.end()) {
printf("%d", l);
l -= 1;
if (cks.find(l) != cks.end()) {
printf(" ");
}
}
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 main() {
int n;
scanf("%d", &n);
int l = n;
map<int, int> cks;
while (n--) {
int x;
scanf("%d", &x);
cks[x]++;
while (cks.find(l) != cks.end()) {
printf("%d", l);
l -= 1;
if (cks.find(l) != cks.end()) {
printf(" ");
}
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
const int inf_int = 2e9;
const long long inf_ll = 2e18;
const int maxn = 1e5 + 10;
using namespace std;
long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); }
long long qpow(long long p, long long q) {
long long f = 1;
while (q) {
if (q & 1) f = f * p;
p = p * p;
q >>= 1;
}
return f;
}
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
inline long long Scan() {
char ch;
long long flag = 1, Num = 0;
while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
;
if (ch == '-')
flag = -1;
else if (ch >= '0' && ch <= '9')
Num = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
Num = Num * 10 + ch - '0';
}
return flag * Num;
}
int a[maxn];
int n;
set<int> s;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int top = n;
for (int i = 1; i <= n; i++) {
s.insert(a[i]);
while (1) {
if (s.find(top) != s.end()) {
printf("%d ", top);
top--;
} else {
break;
}
}
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>
const int inf_int = 2e9;
const long long inf_ll = 2e18;
const int maxn = 1e5 + 10;
using namespace std;
long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); }
long long qpow(long long p, long long q) {
long long f = 1;
while (q) {
if (q & 1) f = f * p;
p = p * p;
q >>= 1;
}
return f;
}
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
inline long long Scan() {
char ch;
long long flag = 1, Num = 0;
while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
;
if (ch == '-')
flag = -1;
else if (ch >= '0' && ch <= '9')
Num = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
Num = Num * 10 + ch - '0';
}
return flag * Num;
}
int a[maxn];
int n;
set<int> s;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int top = n;
for (int i = 1; i <= n; i++) {
s.insert(a[i]);
while (1) {
if (s.find(top) != s.end()) {
printf("%d ", top);
top--;
} else {
break;
}
}
printf("\n");
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool prime[100005];
vector<int> allprime;
void seive() {
memset(prime, true, sizeof(prime));
prime[0] = false;
prime[1] = false;
for (int i = 2; i * i <= 100005; i++) {
if (prime[i] == true) {
for (int j = i * i; j <= 100005; j += i) {
prime[j] = false;
}
}
}
for (int i = 0; i <= 100005; i++) {
if (prime[i]) {
allprime.push_back(i);
}
}
}
bool cmp(string s, string k) { return s.size() < k.size(); }
void solve() {
long long n;
cin >> n;
vector<int> np(n + 1, 0);
int p = n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
np[a] = 1;
while (np[p]) {
cout << p << " ";
p--;
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int t = 1;
while (t--) {
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;
bool prime[100005];
vector<int> allprime;
void seive() {
memset(prime, true, sizeof(prime));
prime[0] = false;
prime[1] = false;
for (int i = 2; i * i <= 100005; i++) {
if (prime[i] == true) {
for (int j = i * i; j <= 100005; j += i) {
prime[j] = false;
}
}
}
for (int i = 0; i <= 100005; i++) {
if (prime[i]) {
allprime.push_back(i);
}
}
}
bool cmp(string s, string k) { return s.size() < k.size(); }
void solve() {
long long n;
cin >> n;
vector<int> np(n + 1, 0);
int p = n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
np[a] = 1;
while (np[p]) {
cout << p << " ";
p--;
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int t = 1;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int top = n;
vector<int> v(n + 1, 0);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v[x] = 1;
while (top > 0 && v[top] > 0) {
cout << top << " ";
top--;
}
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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int top = n;
vector<int> v(n + 1, 0);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v[x] = 1;
while (top > 0 && v[top] > 0) {
cout << top << " ";
top--;
}
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
char s1[15][15], s2[15][15];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int idx[65][15][15];
int dp[15][15];
int id = 1;
struct Edge {
int u, v;
long long cap, flow;
Edge() {}
Edge(int u, int v, long long cap) : u(u), v(v), cap(cap), flow(0) {}
};
struct Dinic {
int N;
vector<Edge> E;
vector<vector<int>> g;
vector<int> d, pt;
Dinic(int N) : N(N), E(0), g(N), d(N), pt(N) {}
void AddEdge(int u, int v, long long cap) {
if (u != v) {
E.emplace_back(Edge(u, v, cap));
g[u].emplace_back(E.size() - 1);
E.emplace_back(Edge(v, u, 0));
g[v].emplace_back(E.size() - 1);
}
}
bool BFS(int S, int T) {
queue<int> q({S});
fill(d.begin(), d.end(), N + 1);
d[S] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == T) break;
for (int k : g[u]) {
Edge &e = E[k];
if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
d[e.v] = d[e.u] + 1;
q.emplace(e.v);
}
}
}
return d[T] != N + 1;
}
long long DFS(int u, int T, long long flow = -1) {
if (u == T || flow == 0) return flow;
for (int &i = pt[u]; i < g[u].size(); ++i) {
Edge &e = E[g[u][i]];
Edge &oe = E[g[u][i] ^ 1];
if (d[e.v] == d[e.u] + 1) {
long long amt = e.cap - e.flow;
if (flow != -1 && amt > flow) amt = flow;
if (long long pushed = DFS(e.v, T, amt)) {
e.flow += pushed;
oe.flow -= pushed;
return pushed;
}
}
}
return 0;
}
long long MaxFlow(int S, int T) {
long long total = 0;
while (BFS(S, T)) {
fill(pt.begin(), pt.end(), 0);
while (long long flow = DFS(S, T)) total += flow;
}
return total;
}
};
int main() {
memset(dp, -1, sizeof(dp));
int n, t;
scanf("%d%d", &n, &t);
for (int i = 1; i <= n; i++) {
scanf("%s", s1[i] + 1);
}
for (int i = 0; i <= t + 1; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
idx[i][j][k] = id++;
}
}
}
id++;
for (int i = 1; i <= n; i++) {
scanf("%s", s2[i] + 1);
}
queue<pair<int, int>> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Z') {
q.push(pair<int, int>(i, j));
dp[i][j] = 0;
}
}
}
while (!q.empty()) {
int uy = q.front().first, ux = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int vy = uy + dir[i][0], vx = ux + dir[i][1];
if (vy >= 1 && vy <= n && vx >= 1 && vx <= n && s1[vy][vx] != 'Y' &&
dp[vy][vx] == -1) {
dp[vy][vx] = dp[uy][ux] + 1;
q.push(pair<int, int>(vy, vx));
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dp[i][j] == -1) dp[i][j] = 1e9;
if (dp[i][j] > t) dp[i][j] = 1e9;
}
}
int source = 0, sink = id - 1;
Dinic dinic(id);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] >= '1' && s1[i][j] <= '9') {
dinic.AddEdge(source, idx[0][i][j], s1[i][j] - '0');
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;
for (int k = 0; k < 4; k++) {
int vy = i + dir[k][0], vx = j + dir[k][1];
if (vy >= 1 && vy <= n && vx >= 1 && vx <= n && s1[vy][vx] != 'Y' &&
s1[vy][vx] != 'Z') {
for (int x = 0; x < t; x++) {
if (dp[vy][vx] >= x + 1 && dp[i][j] > x) {
dinic.AddEdge(idx[x][i][j], idx[x + 1][vy][vx], 1000);
}
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;
for (int x = 0; x <= t; x++) {
if (s2[i][j] >= '1' && s2[i][j] <= '9') {
dinic.AddEdge(idx[x][i][j], idx[t + 1][i][j], s2[i][j] - '0');
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;
if (s2[i][j] >= '1' && s2[i][j] <= '9') {
dinic.AddEdge(idx[t + 1][i][j], sink, s2[i][j] - '0');
}
}
}
printf("%d\n", dinic.MaxFlow(source, sink));
}
| ### Prompt
Please create a solution in CPP to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char s1[15][15], s2[15][15];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int idx[65][15][15];
int dp[15][15];
int id = 1;
struct Edge {
int u, v;
long long cap, flow;
Edge() {}
Edge(int u, int v, long long cap) : u(u), v(v), cap(cap), flow(0) {}
};
struct Dinic {
int N;
vector<Edge> E;
vector<vector<int>> g;
vector<int> d, pt;
Dinic(int N) : N(N), E(0), g(N), d(N), pt(N) {}
void AddEdge(int u, int v, long long cap) {
if (u != v) {
E.emplace_back(Edge(u, v, cap));
g[u].emplace_back(E.size() - 1);
E.emplace_back(Edge(v, u, 0));
g[v].emplace_back(E.size() - 1);
}
}
bool BFS(int S, int T) {
queue<int> q({S});
fill(d.begin(), d.end(), N + 1);
d[S] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == T) break;
for (int k : g[u]) {
Edge &e = E[k];
if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
d[e.v] = d[e.u] + 1;
q.emplace(e.v);
}
}
}
return d[T] != N + 1;
}
long long DFS(int u, int T, long long flow = -1) {
if (u == T || flow == 0) return flow;
for (int &i = pt[u]; i < g[u].size(); ++i) {
Edge &e = E[g[u][i]];
Edge &oe = E[g[u][i] ^ 1];
if (d[e.v] == d[e.u] + 1) {
long long amt = e.cap - e.flow;
if (flow != -1 && amt > flow) amt = flow;
if (long long pushed = DFS(e.v, T, amt)) {
e.flow += pushed;
oe.flow -= pushed;
return pushed;
}
}
}
return 0;
}
long long MaxFlow(int S, int T) {
long long total = 0;
while (BFS(S, T)) {
fill(pt.begin(), pt.end(), 0);
while (long long flow = DFS(S, T)) total += flow;
}
return total;
}
};
int main() {
memset(dp, -1, sizeof(dp));
int n, t;
scanf("%d%d", &n, &t);
for (int i = 1; i <= n; i++) {
scanf("%s", s1[i] + 1);
}
for (int i = 0; i <= t + 1; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
idx[i][j][k] = id++;
}
}
}
id++;
for (int i = 1; i <= n; i++) {
scanf("%s", s2[i] + 1);
}
queue<pair<int, int>> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Z') {
q.push(pair<int, int>(i, j));
dp[i][j] = 0;
}
}
}
while (!q.empty()) {
int uy = q.front().first, ux = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int vy = uy + dir[i][0], vx = ux + dir[i][1];
if (vy >= 1 && vy <= n && vx >= 1 && vx <= n && s1[vy][vx] != 'Y' &&
dp[vy][vx] == -1) {
dp[vy][vx] = dp[uy][ux] + 1;
q.push(pair<int, int>(vy, vx));
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dp[i][j] == -1) dp[i][j] = 1e9;
if (dp[i][j] > t) dp[i][j] = 1e9;
}
}
int source = 0, sink = id - 1;
Dinic dinic(id);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] >= '1' && s1[i][j] <= '9') {
dinic.AddEdge(source, idx[0][i][j], s1[i][j] - '0');
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;
for (int k = 0; k < 4; k++) {
int vy = i + dir[k][0], vx = j + dir[k][1];
if (vy >= 1 && vy <= n && vx >= 1 && vx <= n && s1[vy][vx] != 'Y' &&
s1[vy][vx] != 'Z') {
for (int x = 0; x < t; x++) {
if (dp[vy][vx] >= x + 1 && dp[i][j] > x) {
dinic.AddEdge(idx[x][i][j], idx[x + 1][vy][vx], 1000);
}
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;
for (int x = 0; x <= t; x++) {
if (s2[i][j] >= '1' && s2[i][j] <= '9') {
dinic.AddEdge(idx[x][i][j], idx[t + 1][i][j], s2[i][j] - '0');
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i][j] == 'Y' || s1[i][j] == 'Z') continue;
if (s2[i][j] >= '1' && s2[i][j] <= '9') {
dinic.AddEdge(idx[t + 1][i][j], sink, s2[i][j] - '0');
}
}
}
printf("%d\n", dinic.MaxFlow(source, sink));
}
``` |
#include <bits/stdc++.h>
using namespace std;
class point {
public:
int first, second;
point() : first(0), second(0) {}
point(int _x, int _y) : first(_x), second(_y) {}
point operator+(point &alpha) const {
return point(first + alpha.first, second + alpha.second);
}
};
const int inf = 0x3f3f3f3f;
int men(char first) {
if (first < '0' || first > '9') return 0;
return first - '0';
}
bool okay(point first, int N) {
if (first.first < 0 || first.second < 0 || first.first >= N ||
first.second >= N)
return false;
return true;
}
point dd[4] = {point(-1, 0), point(0, 1), point(1, 0), point(0, -1)};
vector<vector<int> > bfs(int T, vector<vector<int> > &matrix, point start) {
queue<point> Q;
Q.push(start);
vector<vector<int> > dis(matrix.size(), vector<int>(matrix.size(), inf));
dis[start.first][start.second] = 0;
while (!Q.empty()) {
point now = Q.front();
Q.pop();
if (dis[now.first][now.second] == matrix[now.first][now.second]) continue;
for (int i = 0; i < 4; ++i)
if (okay(now + dd[i], matrix.size())) {
int first = (now + dd[i]).first;
int second = (now + dd[i]).second;
if (dis[first][second] == inf &&
matrix[first][second] >= dis[now.first][now.second] + 1 &&
T > dis[now.first][now.second]) {
Q.push(now + dd[i]);
dis[first][second] = dis[now.first][now.second] + 1;
}
}
}
for (size_t i = 0; i < matrix.size(); ++i)
for (size_t j = 0; j < matrix.size(); ++j)
if (matrix[i][j] == -1) dis[i][j] = -1;
return dis;
}
vector<vector<int> > get_matrix(vector<string> matrix) {
vector<vector<int> > alpha(matrix.size(), vector<int>(matrix.size()));
for (size_t i = 0; i < matrix.size(); ++i)
for (size_t j = 0; j < matrix.size(); ++j)
if (matrix[i][j] == 'Y')
alpha[i][j] = -1;
else
alpha[i][j] = inf;
return alpha;
}
int bfs(int S, int D, vector<vector<int> > &E, vector<vector<int> > &cap) {
queue<int> Q;
Q.push(S);
vector<int> A(E.size(), -1);
A[S] = S;
while (!Q.empty()) {
int first = Q.front();
Q.pop();
for (vector<int>::iterator it = E[first].begin(); it != E[first].end();
++it)
if (A[*it] == -1 && cap[first][*it] > 0) A[*it] = first, Q.push(*it);
}
if (A[D] == -1) return 0;
int flow = inf;
for (int i = D; i != S; i = A[i]) flow = min(flow, cap[A[i]][i]);
for (int i = D; i != S; i = A[i]) {
cap[A[i]][i] -= flow;
cap[i][A[i]] += flow;
}
return flow;
}
void afis(vector<vector<int> > &matrix) {
int N = matrix.size();
for (int i = 0; i < N; ++i, cerr << "\n")
for (int j = 0; j < N; ++j) cerr << matrix[i][j] << " ";
cerr << "\n";
}
int main() {
int N, T;
cin >> N >> T;
vector<string> science(N);
for (int i = 0; i < N; ++i) cin >> science[i];
vector<string> capsule(N);
for (int i = 0; i < N; ++i) cin >> capsule[i];
point reactor;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
if (science[i][j] == 'Z') {
reactor = point(i, j);
}
vector<vector<int> > matrix = get_matrix(capsule);
vector<vector<int> > poison = bfs(inf, matrix, reactor);
vector<vector<int> > E(2 * N * N + 2);
vector<vector<int> > cap(2 * N * N + 2, vector<int>(2 * N * N + 2));
for (int i = 0; i < N * N; ++i) {
E[2 * N * N].push_back(i);
E[i].push_back(2 * N * N);
cap[2 * N * N][i] = men(science[i / N][i % N]);
E[2 * N * N + 1].push_back(i + N * N);
E[i + N * N].push_back(2 * N * N + 1);
cap[i + N * N][2 * N * N + 1] = men(capsule[i / N][i % N]);
}
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j) {
vector<vector<int> > save_spots = bfs(T, poison, point(i, j));
for (int k = 0; k < N; ++k)
for (int l = 0; l < N; ++l)
if (save_spots[k][l] != inf) {
E[i * N + j].push_back(N * N + k * N + l);
E[N * N + k * N + l].push_back(i * N + j);
cap[i * N + j][N * N + k * N + l] = inf;
}
}
int S = 2 * N * N;
int D = 2 * N * N + 1;
int flow, aux;
for (flow = 0; (aux = bfs(S, D, E, cap)); flow += aux)
;
cout << flow << "\n";
};
| ### Prompt
Please formulate a cpp solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
class point {
public:
int first, second;
point() : first(0), second(0) {}
point(int _x, int _y) : first(_x), second(_y) {}
point operator+(point &alpha) const {
return point(first + alpha.first, second + alpha.second);
}
};
const int inf = 0x3f3f3f3f;
int men(char first) {
if (first < '0' || first > '9') return 0;
return first - '0';
}
bool okay(point first, int N) {
if (first.first < 0 || first.second < 0 || first.first >= N ||
first.second >= N)
return false;
return true;
}
point dd[4] = {point(-1, 0), point(0, 1), point(1, 0), point(0, -1)};
vector<vector<int> > bfs(int T, vector<vector<int> > &matrix, point start) {
queue<point> Q;
Q.push(start);
vector<vector<int> > dis(matrix.size(), vector<int>(matrix.size(), inf));
dis[start.first][start.second] = 0;
while (!Q.empty()) {
point now = Q.front();
Q.pop();
if (dis[now.first][now.second] == matrix[now.first][now.second]) continue;
for (int i = 0; i < 4; ++i)
if (okay(now + dd[i], matrix.size())) {
int first = (now + dd[i]).first;
int second = (now + dd[i]).second;
if (dis[first][second] == inf &&
matrix[first][second] >= dis[now.first][now.second] + 1 &&
T > dis[now.first][now.second]) {
Q.push(now + dd[i]);
dis[first][second] = dis[now.first][now.second] + 1;
}
}
}
for (size_t i = 0; i < matrix.size(); ++i)
for (size_t j = 0; j < matrix.size(); ++j)
if (matrix[i][j] == -1) dis[i][j] = -1;
return dis;
}
vector<vector<int> > get_matrix(vector<string> matrix) {
vector<vector<int> > alpha(matrix.size(), vector<int>(matrix.size()));
for (size_t i = 0; i < matrix.size(); ++i)
for (size_t j = 0; j < matrix.size(); ++j)
if (matrix[i][j] == 'Y')
alpha[i][j] = -1;
else
alpha[i][j] = inf;
return alpha;
}
int bfs(int S, int D, vector<vector<int> > &E, vector<vector<int> > &cap) {
queue<int> Q;
Q.push(S);
vector<int> A(E.size(), -1);
A[S] = S;
while (!Q.empty()) {
int first = Q.front();
Q.pop();
for (vector<int>::iterator it = E[first].begin(); it != E[first].end();
++it)
if (A[*it] == -1 && cap[first][*it] > 0) A[*it] = first, Q.push(*it);
}
if (A[D] == -1) return 0;
int flow = inf;
for (int i = D; i != S; i = A[i]) flow = min(flow, cap[A[i]][i]);
for (int i = D; i != S; i = A[i]) {
cap[A[i]][i] -= flow;
cap[i][A[i]] += flow;
}
return flow;
}
void afis(vector<vector<int> > &matrix) {
int N = matrix.size();
for (int i = 0; i < N; ++i, cerr << "\n")
for (int j = 0; j < N; ++j) cerr << matrix[i][j] << " ";
cerr << "\n";
}
int main() {
int N, T;
cin >> N >> T;
vector<string> science(N);
for (int i = 0; i < N; ++i) cin >> science[i];
vector<string> capsule(N);
for (int i = 0; i < N; ++i) cin >> capsule[i];
point reactor;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
if (science[i][j] == 'Z') {
reactor = point(i, j);
}
vector<vector<int> > matrix = get_matrix(capsule);
vector<vector<int> > poison = bfs(inf, matrix, reactor);
vector<vector<int> > E(2 * N * N + 2);
vector<vector<int> > cap(2 * N * N + 2, vector<int>(2 * N * N + 2));
for (int i = 0; i < N * N; ++i) {
E[2 * N * N].push_back(i);
E[i].push_back(2 * N * N);
cap[2 * N * N][i] = men(science[i / N][i % N]);
E[2 * N * N + 1].push_back(i + N * N);
E[i + N * N].push_back(2 * N * N + 1);
cap[i + N * N][2 * N * N + 1] = men(capsule[i / N][i % N]);
}
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j) {
vector<vector<int> > save_spots = bfs(T, poison, point(i, j));
for (int k = 0; k < N; ++k)
for (int l = 0; l < N; ++l)
if (save_spots[k][l] != inf) {
E[i * N + j].push_back(N * N + k * N + l);
E[N * N + k * N + l].push_back(i * N + j);
cap[i * N + j][N * N + k * N + l] = inf;
}
}
int S = 2 * N * N;
int D = 2 * N * N + 1;
int flow, aux;
for (flow = 0; (aux = bfs(S, D, E, cap)); flow += aux)
;
cout << flow << "\n";
};
``` |
#include <bits/stdc++.h>
using namespace std;
const int inf = 987654321;
int V, m, S, T;
vector<int> last, nxt, oppo, capa;
void init() {
m = 0;
last.clear();
nxt.clear();
oppo.clear();
capa.clear();
last = vector<int>(V, -1);
}
void add_edge(int u, int v, int capa_) {
nxt.push_back(last[u]);
last[u] = m;
oppo.push_back(v);
capa.push_back(capa_);
m++;
}
vector<int> lv;
bool bfs() {
lv.clear();
lv = vector<int>(V, -1);
queue<int> q;
q.push(S);
lv[S] = 0;
while (!q.empty()) {
int fq = q.front();
q.pop();
for (int i = last[fq]; i != -1; i = nxt[i]) {
int j = oppo[i];
if (lv[j] == -1 && capa[i]) {
q.push(j);
lv[j] = lv[fq] + 1;
}
}
}
if (lv[T] == -1) return false;
return true;
}
int dfs(int u, int flow) {
if (u == T || flow == 0) return flow;
int used = 0;
for (int i = last[u]; i != -1; i = nxt[i]) {
int v = oppo[i];
if (lv[v] == lv[u] + 1) {
int tmp = flow - used;
tmp = dfs(v, min(tmp, capa[i]));
capa[i] -= tmp;
capa[i ^ 1] += tmp;
used += tmp;
}
}
if (used == 0) lv[u] = -1;
return used;
}
int dinic() {
int tf = 0;
while (bfs()) tf += dfs(S, inf);
return tf;
}
int N, tm, z;
vector<vector<char> > sc, cp;
vector<int> dist;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void get_dist() {
queue<int> q;
dist = vector<int>(N * N, inf);
q.push(z);
dist[z] = 0;
while (!q.empty()) {
int fq = q.front();
q.pop();
int y = fq / N;
int x = fq % N;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < 0 || N <= ny) continue;
if (nx < 0 || N <= nx) continue;
if (sc[ny][nx] == 'Y') continue;
int nq = ny * N + nx;
if (dist[nq] != inf) continue;
dist[nq] = dist[fq] + 1;
q.push(nq);
}
}
}
void get_edge() {
V = N * N * (tm + 1) + 2 + N * N;
S = V - 2;
T = V - 1;
init();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (sc[i][j] == 'Y') continue;
if (sc[i][j] == 'Z') continue;
add_edge(S, i * N + j, sc[i][j] - '0');
add_edge(i * N + j, S, 0);
add_edge(N * N * (tm + 1) + i * N + j, T, cp[i][j] - '0');
add_edge(T, N * N * (tm + 1) + i * N + j, 0);
for (int k = 0; k <= tm; k++) {
if (dist[i * N + j] >= k) {
add_edge(k * N * N + i * N + j, N * N * (tm + 1) + i * N + j, inf);
add_edge(N * N * (tm + 1) + i * N + j, k * N * N + i * N + j, 0);
} else
continue;
if (dist[i * N + j] == k) continue;
for (int d = 0; d < 4; d++) {
int ni = i + dy[d];
int nj = j + dx[d];
if (ni < 0 || N <= ni) continue;
if (nj < 0 || N <= nj) continue;
if (sc[ni][nj] == 'Y') continue;
if (sc[ni][nj] == 'Z') continue;
if (k == tm) continue;
if (dist[ni * N + nj] >= k + 1) {
add_edge(k * N * N + i * N + j, (k + 1) * N * N + ni * N + nj, inf);
add_edge((k + 1) * N * N + ni * N + nj, k * N * N + i * N + j, 0);
}
}
}
}
}
}
int main() {
scanf("%d %d", &N, &tm);
sc = cp = vector<vector<char> >(N, vector<char>(N));
for (int i = 0; i < N; i++) {
scanf("\n");
for (int j = 0; j < N; j++) {
scanf("%c", &sc[i][j]);
if (sc[i][j] == 'Z') z = i * N + j;
}
}
scanf("\n");
for (int i = 0; i < N; i++) {
scanf("\n");
for (int j = 0; j < N; j++) {
scanf("%c", &cp[i][j]);
}
}
get_dist();
get_edge();
printf("%d", dinic());
}
| ### Prompt
Please create a solution in cpp to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 987654321;
int V, m, S, T;
vector<int> last, nxt, oppo, capa;
void init() {
m = 0;
last.clear();
nxt.clear();
oppo.clear();
capa.clear();
last = vector<int>(V, -1);
}
void add_edge(int u, int v, int capa_) {
nxt.push_back(last[u]);
last[u] = m;
oppo.push_back(v);
capa.push_back(capa_);
m++;
}
vector<int> lv;
bool bfs() {
lv.clear();
lv = vector<int>(V, -1);
queue<int> q;
q.push(S);
lv[S] = 0;
while (!q.empty()) {
int fq = q.front();
q.pop();
for (int i = last[fq]; i != -1; i = nxt[i]) {
int j = oppo[i];
if (lv[j] == -1 && capa[i]) {
q.push(j);
lv[j] = lv[fq] + 1;
}
}
}
if (lv[T] == -1) return false;
return true;
}
int dfs(int u, int flow) {
if (u == T || flow == 0) return flow;
int used = 0;
for (int i = last[u]; i != -1; i = nxt[i]) {
int v = oppo[i];
if (lv[v] == lv[u] + 1) {
int tmp = flow - used;
tmp = dfs(v, min(tmp, capa[i]));
capa[i] -= tmp;
capa[i ^ 1] += tmp;
used += tmp;
}
}
if (used == 0) lv[u] = -1;
return used;
}
int dinic() {
int tf = 0;
while (bfs()) tf += dfs(S, inf);
return tf;
}
int N, tm, z;
vector<vector<char> > sc, cp;
vector<int> dist;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void get_dist() {
queue<int> q;
dist = vector<int>(N * N, inf);
q.push(z);
dist[z] = 0;
while (!q.empty()) {
int fq = q.front();
q.pop();
int y = fq / N;
int x = fq % N;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < 0 || N <= ny) continue;
if (nx < 0 || N <= nx) continue;
if (sc[ny][nx] == 'Y') continue;
int nq = ny * N + nx;
if (dist[nq] != inf) continue;
dist[nq] = dist[fq] + 1;
q.push(nq);
}
}
}
void get_edge() {
V = N * N * (tm + 1) + 2 + N * N;
S = V - 2;
T = V - 1;
init();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (sc[i][j] == 'Y') continue;
if (sc[i][j] == 'Z') continue;
add_edge(S, i * N + j, sc[i][j] - '0');
add_edge(i * N + j, S, 0);
add_edge(N * N * (tm + 1) + i * N + j, T, cp[i][j] - '0');
add_edge(T, N * N * (tm + 1) + i * N + j, 0);
for (int k = 0; k <= tm; k++) {
if (dist[i * N + j] >= k) {
add_edge(k * N * N + i * N + j, N * N * (tm + 1) + i * N + j, inf);
add_edge(N * N * (tm + 1) + i * N + j, k * N * N + i * N + j, 0);
} else
continue;
if (dist[i * N + j] == k) continue;
for (int d = 0; d < 4; d++) {
int ni = i + dy[d];
int nj = j + dx[d];
if (ni < 0 || N <= ni) continue;
if (nj < 0 || N <= nj) continue;
if (sc[ni][nj] == 'Y') continue;
if (sc[ni][nj] == 'Z') continue;
if (k == tm) continue;
if (dist[ni * N + nj] >= k + 1) {
add_edge(k * N * N + i * N + j, (k + 1) * N * N + ni * N + nj, inf);
add_edge((k + 1) * N * N + ni * N + nj, k * N * N + i * N + j, 0);
}
}
}
}
}
}
int main() {
scanf("%d %d", &N, &tm);
sc = cp = vector<vector<char> >(N, vector<char>(N));
for (int i = 0; i < N; i++) {
scanf("\n");
for (int j = 0; j < N; j++) {
scanf("%c", &sc[i][j]);
if (sc[i][j] == 'Z') z = i * N + j;
}
}
scanf("\n");
for (int i = 0; i < N; i++) {
scanf("\n");
for (int j = 0; j < N; j++) {
scanf("%c", &cp[i][j]);
}
}
get_dist();
get_edge();
printf("%d", dinic());
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
const int N = 212;
const int M = 11234 * 2;
const int eps = 0;
struct dinic {
int hd[N], seen[N], qu[N], lv[N], ei[N], to[M], nx[M];
int fl[M], cp[M];
int en = 2;
int tempo = 0;
void reset(int n = N) {
en = 2;
memset(hd, 0, sizeof(int) * n);
}
void reset_flow() { memset(fl, 0, sizeof(int) * en); }
void add_edge(int a, int b, int c, int rc = 0) {
to[en] = b;
nx[en] = hd[a];
fl[en] = 0;
cp[en] = c;
hd[a] = en++;
to[en] = a;
nx[en] = hd[b];
fl[en] = 0;
cp[en] = rc;
hd[b] = en++;
}
bool bfs(int s, int t) {
seen[t] = ++tempo;
lv[t] = 0;
int ql = 0, qr = 0;
qu[qr++] = t;
while (ql != qr) {
t = qu[ql++];
ei[t] = hd[t];
if (s == t) return true;
for (int e = hd[t]; e; e = nx[e])
if (seen[to[e]] != tempo && cp[e ^ 1] - fl[e ^ 1] > eps) {
seen[to[e]] = tempo;
lv[to[e]] = lv[t] + 1;
qu[qr++] = to[e];
}
}
return false;
}
int dfs(int s, int t, int f) {
if (s == t) return f;
for (int &e = ei[s]; e; e = nx[e])
if (ei[to[e]] && seen[to[e]] == tempo && cp[e] - fl[e] > eps &&
lv[to[e]] == lv[s] - 1)
if (int rf = dfs(to[e], t, min(f, cp[e] - fl[e]))) {
fl[e] += rf;
fl[e ^ 1] -= rf;
return rf;
}
return 0;
}
int max_flow(int s, int t) {
int fl = 0;
while (bfs(s, t))
while (int f = dfs(s, t, numeric_limits<int>::max())) fl += f;
return fl;
}
};
dinic d;
char S[112][112], C[112][112];
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
int n;
pair<int, int> o;
int ds[11][11][11][11];
void go(int si, int sj) {
queue<pair<int, int> > q;
q.push(pair<int, int>(si, sj));
ds[si][sj][si][sj] = 0;
while (!q.empty()) {
pair<int, int> x = q.front();
q.pop();
int i = x.first, j = x.second;
if ((o.first != si || o.second != sj) &&
ds[o.first][o.second][i][j] != -1 &&
ds[si][sj][i][j] >= ds[o.first][o.second][i][j])
continue;
for (int d = 0; d < 4; d++) {
int ni = i + di[d], nj = j + dj[d];
if (ni < 0 || nj < 0 || ni >= n || nj >= n || C[ni][nj] == 'Y' ||
ds[si][sj][ni][nj] != -1)
continue;
ds[si][sj][ni][nj] = ds[si][sj][i][j] + 1;
q.push(pair<int, int>(ni, nj));
}
}
}
int main() {
memset(ds, -1, sizeof ds);
int i, j, T;
scanf("%d %d", &n, &T);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) scanf(" %c", &S[i][j]);
vector<pair<int, int> > st;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
scanf(" %c", &C[i][j]);
if (isdigit(C[i][j])) {
st.push_back(pair<int, int>(i, j));
}
if (C[i][j] == 'Z') o = pair<int, int>(i, j);
}
go(o.first, o.second);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (C[i][j] != 'Y') go(i, j);
int s = 2 * n * n, t = 2 * n * n + 1;
for (i = 0; i < st.size(); i++) {
int si = st[i].first, sj = st[i].second;
d.add_edge(s, i, S[si][sj] - '0');
d.add_edge(i + n * n, t, C[si][sj] - '0');
for (j = 0; j < st.size(); j++) {
int d = ds[si][sj][st[j].first][st[j].second];
int d2 = ds[o.first][o.second][st[j].first][st[j].second];
if (d != -1 && d <= T && (d2 == -1 || d <= d2))
::d.add_edge(i, j + n * n, 10000);
}
}
printf("%d\n", d.max_flow(s, t));
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape.
Input
The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.
Output
Print a single number — the maximum number of scientists who will manage to save themselves.
Examples
Input
3 3
1YZ
1YY
100
0YZ
0YY
003
Output
2
Input
4 4
Y110
1Y1Z
1Y0Y
0100
Y001
0Y0Z
0Y0Y
0005
Output
3
Note
In the second sample the events could take place as follows:
<image>
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
const int N = 212;
const int M = 11234 * 2;
const int eps = 0;
struct dinic {
int hd[N], seen[N], qu[N], lv[N], ei[N], to[M], nx[M];
int fl[M], cp[M];
int en = 2;
int tempo = 0;
void reset(int n = N) {
en = 2;
memset(hd, 0, sizeof(int) * n);
}
void reset_flow() { memset(fl, 0, sizeof(int) * en); }
void add_edge(int a, int b, int c, int rc = 0) {
to[en] = b;
nx[en] = hd[a];
fl[en] = 0;
cp[en] = c;
hd[a] = en++;
to[en] = a;
nx[en] = hd[b];
fl[en] = 0;
cp[en] = rc;
hd[b] = en++;
}
bool bfs(int s, int t) {
seen[t] = ++tempo;
lv[t] = 0;
int ql = 0, qr = 0;
qu[qr++] = t;
while (ql != qr) {
t = qu[ql++];
ei[t] = hd[t];
if (s == t) return true;
for (int e = hd[t]; e; e = nx[e])
if (seen[to[e]] != tempo && cp[e ^ 1] - fl[e ^ 1] > eps) {
seen[to[e]] = tempo;
lv[to[e]] = lv[t] + 1;
qu[qr++] = to[e];
}
}
return false;
}
int dfs(int s, int t, int f) {
if (s == t) return f;
for (int &e = ei[s]; e; e = nx[e])
if (ei[to[e]] && seen[to[e]] == tempo && cp[e] - fl[e] > eps &&
lv[to[e]] == lv[s] - 1)
if (int rf = dfs(to[e], t, min(f, cp[e] - fl[e]))) {
fl[e] += rf;
fl[e ^ 1] -= rf;
return rf;
}
return 0;
}
int max_flow(int s, int t) {
int fl = 0;
while (bfs(s, t))
while (int f = dfs(s, t, numeric_limits<int>::max())) fl += f;
return fl;
}
};
dinic d;
char S[112][112], C[112][112];
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
int n;
pair<int, int> o;
int ds[11][11][11][11];
void go(int si, int sj) {
queue<pair<int, int> > q;
q.push(pair<int, int>(si, sj));
ds[si][sj][si][sj] = 0;
while (!q.empty()) {
pair<int, int> x = q.front();
q.pop();
int i = x.first, j = x.second;
if ((o.first != si || o.second != sj) &&
ds[o.first][o.second][i][j] != -1 &&
ds[si][sj][i][j] >= ds[o.first][o.second][i][j])
continue;
for (int d = 0; d < 4; d++) {
int ni = i + di[d], nj = j + dj[d];
if (ni < 0 || nj < 0 || ni >= n || nj >= n || C[ni][nj] == 'Y' ||
ds[si][sj][ni][nj] != -1)
continue;
ds[si][sj][ni][nj] = ds[si][sj][i][j] + 1;
q.push(pair<int, int>(ni, nj));
}
}
}
int main() {
memset(ds, -1, sizeof ds);
int i, j, T;
scanf("%d %d", &n, &T);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) scanf(" %c", &S[i][j]);
vector<pair<int, int> > st;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
scanf(" %c", &C[i][j]);
if (isdigit(C[i][j])) {
st.push_back(pair<int, int>(i, j));
}
if (C[i][j] == 'Z') o = pair<int, int>(i, j);
}
go(o.first, o.second);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (C[i][j] != 'Y') go(i, j);
int s = 2 * n * n, t = 2 * n * n + 1;
for (i = 0; i < st.size(); i++) {
int si = st[i].first, sj = st[i].second;
d.add_edge(s, i, S[si][sj] - '0');
d.add_edge(i + n * n, t, C[si][sj] - '0');
for (j = 0; j < st.size(); j++) {
int d = ds[si][sj][st[j].first][st[j].second];
int d2 = ds[o.first][o.second][st[j].first][st[j].second];
if (d != -1 && d <= T && (d2 == -1 || d <= d2))
::d.add_edge(i, j + n * n, 10000);
}
}
printf("%d\n", d.max_flow(s, t));
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.