ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1135. Recruits

O(n) solution possible, but ...
Posted by Peca 29 Nov 2012 19:03
So what I am doing is to note 1 instead of > and 0 instead of <. Therefore if I have the configuration 10 then it will become 01. The 01 configuration doesn't change.
Therefore if I take the example from the problem I have

110010
101001
010101
001011
000111

So I see there is a shifting of the zero's to the left and a of one's to the right.
So I am saving in the beginning the positions of zero's in an array (zero[]) and the positions of the one's in another array (one[]).
then I consider k = one[0].

Therefore the all algorithm (after reading the data) looks practically like this

k=one[0];
for (int i=0;i<z;i++){
    if ((zero[i]-k) >=0){
     result+=(zero[i]-k);
     k++;
    }
}

I think the complexity of this is even O(z) where z is the numbers of zero's.

The problem with this is that I obtain TL on test 15. Is there anything faster or is only my coding problme (java)?

Edited by author 29.11.2012 19:22

Edited by author 29.11.2012 19:37
Re: O(n) solution possible, but ...
Posted by Flux 19 Mar 2013 08:20
The problem is in your code.
Next solution gets AC with O(n) complexity

// obtaining bool array
int pos = 0, res = 0;
for (int i = 0; i < n; i++) if (!arr[i]) res += i - pos++;