Sammmmmmm's blog

By Sammmmmmm, history, 23 months ago, In English

Given an array of n integers. In 1 operation you can swap 2 adjacent elements a[i] and a[i + 1] if and only if a[i] + a[i + 1] <=

k. Count the number of possible arrays that can be created using a series of operations (possibly none). Two arrays a and b are

considered different if there exists an index i so that a[i] != b[i]

N <= 1e5

K <= 1e9

Ai <= 1e9

Thanks!

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
23 months ago, hide # |
Rev. 2  
Vote: I like it +13 Vote: I do not like it
correct me if i'm wrong
»
23 months ago, hide # |
Rev. 5  
Vote: I like it +1 Vote: I do not like it

I have this solution:(the indexing for the array is one based):

If too long to read: https://mirror.codeforces.com/blog/entry/129493?#comment-1149165 simplifies it
EDIT: Now that I think about it, there exist a more clever and cleaner way to implement this approach, but I wrote the first thing that came to mind, so if you wanna read it:

Although the part for calculation the permutations is indeed required, even if u use the other clever implementation. So yes that might be helpful

Detailed solution, also talked about details way of implementation
  • »
    »
    23 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You don't need to have a multiset, have 3 variables: mn, mx, and length, while advancing the new vertex, if the new_mn - new_mx <= k, the length gets incremented.
    otherwise, the answer gets multiplied by (length! / (all those cnt factorials)). which may help the complexity to lose some constant factor

    • »
      »
      »
      23 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Also, keeping the dominator and numerator in 2 different variables all the time and calculating the mod division only once is a good performance boost

»
23 months ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

Bro this comment section turned into a place for specialists(everyone who commented was spec)