Блог пользователя ToxicPie9

Автор ToxicPie9, 16 месяцев назад, По-английски

Yesterday YouKn0wWho posted a blog explaining common mistakes in competitive programming and how to avoid them. I was greatly inspired by that post so I decided to make my own.

I have compiled some of the mistakes that I didn't make in my early Competitive Programming phase. I also mentioned how to avoid them. Also, in most cases, I will give you a chance to find out what the bug is before I reveal the culprit as I tried to make this blog interactive. The codes that I have used in this blog have been written in Rust as it is the most beloved language for CP.

Mistake 1

Check out the following code:

Code

The output should be $$$10^{18}$$$. But if you run the code, you will get a different output. Why?

Reason

Mistake 2

Check out the following code:

Code

Try to run this locally. What is the time complexity of this?

Is it $$$O(n)$$$?

Reason

Mistake 3

Check out the following code:

Code

Notice that it is guaranteed that total sum of n is <= 100000. So how many operations will the code take in the worst case?

Reason

Mistake 4

What is happening in the following code?

Code

The output is supposed to be [1, 1, 1, 1, 1]. But it's not the case actually! Why?

Reason

Mistake 5

Don't use endl! If your code needs to print millions of newlines, then using endl turns out to be really slower than using '\n'. Why?

Reason

Mistake 6

Use pow() function for integer calculations.

Why?

Mistake 7

Run the following code

Code

You might expect the output to be $$$-1$$$. But the output is actually not! Why?

Reason

Mistake 8

Using eprintln! might be a good way to debug your code as it doesn't output to the standard output. But leaving the eprintln! instances in your code while submitting in OJ might be one of the worst ways of getting TLE.

Smash me for more info.

Mistake 9

Look at the following code

Code

The output is $$$0$$$, which is correct. Why?

Reason

Mistake 11

Consider the following code for calculating the maximum occurrence in an array.

Code

This code seems like it can be hacked easily but in fact, for almost every valid input, it should get AC.

Why?

Mistake 12

Run the following code:

Code

What will be the size of the map? $$$5$$$?

Check

Mistake 13

I forgot Mistake 10.

Mistake 14

Check this out.

Code

What is the time complexity of this?

Check

More Mistakes and Non-Mistakes

  • Do not insert or erase from a container (Vec, HashSet etc) while traversing it using for x in s.iter() like syntax at the same time. This is because the compiler won't let you do that. In Rust, you are not allowed to borrow a variable while it is also borrowed as mutable at the same time (e.g. when inserting).
  • Create variables only when you need them instead of just declaring let a, b, c, d, e, f, g, h; and 69 other variables at the beginning of your code! This is because Rust's grammar doesn't let you do that.
  • If you want to count the number of set bits in an i64 number, then use the count_ones() method instead of the __builtin_popcount function. This is because there is no __builtin_popcount function.
  • If you want to compute the square root of an f64 number, then use the sqrt() method instead of sqrt() because sqrt() function takes self as input whereas sqrt() takes self.
  • Speaking of Runtime Error, the most likely case of getting Runtime Error is when your code encounters an error while running.
  • let x: i64 = 1 << 40; will not overflow as Rust will try to deduce the types of integer literals if they're not specified. In this case, the compiler determines that 1 is i64.
  • Don't accidently write your code in C++.

Thanks for reading the blog. Feel free to add more mistakes that are common in CP in the comment section.

See you on a later episode, friend blobheart!

P.S. Although this is mostly a joke post, I tried to be accurate about the facts and did not intentionally put incorrect information in it (which means you may treat it as a somewhat educational blog). It also aims to showcase some features of Rust that helps you avoid some common CP mistakes in languages like C++.

  • Проголосовать: нравится
  • +337
  • Проголосовать: не нравится

»
16 месяцев назад, # |
  Проголосовать: нравится +109 Проголосовать: не нравится

Thank you sir, definitely one of the blog posts of all time.

»
16 месяцев назад, # |
  Проголосовать: нравится +236 Проголосовать: не нравится

Feel free to add more mistakes that are common in CP in the comment section.

using rust

  • »
    »
    16 месяцев назад, # ^ |
      Проголосовать: нравится +115 Проголосовать: не нравится

    As a proud Rustacean and Ferris the Crab adorer, I regret to inform you that your taste in languages sucks. This is sad. You can do better. You know how easy package and dependancy management is with Cargo? Not to mention you don’t even need a Makefile. It’s great. Dynamically typed languages need to die. There’s no other option. They just do. If you like dynamic typing, you need some help. Seriously. By using a dynamically typed and interpreted language (which means its @#*!&!@ slow!!!), you are committing genocide and harming the environment more than gas cars. Rust is fast and uses clean, renewable energy through the magic of being a language compiled with LLVM. Tired of memory bugs? You should be. Shame on you for still having them when Rust exists. Tired of being bad? Time to go to Rust. Tired of being slow because you’re not smart and your friends laugh at you? Rust is quite speedy indeed (all thanks to the big brain of the compiler). Tired of not getting off the normal way? Match statements, loops, and the compiler for Rust give the best orgasms 10/10 (completely legit). Not to mention the superiority you get to feel when you show off your superior Rust code to your inferior “friends” still using some other language. Want to get rid of malware? Rust is safe, therefore malware is noware (also completely legit). You quite honestly will forget about any other language (including English because it’s slow and unsafe). You even get to add the Rust Book and its brothers to your Bible collection alongside the Arch and Gentoo Wikis. All hail Rust. TempleOS pales in religious comparison to the faith of Rustaceans. Graydon Hoare is Jesus. Amen.

»
16 месяцев назад, # |
  Проголосовать: нравится +96 Проголосовать: не нравится

I especially love it when the Rust compiler said "It's Rustin' time" and Rust'd all over the mistakes. Truly the pinnacle of Competitive Programming.

»
16 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

This suggests why you should choose a compiler that does most of the work for you while letting you focus on the core business in hand.

Looking at this blog, RUST seems great to learn in 2023.

»
16 месяцев назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

I like this semi-troll style of writing you have!