getting tle

I am getting tle for this question I dont kw why please help me know about this
question link: SPOJ.com - Problem STPAR
my code link:6PKugy - Online C++0x Compiler & Debugging Tool - Ideone.com

You can check you code manually:

First, j=0; then in “while (j < n)”, check some conditions:

  1. j!=n-1, so

    if(j==n-1){
    while(s.empty()==true){
    if (s.top()==z){
    s.pop();
    }
    else{
    flag=1;
    break;
    }
    }
    }

will not be run.

  1. j=0, so a[j]=4 != z, which is equal 1, so

    if(a[j]==z){
    cout<<“1”;
    z++;
    if(j<n-1){
    j++;
    }
    }

will not be run, check else condition:

else if(!s.empty()){
		cout<<"2";
		if(s.top()>z){
			s.push(a[j]);
			j++;
		}
		else if(s.top()==z+1){
				s.pop();
			} 
		}

from begin of code, you haven’t push anything into stack, so stack will be empty, and as it is empty, it will never have anything called “top” as you write here: “s.top()==z+1”, so after many conditions, variable j won’t be changed, and j is still equal 0, of course, j<n, and it ran to TLE :slight_smile:

So check your algorithm again.

Notes: I have used “bits/stdc++.h” sometimes, but my advice for you: It’s not good to use non-standard library for C++, it will be judged as “runtime error” on many judger, such as Codeforces, SPOJ.

Good luck :slight_smile:

Sorry for my bad English :frowning:

Here is my code for that problem, which is Accepted :slight_smile: My code is seem to be clear, and I hope that will help you :slight_smile:

http://ideone.com/7DNddh