Created
August 28, 2016 19:33
-
-
Save jianminchen/18f3cd2321cfee9226d740e239ec8418 to your computer and use it in GitHub Desktop.
HackerRank world code sprint #6 - study code #1 - BrontTrousle
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.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