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 Jutsu

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

Question

Codey has mastered the clone jutsu recently, and it's now on a mission with its weapon to fire n aliens.

Codey creates n copies of itselves, each positioned on the y-axis of a 2D plane, while the aliens are positioned on the x-axis. No one will be at the origin, and they can't move around. Every "Codey" should fire exactly one alien. If Codey at point (a,b)(a,b)(a,b) uses his weapon to fire an alien at point (c,d)(c, d)(c,d), the fire distance will be (a−c)2+(b−d)2\sqrt{(a-c)^2+(b-d)^2}(a−c)2+(b−d)2​ (the distance between the points).

Input Format

The first line contains an integer n, where n represents the number of Codey copies and aliens.

The following 2∗n2*n2∗n lines contain two space-separated integers x and y, which represent a clone or alien's position.

Constraints

1≤n≤1051 \le n \le 10^51≤n≤105
−105≤x,y≤105-10^5 \le x,y\le10^5−105≤x,y≤105
  • It is guaranteed that no point is at the origin.

  • It is guaranteed that the number of points on the x-axis is equal to n and the number of points on the y-axis is equal to n.

Output Format

Output the minimal sum of all fire distances. Your answer is considered correct if its absolute or relative error does not exceed 10−910^{-9}10−9.

Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if ∣a−b∣max(1,∣b∣)≤10−9\frac{|a-b|}{max(1,|b|)}\le10^{-9}max(1,∣b∣)∣a−b∣​≤10−9.

To set precision when printing values you can use:

  • print(f"{distance:.9f}") in Python;

  • std::cout << fixed << std::setprecision(9) << distance << std::endl; in C++;

  • System.out.printf("%.9f\n", distance); in Java;

  • console.log(distance.toFixed(9)); in JavaScript;

  • printf("%.9f\n", distance); in C.

Sample Inputs:

Input

2
-1 0
0 6
0 2
4 0

Output

9.447170528427769

Explanation

Codey(s) are at (0,2)(0,2)(0,2) and (0,6)(0,6)(0,6), while the aliens are at (−1,0)(-1,0)(−1,0) and (4,0)(4,0)(4,0). To minimize the total fire distance, we pair Codey 1 with Alien 1, and Codey 2 with Alien 2:

Hence, the minimum sum of all fire distances will be 52+5\sqrt{52} + \sqrt{5}52​+5​.


Solution - Distance Formula

Basically, the negative coordinates are same as positive coordinates. The first thing we can do is flip the negative jutsu and aliens onto positive side.

Then,

Here's the code:

import math

n = int(input())
x_arr = [0] * n * 2
y_arr = [0] * n * 2
for i in range(n * 2):
    x, y = map(int, (input().split()))
    x_arr[i], y_arr[i] = abs(x), abs(y)

x_arr = sorted(x for x in x_arr if x != 0)
y_arr = sorted(y for y in y_arr if y != 0)

print(sum(math.sqrt((x ** 2) + (y ** 2)) for x,y in zip(x_arr,y_arr)))

Note that in line 13, I used the one-liner to attempt the question. If you want breakdowns, there you go:

Start from Line 13
distances = []
for x, y in zip(x_arr, y_arr):
    distance = math.sqrt((x ** 2) + (y ** 2))
    distance.append(distance)

print(sum(distances))
PreviousCodey and Rectangles 2NextCodey and Toy Kingdom 2

Last updated 2 months ago

🇲🇾