I am not able to follow the solution completely. Suppose if the testcase is
6
8 geography
3 Maths
9 english
-1
-1
-1
this is i think a valid testcase but with the solution explained in editorials, i dont think the answer would be right as we have never pushed english on the stack and the last -1 wont be properly answered. Could you please explain this? thanks
1 Like
Can anyone figure out why my code is getting WA? I am using two stacks- one for storing the pile of books in a general fashion and the other is a special stack which has the minimum number of exercises in any book present in stack 1.Every time a new book is pushed in stack 1, i am checking if the top of stack 2 has a book that has left exercises greater than that new book- if yes then that book is even pushed in the 2nd stack,otherwise ignored. In case of a query i am counting …i pop the topmost book from stack 2 and keep poping the books from stack 1 until that book matches.I have tried all sample test cases that i could find anywhere and my code is giving the correct output but the judge rules it out.
Any help would be hoghly appreciated.
Here is the link~~ CodeChef: Practical coding for everyone
can someone tell why i am getting runtime error sigsegv… my code is here:
http://www.codechef.com/viewsolution/3599271
I tried the same algorithm as author’s using both vector and stack data type. I am getting correct answers for small test cases, but TLE on submission. Could someone help out?
https://www.codechef.com/viewsolution/8924342
#include
#include
#include
struct Book
{
char name[16];
int booksOnTop;
int exercisesLeft;
Book()
{
}
Book(char *_name, int _exercisesLeft)
: booksOnTop(0), exercisesLeft(_exercisesLeft)
{
strcpy(name, _name);
}
};
int main()
{
int n, ex;
std::stack nextMin;
char name[16];
scanf("%d", &n);
while (n--)
{
scanf("%d", &ex);
if (ex != -1)
{
scanf("%s", name);
if (ex == 0)
continue;
if (nextMin.size() == 0)
nextMin.push(Book(name, ex));
else
{
Book& b = nextMin.top();
if (b.exercisesLeft >= ex)
nextMin.push(Book(name, ex));
else
b.booksOnTop++;
}
}
else
{
Book b = nextMin.top();
nextMin.pop();
printf("%d %s\n", b.booksOnTop, b.name);
}
}
return 0;
}
I tried your code and I’m getting RE - BGO4YF - Online C++ Compiler & Debugging Tool - Ideone.com
I tried correctness of the input with tester’s solution - http://ideone.com/Yy98Ow
Try this two test cases:
5
8 a
6 b
-1
7 c
-1
6
8 a
6 b
-1
7 c
-1
-1
1 Like
Thank you, with your help, I fixed the bug caused RE. However, the new code have a unexpected TLE error while I thought its time complexity is O(N), can you give me a hint?
The problem is that you are using cin and it is slow, replace cin with scanf and it will get AC 
@jigsaw004: reading someone’s code is good practice for challenges (for example fot TopCoder or CodeForces competitions). hacker007’s code is really well written and short, it’s bigger problem with longer codes
2 Likes
@betlista, thanks for the help!
my code is showing SIGSEV error … if it due to large memory … then why it is not showing for tester solution … i have also used same type of arrays having same size … here is link to my code
http://www.codechef.com/viewsolution/1650094
figured the mistake… we can declare at max 10 power 5 size array in c functions… so to declare 10 power 6 size we must declare the array as a global array.
could anyone tell why i am getting the WA?
here’s my code:-
l = []
nm = []
cnt = 0
t = int(input())
for _ in range(t):
s = list(input().split(" "))
if(int(s[0]) != -1):
rm = int(s[0])
if(len(l) == 0):
l.append(int(s[0]))
nm.append(s[1])
elif rm > l[-1]:
cnt = cnt + 1
else:
l.append(int(s[0]))
nm.append(s[1])
else:
print(cnt,nm.pop())
l.pop()
cnt = 0
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 ?