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. 2023
  3. Final Round

Codey and Zombies

https://www.hackerrank.com/contests/codenection-2023-final-round-open-category/challenges/cn-c14

PreviousCodey and School SuppliesNextCodey and Sightseeing

Last updated 2 months ago

Question

Codey is on its way back home, as usual. To get there, it has to traverse a field that can be represented as an n * m grid. However, today is different because the field is inhabited by zombies! Codey is aware that the only way to eliminate the zombies is by setting them on fire.

So, Codey has devised an ingenious plan for this task, involving the use of bombs. Codey can place a bomb at a cell (i,j)(i, j)(i,j), and doing so requires a total of ai+bja_i + b_jai​+bj​ units of fire powder. Codey is allowed to place at most one bomb on each cell (i,j)(i, j)(i,j). When Codey detonates a bomb in cell (i,j)(i, j)(i,j), it results in the burning of every cell in row i and every cell in column j. This means that all zombies in the corresponding rows and columns will be killed when Codey detonates the bombs.

What are the minimum and maximum amounts of fire powder Codey could use to guarantee the elimination of every zombie?

Input Format

The first line contains n and m, where n represents the number of rows and m represents the columns in the 2D grid.

The second line contains n integers, a1,a2,a3,...,aia_1, a_2, a_3, ..., a_ia1​,a2​,a3​,...,ai​, each representing the values fire powder for each row.

The third line contains m integers, b1,b2,b3,...,bjb_1, b_2, b_3, ..., b_jb1​,b2​,b3​,...,bj​, each representing the values fire powder for each row.

Constraints

1≤n,m≤1051 \le n, m \le 10^51≤n,m≤105
1≤ai,bi≤1051 \le a_i, b_i \le 10^51≤ai​,bi​≤105

Output Format

Output two integers, maximum and minimum amount of firepower Codey can use.

Sample Inputs:

Input

3 3
1 1 2
1 2 1

Output

7 24

Explanation


Solution - Mathes

This question test in 2 parts:

  1. How to calculate minimum using formula

  2. How to rearrange the formula of maximum so it won't get TLE.

Minimum Logic:

Based on the situation, we can now that it must have a row, or a column filled with bombs, otherwise it won't be enough to cover all the zombies. Therefore, all we need to do is:

  1. Find whichever array is longer

That's all for the minimum logic.

Maximum Logic

If we use sum(sum(row) for row in grid) instantly, you will get a TLE as generating the grid is taking too much time if our grid is very big.

Therefore, we need to think a more conventual way to find the answer of sum of every single cell.

That's all for the maximum logic.

Here's the code:

m, n = map(int, input().strip().split())
row_values = list(map(int, input().strip().split()))
col_values = list(map(int, input().strip().split()))

if m > n:
    longer_array = row_values
    shorter_array = col_values
elif m < n:
    longer_array = col_values
    shorter_array = row_values
else:
    if min(row_values) < min(col_values):
        longer_array = row_values
        shorter_array = col_values
    else:
        longer_array = col_values
        shorter_array = row_values

min_sum = min(longer_array) * len(shorter_array) + sum(shorter_array)
max_sum = sum(row_values) * n + sum(col_values) * m

print(min_sum, max_sum)

Note starting from line 6, I added those if-else condition just to speed up the process and only check both arrays if the grid is square. Otherwise, starting from line 13 should be sufficient enough to pass the constraints.

Time complexity should stick with O(n), which n is the longer array. Otherwise, just in case someone is nitpicking, O(n * m).

The picture below shows the position of the bombs that can ensure the minimum amount of fire powder used (1+1)+(1+2)+(1+1)=7(1 + 1) + (1 + 2) + (1 + 1) = 7(1+1)+(1+2)+(1+1)=7.

And this is the maximum amount of fire power Codey can use to eliminate all the zombies which is 242424.

This is because, x>y,x(y1+y2+...+yn)<y(x1+x2+...+xm)x > y, x(y_1 + y_2 + ... + y_n) < y(x_1 + x_2 + ... + x_m)x>y,x(y1​+y2​+...+yn​)<y(x1​+x2​+...+xm​)

Find the minimum value from that longest array, min(longer_array)min(longer\_array)min(longer_array)

Multiply it with length of shortest array, and add the elements on shortest arraymin(longer_array)∗len(shorter_array)+sum(shorter_array)min(longer\_array) * len(shorter\_array) + sum(shorter\_array)min(longer_array)∗len(shorter_array)+sum(shorter_array)

The easiest part should be adding all the numbers in rows/column, then multiply with length of rows/columns, sum(row)∗m+sum(column)∗nsum(row) * m + sum(column) * nsum(row)∗m+sum(column)∗n.

🇲🇾