Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 14, 2016 19:32
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/49f45acc69b4d87c51d02c81a788fbc9 to your computer and use it in GitHub Desktop.
Save jianminchen/49f45acc69b4d87c51d02c81a788fbc9 to your computer and use it in GitHub Desktop.
HackerRank - university codesprint - study code - score 80 of 80, one of only two of C# solutions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
namespace ProblemSolver
{
class Solution
{
static char[] splitors = { ' ' };
/* Input data here */
static int n, s, k;
static bool[][][] f;
static int q, qn, qs, qk;
static void Prepare()
{
n = 50;
s = 200;
k = 2000;
f = new bool[n + 1][][];
for (int i = 0; i <= n; i++)
{
f[i] = new bool[s + 1][];
for (int j = 0; j <= s; j++)
{
f[i][j] = new bool[k + 1];
}
}
for (int i = 0; i <= s; i++)
{
f[0][i][0] = true;
}
for (int i = 0; i < n - 1; i++)
for (int j = 0; j <= s; j++)
for (int p = 0; p <= k; p++)
if (f[i][j][p])
for (int a0 = 0; a0 <= s; a0++)
{
int ss = j + (i + 2) * a0;
int kk = p + j;
if (ss <= s && kk <= k)
{
f[i+1][ss][kk] = true;
}
else
{
break;
}
}
}
static void Input()
{
string[] str = Console.ReadLine().Split(splitors);
qn = int.Parse(str[0]);
qs = int.Parse(str[1]);
qk = int.Parse(str[2]);
}
static void Search()
{
qn--;
if (f[qn][qs][qk] == false)
{
Console.WriteLine(-1);
}
else
{
List<int> ret = new List<int>();
while (qn > 0)
{
for (int a0 = 0; a0 <= s; a0++)
{
int ss = qs - (qn + 1) * a0;
int kk = qk - ss;
if (ss >= 0 && kk >= 0 && f[qn - 1][ss][kk])
{
qn--;
qs = ss;
qk = kk;
ret.Add(a0);
break;
}
}
}
ret.Add(qs);
for (int i = 0; i < ret.Count; i++)
{
int a = 0;
for (int j = 0; j <= i; j++)
{
a += ret[j];
}
Console.Write(a + " ");
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
Prepare();
q = int.Parse(Console.ReadLine());
for (int i = 0; i < q; i++)
{
Input();
Search();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment