Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created September 20, 2016 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/715754585de580142d514905c3ab4fea to your computer and use it in GitHub Desktop.
Save jianminchen/715754585de580142d514905c3ab4fea to your computer and use it in GitHub Desktop.
HackerRank Stryker code sprint - study code - D
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