Redaksiya
Note that if the numbers and are placed in one group, and and are placed in the other (so that the sum of numbers in each group is equal), the problem reduces to a similar problem of smaller size, namely, dividing the numbers into two groups.
For , the numbers are divided into groups as follows:
If three numbers remain, they can be divided into two groups: and .
If two numbers remain, they can be divided into groups: и . The difference between the sums of the groups will be .
If one number remains, it can be placed in either group. The difference between the sums of the groups will be .
If the sum of all numbers from to is even, they can be divided into two groups with equal sums.
If the sum of all numbers from to is odd, they can be divided into two groups with a sum difference of .
Example
Let . The numbers are placed into groups as follows:
Let . The numbers are placed into groups as follows:
Algorithm realization
Read the input value .
scanf("%d", &n);
Initialize the initial sums of the numbers in the groups, and to .
s1 = s2 = 0;
Iterate through the numbers from down to .
for (i = n; i > 0; i--)
If , place the number into the first group. Otherwise, place the number into the second group.
if (s1 < s2) { s1 += i; a.push_back(i); } else { s2 += i; b.push_back(i); }
Print the sizes of the groups.
printf("%d %d\n", a.size(), b.size());
Print the numbers of the first group.
for (i = 0; i < a.size(); i++) printf("%d ", a[i]); printf("\n");
Print the numbers of the second group.
for (i = 0; i < b.size(); i++) printf("%d ", b[i]); printf("\n");