Created
November 14, 2016 19:52
-
-
Save jianminchen/f85de0c2b6014ff79d1097ce12705691 to your computer and use it in GitHub Desktop.
Array construction - study code #3, only score 40 (max score 80) - study coding style as well.
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.Linq; | |
class Solution { | |
static void Main(String[] args) { | |
new Solution().Run(); | |
} | |
int n,s,k; | |
int [] buffer; | |
public void Run() | |
{ | |
int q = int.Parse(Console.ReadLine()); | |
for (int i = 0; i < q; i++) | |
{ | |
var a = Console.ReadLine().Split().Select(int.Parse).ToArray(); | |
n = a[0]; | |
s = a[1]; | |
k = a[2]; | |
buffer = new int[n]; | |
if (Sum(0, s, k, 0)) | |
{ | |
Array.Sort(buffer); | |
Console.WriteLine(string.Join(" ", buffer)); | |
} | |
else | |
{ | |
Console.WriteLine(-1); | |
} | |
} | |
} | |
public bool Sum(int index, int s, int k, int bottom) | |
{ | |
if (s<0 || k<0) return false; | |
if (index >= buffer.Length) return s == 0 && k == 0; | |
if (index == buffer.Length-1) bottom = s; | |
int prevsum = this.s - s; | |
int top = s / (n-index); | |
for (int i = bottom; i <= top; i++) | |
{ | |
int diff = index*i - prevsum; | |
buffer[index] = i; | |
if (Sum(index+1, s - i, k - diff, i)) | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment