Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 1;
int n, p;
pair <int, int> a[N];
string s;
stack <int> st;
int main() {
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i + 1;
}
cin >> s;
sort(a, a + n);
for(int i = 0; i < s.length(); ++i)
if(s[i] == '0') {
cout < < a[p].second < < ' ';
st.push(a[p].second);
++p;
} else {
cout < < st.top() < < ' ';
st.pop();
}
cout < < endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
2
3 1
0011
3 1
0011
Output
2 1 1 2
Demonstration
Codeforces Solution-B. Bus of Characters-Solution in C, C++, Java, Python,Bus of Characters,Codeforces Solution