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???