Codeforces Round #373 Div. 2 review

Link Codeforces Round #373 Div. 2 (Contest 719) A. Vitya in the Countryside Problem Basically, check last 2 character to determine its direction is “UP” or “DOWN”. Special case comes when the last number is 0 or 15, in that case the direction changes and you can determine the direction without looking second last character.

B. Anatoly and Cockroaches Problem We can consider 2 patterns for final alignment. Pattern 0 is to align “rbrbrb…” and pattern 1 is to align “brbrbr…”. We will compare the original alignment and final alignment. For each position i, we need to change the color to… Case A: r to b Case B: b to r If […]

Continue reading →

Codeforces Round #368 Div. 2 review

Link Codeforces Round #368 Div. 2 (Contest 707)   A. Brain’s Photos Problem Just need to check if color (Cyan, Magenta or Yellow) exists or not in the photo.

  B. Bakery Problem The problem sentence looks long, but after summarize, you just need to find the path between flour storage city and non flour storage city with minimum distance. But to find out these city path, I was using vector which is not fast enough and I got TLE during contest.

  We can make search fast, in constant time by using Array to set a type of city.

  C. Pythagorean Triples Problem Consider some cases to […]

Continue reading →

scanf and printf are much faster than cin and cout

When I’m solving the Codeforces problem, 707D. Persistent Bookcase, I made some comparison for the performance of I/O function. scanf() and printf() cin >> and cout <<   Test Let’s start from C++ common way, below code uses cin >> and cout <<

It took 748 ms. Next, (old-school,) C way of implementation using scanf() and printf()

It took only 171 ms.   Result function time scanf() and printf() 171 ms cin >> and cout << 748 ms In this experiment, it appears that the speed of cin and cout is much much slower than scanf() and printf(), the difference is indeed quite huge. The overhead of cin and cout took more than 500 ms! You might get a […]

Continue reading →