EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2024
  3. Final Round

Codey and Gardening

https://www.hackerrank.com/contests/codenection-2024-final-round-closed-category/challenges/cn24-10

Question

The city of CodeNation is having a Gardening Contest, where participants strive to grow as many plants as possible.

The progress of participants is recorded in an array of length n. Each record will have the format a p, where aia_iai​ represents the participant's name, and pip_ipi​ represents how many plants they gained or lost that month. A positive number pip_ipi​ means they added plants to their garden, while a negative number indicates that they lost some due to harsh weather.

If multiple gardeners end up with the same total, they will get the same ranking.

Zoey, the president of CodeNation, needs assistance in determining the ranking of Codey in the contest.

Input Format

The first line contains an integer n, where n represents the number of records.

The following n lines contain the participant's monthly performance in a p format in chronological order, where aia_iai​ is a string of lowercase English letters and pip_ipi​ is an integer.

Constraints

1≤n≤10001 \le n \le 10001≤n≤1000
1≤∣a∣≤321 \le |a| \le 321≤∣a∣≤32
−1000≤p≤1000-1000 \le p \le 1000−1000≤p≤1000

Output Format

Output an integer, the ranking of Codey in the contest.

Sample Inputs:

Input

3
codey 10
zoey 5
codey -5

Output

21

Explanation

Since both Codey and Zoey have 5 plants each, they share the top rank. Codey's rank is 1.


Solution - Standard Ranking System

Personally, I think this question is harder than 3rd question. This is because the ranking system is different than what I used to think.

In general terms, the ranking should be like:

1 100
2 50
2 50
4 25
...

But in this question, the ranking is like this:

1 100
2 50
2 50
3 25
...

Thanks for the second scenario, we have to end up manually code the ranking.

Otherwise, my personal favorite approach is by using dictionary method, store and update their values as line goes on, and start doing the manual sorting.

Here's the final code: (Yes, it's ugly please forgive me)

n = int(input().strip())
scores = {}
standard_ranking = {}
previous_score = None
repeated_counter = 0

for _ in range(n):
    name, score = input().strip().split()
    scores[name] = scores.get(name, 0) + int(score)

ranked_list = sorted(scores.items(), key=lambda x: x[1], reverse=True)

for i, (name, score) in enumerate(ranked_list, start=1):
    if score != previous_score:
        current_rank = i
        previous_score = score
        standard_ranking[name] = current_rank - repeated_counter
    else:
        repeated_counter += 1
        current_rank = i-1
        standard_ranking[name] = current_rank

codey_rank = standard_ranking.get("codey")

print(codey_rank)

Fun fact: This solution is not made during competition; it's after made with all test cases passed after the competition is finished.

PreviousCodey and ExitNextCodey and Symbol

Last updated 2 months ago

Python's default rank sorting method follows the first scenario. But the actual ranking they accept is the second scenario. Due to can't even see the test cases, therefore I had to emphasize that this question is harder than 3rd question. (Which is .)

🇲🇾
Codey and Symbol