Created
November 17, 2016 07:00
-
-
Save jianminchen/cfde9188b8fb1e13f11970fc9b1ff14d to your computer and use it in GitHub Desktop.
Kindergarten adventure - HackerRank - study code C - score 30 (maximum score 30)
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.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Diagnostics; | |
namespace ProblemSolver | |
{ | |
struct Student | |
{ | |
public int Res | |
{ | |
private set; | |
get; | |
} | |
public int Pos | |
{ | |
private set; | |
get; | |
} | |
public Student(int res, int pos) | |
{ | |
Res = res; | |
Pos = pos; | |
} | |
} | |
class StudentComparer : IComparer<Student> | |
{ | |
public int Compare(Student x, Student y) | |
{ | |
int ret = x.Res.CompareTo(y.Res); | |
if (ret == 0) | |
{ | |
return x.Pos.CompareTo(y.Pos); | |
} | |
else | |
{ | |
return ret; | |
} | |
} | |
} | |
class Solution | |
{ | |
static char[] splitors = { ' ' }; | |
/* Input data here */ | |
static int n; | |
static int[] t; | |
static int[] r; | |
static void Input() | |
{ | |
n = int.Parse(Console.ReadLine()); | |
t = new int[n]; | |
r = new int[n]; | |
string[] str = Console.ReadLine().Split(splitors); | |
for (int i = 0; i < n; i++) | |
{ | |
t[i] = int.Parse(str[i]); | |
r[i] = i - t[i]; | |
} | |
} | |
static void Search() | |
{ | |
SortedSet<Student> s = new SortedSet<Student>(new StudentComparer()); | |
for (int i = 0; i < n; i++) | |
if (r[i] >= 0) | |
{ | |
s.Add(new Student(r[i], i)); | |
} | |
int ret = s.Count; | |
int retval = 0; | |
for (int i = 1; i < n; i++) | |
{ | |
while (s.Count > 0) | |
{ | |
Student minS = s.Min; | |
if (minS.Res < i) | |
{ | |
s.Remove(minS); | |
} | |
else | |
{ | |
break; | |
} | |
} | |
if (n - 1 - t[i - 1] >= 0) | |
{ | |
s.Add(new Student(n - 1 - t[i - 1] + i, i)); | |
} | |
if (s.Count > ret) | |
{ | |
ret = s.Count; | |
retval = i; | |
} | |
} | |
Console.WriteLine(retval + 1); | |
} | |
static void Main(string[] args) | |
{ | |
Input(); | |
Search(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment