Peteris's blog

By Peteris, 13 years ago, In English

I wrote the following solution in CF #72 for Problem B in Ruby:

  1. n, k = gets.split.map{|_|_.to_i}
  2. a = gets.split.map{|_|_.to_i}

  3. b = a.sort
  4. if (a.length == 1 ? a[0] : a.inject{|x,y| x + y}) <= k
  5.   puts -1
  6. else
  7.   i = 0
  8.   s = 0
  9.   while (b[i] - s) * (n - i) <= k
  10.     k -= (b[i] - s) * (n - i)
  11.     s = b[i]
  12.     i += 1
  13.   end
  14.   while b[i] - s == 0
  15.     i += 1
  16.   end

  17.   t = k / (n - i)
  18.   k -= t * (n - i)

  19.   j = 0
  20.   while k > 0
  21.     if a[j] >= b[i]
  22.       a[j] -= 1
  23.       k -= 1
  24.     end
  25.     j += 1
  26.   end

  27.   ans = ""
  28.   for k in j...n
  29.     if a[k] > t + s
  30.       ans += (k + 1).to_s + " "
  31.     end
  32.   end
  33.   for k in 0...j
  34.     if a[k] > t + s
  35.       ans += (k + 1).to_s + " "
  36.     end
  37.   end
  38.   puts ans.strip
  39. end

It worked fine on my computer, but on the server, it fails for case 5:

Test: #5, time: 80 ms., memory: 3408 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER
Input
1 1
1
Output
-1
Answer
 
Checker Log
wrong answer Output contains longer sequence [length = 1], but answer contains 0 elements

Any Ruby gurus have a clue?
  • Vote: I like it
  • 0
  • Vote: I do not like it

13 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

I do not know ruby, but it seems you output -1, but actually you should output  nothing in this case.

In the problem statement:

Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.