Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created September 20, 2016 01:48
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/9cdbdcefd84e3cb8e709eabf705ce4b0 to your computer and use it in GitHub Desktop.
Save jianminchen/9cdbdcefd84e3cb8e709eabf705ce4b0 to your computer and use it in GitHub Desktop.
HackerRank - Stryker code sprint - Kth Zero - study the code
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
/// <summary>
/// https://www.hackerrank.com/contests/stryker-codesprint/challenges/kth-zero
/// </summary>
class Solution4
{
static void Main(String[] args)
{
TextReader tIn = Console.In;
TextWriter tOut = Console.Out;
int[] nm = tIn.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToArray();
int N = nm[0];
int M = nm[1];
int[] A = tIn.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToArray();
List<int> list = new List<int>();
for (int i = 0; i < A.Length; i++)
if (A[i] == 0)
list.Add(i);
for (int q = 0; q < M; q++)
{
string[] Q = tIn.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
if (Q[0] == "1")
{
int k = int.Parse(Q[1]);
if (k <= list.Count)
tOut.WriteLine(list[k - 1]);
else
tOut.WriteLine("NO");
continue;
}
if (Q[0] == "2")
{
int p = int.Parse(Q[1]);
int x = int.Parse(Q[2]);
if (A[p] == 0 && x == 0 || A[p] != 0 && x != 0) continue;
A[p] = x;
int index = list.BinarySearch(p);
if (x == 0)
list.Insert(~index, p);
else
list.RemoveAt(index);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment