-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHorseRacingHyper.cs
More file actions
80 lines (70 loc) · 2.51 KB
/
HorseRacingHyper.cs
File metadata and controls
80 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine());
var horses1 = new SortedSet<Horse>(new HorseComparerByVelocityFirst());
var horses2 = new SortedSet<Horse>(new HorseComparerByEleganceFirst());
int curMin = int.MaxValue;
for (int i = 0; i < N; i++)
{
string[] inputs = Console.ReadLine().Split(' ');
int V = int.Parse(inputs[0]);
int E = int.Parse(inputs[1]);
var h = new Horse(V, E);
if (!horses1.Add(h)) curMin = 0;
if (!horses2.Add(h)) curMin = 0;
}
curMin = Math.Min(FindMinDistance(horses1), curMin);
curMin = Math.Min(FindMinDistance(horses2), curMin);
Console.WriteLine(horses1.Count == 1 ? 0 : curMin);
Console.Read();
}
class HorseComparerByVelocityFirst : IComparer<Horse>
{
public int Compare(Horse x, Horse y)
{
if (x.velocity == y.velocity) return x.elegance.CompareTo(y.elegance);
return x.velocity.CompareTo(y.velocity);
}
}
class HorseComparerByEleganceFirst : IComparer<Horse>
{
public int Compare(Horse x, Horse y)
{
if (x.elegance == y.elegance) return x.velocity.CompareTo(y.velocity);
return x.elegance.CompareTo(y.elegance);
}
}
struct Horse
{
public int velocity;
public int elegance;
public int Total => Math.Abs(velocity - elegance);
public Horse(int Velocity, int Elegance)
{
velocity = Velocity;
elegance = Elegance;
}
}
static int FindMinDistance(IEnumerable<Horse> horses)
{
Horse prev = horses.First();
int curMin = int.MaxValue;
foreach (var horse in horses.Skip(1))
{
int distance = Math.Abs(horse.velocity - prev.velocity) + Math.Abs(horse.elegance - prev.elegance);
if (distance < curMin) curMin = distance;
prev = horse;
}
return curMin;
}
}
}