qu_bit's blog

By qu_bit, history, 3 years ago, In English

Link for the contest

A. Who is happy?

Run a loop from $$$i=0$$$ to $$$i=N-1$$$ and iterate through the string $$$S$$$, maintain two variables $$$countA$$$ and $$$countB$$$ initialised to $$$0$$$. Keep incrementing $$$countA$$$ whenever you encounter letter $$$A$$$ i.e. $$$S[i]==A$$$. Similarly keep incrementing $$$countB$$$ whenever you encounter letter $$$B$$$ i.e. $$$S[i]==B$$$. In the end just check if $$$countA > countB$$$, $$$countA < countB$$$ or $$$countA == countB$$$, and print $$$Anirban$$$, $$$Biswa$$$ or $$$Samay$$$ respectively.

Time Complexity: O( N ) for each test case

Code

B. Remainder

We can use divisibility rules of numbers from 2 to 10 but that will be a lot of work. A simple way to solve this problem is to use modular arithmetic to find N % K.

Let's say we are given a number 4367, then 4367 % K = ( 4000 + 300 + 60 + 7 ) % K = ( ( ( ( ( 4*10 + 3) % K )*10 + 6) % K)*10 + 7) % K

We take the first digit, multiply by 10, add next digit then mod by K. Then again we multiply by 10, add next digit then mod by K. We keep on doing this until we have no more digits. At the end we will be left with N % K.

Time Complexity: O( logN ) for each test case

Code

C. Tri-Prime Numbers

If a number has exactly 1 factor other than 1 and itself then it is square of a prime number. So we just need to find how many squares of prime numbers are less than N or how many prime numbers are less than √N. We can use Sieve of Eratosthenes to find and store all the prime numbers less than $$$10^6$$$ in a vector. Then we can just use upper bound on the vector to find the answer for each test case in log√N time.

Time Complexity: O( ( √N + T )log√N )

Code

D. Chess is Hard

A solution of O($$$n^2$$$) will be accepted.

As Bunty cannot lose, the opponents that are valid must have a rating strictly less than Bunty's present rating. To increase his rating in the fastest way possible, we make Bunty play(and win) against the highest rated valid opponent.

The O($$$n^2$$$) approach iterates through all the elements and tries to find the opponent, which Bunty has not played yet, with maximum rating less than Bunty's current rating $$$C$$$. Bunty plays this opponent next and his rating is updated. We take the sum of all the player's ratings, Bunty has played so far, after adding 400 to all these ratings and divide this sum by the number of players Bunty has played so far to get the new rating for Bunty. If Bunty's new rating exceeds the minimum rating to be a grandmaster then we stop and print the number of players Bunty played so far otherwise we continue this process until we run out of valid opponents or Bunty's rating exceeds the Grandmaster's rating.

We can also further optimize this solution by first sorting and using a pointer to mark the element which was the largest element less than B every time we iterate, to help find the next such required element.

Time Complexity: O( $$$n^2$$$ ), optimised version O( $$$nlogn$$$ )

Code
  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Link for the contest: https://codeforces.com/gym/309673

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

Please use spoiler to display codes