You are not logged in. Please login at www.codechef.com to post your questions!

×

getting tle

I am getting tle for this question I dont kw why please help me know about this question link: http://www.spoj.com/problems/STPAR/ my code link:http://ideone.com/6PKugy

asked 27 Nov '14, 22:04

chashmeetsingh's gravatar image

3★chashmeetsingh
521210
accept rate: 7%


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.

2) 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 :)

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 :)

Sorry for my bad English :(

link

answered 27 Nov '14, 23:14

leduongtuananh's gravatar image

2★leduongtuananh
12528
accept rate: 16%

edited 27 Nov '14, 23:15

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

http://ideone.com/7DNddh

link

answered 27 Nov '14, 23:37

leduongtuananh's gravatar image

2★leduongtuananh
12528
accept rate: 16%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,138
×729

question asked: 27 Nov '14, 22:04

question was seen: 899 times

last updated: 27 Nov '14, 23:37