Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

ppastram's blog

By ppastram, history, 4 weeks ago, In English,

Hi everyone. I'm getting the right answer on the first couple of test cases on a fairly easy problem but on the third one I get "time limit exceeded". Here's my submission: https://codeforces.com/contest/1325/submission/82660385 . If anybody can think of a way to make my solution more efficient it would help a lot!

Thank you very much.

 
 
 
 
  • Vote: I like it
  • +6
  • Vote: I do not like it

»
4 weeks ago, # |
  Vote: I like it +9 Vote: I do not like it

Find in array is O(N) I think. You can use std::set (stores every element only once) and the size of the set should give you the answer you are looking for.

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Thank you very much! I'm pretty new to c++ and I didn't know about std::set. It worked perfectly!

»
4 weeks ago, # |
Rev. 3   Vote: I like it +4 Vote: I do not like it

I know you have found your solution Just suggesting you some optimization techniques... 1. "using namespace std;" saves you from typing "std ::" before any library function 2. If you want to initialize every element of a particular array with 0 then you can write like this: int a[100]={0} or you may use it as global array as any global variable is initialized with 0 by default though it is recommended not to use global variables unnecessarily 3. Using only this header file: "#include<bits/stdc++.h>" saves you from writing numerous header files.It alone is enough for the other necessary header files.Pretty cool thing

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Thanks man! Really helpful tips! I’ll start using them.