BEARSEG - Editorial

arr[-1]=0; Is it legally? :slight_smile:

I thought that too. But itā€™s working everywhere (GCC, Codechef Ide, Ideone), doesnā€™t it? But during submission on Codechef, it throws WA. Iā€™m looking for alternative of that but as of now, please let me know if Iā€™ve done something wrong there.

1
5 5
5 5 5 5 5
Your solution output 20, but answer is 15, your process empty segments

1 Like

just 5 5 0 0 0 0 0? what about p?

Why N log^2 N? For me, itā€™s N log N

Got it. 1 5 5 5 5 5 5 5 it is.

bin.search in set is log^2, is not it?

No, log(N)

1 Like

Ok, thanks

Ok, but i think your solution is too hard for novice, so letā€™s do this 1) letā€™s go from i=0 to n and add a[i] to sum 2) then we need the first number >=(sum+1+p)%p because (sum-(sum+1)+p)%p=p-1; so letā€™s make bin.search to the set for number>=(sum+1+p)%p so itā€™s work Nlog(N) (link: Y8l5mm - Online C++0x Compiler & Debugging Tool - Ideone.com )

If you donā€™t understand, say me!

Really happy to see editorialist actively helping users. Really appreciate it @mgch :slight_smile:

1 Like

I donā€™t know anything about sets/maps/pairs. I canā€™t quite understand your code.
" we need the first number >=(sum+1+p)%p because (sum-(sum+1)+p)%p=p-1"
Can you please elaborate what you mean?
I really appreciate the help, buddy :slight_smile:

ok, letā€™s count the sum on prefixs! Then the sum of every subarray is the sum on prefix(0,r)-prefix(0,l-1)!

Thatā€™s about subtask 2, I understood it completely.

Then letā€™s go from prefix(0,0) to(0,n-1)! Letā€™s firstly find the most optimal prefix,that give the closest to p number and then add new prefix to our list of prefixes !

ok, what is the most optimal prefix? Itā€™s the prefix, which gives sum on prefix(0,x)+1 or more! ( because sum on prefix(0,x)-(sum on prefix(0,x)+1) gives -1, which is equal to p-1)

then how to save all prefixes

  1. vector, but itā€™s n^2
  2. we can save the amount of every prefix in array,but itā€™s give p memory and pn time;
  3. then you can do set!
    what is your language(c++,java, pascal)?

itā€™s the habit, itā€™s not Obligated

and why we are adding 1?

what does this line means (sum-(sum+1)+p)%p=p-1??

tell me the meaning /significance of this line???