Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 14, 2016 19:52
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/f85de0c2b6014ff79d1097ce12705691 to your computer and use it in GitHub Desktop.
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.
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