problem:
There is an infinite integer grid where N people live in N different houses. They decide to create a meeting point at one person’s house.
From any given cell, all 8 adjacent cells are reachable in 1 unit of time, e.g. (x,y) can be reached from (x-1,y+1) in one unit of time. Find a common meeting place which minimizes the combined travel time of everyone.
Input Format
N for N houses.
The following N lines will contain two integers for the x and y coordinate of the nth house.
Output Format
M for the minimum sum of all travel times when everyone meets at the best location.
Constraints
N <= 105
The absolute value of each co-ordinate in the input will be at most 109
HINT: Please use long long 64-bit integers;
Input #1
4
0 1
2 5
3 1
4 0
Output #1
8
Explanation
The houses will have a travel-sum of 11, 13, 8 or 10. 8 is the minimum.
Input #2
6
12 -14
-3 3
-14 7
-14 -3
2 -12
-1 -6
Output #2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Solution
{
static string[] input = new string[2];
static void Main(string[] args)
{
long N = Int64.Parse(Console.ReadLine());
long[] X = new long[N];
long[] Y = new long[N];
long results = 0;
double avg_x = 0;
double avg_y = 0;
double min = 0;
int point = 0;
for (int c = 0; c {
input = Console.ReadLine().Split(' ');
X[c] = long.Parse(input[0]);
Y[c] = long.Parse(input[1]);
}
for (int i = 0; i {
avg_x += (double)X[i] / N;
avg_y += (double)Y[i] / N;
}
min = Math.Sqrt(((avg_x - X[0]) * (avg_x - X[0])) + ((avg_y - Y[0]) * (avg_y - Y[0])));
for (int i = 1; i < N; i++)
{
double diff = Math.Sqrt(((avg_x - X[i]) * (avg_x - X[i])) + ((avg_y - Y[i]) * (avg_y - Y[i])));
if (diff {
min = diff;
point = i;
}
}
for (int i = 0; i {
results += Math.Max((Math.Abs(X[point] - X[i])), (Math.Abs(Y[point] - Y[i])));
}
Console.WriteLine(results);
}
}
There is an infinite integer grid where N people live in N different houses. They decide to create a meeting point at one person’s house.
From any given cell, all 8 adjacent cells are reachable in 1 unit of time, e.g. (x,y) can be reached from (x-1,y+1) in one unit of time. Find a common meeting place which minimizes the combined travel time of everyone.
Input Format
N for N houses.
The following N lines will contain two integers for the x and y coordinate of the nth house.
Output Format
M for the minimum sum of all travel times when everyone meets at the best location.
Constraints
N <= 105
The absolute value of each co-ordinate in the input will be at most 109
HINT: Please use long long 64-bit integers;
Input #1
4
0 1
2 5
3 1
4 0
Output #1
8
Explanation
The houses will have a travel-sum of 11, 13, 8 or 10. 8 is the minimum.
Input #2
6
12 -14
-3 3
-14 7
-14 -3
2 -12
-1 -6
Output #2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Solution
{
static string[] input = new string[2];
static void Main(string[] args)
{
long N = Int64.Parse(Console.ReadLine());
long[] X = new long[N];
long[] Y = new long[N];
long results = 0;
double avg_x = 0;
double avg_y = 0;
double min = 0;
int point = 0;
for (int c = 0; c
input = Console.ReadLine().Split(' ');
X[c] = long.Parse(input[0]);
Y[c] = long.Parse(input[1]);
}
for (int i = 0; i
avg_x += (double)X[i] / N;
avg_y += (double)Y[i] / N;
}
min = Math.Sqrt(((avg_x - X[0]) * (avg_x - X[0])) + ((avg_y - Y[0]) * (avg_y - Y[0])));
for (int i = 1; i < N; i++)
{
double diff = Math.Sqrt(((avg_x - X[i]) * (avg_x - X[i])) + ((avg_y - Y[i]) * (avg_y - Y[i])));
if (diff
min = diff;
point = i;
}
}
for (int i = 0; i
results += Math.Max((Math.Abs(X[point] - X[i])), (Math.Abs(Y[point] - Y[i])));
}
Console.WriteLine(results);
}
}
No comments:
Post a Comment