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. Preliminary Round

Codey and Coins

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

Question

Codey is hanging around a bustling market when it spots n money bags scattered along a straight path. This market can be visualized as a one-dimensional axis, with Codey starting at position x=0x=0x=0. Codey wants to gather these coins without drawing too much attention, so it follows a specific pattern to avoid being noticed.

Each money bag i is located at position at and contains cic_ici​ coins. At the start, Codey can choose to walk either to the right or to the left. Each time Codey collects the coins from the money bag at a specific position, it must reverse its direction and continue its search for money bags in the opposite direction.

Codey will keep switching directions and collect coins until there are no more money bags available in the direction it is facing that it hasn’t already collected.

What is the maximum number of coins Codey can gather?

Input Format

The first line contains an integer, n, where n represents the number of money bags in the market.

The following n lines contains two integers, xix_ixi​and cic_ici​, where xix_ixi​ represents the position of the i-th money bag and cic_ici​ represents the number of coins in it.

Constraints

1≤n≤1001 \le n \le 1001≤n≤100
−105≤xi≤105,xi≠0-10^5 \le x_i \le 10^5, x_i \ne 0−105≤xi​≤105,xi​=0
1≤ci≤1051 \le c_i \le 10^51≤ci​≤105
  • It's guaranteed that there's at most one money bag at each x-coordinate.

  • There are no money bag at x = 0.

Output Format

Output the maximum number of coins Codey can collect.

Sample Inputs:

Input

4
4 8
1 5
-2 5
-1 9

Output

27

Explanation

There are 4 money bags, each positioned at -2, -1, 1 and 4. Codey starts at 0 and collects coins by moving as follows:

  1. Moves right to 1,

  2. Reverses direction and moves left to -1,

  3. Reverses direction and moves right to 4,

  4. Reverses direction and moves left to -2.

Hence, the maximum number of coin collected is 5+9+8+5=275+9+8+5=275+9+8+5=27.


Solution - Cocktail Method

I named it cocktail method because Codey had to shake the cocktail left and right, just like how he steals the money XD

Actually, the solution is simple. First, separate the positive coords and negative coords into 2 different arrays.

The positive array is sorted ascending order, and the negative array is sorted descending order.

After that, trim the longer array to the size of shorter array.

Then, we begin the cocktail shake, which uses 2 pointers to move through both of the arrays and collects the money.

But there's an issue - if the difference of length of two arrays is 1, Codey is possible to get all of the money if he starts from opposite direction.

Therefore, I made Codey to move positive array first, just in case if the negative array had all traversed, Codey can move once more positive to get the last bag of money. Otherwise, the iteration ends and outputs the sum of money.

Here's my code in advance:

t = int(input().strip())
neg_arr = []
pos_arr = []
total = 0

for a0 in range(t):
    n = list(map(int, input().strip().split()))
    if int(n[0]) < 0:
        neg_arr.append(n)
    else:
        pos_arr.append(n)

neg_arr = sorted(neg_arr, reverse=True)
pos_arr = sorted(pos_arr)

if len(neg_arr) > len(pos_arr):
    pos_arr = pos_arr[:len(neg_arr) + 1]
else:
    neg_arr = neg_arr[:len(neg_arr) + 1]

for i in range(max(len(pos_arr), len(neg_arr))):
    try:
        total += neg_arr[i][1]
    except:
        total += pos_arr[i][1]
        break
    try:
        total += pos_arr[i][1]
    except:
        break

print(total)

Note that I used try, except and break method so I don't have to explicitly care if both of the length of array is same, which potentially running out of index.

PreviousCodey and SpamNextCodey and Rectangles

Last updated 2 months ago

🇲🇾