Problem: [BEX][1]
My code:
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
lld T;
cin >> T;
deque<pair<string, lld>> name;
while (T--) {
string str;
lld diff;
cin >> diff;
if (diff < 0) {
lld idx = 0, mn = LONG_LONG_MAX;
for (lld i = 0; i < name.size(); i++)
if (name[i].second < mn) {
idx = i;
mn = name[i].second;
}
cout << idx << " " << name[idx].first << '\n';
name.erase(name.begin(), name.begin() + idx + 1);
} else {
cin >> str;
name.emplace_front(str, diff);
}
}
return 0;
}
My Approach: I have had trouble understanding the problem statement and so my approach might be wrong too. What I do is, I input all values until -1 is hit. Then I check for the minimum value in the array and calculate the number of books required to remove above it. I assume that this value is going to be equal to the index of the book in the pile. In doing so, I remove all the books from above, including the solved book.
[1]: BEX Problem - CodeChef