Somebody please help me.
My code is passing the test cases given in the question. But it’s still giving wrong answer.
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned int n;
cin>>n;
stack stack2;
stack stack1;
int books_on_top=0;
while(n–){
int num;
cin>>num;
if(num!=-1){
string s;
cin>>s;
if(num==0){
continue;
}
if(stack1.empty()){
stack1.push(num);
stack2.push(s);
}
else if(num<=stack1.top()){
stack1.push(num);
stack2.push(s);
}
else{
books_on_top++;
}
}
else if(num==-1){
if(stack2.empty()){
cout<<books_on_top<<"\n";
}
else{
cout<<books_on_top<<" "<<stack2.top()<<"\n";
stack1.pop();
stack2.pop();
books_on_top=0;
}
}
}
return 0;
}
I tried these test cases and my code passing them.
But when I submit, I still get wrong answer.
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned int n;
cin>>n;
stack stack2;
stack stack1;
int books_on_top=0;
while(n–){
int num;
cin>>num;
if(num!=-1){
string s;
cin>>s;
if(num==0){
continue;
}
if(stack1.empty()){
stack1.push(num);
stack2.push(s);
}
else if(num<=stack1.top()){
stack1.push(num);
stack2.push(s);
}
else{
books_on_top++;
}
}
else if(num==-1){
if(stack2.empty()){
cout<<books_on_top<<"\n";
}
else{
cout<<books_on_top<<" "<<stack2.top()<<"\n";
stack1.pop();
stack2.pop();
books_on_top=0;
}
}
}
return 0;
}
this test case is not valid,
after the first query of type (-1), only one element is left in the stack and that is 8 geography.
after the second query of type (-1), stack is empty. and hence third -1 makes the test case invalid.
please provide a test case, where my code gives WA.
#include <bits/stdc++.h>
using namespace std;
int32_t main()
{
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, x;
string s;
stack<pair<int, pair<int, string>>> st;
int min = INT_MAX;
cin >> n;
while (n--)
{
cin >> x;
if (x == -1)
{
int cnt = 0;
//cout << st.top().first << " ";
while (st.top().first != st.top().second.first)
{
cnt++;
st.pop();
}
cout << cnt << " " << st.top().second.second << "\n";
st.pop();
if (!st.empty())
{
min = st.top().first;
}
else
{
min = INT_MAX;
}
}
else
{
cin >> s;
if (min > x)
{
min = x;
}
st.push({min, {x, s}});
}
}
return 0;
}
can someone give an example for test case 6 ?
my code goes through the other test cases and i tried to find a case where my code gives wrong answer
or does anybody see where my code does wrong ?