Created
August 23, 2022 23:10
-
-
Save someodd/a2fb24ab4682bfd44dda9d8720f43391 to your computer and use it in GitHub Desktop.
Codeforce Problem 821: Double Cola
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TupleSections #-} | |
{- | |
Double Cola | |
https://codeforces.com/problemset/problem/82/A | |
EFFICIENCY REQUIREMENTS | |
time limit per test: 1 second | |
memory limit per test: 256 megabytes | |
input: standard input | |
output: standard output | |
PROBLEM | |
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" | |
drink vending machine; there are no other people in the queue. The first one in | |
the queue (Sheldon) buys a can, drinks it and doubles! The resulting two | |
Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys | |
a can, drinks it and gets to the end of the queue as two Leonards, and so on. | |
This process continues ad infinitum. | |
For example, Penny drinks the third can of cola and the queue will look like | |
this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny. | |
Write a program that will print the name of a man who will drink the n-th can. | |
Note that in the very beginning the queue looks like that: Sheldon, Leonard, | |
Penny, Rajesh, Howard. The first person is Sheldon. | |
INPUT | |
The input data consist of a single integer n (1 ≤ n ≤ 109). | |
It is guaranteed that the pretests check the spelling of all the five names, | |
that is, that they contain all the five possible answers. | |
OUTPUT | |
Print the single line — the name of the person who drinks the n-th can of cola. | |
The cans are numbered starting from 1. Please note that you should spell the | |
names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the | |
quotes). In that order precisely the friends are in the queue initially. | |
EXAMPLES | |
1 -> Sheldon | |
6 -> Sheldon | |
1802 -> Penny | |
-} | |
data BigBangCharacter | |
= Sheldon | |
| Leonard | |
| Penny | |
| Rajesh | |
| Howard | |
deriving (Enum, Eq, Show) | |
queue :: [(BigBangCharacter, Int)] | |
queue = map (,1) [Sheldon .. Howard] | |
doubleCola :: Int -> [(BigBangCharacter, Int)] -> BigBangCharacter | |
doubleCola n characters = do | |
let | |
([(firstChar, clones)] :: [(BigBangCharacter, Int)], rest) = splitAt 1 characters | |
if n == 0 || clones >= n | |
then firstChar | |
else doubleCola (n - clones) (rest ++ [(firstChar, clones * 2)]) | |
tests :: IO () | |
tests = do | |
test Sheldon 1 | |
test Sheldon 6 | |
test Penny 52 | |
test Penny 1802 | |
test Leonard 7230702951 | |
where | |
test :: BigBangCharacter -> Int -> IO () | |
test c n = print $ c == doubleCola n queue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment