pritishn's blog

By pritishn, 3 years ago, In English

demoralizer and I were discussing our 2+ years old code and I thought it'd be a good motivation for newbies and pupil who write cancer currently.

Please contibute by sharing code(in spoiler tag) or links to your of your big brain moments from back when you were low rated.

My biggest big brain moment was when I assumed inbuilt std::sort must be slower, so copied merge sort from gfg. https://www.codechef.com/viewsolution/23270721

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

| Write comment?
»
3 years ago, # |
  Vote: I like it +33 Vote: I do not like it

This is where this discussion began:

When I was too dumb to use loops
  • »
    »
    3 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    Ultimate toga moment for all programmers.

    Bravo Napoleon.

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

    What about gauss elimination by hand?

    pic
  • »
    »
    3 years ago, # ^ |
      Vote: I like it +61 Vote: I do not like it

    You were too dumb to even spell eight correctly back then XD

  • »
    »
    3 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    I had the opposite problem, I was trying to solve a puzzle where you needed to fill in around 30 cells with either 0 or 1, so I wrote 30 nested for loops

»
3 years ago, # |
  Vote: I like it +60 Vote: I do not like it

assumed inbuilt std::sort must be slower atleast you had knowledge of stl,I used to write gcd ,sort functions on my own and instead of pair I was using 2d array.

»
3 years ago, # |
  Vote: I like it +74 Vote: I do not like it

newbies and pupil who write cancer currently.

Don't underestimate me, I still write cancer solutions to simple problems. (I remember spamming maximum bipartite matching on a div2 B in-contest.)

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

    I once used Backtracking + CircularLinkedList + Sets and Randomization to solve a div2 A, felt empty despite the AC, but at that moment, not solving A would've been harder to digest. submission

»
3 years ago, # |
Rev. 2   Vote: I like it +66 Vote: I do not like it

As someone already pointed out, all of us write cancer solutions from time to time.

For example, this problem from the ICPC WF Invitational mirror had a trivial $$$O(n^2)$$$ solution, but I didn't read the constraints and used lex and yacc to generate this monstrosity of a solution that works in $$$O(n)$$$ instead.

Edit: since someone said the submission isn't visible, here you go.

»
3 years ago, # |
  Vote: I like it +13 Vote: I do not like it

I remember one time I solved a rather tough problem for my level but ended up not implementing it because I thought time complexity for nCr is O(n) and not O(r).

»
3 years ago, # |
  Vote: I like it +100 Vote: I do not like it

I thought writing a $$$O(n^2)$$$ in cpp instead of java would get me an Accepted solution.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it -24 Vote: I do not like it

    In some special cases you can actually do it as there are some optimization tricks available on C++

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Not quite sure why am I wrong here. It is not all the cases but rather than special cases.

      Such as:

      • The case when $$$O(n^2)$$$ is enough and intended solution yet your constant factor is bigger than the author.
      • There is a difference in some functionalities that you can use to improve the speed
      • Not kidding but some C++ template functions is actually very good compared to some java functions that you can use for problem like flow, matching, fft, or geometric, polygon stuffs for one that dont have
      • Friendly Cache Implementation
      • Constant Optimization
      • Pragma Vectorization
      • Bit-hack Tricks
»
3 years ago, # |
  Vote: I like it +4 Vote: I do not like it

I thought that binary search is so slow in special cases so I use random middle pivot to boost it in those cases :)

»
3 years ago, # |
  Vote: I like it +61 Vote: I do not like it

I thought inserting #pragmas in my code will magically make my code fast, so I should not bother about time complexities anymore!

»
3 years ago, # |
  Vote: I like it +33 Vote: I do not like it

I thought maps are magical. Just insert elements into a map, and voila, you have sorted the input in O(n). I remember telling this to my friend and then proceeding to have a clown moment for the rest of the day.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it -7 Vote: I do not like it

    Well, if you consider frequency array as map...

    int mpp[maxN]={0};
    for(int i=0;i<n;i++)
    {mpp[a[i]]++;} //O(n) 
    for(int i=0;i<maxN;i++)
    {while(mpp[i]--)cout<<i<<",";}//O(maxN)
    

    O(n+maxN) is O(n)
    Yet another big brain moment xD

»
3 years ago, # |
  Vote: I like it +9 Vote: I do not like it

I thought most of the $$$O(n^2)$$$ solutions would pass if I use correct pragma :(

»
3 years ago, # |
  Vote: I like it +45 Vote: I do not like it

I thought printing sample test cases output will give me accepted

»
3 years ago, # |
  Vote: I like it +18 Vote: I do not like it

I used to think set operators like |(Union),&(Intersection) in python has O(1) time complexity.

»
3 years ago, # |
  Vote: I like it +17 Vote: I do not like it

I used to learn Z-function by heart without understanding how it works at all before I attended offline competitions. I was afraid of tasks about strings and thought that somehow z-function might save me. It did not work out even once)

»
3 years ago, # |
  Vote: I like it +17 Vote: I do not like it
string a, b;

1. a = a + b;
2. a += b;

I used to think both 1 and 2 were equally efficient.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it -19 Vote: I do not like it

    I still think they are equally of same time complexity :( Aren't they?

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Let $$$n$$$ be the size of $$$a$$$ and $$$m$$$ the size of $$$b$$$, then the first one is $$$O(n + m)$$$ and the other is $$$O(m)$$$.

      This is because it returns a new string by doing a + b. a += b on the other hand, just appends b to a, it's like push back in vector. As a side note, push back also works there: a.push_back(b).

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

        It's not strictly $$$O(m)$$$, but it's $$$O(m)$$$ on average and $$$O(n+m)$$$ at max. That's also important to know, though I don't have a sample of code, where it matters.

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

          Are you talking about the case when a reaches its maximum capacity by doing a += b?

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

        Thanks for clarification

»
3 years ago, # |
  Vote: I like it +51 Vote: I do not like it
Younger me refuses to acknowledge the existence of arrays...
»
3 years ago, # |
  Vote: I like it +159 Vote: I do not like it

Wrote an solution with exponential complexity where $$$n = 2e5$$$, got TLE, searched Google: How to speed up program in C++ then added ios_base::sync_with_stdio(false); cin.tie(NULL);, spent the rest of the contest wondering why I still got TLE...

»
3 years ago, # |
  Vote: I like it +37 Vote: I do not like it

Will let you know once I become high rated :).

»
3 years ago, # |
  Vote: I like it +84 Vote: I do not like it

When in my first contest on codeforces (div3 round) I got +500, I thought I will become red after few more contests.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Anyone else once thought that vector.insert works in $$$O(1)$$$?

»
3 years ago, # |
  Vote: I like it +32 Vote: I do not like it

me thinking that $$$O(n^2)$$$ is faster than $$$O(n log n)$$$

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Those cases in which I was getting TLE or WA on gfg, I just copy those cases, and put if condition for those and print the result in O(1)

Spoiler
»
3 years ago, # |
  Vote: I like it +15 Vote: I do not like it

I had some weird obsession with macros.. warning: disturbing content below

look
»
3 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

I used to think that std::lower_bound in a set was $$$\mathcal{O}(\log N)$$$ and surprisingly solved many problems, until the day when I TLE'd

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

    Didn't you mean std::lower_bound on set?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Umm.. Noob here ! Isn't lower_bound operation on a set or sorted vector O(log n) ??

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

      For sorted vectors it's logarithmic but for sets it's linear
      std::lower_bound on set

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Are you sure?, I still think that it's logn and used it many times and didn't got TLE.

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

          As mentioned here, std::set doesn't has random access iterators, so the time complexity is linear.

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
          Rev. 2   Vote: I like it +9 Vote: I do not like it

          If you have a set S, then "S.lower_bound(number)" is O(logN), but "lower_bound(S.begin(), S.end(), number)" is O(N). One is a member function, the other is a generic function in the "algorithm" header. Make sure to not mix these up!

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

    Lesson: use std::set::lower_bound instead of std::lower_bound on set

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Another edition of "I still write cancer":

Switch case is something that I will not use in 100 years. If else it is. So at my latest test at the university, we had a trivial problem with dates (day,months,years) and some operations with them. So my code has like a million of:

if (m == 1 || m == 3 || m == 5 || m == 7 || ) ...
»
3 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Not high rated, but in my first contest which was a google kickstart, I didn't knew that godly things like std::sort exist, and as I just completed the MIT course on algorithms (because for some reason I thought I'd clear IOI by watching it), I know heap sort is the fastest thing I know so I copied the heap sort code from gfg (that too in java cuz aman dhotarchor said java is better) and ended up in getting an RTE which I spent whole hour figuring out.

Second one: my first contest on CF was by coincidence an April Fools round and I ended up solving three problems, so I thought I am cyan level from the start

»
3 years ago, # |
Rev. 2   Vote: I like it +48 Vote: I do not like it
Binary search
»
3 years ago, # |
Rev. 3   Vote: I like it +9 Vote: I do not like it

learning not basic data structures while being green

also tried reading about splay tree when i was 1300 to flex on my friends with it

»
3 years ago, # |
  Vote: I like it +70 Vote: I do not like it

When I heard people talking about "node", I thought it was some graph shits. Turned out to be nodeJS.

»
3 years ago, # |
  Vote: I like it +34 Vote: I do not like it

I used to think rating was directly proportional to how many macros you used.

Viewer discussion is advised
»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Wrote an exponential solution to this problem and it passed pretests and FSTed. I thought I solved a Div2 C for the first time :(

»
3 years ago, # |
  Vote: I like it +9 Vote: I do not like it

I used to believe std::lower_bound and std::upper_bound is all that binary search algorithm is.

»
3 years ago, # |
  Vote: I like it +13 Vote: I do not like it

i thought $$$\frac{10^6}{2} = 10^3$$$ in a contest and proceeds to spend whole contest wondering why my code got TLE.

and this has happened more than once :)

»
3 years ago, # |
  Vote: I like it +5 Vote: I do not like it

I thought the compiler on cf wont be able to tell what the result of program was. So I quoted with the output

Spoiler
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

And then also you got WA :)

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Me doing binary search as: checking mid element and if mid<element : then doing linear serach from 1 to mid else linear search from mid to n

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

To get the minimum integer of an array I used *min(vec.begin(), vec.end()). To test this I took a "random" array in which the first element was the minimum of the array :). So it took me a long time to figure out what was wrong.

  • »
    »
    13 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Fun fact: You can now use std::ranges::min(vec) from C++20 onwards.

»
3 years ago, # |
  Vote: I like it +6 Vote: I do not like it

I used to think that stl size() function has complexity of O(n) and always stored the size in a variable before writing a loop.

»
3 years ago, # |
Rev. 2   Vote: I like it +26 Vote: I do not like it

I once thought I had invented an O(n) sorting algorithm. Didn't know time complexity of map back then.

Spoiler
  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    -

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      You can't maintain sorted order in unordered_map :p

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

        And that's what happens when you comment before coffee! Yeah no way that works

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

56589098, I solve this during the contest when I was newbie, a Few days later I got that this was a "prefix sum" approach.

»
3 years ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

A fairly long time ago I had no idea functions exist and wrote everything in the main()

Without an understanding how to do functions, when I encountered any problem with some graphs, I would be trying to make graph traversal algorithms using for loops, gotos(yeah...), and bools, all in main(), and it was WILD.

I remember how I would have 5 nested for loops and a million ifs inside and would be thinking "why does it have to be so complicated". Debugging was a total nightmare, and it was more or less like "if i don't fix this within an hour i give up". Unfortunately i cant get any code from that time cause it was on an old laptop that doesnt work anymore lol.

You can imagine how it felt to find out about recursions and dfs lmao.

»
3 years ago, # |
  Vote: I like it +8 Vote: I do not like it

I once had an $$$O(n)$$$ solution to a problem. Complexity was ok but the problem had $$$1e5$$$ testcases(so solution must be calculated in a single $$$O(n)$$$ pre-processing). Got a TLE on pretests so I made a global map which stores the values after printing them in each testcase. Passed pretests and FSTed.

»
3 years ago, # |
Rev. 2   Vote: I like it +33 Vote: I do not like it

More than 6.5 years ago. Must've been doing competitive programming for 7-8 months or so, I didn't know about digit dp. Was trying to solve https://projecteuler.net/problem=164, so I ended up writing code whose output was the code that solves this problem. https://ideone.com/XgqZDh

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

I used Pascal

»
3 years ago, # |
  Vote: I like it +81 Vote: I do not like it

Recently I invented centroid decomposition on array

(I forgot that it's called segment tree)

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Back when I didn’t know what the Sieve of Eratosthenes was, I used to make an array with all the prime numbers within a range for any solution that required them. Ex. 104820322

Needless to say, the speed at which I came up with this type of solution was awfully slow.

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

I used t think that we need to learn whole C++ along with all the Data Structures and Algorithms in the world before starting Competitive Programming, as the word Competitive seemed too scary. I used to think that only the best coders need to compete in order to deicide the best of the best, like in the Olympics.

Thanks to my college seniors for kicking my ass right from the beginning, I didn't end up making that mistake.

»
3 years ago, # |
Rev. 5   Vote: I like it +1 Vote: I do not like it

never trusted function swap(a, b); I always wrote:

my code:
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Used to generate short permutations without using array.

code
»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

I thought printing sample cases brings me above those who do not attempt the questions and gives me + rating.

»
3 years ago, # |
  Vote: I like it +8 Vote: I do not like it

I used to be obsessed with doing the XOR trick to swap two integers w/out a dummy variable. I thought it was a galaxy brain thing to do.

»
3 years ago, # |
  Vote: I like it +7 Vote: I do not like it

Once doing a problem during some contest I was too amateur to understand what sum of k over test cases meant . So had an idea to solve the problem using two nested for loops one for N and another for K . Thought the complexity was O(N*K) and it would Tle , so didnt proceeded with the solution only to find out editorial had the same solution and spent like two weeks wondering why it would not Tle

Later on understood complexity was O(N+K) because it was written sum of K over all test cases was less than 1e5

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I used to assign whatever phrase came to mind at the time to strings whenever I needed a way to set them to null.

I also thought array indices started from 1.

Enjoy: 26783948

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

thought that problems were stupid and C++ was too slow because my O(n^2) solution gives TLE.

»
3 years ago, # |
  Vote: I like it +11 Vote: I do not like it

learned dinic's flow algo while struggling to get out of green!

»
13 months ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it
wrote this to find max of 3 numbers
»
13 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I remember reading about what union find does and then thinking "oh that sounds easy enough to implement".

I then went on to code some $$$O(n^2)$$$ algorithm for the unite and find functions and was confused why it was too slow

That was the day I learnt about the importance of time complexity.