Блог пользователя Sammmmmmm

Автор Sammmmmmm, история, 4 недели назад, По-английски

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!

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
4 недели назад, # |
Rev. 2   Проголосовать: нравится +13 Проголосовать: не нравится
correct me if i'm wrong
  • »
    »
    4 недели назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    No it is wrong
    Consider this array

    K=10, arr = [3,100,2]

    your algorithm will give 2 permutations, but actual answer is 1
    • »
      »
      »
      4 недели назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      you are correct. i misread the problem please verify the edit part. Thanks!

  • »
    »
    4 недели назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    [1, 10, 2, 10, 3, 10], k: 5
    the answer here is one since you can't swap any two adjacent element, but the B array will be: 1,2,3, which the number of permutations are 6

    • »
      »
      »
      4 недели назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      yes, you are correct i misread the problem i have corrected it can you please verify it again

»
4 недели назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

why downvotes this is a good question

»
4 недели назад, # |
Rev. 5   Проголосовать: нравится +1 Проголосовать: не нравится

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

If too long to read: https://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
  • »
    »
    4 недели назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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

    • »
      »
      »
      4 недели назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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

»
4 недели назад, # |
  Проголосовать: нравится -8 Проголосовать: не нравится

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