Turbo Sort Time Time Limit Exceeded Round 2

I tried using std::sort and std::vector and i’m still getting the time limit error. Perhaps using vectors isn’t fast enough.

#include <iostream>
#include <algorithm>
#include <vector>

int main(int argc, const char * argv[])
{
    std::vector<int> array;
    int size = 0;
    int temp = 0;

    std::cin >> size;
    for(int i = 0; i<array.size(); i++){
        std::cout << "Enter data: ";
        std::cin >> temp;
        array.push_back(temp);
      
    }
    
    std::sort(array.begin(), array.end());
    
    for(int j = 0; j<array.size(); j++){
        std::cout << array[j] << std::endl;
    }
    return 0;
}

Hi,

You need to use faster I/O methods, like, scanf()/printf().

Also, after fixing this you will get WA, because you are NOT respecting I/O format because you are sending to stdout the sentence: "Enter data: ".

This is not correct and you should remove it to get AC…

Besides, there’s a bigger mistake in your code.

You attempt to access the size of the array, by doing array.size(), but, you declared the vector without any size beforehand.

To declare a vector called array which starts with 10 numbers (i.e. array.size()==10) you can do:

vector<int> array(10);

If you do:

vector<int> array;

then, on the second declaration the vector is empty and using array.size() is bit meaningless…

If you want to see my AC solution as reference:

http://www.codechef.com/viewsolution/3253358

Also, use this problem (and some more in practice section) to get to know C++… You won’t regret it :smiley:

Best,

Bruno