Is the following solution for this greedy problem correct?

Правка en1, от xuanquang1999, 2017-03-26 20:01:07

One of my friend recently asked me the following problem:

"Given an array a1, a2, ..., an and a number K (n <  = 105, ai <  = 109). Is there a way split the array into K disjoint sub-arrays (don't have to be contiguous) such that sum of each sub-array is equal to each other (and equal to )?"

My teacher came up with the following greedy solution: for each sub-array, keep choosing the maximum number such that sum of sub-array with that number is still smaller or equal to S. When it's impossible to add more number from a to this sub-array, check if the current sum is equal to S.

However, both my teacher and me are unable to neither prove the correctness of the solution nor find a counter-example for it. Is this greedy solution always optimal? How to prove (or disprove) it? Thanks in advance.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский xuanquang1999 2017-03-26 20:01:40 12 Tiny change: 'r $K$ ($n <= 10^5$, $a_i <= 10^9$). I' -> 'r $K$ ($n \leq 10^5$, $a_i \leq 10^9$). I'
en1 Английский xuanquang1999 2017-03-26 20:01:07 898 Initial revision (published)