Created
September 20, 2016 04:52
-
-
Save jianminchen/715754585de580142d514905c3ab4fea to your computer and use it in GitHub Desktop.
HackerRank Stryker code sprint - study code - D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
class Solution { | |
public class Point : IComparable { | |
public double x, y, z; | |
public int index; | |
public Point(double xx, double yy, double zz, int ii) { | |
x = xx; y = yy; z = zz; index = ii; | |
} | |
public int CompareTo(Object o) { | |
Point p = (Point)o; | |
return this.z.CompareTo(p.z); | |
} | |
} | |
static void Main(String[] args) { | |
string[] line = Console.ReadLine().Split(new char[0], StringSplitOptions.RemoveEmptyEntries); | |
int n = Int32.Parse(line[0]); | |
int b = Int32.Parse(line[1]); | |
Point[] points = new Point[n]; | |
for(int i=0; i<n; i++) { | |
line = Console.ReadLine().Split(new char[0], StringSplitOptions.RemoveEmptyEntries); | |
points[i] = new Point(Double.Parse(line[1]), Double.Parse(line[2]), | |
Double.Parse(line[3]), Int32.Parse(line[0])); | |
} | |
Array.Sort(points); | |
string s; | |
Dictionary<int, Point> dict = new Dictionary<int, Point>(); | |
int index = n-1; | |
for(int i=0; i<b; i++) { | |
dict.Add(points[index].index, points[index]); | |
index--; | |
} | |
s = Console.ReadLine(); | |
while(s != null && s.Trim().Length > 0) { | |
line = s.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); | |
int z = Int32.Parse(line[1]); | |
switch(line[0]) { | |
case "F": | |
case "f": | |
if(dict.ContainsKey(z)) { | |
Point point = dict[z]; | |
Console.WriteLine(z + " = (" + point.x.ToString("F3") + "," | |
+ point.y.ToString("F3") + "," + point.z.ToString("F3") + ")"); | |
} else { | |
Console.WriteLine("Point doesn't exist in the bucket."); | |
} | |
break; | |
case "R": | |
case "r": | |
if(dict.ContainsKey(z)) { | |
if(index >= 0) { | |
dict.Remove(z); | |
dict.Add(points[index].index, points[index]); | |
index--; | |
Console.WriteLine("Point id " + z + " removed."); | |
} else { | |
Console.WriteLine("No more points can be deleted."); | |
} | |
} else { | |
Console.WriteLine("Point doesn't exist in the bucket."); | |
} | |
break; | |
} | |
s = Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment