Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created August 28, 2016 19:33
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/18f3cd2321cfee9226d740e239ec8418 to your computer and use it in GitHub Desktop.
Save jianminchen/18f3cd2321cfee9226d740e239ec8418 to your computer and use it in GitHub Desktop.
HackerRank world code sprint #6 - study code #1 - BrontTrousle
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Numerics;
class Solution {
static void Main(String[] args) {
var t = int.Parse(Console.ReadLine());
for(var test = 0; test < t; test++){
var nkb = Console.ReadLine().Split(' ');
var n = BigInteger.Parse(nkb[0]);
var k = BigInteger.Parse(nkb[1]);
var b = BigInteger.Parse(nkb[2]);
Console.WriteLine(BuyBoxes(n, k, b));
}
}
static string BuyBoxes(BigInteger n, BigInteger k, BigInteger b){
BigInteger count = 0;
BigInteger sum = 0;
if(n < b*(b + 1) / 2){
return "-1";
}
if( n > (k*(k + 1) / 2 - (k - b)*(k - b + 1) / 2)){
return "-1";
}
BigInteger diff = n - b*(b + 1) / 2;
var sb = new StringBuilder();
var tochange = b;
var maxvalue = k;
while(tochange > 0){
BigInteger change = BigInteger.Min(maxvalue - tochange, diff);
sb.Append(tochange + change);
sum += tochange + change;
count++;
sb.Append(' ');
diff -= change;
maxvalue = tochange + change - 1;
tochange--;
}
if(sum != n){
throw new NotImplementedException();
}
if(count != b){
throw new Exception();
}
return sb.ToString().TrimEnd();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment