给定整数S , N , K , L和R ,其中S必须分布在大小为N的数组中,以使每个元素都必须在[L,R]范围内,并且数组中K个元素的总和应大于剩余的N – K个元素的总和等于S k,并且这些元素的顺序不增加。
例子:
Input: N = 5, K = 3, L = 1, R = 3, S = 13, Sk = 9
Output: 3 3 3 2 2
Input: N = 5, K = 3, L = 1, R = 3, S = 15, Sk = 9
Output: 3 3 3 3 3
方法:如果可以将S k平均分配到k个元素中,则将S k / k存储到数组的所有前k个元素中,否则数组的第一个元素将是(S k / k)+(S k %k)且剩余的k – 1个元素将是(S k – S k %k)%k –1。类似地,将SS k分配到nk个元素中。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function for the distribution of the number
void distribution(int n, int k, int l,
int r, int S, int Sk)
{
int a[n];
int len = k, temp, rem, s;
int diff = S - Sk;
for (int i = 0; i < len; i++) {
// Distribute the number
// among k elements
temp = Sk / k;
rem = Sk % k;
if (temp + rem >= l && temp + rem <= r) {
a[i] = temp;
}
else if (temp + rem > r) {
a[i] = r;
}
else if (temp + rem < r) {
cout << "-1";
return;
}
Sk = Sk - a[i];
k = k - 1;
}
// If there is some remaining
// sum to distribute
if (Sk > 0) {
cout << "-1";
return;
}
// If there are elements remaining
// to distribute i.e. (n - k)
if (len) {
k = n - len;
for (int i = len; i < n; i++) {
// Divide the remaining sum into
// n-k elements
temp = diff / k;
rem = diff % k;
if (temp + rem >= l && temp + rem <= r) {
a[i] = temp;
}
else if (temp + rem > r) {
a[i] = r;
}
else if (temp + rem < r) {
cout << "-1";
return;
}
diff = diff - a[i];
k = k - 1;
}
if (diff) {
cout << "-1";
return;
}
}
// Print the distribution
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
}
// Driver code
int main()
{
int n = 5, k = 3, l = 1,
r = 5, S = 13, Sk = 9;
distribution(n, k, l, r, S, Sk);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function for the distribution of the number
static void distribution(int n, int k, int l,
int r, int S, int Sk)
{
int a[] = new int[n];
int len = k, temp, rem, s;
int diff = S - Sk;
for (int i = 0; i < len; i++)
{
// Distribute the number
// among k elements
temp = Sk / k;
rem = Sk % k;
if (temp + rem >= l && temp + rem <= r)
{
a[i] = temp;
}
else if (temp + rem > r)
{
a[i] = r;
}
else if (temp + rem < r)
{
System.out.print("-1");
return;
}
Sk = Sk - a[i];
k = k - 1;
}
// If there is some remaining
// sum to distribute
if (Sk > 0)
{
System.out.print("-1");
return;
}
// If there are elements remaining
// to distribute i.e. (n - k)
if (len != 0)
{
k = n - len;
for (int i = len; i < n; i++)
{
// Divide the remaining sum into
// n-k elements
temp = diff / k;
rem = diff % k;
if (temp + rem >= l && temp + rem <= r)
{
a[i] = temp;
}
else if (temp + rem > r)
{
a[i] = r;
}
else if (temp + rem < r)
{
System.out.print("-1");
return;
}
diff = diff - a[i];
k = k - 1;
}
if (diff != 0)
{
System.out.print("-1");
return;
}
}
// Print the distribution
for (int i = 0; i < n; i++)
{
System.out.print(a[i] + " ");
}
}
// Driver code
public static void main (String[] args)
{
int n = 5, k = 3, l = 1,
r = 5, S = 13, Sk = 9;
distribution(n, k, l, r, S, Sk);
}
}
// This code is contributed by AnkitRai01
Python3
# Python implementation of the approach
# Function for the distribution of the number
def distribution(n, k, l, r, S, Sk):
a = [0] * n;
len = k;
temp, rem, s = 0, 0, 0;
diff = S - Sk;
for i in range(len):
# Distribute the number
# among k elements
temp = Sk / k;
rem = Sk % k;
if (temp + rem >= l and temp + rem <= r):
a[i] = temp;
elif(temp + rem > r):
a[i] = r;
elif(temp + rem < r):
print("-1");
return;
Sk = Sk - a[i];
k = k - 1;
# If there is some remaining
# sum to distribute
if (Sk > 0):
print("-1");
return;
# If there are elements remaining
# to distribute i.e. (n - k)
if (len != 0):
k = n - len;
for i in range(len, n):
# Divide the remaining sum into
# n-k elements
temp = diff / k;
rem = diff % k;
if (temp + rem >= l and temp + rem <= r):
a[i] = temp;
elif(temp + rem > r):
a[i] = r;
elif(temp + rem < r):
print("-1");
return;
diff = diff - a[i];
k = k - 1;
if (diff != 0):
print("-1");
return;
# Print the distribution
for i in range(n):
print(int(a[i]), end=" ");
# Driver code
if __name__ == '__main__':
n, k, l, r, S, Sk = 5, 3, 1, 5, 13, 9;
distribution(n, k, l, r, S, Sk);
# This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GFG
{
// Function for the distribution of the number
static void distribution(int n, int k, int l,
int r, int S, int Sk)
{
int []a = new int[n];
int len = k, temp, rem;
int diff = S - Sk;
for (int i = 0; i < len; i++)
{
// Distribute the number
// among k elements
temp = Sk / k;
rem = Sk % k;
if (temp + rem >= l && temp + rem <= r)
{
a[i] = temp;
}
else if (temp + rem > r)
{
a[i] = r;
}
else if (temp + rem < r)
{
Console.Write("-1");
return;
}
Sk = Sk - a[i];
k = k - 1;
}
// If there is some remaining
// sum to distribute
if (Sk > 0)
{
Console.Write("-1");
return;
}
// If there are elements remaining
// to distribute i.e. (n - k)
if (len != 0)
{
k = n - len;
for (int i = len; i < n; i++)
{
// Divide the remaining sum into
// n-k elements
temp = diff / k;
rem = diff % k;
if (temp + rem >= l && temp + rem <= r)
{
a[i] = temp;
}
else if (temp + rem > r)
{
a[i] = r;
}
else if (temp + rem < r)
{
Console.Write("-1");
return;
}
diff = diff - a[i];
k = k - 1;
}
if (diff != 0)
{
Console.Write("-1");
return;
}
}
// Print the distribution
for (int i = 0; i < n; i++)
{
Console.Write(a[i] + " ");
}
}
// Driver code
public static void Main(String[] args)
{
int n = 5, k = 3, l = 1,
r = 5, S = 13, Sk = 9;
distribution(n, k, l, r, S, Sk);
}
}
// This code is contributed by PrinciRaj1992
输出:
3 3 3 2 2