Created
September 20, 2016 04:44
-
-
Save jianminchen/5dc56ab8d69496c0c12e35ab5ccbeb73 to your computer and use it in GitHub Desktop.
HackerRank world code sprint - study C# submission -
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.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
public class Solver | |
{ | |
public void Solve() | |
{ | |
int n = ReadInt(); | |
int m = ReadInt(); | |
var a = new Tuple<int, double, double, double>[n]; | |
for (int i = 0; i < n; i++) | |
a[i] = Tuple.Create(ReadInt(), ReadDouble(), ReadDouble(), ReadDouble()); | |
Array.Sort(a, (a1, a2) => a2.Item4.CompareTo(a1.Item4)); | |
var d = new Dictionary<int, Tuple<int, double, double, double>>(); | |
int p = 0; | |
while (d.Count < m) | |
{ | |
d[a[p].Item1] = a[p]; | |
p++; | |
} | |
while (reader.Peek() != -1) | |
{ | |
char t = ReadToken().ToUpper()[0]; | |
int x = ReadInt(); | |
if (t == 'F') | |
{ | |
if (d.ContainsKey(x)) | |
writer.WriteLine("{0} = ({1:0.000},{2:0.000},{3:0.000})", x, d[x].Item2, d[x].Item3, d[x].Item4); | |
else | |
Write("Point doesn't exist in the bucket."); | |
} | |
else | |
{ | |
if (!d.ContainsKey(x)) | |
Write("Point doesn't exist in the bucket."); | |
else | |
{ | |
if (p == n) | |
Write("No more points can be deleted."); | |
else | |
{ | |
writer.WriteLine("Point id {0} removed.", x); | |
d.Remove(x); | |
d[a[p].Item1] = a[p]; | |
p++; | |
} | |
} | |
} | |
} | |
} | |
#region Main | |
protected static TextReader reader; | |
protected static TextWriter writer; | |
static void Main() | |
{ | |
#if DEBUG | |
reader = new StreamReader("..\\..\\input.txt"); | |
//reader = new StreamReader(Console.OpenStandardInput()); | |
writer = Console.Out; | |
//writer = new StreamWriter("..\\..\\output.txt"); | |
#else | |
reader = new StreamReader(Console.OpenStandardInput()); | |
writer = new StreamWriter(Console.OpenStandardOutput()); | |
//reader = new StreamReader("input.txt"); | |
//writer = new StreamWriter("output.txt"); | |
#endif | |
try | |
{ | |
new Solver().Solve(); | |
//var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128); | |
//thread.Start(); | |
//thread.Join(); | |
} | |
catch (Exception ex) | |
{ | |
#if DEBUG | |
Console.WriteLine(ex); | |
#else | |
throw; | |
#endif | |
} | |
reader.Close(); | |
writer.Close(); | |
} | |
#endregion | |
#region Read / Write | |
private static Queue<string> currentLineTokens = new Queue<string>(); | |
private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); } | |
public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); } | |
public static int ReadInt() { return int.Parse(ReadToken()); } | |
public static long ReadLong() { return long.Parse(ReadToken()); } | |
public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); } | |
public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); } | |
public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); } | |
public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); } | |
public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; } | |
public static int[][] ReadAndTransposeIntMatrix(int numberOfRows) | |
{ | |
int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][]; | |
for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret; | |
} | |
public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; } | |
public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); } | |
public static void Write(params object[] array) { WriteArray(array); } | |
public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); } | |
private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue> | |
{ | |
public new TValue this[TKey key] | |
{ | |
get { return ContainsKey(key) ? base[key] : default(TValue); } | |
set { base[key] = value; } | |
} | |
} | |
private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; } | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment