Here is my problem:
- There are n companies distributing
products. - All products should be distributed in k days
- Distributing products of company Ci should be consecutive – it means that it can be distributed on days 2,3,4,5 but not 2,3,6,7
- number of distributed products by company Ci on day j should be less than (or equal) on day j-1 (if there were any on day j-1)
- difference between distributed products between days i and j should not be greater than 1
Example:
We have 3 days to distribute products. Products of company A: a,a,a,a,a. Products of company B: b,b,b. Products of company C: c,c
Fair distribution:
[aab,aabc,abc]
Invalid distribution:
[aabc,aabc,ab]
because on 1st day there are 4 products, on 3rd day 2 products (difference > 1)
Invalid distribution:
[abc,aabc,aab]
because on 1st day there is one product A, and on 2nd day there are 2 products A, so distribution of product A is not non-decreasing
EDIT
if there is a case that makes fair distribution impossible please provide it with short description, I’ll accept the answer
Gareth Rees’s comment on djna’s answer is right — the following counterexample is unsolvable:
I tested this with the following dumbest-possible brute-force Perl program (which takes well under a second, despite being very inefficient):
Please have a look and verify that I haven’t made any stupid mistakes. (I’ve omitted
max()andmin()for brevity — they just do what you’d expect.)