Created
August 28, 2016 19:35
-
-
Save jianminchen/835965989b54834583266b6dba5fa570 to your computer and use it in GitHub Desktop.
BrontTrousle - HackerRank world code sprint #6 - study code #2
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.Text; | |
using System.Linq; | |
class Solution { | |
static void Main(String[] args) { | |
var sb = new StringBuilder(); | |
int _tc_ = int.Parse(Console.ReadLine()); | |
while (_tc_-- > 0) { | |
var tmp = Console.ReadLine().Split(' '); | |
long n = long.Parse(tmp[0]); // buy these sticks | |
long k = long.Parse(tmp[1]); // they have these boxes | |
long b = long.Parse(tmp[2]); // buy these boxes | |
sb.Append(Skeleton(n, k, b)).Append("\n"); | |
} | |
sb.Length--; | |
Console.WriteLine(sb.ToString()); | |
} | |
static string Skeleton(long n, long k, long b) { | |
List<long> sticks = new List<long>(); | |
while (b > 0 && k > 0) { | |
long r = one23(b - 1); | |
var cur = Math.Min(k, n - r); | |
sticks.Add(cur); | |
k = cur - 1; | |
n -= cur; | |
b--; | |
} | |
if (b != 0 || n != 0) return "-1"; | |
return string.Join(" ", sticks); | |
} | |
static long one23(long n) { return (n * (n + 1)) / 2; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment