Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

BRLADDER - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Kamil Dębowski
Testers:Hasan Jaddouh, Sergey Kulik
Editorialist:Pawel Kacprzak

DIFFICULTY:

Cakewalk

PREREQUISITES:

Elementary math

PROBLEM:

For a given infiniteladder graph $G$ with vertices numbered from $1$, and integer $Q$, the goal is to answer $Q$ queries. Each query contains two integers $a$ and $b$ and asks if there is an edge between vertices $a$ and $b$ in $G$.

There are $3$ types of edges between vertices of $G$:

  1. There is an edge between vertices labeled with two consecutive numbers if and only if the smaller label is odd

  2. There is an edge between vertices labeled with two consecutive odd numbers

  3. There is an edge between vertices labeled with two consecutive even numbers

EXPLANATION:

In order to solve the problem, we are going to answer queries one after another. For a fixed query $(a, b)$, we simply check if $a, b$ can form an edge within any of the classes defined above. Notice that performing such a check for each class is easy:

Class 1: Checking if $a, b$ forms an edge of the first class can be done by just checking if the smaller label is odd and the difference between the larger and the smaller label is exactly $1$.

Classes 2 and 3: Checking if $a, b$ forms an edge of either the second or the third class can be done by just checking if the difference between the larger and the smaller label is exactly $2$.

Since performing each of the above checks takes a constant time, the total complexity of the solution is $O(Q)$.

AUTHOR'S AND TESTER'S SOLUTIONS:


Setter's solution can be found here.
First tester's solution can be found here.
Second tester's solution can be found here.


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>