Advertisement
Kali_prasad

increase good prefixes in the permutation

Apr 20th, 2025
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. # cook your dish here
  2. # https://www.codechef.com/problems/PREPER?tab=statement
  3. def check(sum,i):
  4.     return sum == ((i*(i+1))//2)# a permutation pref is good if its sum is matching with sum of N natural numbers that means all numbers are present none are missing
  5. def maxSubArrSum(p):
  6.     maxi = float('-inf')
  7.     currMax = 0
  8.     for ele in p:
  9.         currMax += ele
  10.         if currMax > maxi:
  11.             maxi = currMax
  12.         if currMax < 0:
  13.             currMax = 0#resetting
  14.    
  15.     return maxi
  16.    
  17.  
  18.  
  19. def getMaxGoodPrefs(arr):
  20.     #let g be the current good prefixes
  21.     g = 0
  22.     sum = 0
  23.     for i in range(len(arr)):
  24.         sum += arr[i]
  25.         if check(sum,i+1):
  26.             g += 1
  27.    
  28.     '''
  29.    
  30.    now you have 2 ways to swap
  31.    important thing is no matter what L and R you choose
  32.    only adjacent elements will be swapped
  33.    
  34.    which is odd even swapping
  35.    and even odd swapping
  36.    we need to perform 2 ways and check the max possible
  37.    
  38.    and also our aim is to enhance g not to ruin it
  39.    
  40.    '''
  41.     #include first element and check 2 steps
  42.     #if they swapped and not swapped
  43.     sumf = 0
  44.     poe = []
  45.     n = len(arr)
  46.     for i in range(0,n,2):
  47.         gnotswap = 0
  48.         gswap = 0
  49.         if i+1<n:
  50.             #if not swap
  51.             if check(sumf+arr[i],i+1):
  52.                 gnotswap += 1
  53.             #for next position arr[i] needs to be included  
  54.             sumf += arr[i]
  55.             if check(sumf+arr[i+1],i+2):
  56.                 gnotswap += 1
  57.             sumf -= arr[i] #nullifying after check
  58.            
  59.             #if swapped that means arr[i+1] is the first element in this pair
  60.            
  61.             if check(sumf+arr[i+1],i+1):
  62.                 gswap += 1
  63.             #for next position arr[i+1] needs to be included  
  64.             sumf += arr[i+1]
  65.             if check(sumf+arr[i],i+2):
  66.                 gswap += 1
  67.             sumf -= arr[i+1] #nullifying after check
  68.            
  69.             sumf += arr[i] + arr[i+1]#preparing for next pair
  70.             diff = gswap-gnotswap
  71.             poe.append(diff)
  72.            
  73.     sumse = arr[0] #as we are not touching for changing
  74.     peo = []
  75.     for i in range(1,n,2):
  76.         gnotswap = 0
  77.         gswap = 0
  78.         if i+1<n:
  79.             #if not swap
  80.             if check(sumse+arr[i],i+1):
  81.                 gnotswap += 1
  82.             #for next position arr[i] needs to be included  
  83.             sumse += arr[i]
  84.             if check(sumse+arr[i+1],i+2):
  85.                 gnotswap += 1
  86.             sumse -= arr[i] #nullifying after check
  87.            
  88.             #if swapped that means arr[i+1] is the first element in this pair
  89.            
  90.             if check(sumse+arr[i+1],i+1):
  91.                 gswap += 1
  92.             #for next position arr[i+1] needs to be included  
  93.             sumse += arr[i+1]
  94.             if check(sumse+arr[i],i+2):
  95.                 gswap += 1
  96.             sumse -= arr[i+1] #nullifying after check
  97.            
  98.             sumse += arr[i] + arr[i+1]#preparing for next pair
  99.  
  100.             diff = gswap-gnotswap
  101.             peo.append(diff)
  102.        
  103.     max1 = maxSubArrSum(poe)
  104.     max2 = maxSubArrSum(peo)
  105.     maxChangedG = max(0,max1,max2)
  106.     return g+maxChangedG
  107.    
  108.    
  109.    
  110. def main():
  111.     tc = int(input())
  112.     for i in range(tc):
  113.         n = int(input())
  114.         arr = list(map(int,input().split()))
  115.         curr = getMaxGoodPrefs(arr)
  116.         print(curr)
  117.        
  118. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement