message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. You can remove at most one element from this array. Thus, the final length of the array is n-1 or n. Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array. Recall that the contiguous subarray a with indices from l to r is a[l ... r] = a_l, a_{l + 1}, ..., a_r. The subarray a[l ... r] is called strictly increasing if a_l < a_{l+1} < ... < a_r. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. Output Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element. Examples Input 5 1 2 5 3 4 Output 4 Input 2 1 2 Output 2 Input 7 6 5 4 3 2 4 3 Output 2 Note In the first example, you can delete a_3=5. Then the resulting array will be equal to [1, 2, 3, 4] and the length of its largest increasing subarray will be equal to 4. Submitted Solution: ``` n = int(input()) a = list(map(int,input().split())) i = 0 b = [] while i<n: j = i+1 l = 1 while j<n: if a[j]>a[j-1]: l+=1 j+=1 else: break while i<j: b.append(l) i+=1 c = [] for i in range(1,n-1): if a[i]<=a[i-1] and a[i]<=a[i+1]: c.append(i) if a[i]>=a[i-1] and a[i]>=a[i+1]: c.append(i) d = [] d.append(max(b)) for i in c: if a[i]<=a[i-1] and a[i]<=a[i+1]: if(a[i+1]>a[i-1]): d.append(b[i-1]+b[i+1]-1) if a[i]>=a[i-1] and a[i]>=a[i+1]: if(a[i+1]>a[i-1]): d.append(b[i-1]-1+b[i+1]) print(max(d)) ```
instruction
0
48,031
12
96,062
Yes
output
1
48,031
12
96,063
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. You can remove at most one element from this array. Thus, the final length of the array is n-1 or n. Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array. Recall that the contiguous subarray a with indices from l to r is a[l ... r] = a_l, a_{l + 1}, ..., a_r. The subarray a[l ... r] is called strictly increasing if a_l < a_{l+1} < ... < a_r. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. Output Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element. Examples Input 5 1 2 5 3 4 Output 4 Input 2 1 2 Output 2 Input 7 6 5 4 3 2 4 3 Output 2 Note In the first example, you can delete a_3=5. Then the resulting array will be equal to [1, 2, 3, 4] and the length of its largest increasing subarray will be equal to 4. Submitted Solution: ``` n=int(input()) li=list(map(int,input().split())) mat=[] i=0 count=0 ans=1 while i<n: j=i start=j while(j+1<n): if li[j+1]>li[j]: j+=1 else: end=j mat.append([start,end]) ans=max(ans,end-start+1) count+=1 break i=j+1 mat.append([start,j]) ans=max(ans,j-start+1) count+=1 for i in range(1,count): if mat[i][0]-mat[i-1][1]==1 and li[mat[i-1][1]-1]<li[mat[i][0]]: ans=max(ans,mat[i][1]-mat[i-1][0]) if mat[i][0]-mat[i-1][1]==2 and li[mat[i-1][1]]<li[mat[i][0]]: ans=max(ans,mat[i][1]-mat[i-1][0]) print(ans) ```
instruction
0
48,032
12
96,064
No
output
1
48,032
12
96,065
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. You can remove at most one element from this array. Thus, the final length of the array is n-1 or n. Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array. Recall that the contiguous subarray a with indices from l to r is a[l ... r] = a_l, a_{l + 1}, ..., a_r. The subarray a[l ... r] is called strictly increasing if a_l < a_{l+1} < ... < a_r. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. Output Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element. Examples Input 5 1 2 5 3 4 Output 4 Input 2 1 2 Output 2 Input 7 6 5 4 3 2 4 3 Output 2 Note In the first example, you can delete a_3=5. Then the resulting array will be equal to [1, 2, 3, 4] and the length of its largest increasing subarray will be equal to 4. Submitted Solution: ``` def turn(): global a global dp for i in range(len(a)): if i - a[i] >= 0: if a[i - a[i]] % 2 == a[i] % 2: dp[i] = min(dp[i], dp[i - a[i]]+1) else: dp[i] = 1 import sys import math n = int(input()) a = list(map(int, sys.stdin.readline().split())) dp = [math.inf] * len(a) for i in range(20): a = list(reversed(a)) dp = list(reversed(dp)) turn() for i in range(len(dp)): if dp[i] == math.inf: dp[i] = -1 print(' '.join(list(map(str, dp)))) ```
instruction
0
48,033
12
96,066
No
output
1
48,033
12
96,067
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. You can remove at most one element from this array. Thus, the final length of the array is n-1 or n. Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array. Recall that the contiguous subarray a with indices from l to r is a[l ... r] = a_l, a_{l + 1}, ..., a_r. The subarray a[l ... r] is called strictly increasing if a_l < a_{l+1} < ... < a_r. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. Output Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element. Examples Input 5 1 2 5 3 4 Output 4 Input 2 1 2 Output 2 Input 7 6 5 4 3 2 4 3 Output 2 Note In the first example, you can delete a_3=5. Then the resulting array will be equal to [1, 2, 3, 4] and the length of its largest increasing subarray will be equal to 4. Submitted Solution: ``` import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools,itertools,operator,bisect,fractions,statistics from collections import deque,defaultdict,OrderedDict,Counter from fractions import Fraction from decimal import Decimal from sys import stdout def main(): starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") for _ in range(1): n=ri() a=ria() zaa=a.copy() a=[0]+a+[0] strincsubarrays=[] top=[] s=0 for i in range(1,n+2): if a[i]-a[i-1]<=0: strincsubarrays.append(s) s=1 else: s+=1 # if i>1 and i<n+1 and a[i]>=a[i-1] and a[i+1]>a[i-1]: # top.append(i-1) ans=max(strincsubarrays) na=[] c=0 for i in strincsubarrays: c+=1 na+=[[i,c]]*i for i in range(1,n-1): if zaa[i-1]<zaa[i+1] and na[i+1][1]!=na[i-1][1]: k=na[i+1][0]-1+na[i-1][0] if k>ans: ans=k print(ans) print(strincsubarrays) print(top) print(na) # for i in range(len(top)): # if top[i]+1<len(na): # A=na[top[i]] # B=na[top[i]+1] # nm=A-1+B # if nm>ans: # ans=nm # print(ans) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main() ```
instruction
0
48,034
12
96,068
No
output
1
48,034
12
96,069
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a consisting of n integers. You can remove at most one element from this array. Thus, the final length of the array is n-1 or n. Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array. Recall that the contiguous subarray a with indices from l to r is a[l ... r] = a_l, a_{l + 1}, ..., a_r. The subarray a[l ... r] is called strictly increasing if a_l < a_{l+1} < ... < a_r. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a. Output Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array a after removing at most one element. Examples Input 5 1 2 5 3 4 Output 4 Input 2 1 2 Output 2 Input 7 6 5 4 3 2 4 3 Output 2 Note In the first example, you can delete a_3=5. Then the resulting array will be equal to [1, 2, 3, 4] and the length of its largest increasing subarray will be equal to 4. Submitted Solution: ``` import sys import bisect as bi import math from collections import defaultdict as dd input=sys.stdin.readline ##sys.setrecursionlimit(10**7) def cin(): return map(int,sin().split()) def ain(): return list(map(int,sin().split())) def sin(): return input() def inin(): return int(input()) for _ in range(1): n=inin() l=ain() pre=[0]*n;post=[0]*n pre[0]=1;post[-1]=1 for i in range(1,n): if(l[i-1]<l[i]):pre[i]=pre[i-1]+1 else:pre[i]=1 for i in range(n-2,-1,-1): if(l[i+1]>l[i]):post[i]=post[i+1]+1 else:post[i]=1 ## print(l);print(pre);print(post) if(n==2): if(l[0]<l[1]):print(2) else:print(1) else: ans=1 for i in range(0,n): if(i==0): if(l[i]>=l[i+1]):ans=max(ans,post[1],pre[i]) else: ans=max(ans,post[0]) continue elif(i==n-1): if(l[-1]<=l[-2]):ans=max(ans,pre[-2],pre[-1]) else: ans=max(pre[-1],ans) continue elif(l[i+1]>l[i-1]): ans=max(pre[i-1]+post[i+1],ans,pre[i]) print(ans) #### s=input() #### def minCut( s: str) -> int: #### #### n=len(s) #### dp=[[-1]*(n+2) for i in range(n+2)] #### def pp(s,i,j,dp,mn): #### if(i>=j or s[i:j+1]==s[i:j+1:-1]):return 0 #### if(dp[i][j]!=-1):return dp[i][j] #### for k in range(i,j): #### if(dp[i][k]!=-1):return dp[i][k] #### else: #### dp[i][k]=pp(s,i,k,dp,mn) #### return dp[i][k] #### if(dp[k+1][j]!=-1):return dp[k+1][j] #### else: #### dp[i][k]=pp(s,k+1,j,dp,mn) #### return dp[i][k] #### temp=dp[i][k] + dp[i][k] +1 #### if(temp<mn):mn=tmp #### dp[0][n]=mn #### return dp[0][n] #### x=pp(s,0,n,dp,999999999999999) #### for i in dp:print(i) #### return x #### #### print(minCut(s)) ##dp=[[0 if (i==j) else -1 for j in range(n+1)] for i in range(n+1)] ##for i in range(1,n): ## for j in range( ## ## ## ## ## ## ## ####n=m=0 ####s='' ####t='' ####dp=[] ####def solve(inds,indt,k,cont): #### ans=-999999999999999 #### print(dp) #### if(k<0):return 0 #### elif(inds>=n and indt>=m):return 0 #### elif(dp[inds][indt][k][cont]!=-1):return dp[inds][indt][k][cont] #### else: #### if(indt<m):ans=max(ans,solve(inds,indt+1,k,0)) #### if(inds<n):ans=max(ans,solve(inds+1,indt,k,0)) #### if(s[inds]==t[indt]): #### ans=max(ans,solve(inds+1,indt+1,k-1,1)+1) #### if(cont):ans=max(ans,solve(inds+1,indt+1,k,1)+1) #### dp[inds][indt][k][cont]=ans #### return ans ## #### n,m,k=cin() #### s=sin().strip() #### t=sin().strip() #### dp=[[[[-1]*2 for i in range(k)] for i in range(m+1)] for i in range(n+1)] #### c=0 #### for i in dp: #### for j in i: #### for l in j: #### c+=1 #### print(l,c) #### print(solve(0,0,k,0)) ```
instruction
0
48,035
12
96,070
No
output
1
48,035
12
96,071
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2
instruction
0
48,036
12
96,072
Tags: data structures, divide and conquer Correct Solution: ``` from operator import add class LazySegmentTree(): def __init__(self,n,init,merge=min,merge_unit=10**18,operate=add,operate_unit=0): self.merge=merge self.merge_unit=merge_unit self.operate=operate self.operate_unit=operate_unit self.n=(n-1).bit_length() self.data=[0 for i in range(1<<(self.n+1))] self.lazy=[0 for i in range(1<<(self.n+1))] for i in range(n): self.data[2**self.n+i]=init[i] for i in range(2**self.n-1,0,-1): self.data[i]=self.merge(self.data[2*i],self.data[2*i+1]) def propagate_above(self,i): m=i.bit_length()-1 for bit in range(m,0,-1): v=i>>bit add=self.lazy[v] self.lazy[v]=0 self.data[2*v]=self.operate(self.data[2*v],add) self.data[2*v+1]=self.operate(self.data[2*v+1],add) self.lazy[2*v]=self.operate(self.lazy[2*v],add) self.lazy[2*v+1]=self.operate(self.lazy[2*v+1],add) def remerge_above(self,i): while i: i>>=1 self.data[i]=self.operate(self.merge(self.data[2*i],self.data[2*i+1]),self.lazy[i]) def update(self,l,r,x): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 while l<r: if l&1: self.data[l]=self.operate(self.data[l],x) self.lazy[l]=self.operate(self.lazy[l],x) l+=1 if r&1: self.data[r-1]=self.operate(self.data[r-1],x) self.lazy[r-1]=self.operate(self.lazy[r-1],x) l>>=1 r>>=1 self.remerge_above(l0) self.remerge_above(r0) def query(self,l,r): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 self.propagate_above(l0) self.propagate_above(r0) res=self.merge_unit while l<r: if l&1: res=self.merge(res,self.data[l]) l+=1 if r&1: res=self.merge(res,self.data[r-1]) l>>=1 r>>=1 return res import sys,random input=sys.stdin.buffer.readline n=int(input()) #p=[i for i in range(n)] #random.shuffle(p) p=list(map(lambda x:int(x)-1,input().split())) #a=[float(random.randint(1,10**5)) for i in range(n)] a=list(map(float,input().split())) pos=[0 for i in range(n+1)] for i in range(n): pos[p[i]]=i data=[0]*n S=0 for i in range(n): S+=a[pos[i]] data[i]=S init=[0]*n for i in range(0,n): if p[i]>0: init[i]+=a[i] else: init[i]-=a[i] init[i]+=init[i-1] LST=LazySegmentTree(n,init) ans=a[0] for i in range(n): tmp=data[i]+LST.query(0,n-1) ans=min(ans,tmp) LST.update(pos[i+1],n,-2*a[pos[i+1]]) print(int(ans)) ```
output
1
48,036
12
96,073
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2
instruction
0
48,037
12
96,074
Tags: data structures, divide and conquer Correct Solution: ``` import sys readline = sys.stdin.readline from itertools import accumulate class Lazysegtree: #RAQ def __init__(self, A, intv, initialize = True, segf = min): #区間は 1-indexed で管理 self.N = len(A) self.N0 = 2**(self.N-1).bit_length() self.intv = intv self.segf = segf self.lazy = [0]*(2*self.N0) if initialize: self.data = [intv]*self.N0 + A + [intv]*(self.N0 - self.N) for i in range(self.N0-1, 0, -1): self.data[i] = self.segf(self.data[2*i], self.data[2*i+1]) else: self.data = [intv]*(2*self.N0) def _ascend(self, k): k = k >> 1 c = k.bit_length() for j in range(c): idx = k >> j self.data[idx] = self.segf(self.data[2*idx], self.data[2*idx+1]) \ + self.lazy[idx] def _descend(self, k): k = k >> 1 idx = 1 c = k.bit_length() for j in range(1, c+1): idx = k >> (c - j) ax = self.lazy[idx] if not ax: continue self.lazy[idx] = 0 self.data[2*idx] += ax self.data[2*idx+1] += ax self.lazy[2*idx] += ax self.lazy[2*idx+1] += ax def query(self, l, r): L = l+self.N0 R = r+self.N0 Li = L//(L & -L) Ri = R//(R & -R) self._descend(Li) self._descend(Ri - 1) s = self.intv while L < R: if R & 1: R -= 1 s = self.segf(s, self.data[R]) if L & 1: s = self.segf(s, self.data[L]) L += 1 L >>= 1 R >>= 1 return s def add(self, l, r, x): L = l+self.N0 R = r+self.N0 Li = L//(L & -L) Ri = R//(R & -R) while L < R : if R & 1: R -= 1 self.data[R] += x self.lazy[R] += x if L & 1: self.data[L] += x self.lazy[L] += x L += 1 L >>= 1 R >>= 1 self._ascend(Li) self._ascend(Ri-1) def binsearch(self, l, r, check, reverse = False): L = l+self.N0 R = r+self.N0 Li = L//(L & -L) Ri = R//(R & -R) self._descend(Li) self._descend(Ri-1) SL, SR = [], [] while L < R: if R & 1: R -= 1 SR.append(R) if L & 1: SL.append(L) L += 1 L >>= 1 R >>= 1 if reverse: for idx in (SR + SL[::-1]): if check(self.data[idx]): break else: return -1 while idx < self.N0: ax = self.lazy[idx] self.lazy[idx] = 0 self.data[2*idx] += ax self.data[2*idx+1] += ax self.lazy[2*idx] += ax self.lazy[2*idx+1] += ax idx = idx << 1 if check(self.data[idx+1]): idx += 1 return idx - self.N0 else: for idx in (SL + SR[::-1]): if check(self.data[idx]): break else: return -1 while idx < self.N0: ax = self.lazy[idx] self.lazy[idx] = 0 self.data[2*idx] += ax self.data[2*idx+1] += ax self.lazy[2*idx] += ax self.lazy[2*idx+1] += ax idx = idx << 1 if not check(self.data[idx]): idx += 1 return idx - self.N0 N = int(readline()) P = list(map(lambda x:int(x)-1 , readline().split())) Pinv = [None]*N for i in range(N): Pinv[P[i]] = i A = list(map(int, readline().split())) dp = A[:-1] for i in range(1, N-1): dp[i] += dp[i-1] inf = 1<<60 ans = dp[0] dp = Lazysegtree(dp, inf, initialize = True, segf = min) N0 = dp.N0 cnt = 0 for i in range(N): a = A[Pinv[i]] p = Pinv[i] dp.add(p, N0, -2*a) cnt += a ans = min(ans, cnt + dp.query(0, N0)) print(ans) ```
output
1
48,037
12
96,075
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2
instruction
0
48,038
12
96,076
Tags: data structures, divide and conquer Correct Solution: ``` def main(): import sys input = sys.stdin.buffer.readline N = int(input()) P = list(map(int, input().split())) A = list(map(float, input().split())) class LazySegTree: # Range add query def __init__(self, A, initialize=True, segfunc=min, ident=2000000000): self.N = len(A) self.LV = (self.N - 1).bit_length() self.N0 = 1 << self.LV self.segfunc = segfunc self.ident = ident if initialize: self.data = [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N) for i in range(self.N0 - 1, 0, -1): self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1]) else: self.data = [self.ident] * (self.N0 * 2) self.lazy = [0] * (self.N0 * 2) def _ascend(self, i): for _ in range(i.bit_length() - 1): i >>= 1 self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1]) + self.lazy[i] def _descend(self, idx): lv = idx.bit_length() for j in range(lv - 1, 0, -1): i = idx >> j x = self.lazy[i] if not x: continue self.lazy[i] = 0 self.lazy[i * 2] += x self.lazy[i * 2 + 1] += x self.data[i * 2] += x self.data[i * 2 + 1] += x # open interval [l, r) def add(self, l, r, x): l += self.N0 - 1 r += self.N0 - 1 l_ori = l r_ori = r while l < r: if l & 1: self.data[l] += x self.lazy[l] += x l += 1 if r & 1: r -= 1 self.data[r] += x self.lazy[r] += x l >>= 1 r >>= 1 self._ascend(l_ori // (l_ori & -l_ori)) self._ascend(r_ori // (r_ori & -r_ori) - 1) # open interval [l, r) def query(self, l, r): l += self.N0 - 1 r += self.N0 - 1 self._descend(l // (l & -l)) self._descend(r // (r & -r) - 1) ret = self.ident while l < r: if l & 1: ret = self.segfunc(ret, self.data[l]) l += 1 if r & 1: ret = self.segfunc(self.data[r - 1], ret) r -= 1 l >>= 1 r >>= 1 return ret p2i = {} for i, p in enumerate(P): p2i[p] = i S = 0 init = [0.] * N for i in range(1, N): S += A[i-1] init[i-1] = S segtree = LazySegTree(init, ident=float('inf')) ans = A[0] cnt = 0 for p in range(1, N+1): i = p2i[p] a = A[i] cnt += a segtree.add(i+1, N, -a * 2) ans = min(ans, segtree.query(1, N) + cnt) print(int(ans)) if __name__ == '__main__': main() ```
output
1
48,038
12
96,077
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2
instruction
0
48,039
12
96,078
Tags: data structures, divide and conquer Correct Solution: ``` import sys from itertools import accumulate input = sys.stdin.readline n=int(input()) P=tuple(map(int,input().split())) A=tuple(map(int,input().split())) seg_el=1<<((n+1).bit_length()) SEGMIN=[0]*(2*seg_el) SEGADD=[0]*(2*seg_el) for i in range(n): SEGMIN[P[i]+seg_el]=A[i] SEGMIN=list(accumulate(SEGMIN)) for i in range(seg_el-1,0,-1): SEGMIN[i]=min(SEGMIN[i*2],SEGMIN[(i*2)+1]) def adds(l,r,x): L=l+seg_el R=r+seg_el while L<R: if L & 1: SEGADD[L]+=x L+=1 if R & 1: R-=1 SEGADD[R]+=x L>>=1 R>>=1 L=(l+seg_el)>>1 R=(r+seg_el-1)>>1 while L!=R: if L>R: SEGMIN[L]=min(SEGMIN[L*2]+SEGADD[L*2],SEGMIN[1+(L*2)]+SEGADD[1+(L*2)]) L>>=1 else: SEGMIN[R]=min(SEGMIN[R*2]+SEGADD[R*2],SEGMIN[1+(R*2)]+SEGADD[1+(R*2)]) R>>=1 while L!=0: SEGMIN[L]=min(SEGMIN[L*2]+SEGADD[L*2],SEGMIN[1+(L*2)]+SEGADD[1+(L*2)]) L>>=1 S=tuple(accumulate(A)) ANS=1<<31 for i in range(n-1): adds(P[i],n+1,-A[i]*2) ANS=min(ANS,SEGMIN[1]+S[i]) print(ANS) ```
output
1
48,039
12
96,079
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2 Submitted Solution: ``` n=int(input()) p=list(map(int,input().split())) a=list(map(int,input().split())) sumves=[] sumnum=[] weight=[0 for i in range(len(p))] ix=[0 for i in range(len(p))] sumves.append(a[0]) weight[p[0]-1]=a[0] ix[0]=p.index(1) weight[0]=a[ix[0]] sumnum.append(weight[0]) for i in range(1,len(a)): sumves.append(sumves[i-1]+a[i]) weight[p[i]-1]=a[i] ix[p[i]-1]=i for i in range(1,len(a)): sumnum.append(sumnum[i-1]+weight[i]) #print(ix) #print(weight) #print(sumves) #print(sumnum) r=[] r.append(sumves[ix[0]]-weight[0]) for i in range(1,n-1): if ix[i]==n-1: r.append(r[i-1]+weight[i]) else: if ix[i]<ix[i-1]: r.append(r[i-1]-weight[i]) else: r.append(min(r[i-1]+weight[i],sumnum[ix[i]]-sumves[i])) mn=min(a[0],a[n-1]) for i in r: if i>0:mn=min(mn,i) print(mn) ```
instruction
0
48,040
12
96,080
No
output
1
48,040
12
96,081
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2 Submitted Solution: ``` def main(): import sys import time input = sys.stdin.buffer.readline t0 = time.time() N = int(input()) P = list(map(int, input().split())) A = list(map(int, input().split())) # N: 処理する区間の長さ INF = 10**15 LV = (N - 1).bit_length() N0 = 2 ** LV data = [0] * (2 * N0) lazy = [0] * (2 * N0) def gindex(l, r): L = (l + N0) >> 1 R = (r + N0) >> 1 lc = 0 if l & 1 else (L & -L).bit_length() rc = 0 if r & 1 else (R & -R).bit_length() for i in range(LV): if rc <= i: yield R if L < R and lc <= i: yield L L >>= 1 R >>= 1 # 遅延伝搬処理 def propagates(*ids): for i in reversed(ids): v = lazy[i - 1] if not v: continue lazy[2 * i - 1] += v lazy[2 * i] += v data[2 * i - 1] += v data[2 * i] += v lazy[i - 1] = 0 # 区間[l, r)にxを加算 def update(l, r, x): *ids, = gindex(l, r) propagates(*ids) L = N0 + l R = N0 + r while L < R: if R & 1: R -= 1 lazy[R - 1] += x data[R - 1] += x if L & 1: lazy[L - 1] += x data[L - 1] += x L += 1 L >>= 1 R >>= 1 for i in ids: data[i - 1] = min(data[2 * i - 1], data[2 * i]) # 区間[l, r)内の最小値を求める def query(l, r): propagates(*gindex(l, r)) L = N0 + l R = N0 + r s = INF while L < R: if R & 1: R -= 1 s = min(s, data[R - 1]) if L & 1: s = min(s, data[L - 1]) L += 1 L >>= 1 R >>= 1 return s p2i = {} for i, p in enumerate(P): p2i[p] = i S = 0 for i in range(1, N): S += A[i-1] update(i, i+1, S) ans = A[0] cnt = 0 if N > 1000: print(time.time()-t0) exit() for p in range(1, N+1): i = p2i[p] a = A[i] cnt += a update(i+1, N, -a * 2) ans = min(ans, query(1, N) + cnt) print(ans) if __name__ == '__main__': main() ```
instruction
0
48,041
12
96,082
No
output
1
48,041
12
96,083
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2 Submitted Solution: ``` def main(): import sys input=sys.stdin.buffer.readline n=int(input()) p=list(map(lambda x:int(x)-1,input().split())) a=list(map(int,input().split())) class LazySegmentTree(): def __init__(self,n,init,merge_func=min,ide_ele=10**18): self.n=(n-1).bit_length() self.merge_func=merge_func self.ide_ele=ide_ele self.data=[0 for i in range(1<<(self.n+1))] self.lazy=[0 for i in range(1<<(self.n+1))] for i in range(n): self.data[2**self.n+i]=init[i] for i in range(2**self.n-1,0,-1): self.data[i]=self.merge_func(self.data[2*i],self.data[2*i+1]) def propagate_above(self,i): m=i.bit_length()-1 for bit in range(m,0,-1): v=i>>bit self.data[v]+=self.lazy[v] self.lazy[2*v]+=self.lazy[v] self.lazy[2*v+1]+=self.lazy[v] self.lazy[v]=0 def remerge_above(self,i): while i: i>>=1 self.data[i]=self.merge_func(self.data[2*i]+self.lazy[2*i],self.data[2*i+1]+self.lazy[2*i+1])+self.lazy[i] def update(self,l,r,x): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 while l<r: self.lazy[l]+=x*(l&1) l+=(l&1) self.lazy[r-1]+=x*(r&1) l>>=1 r>>=1 self.remerge_above(l0) self.remerge_above(r0) def query(self,l,r): l+=1<<self.n r+=1<<self.n l0=l//(l&-l) r0=r//(r&-r)-1 self.propagate_above(l0) self.propagate_above(r0) res=self.ide_ele while l<r: if l&1: res=self.merge_func(res,self.data[l]+self.lazy[l]) l+=1 if r&1: res=self.merge_func(res,self.data[r-1]+self.lazy[r-1]) l>>=1 r>>=1 return res pos=[0 for i in range(n+1)] for i in range(n): pos[p[i]]=i data=[0]*n S=0 for i in range(n): S+=a[pos[i]] data[i]=S init=[0]*n for i in range(0,n): if p[i]>0: init[i]+=a[i] else: init[i]-=a[i] init[i]+=init[i-1] LST=LazySegmentTree(n,init) ans=a[0] for i in range(n): tmp=data[i]+LST.query(0,n-1) ans=min(ans,tmp) LST.update(pos[i+1],n,-2*a[pos[i+1]]) print(ans) if __name__=="__main__": main() ```
instruction
0
48,042
12
96,084
No
output
1
48,042
12
96,085
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2 Submitted Solution: ``` def main(): import sys import time input = sys.stdin.buffer.readline t0 = time.time() N = int(input()) P = list(map(int, input().split())) A = list(map(int, input().split())) # N: 処理する区間の長さ INF = 10**15 LV = (N - 1).bit_length() N0 = 2 ** LV data = [0] * (2 * N0) lazy = [0] * (2 * N0) def gindex(l, r): L = (l + N0) >> 1 R = (r + N0) >> 1 lc = 0 if l & 1 else (L & -L).bit_length() rc = 0 if r & 1 else (R & -R).bit_length() for i in range(LV): if rc <= i: yield R if L < R and lc <= i: yield L L >>= 1 R >>= 1 # 遅延伝搬処理 def propagates(*ids): for i in reversed(ids): v = lazy[i - 1] if not v: continue lazy[2 * i - 1] += v lazy[2 * i] += v data[2 * i - 1] += v data[2 * i] += v lazy[i - 1] = 0 # 区間[l, r)にxを加算 def update(l, r, x): *ids, = gindex(l, r) propagates(*ids) L = N0 + l R = N0 + r while L < R: if R & 1: R -= 1 lazy[R - 1] += x data[R - 1] += x if L & 1: lazy[L - 1] += x data[L - 1] += x L += 1 L >>= 1 R >>= 1 for i in ids: data[i - 1] = min(data[2 * i - 1], data[2 * i]) # 区間[l, r)内の最小値を求める def query(l, r): propagates(*gindex(l, r)) L = N0 + l R = N0 + r s = INF while L < R: if R & 1: R -= 1 s = min(s, data[R - 1]) if L & 1: s = min(s, data[L - 1]) L += 1 L >>= 1 R >>= 1 return s p2i = {} for i, p in enumerate(P): p2i[p] = i S = 0 for i in range(1, N): S += A[i-1] update(i, i+1, S) ans = A[0] cnt = 0 for p in range(1, N+1): i = p2i[p] a = A[i] cnt += a update(i+1, N, -a * 2) ans = min(ans, query(1, N) + cnt) if p%100 == 0: if time.time() - t0 > 1.8: print(p) exit() print(ans) if __name__ == '__main__': main() ```
instruction
0
48,043
12
96,086
No
output
1
48,043
12
96,087
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,051
12
96,102
Tags: greedy, math Correct Solution: ``` import math for i in range(int(input())): n=int(input()) w=list(map(int,input().split())) b=0 for j in range(n-1): if(w[j]>w[j+1]): b=max(b,int(math.log2(w[j]-w[j+1]))+1) w[j+1]=w[j] print(b) ```
output
1
48,051
12
96,103
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,052
12
96,104
Tags: greedy, math Correct Solution: ``` import math as M for _ in range(int(input())): n=int(input()) A=list(int(i) for i in input().split()) ans=0 for i in range(1,n): if A[i-1]>A[i]: d=A[i-1]-A[i] x=int(M.log2(d)+1) ans=max(ans,x) #comparing the vlaues of x(real) and th eansd A[i]=A[i-1] print(ans) ```
output
1
48,052
12
96,105
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,053
12
96,106
Tags: greedy, math Correct Solution: ``` for _ in range(int(input())): n=int(input()) a=[int(i) for i in input().split()] m=a[0] p=0 for i in range(1,n): m=max(m,a[i]) p=max(p,m-a[i]) c=0 while 2**c<=p: c+=1 print(c) ```
output
1
48,053
12
96,107
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,054
12
96,108
Tags: greedy, math Correct Solution: ``` for t in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] m = a[0] v = 0 for i in a: v = max(v,m-i) m = max(m,i) if v == 0: print(0) else: p = 1 c = 0 while p<=v: p *= 2 c += 1 print(c) ```
output
1
48,054
12
96,109
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,055
12
96,110
Tags: greedy, math Correct Solution: ``` t = int(input()) for case in range(1, t + 1): n = int(input()) arr = [int(x) for x in input().split(" ")] tick = 0 prev = -2e9 for a in arr: if a > prev: prev = a else: while a + (2 ** tick) - 1 < prev: tick += 1 print(tick) ```
output
1
48,055
12
96,111
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,056
12
96,112
Tags: greedy, math Correct Solution: ``` from sys import stdin t=int(input()) for _ in range(t): n=int(input()) l=list(map(int,stdin.readline().split())) prev=l[0] diff=[] for e in l: if(e<prev): diff.append(prev-e) else: prev=e if(len(diff)==0): print(0) else: diff.sort() a=diff[-1] i=0 s=0 while(a>s): s+=2**i i+=1 print(i) ```
output
1
48,056
12
96,113
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,057
12
96,114
Tags: greedy, math Correct Solution: ``` from math import log2 for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) c=0 for i in range(1,len(l)): if(l[i]<l[i-1]):c=max(c,int(log2(l[i-1]-l[i]))+1);l[i]=l[i-1] print(c) ```
output
1
48,057
12
96,115
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
48,058
12
96,116
Tags: greedy, math Correct Solution: ``` # https://codeforces.com/problemset/problem/1338/A from math import log2 as l, floor as f for _ in range(int(input())): n = int(input()) a = map(int, input().split()) x = 0 m = -1e100 for i in a: m = max(m, i) x = max(x, m-i) if x == 0: print(0) else: print(f(l(x)+1)) ```
output
1
48,058
12
96,117
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` t = int(input()) for _ in range(t): n = int(input()) a = [int(e) for e in input().split()] max_a = a[0] ans = 0 for i in range(1, n): if a[i] < max_a: delta = max_a - a[i] c = 0 while delta > 0: delta = delta // 2 c = c + 1 if c > ans: ans = c else: max_a = a[i] print(ans) ```
instruction
0
48,059
12
96,118
Yes
output
1
48,059
12
96,119
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` #This code is contributed by Siddharth import time import heapq import sys from collections import * from heapq import * import math import bisect from itertools import * mod=10**9+7 for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) temp= [0 for i in range(n)] for i in range(1,n): if l[i]<l[i-1]: temp.append(l[i-1]-l[i]) l[i]=l[i-1] if max(temp)==0: print(0) else: print(math.floor(math.log2((max(temp))))+1) ```
instruction
0
48,060
12
96,120
Yes
output
1
48,060
12
96,121
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` from math import log2 for _ in range(int(input())): n=int(input()) arr = list(map(int, input().split())) ans=0 for i in range(1,n): if arr[i]<arr[i-1]: ans=max(ans,int(log2(arr[i-1]-arr[i]))+1) arr[i]=arr[i-1] print(ans) ```
instruction
0
48,061
12
96,122
Yes
output
1
48,061
12
96,123
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` # It's never too late to start! from bisect import bisect_left import os import sys from io import BytesIO, IOBase from collections import Counter, defaultdict from collections import deque from functools import cmp_to_key import math # import heapq def sin(): return input() def ain(): return list(map(int, sin().split())) def sain(): return input().split() def iin(): return int(sin()) MAX = float('inf') MIN = float('-inf') MOD = 1000000007 def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 s = set() for p in range(2, n+1): if prime[p]: s.add(p) return s def readTree(n, m): adj = [deque([]) for _ in range(n+1)] for _ in range(m): u,v = ain() adj[u].append(v) adj[v].append(u) return adj def solve(c1,c2,maxi1,maxi2): for i in range(1,maxi1+1): if i not in c1: return False for i in range(1,maxi2+1): if i not in c2: return False return True # Stay hungry, stay foolish! def main(): for _ in range(iin()): n = iin() l = ain() x = l[0] ans = 0 for i in range(n): ans = max(x-l[i], ans) x = max(x,l[i]) if ans == 0: print(0) else: # print(ans) p = math.log(ans,2) if p%2 == 0: print(int(p+1)) else: print(int(p+1)) # Fast IO Template starts BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") if os.getcwd() == 'D:\\code': sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # Fast IO Template ends if __name__ == "__main__": main() # Never Give Up - John Cena ```
instruction
0
48,062
12
96,124
Yes
output
1
48,062
12
96,125
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` from sys import stdin t=int(input()) for _ in range(t): n=int(input()) l=list(map(int,stdin.readline().split())) prev=l[0] diff=[] for e in l: if(e<prev): diff.append(prev-e) else: prev=e if(len(diff)==0): print(0) else: a=diff[-1] i=0 s=0 while(a>s): s+=2**i i+=1 print(i) ```
instruction
0
48,063
12
96,126
No
output
1
48,063
12
96,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` import os, sys from io import BytesIO, IOBase from types import GeneratorType from bisect import * from collections import defaultdict, deque, Counter import math, string from heapq import * from operator import add from itertools import accumulate BUFSIZE = 8192 sys.setrecursionlimit(10 ** 5) class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") inf = float("inf") en = lambda x: list(enumerate(x)) ceil_ = lambda a, b: (a + b - 1) // b ii = lambda: int(input()) r = lambda: map(int, input().split()) rr = lambda: list(r()) # -------------------------- brr = [0] + [1 << i for i in range(60)] def solve(): n = ii() arr = rr() ans = 0 for i in range(1, n): if arr[i] >= arr[i - 1]: continue val = 60 for j in range(60): if arr[i] + brr[j] >= arr[i - 1]: if arr[i] + brr[j] == arr[i - 1]: val = j else: val = j - 1 break for k in range(val, -1, -1): if arr[i] >= arr[i - 1]: break arr[i] += brr[k] ans = max(ans, val) print(ans) for _ in " " * ii(): solve() ```
instruction
0
48,064
12
96,128
No
output
1
48,064
12
96,129
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` import sys q = int(input()) ans = [] for _ in range(q): n = int(input()) a = list(map(int, input().split())) last = a[0] - 1 need = 0 p = 1 for v in a: while v < last: v += p need += 1 p *= 2 last = v ans.append(need) print('\n'.join(map(str, ans))) ```
instruction
0
48,065
12
96,130
No
output
1
48,065
12
96,131
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≤ a_{2} ≤ … ≤ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≤ t ≤ 10^{4}) — the number of test cases. The first line of each test case contains single integer n (1 ≤ n ≤ 10^{5}) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≤ a_{i} ≤ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` import math t=int(input()) for i in range(t): n=int(input()) l=list(map(int,input().split())) l1=[] for i in range(0,n): l1.append(l[i]) l1.sort() max1=0 for i in range(0,n): d=abs(l[i]-l1[i]) if(d>max1): max1=d if(max1==0): print(0) else: count1=int(math.log(max1,2))+1 print(count1) ```
instruction
0
48,066
12
96,132
No
output
1
48,066
12
96,133
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,067
12
96,134
Tags: brute force, data structures Correct Solution: ``` def helper(n,arr): # j=n # for i in range(0,n): # index=arr.index(j) # if index==0 or index==n-1: #6 5 3 1 2 4 # j-=1 # continue # elif: # break if len(arr)<=2: print('NO') return l=0 maxx=n+1 while len(arr)>2: # print(arr) maxi=maxx-1 # print(maxi) index=None index=arr.index(maxi) index+=1 # print(index) if arr[0]==maxi: arr=arr[1:] l+=1 maxx=maxx-1 continue elif arr[len(arr)-1]==maxi: arr=arr[0:len(arr)-1] maxx=maxx-1 continue else: j=index+l i=index-1+l k=index+1+l print("YES") print(i,j,k) return print('NO') return for _ in range(int(input())): n=int(input()) arr=[int(x) for x in input().split()] helper(n,arr) ```
output
1
48,067
12
96,135
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,068
12
96,136
Tags: brute force, data structures Correct Solution: ``` tests = int(input()) for t in range(tests): n = int(input()) ls = list(map(int, input().split())) high = ls[1] high_idx = 1 low = ls[0] low_idx = 0 res = [] if low > high: low = high low_idx = high_idx high = None for idx in range(2, len(ls)): curr = ls[idx] if high is None: if curr > low: high = curr high_idx = idx else: low = curr low_idx = idx else: if curr > high: high = curr high_idx = idx else: res = [low_idx+1, high_idx+1, idx+1] break if res: print('YES') print(' '.join(list(map(str, res)))) else: print('NO') ```
output
1
48,068
12
96,137
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,069
12
96,138
Tags: brute force, data structures Correct Solution: ``` cases = int(input()) for case in range(cases): n = int(input()) list_num = list(map(lambda x: int(x), input().split())) unchanged = list.copy(list_num) maximum = n pos_max = list_num.index(maximum) length = len(list_num) flush_counter = 0 for i in range(n): if list_num[i] == i + 1: flush_counter += 1 reverse_flush_counter = 0 for j in range(1, n + 1): if list_num[-j] == j: reverse_flush_counter += 1 if flush_counter == n or reverse_flush_counter == n: print("NO") continue while ((pos_max == 0 or pos_max == length - 1) and length >= 3): list_num.remove(maximum) maximum -= 1 length -= 1 pos_max = list_num.index(maximum) if length > 2: pos_left = unchanged.index(list_num[0]) + 1 pos_right = unchanged.index(list_num[-1]) + 1 pos_middle = unchanged.index(maximum) + 1 print("YES") print(pos_left, pos_middle, pos_right) else: print("NO") ```
output
1
48,069
12
96,139
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,070
12
96,140
Tags: brute force, data structures Correct Solution: ``` for i in range(int(input())): n=int(input()) a=list(map(int,input().split())) l=[] d=0 j=0 for j in range(1,n-1): if(a[j]>a[j-1] and a[j]>a[j+1]): l.append(j) l.append(j+1) l.append(j+2) d=1 break if(d==1): print('YES') print(*l) else: print('NO') ```
output
1
48,070
12
96,141
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,071
12
96,142
Tags: brute force, data structures Correct Solution: ``` def solve(): n = int(input()) p = list(map(int, input().split())) k = False for i in range(1, n): if p[i] > p[i - 1]: k = True else: if k: print("YES") print(i - 1, i, i + 1) return print("NO") for _ in range(int(input())): solve() ```
output
1
48,071
12
96,143
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,072
12
96,144
Tags: brute force, data structures Correct Solution: ``` def solve(): n = int(input()) a = [int(x) for x in input().split()] for x in range(1,n-1): if a[x] > a[x-1] and a[x]>a[x+1]: print("YES") return "{} {} {}".format(x,x+1,x+2) return "NO" for q in range(int(input())): print(solve()) ```
output
1
48,072
12
96,145
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,073
12
96,146
Tags: brute force, data structures Correct Solution: ``` for _ in range(int(input())): n=int(input()) x=[int(x) for x in input().split()] t=[] f=1 for i in range(1,len(x)-1): if x[i]>x[i-1] and x[i+1]<x[i]: print('yes') print(i,i+1,i+2) f=0 break if f: print('no') ```
output
1
48,073
12
96,147
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO
instruction
0
48,074
12
96,148
Tags: brute force, data structures Correct Solution: ``` for _ in range(int(input())): n = int(input()) x = list(map(int,input().split())) c = 0 for j in range(1,len(x)-1): if x[j] > x[j-1] and x[j] > x[j+1]: c = 1 print("YES") print(j,j+1,j+2) break if c == 0: print("NO") ```
output
1
48,074
12
96,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` for _ in range (int(input())): n=int(input()) a=list(map(int,input().split())) f=0 for i in range (1,n-1): if a[i]>a[i-1] and a[i]>a[i+1]: print("YES") print(i,i+1,i+2) f=1 break if f==0: print("NO") ```
instruction
0
48,075
12
96,150
Yes
output
1
48,075
12
96,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` for _ in range(int(input())): n=int(input()) a=list(map(int,input().strip().split()))[:n] c=0 for i in range(1,n-1): if(a[i]>a[i-1] and a[i]>a[i+1]): c+=1 x=i if(c>0): print("YES") print(x, x+1, x+2) else: print("NO") ```
instruction
0
48,076
12
96,152
Yes
output
1
48,076
12
96,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` for _ in range (int(input())): n = int(input()) l = list(map(int, input().split())) p = 0 mi = [l[0], 0] for i in range (1, n): if l[i] < mi[0]: mi = [l[i], i] else: p = i break q = n - 1 mo = [l[-1], n - 1] for i in range (n - 2, -1, -1): if l[i] < mo[0]: mo = [l[i], i] else: q = i break if l[p] > l[q]: b = p + 1 else: b = q + 1 a = mi[-1] + 1 c = mo[-1] + 1 if a != b and b != c and c != a: print("YES") print(mi[-1] + 1, b, mo[-1] + 1) else: print("NO") ```
instruction
0
48,077
12
96,154
Yes
output
1
48,077
12
96,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` for _ in range(int(input())): n=int(input()) flag=False listt=[int(e) for e in input().split()] for j in range(1,len(listt)-1): if listt[j-1]<listt[j] and listt[j]>listt[j+1]: i1=j i2=j+1 i3=j+2 flag=True break if flag: print("YES") print(i1,i2,i3) else: print("NO") ```
instruction
0
48,078
12
96,156
Yes
output
1
48,078
12
96,157
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) x=max(l) if x!=l[0] and x!=l[1]: print('YES') x=l.index(x) print(l.index(min(l[:x]))+1,x,l.index(min(l[x+1:]))+1) else: print('NO') ```
instruction
0
48,079
12
96,158
No
output
1
48,079
12
96,159
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` # import numpy as np # np.random.shuffle() test_cases = int(input()) # test_cases = np.random.randint(8000,9000) for t in range(test_cases): n = int(input()) sequence = input().split(' ') i,j,k = 0,None,None # finite state machine for index,num in enumerate(sequence): if j is None: # second state if num > sequence[i]: j = index else: # update i i = index elif k is None: if num < sequence[j]: k = index break else: # update j j = index if j is None or k is None: print('No') else: assert (sequence[j] > sequence[i]) and (sequence[j] > sequence[k])," Incorrect Answer from value\n{0}".format(sequence) assert (j > i) and (k > j)," Incorrect Answer of index order\n{0}".format(sequence) assert (i>= 0) and (k <= n-1), "Incorrect boundary conditions {0},{1},{2}\n{3}".format(i,k,n,sequence) print('YES') print(i+1,j+1,k+1) # results = [i+1,j+1,k+1] ## Naive solution # for t in range(test_cases): # # n = int(input()) # n = np.random.randint(5,10) # # sequence = input().split(' ') # sequence = np.arange(1,n+1,1,dtype=np.int32) # np.random.shuffle(sequence) # # print(sequence) # # print() # seq = [None]*n # for i,v in enumerate(sequence): # seq[i] = int(v) # sequence = seq # index_value_map = {} # for index, v in enumerate(sequence): # index_value_map[int(v)] = index # for max_value in range(n,1,-1): # i,j,k = None,None,None # if index_value_map[max_value] == n or index_value_map[max_value] == 0: # continue # j = index_value_map[max_value] # # look for k # for indx in range(index_value_map[max_value],n,1): # if sequence[indx] < max_value: # k = indx # break # if k is None: # continue # for indx in range(0,index_value_map[max_value],1): # if sequence[indx] < max_value: # i = indx # break # if i is not None and j is not None: # print('Yes') # print(i+1,j+1,k+1) # break # if i is None or j is None: # print('No') # print("Solution 2") # i,j,k = 0,None,None # # finite state machine # for index,num in enumerate(sequence): # if j is None: # # second state # if num > sequence[i]: # j = index # else: # # update i # i = index # elif k is None: # if num < sequence[j]: # k = index # break # else: # # update j # j = index # if j is None or k is None: # print('No') # print(sequence) # else: # # print('YES') # # print(i+1,j+1,k+1) # assert (sequence[j] > sequence[i]) and (sequence[j] > sequence[k])," Incorrect Answer from value\n{0}".format(sequence) # assert (j > i) and (k > i)," Incorrect Answer of index order\n{0}".format(sequence) # assert (i>= 0) and (k <= n-1), "Incorrect boundary conditions {0},{1},{2}\n{3}".format(i,k,n,sequence) # results = [i+1,j+1,k+1] # for t in range(test_cases): # # n = int(input()) # # sequence = input().split(' ') # i,j,k = 0,None,None # # finite state machine # for index,num in enumerate(sequence): # if j is None: # # second state # if num > sequence[i]: # j = index # else: # # update i # i = index # elif k is None: # if num < sequence[j]: # k = index # break # else: # # update j # j = index # if j is None or k is None: # print('No') # else: # print('YES') # print(i+1,j+1,k+1) ```
instruction
0
48,080
12
96,160
No
output
1
48,080
12
96,161
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) i=1 while i<n and l[i-1]>l[i]: i+=1 if i==n: print("NO") else: a=i while i+1<n and l[i]<l[i+1]: i+=1 if i+1>=n: print("NO") else: print("YES") print(a,a+1,i+2) ```
instruction
0
48,081
12
96,162
No
output
1
48,081
12
96,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p_1, p_2, ..., p_n. Recall that sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once. Find three indices i, j and k such that: * 1 ≤ i < j < k ≤ n; * p_i < p_j and p_j > p_k. Or say that there are no such indices. Input The first line contains a single integer T (1 ≤ T ≤ 200) — the number of test cases. Next 2T lines contain test cases — two lines per test case. The first line of each test case contains the single integer n (3 ≤ n ≤ 1000) — the length of the permutation p. The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n; p_i ≠ p_j if i ≠ j) — the permutation p. Output For each test case: * if there are such indices i, j and k, print YES (case insensitive) and the indices themselves; * if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO Submitted Solution: ``` T = int(input()) for _ in range(T): n = int(input()) ls = list(map(int , input().split())) ans = 'NO' for i in range(1, n-1): if ls[i] > ls[i-1] and ls[i] > ls[i+1]: ans = 'YES' if ans == 'NO': print(ans) else: print(ans) print(i-1, i, i+1) ```
instruction
0
48,082
12
96,164
No
output
1
48,082
12
96,165
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≤ n ≤ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a. Output Print one integer — the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
48,103
12
96,206
Tags: combinatorics, math, sortings Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) x = sorted(a[0:n]) y = sorted(a[n:],reverse=True) M = 998244353 Sum = sum(map(lambda z,c: abs(z-c),x,y)) % M count = Sum for i in range(n*2,n,-1): count = count*i % M fact = 1 for i in range(2,n+1): fact = fact * i % M print((pow(fact,M-2,M)*count)%M ) ```
output
1
48,103
12
96,207
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≤ n ≤ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a. Output Print one integer — the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
48,104
12
96,208
Tags: combinatorics, math, sortings Correct Solution: ``` """ #If FastIO not needed, used this and don't forget to strip #import sys, math #input = sys.stdin.readline """ import os import sys from io import BytesIO, IOBase import heapq as h from bisect import bisect_left, bisect_right from types import GeneratorType BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") from collections import defaultdict as dd, deque as dq import math, string def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) MOD = 998244353 """ Sum of the differences of the elements when sorted in opposite ways A1 A2 A3 ... AN BN ... B3 B2 B1 f = |A1-BN| + ... Sum f over all partitions There are 2N choose N partitions so a lot The smallest element will always be A1 or B1 in whichever partition it is in Suppose we have [A1, A2, A3, A4] A1 A2 A4 A3 A1 A3 A4 A2 A1 A4 A3 A2 A2 A3 A4 A1 A2 A4 A3 A1 A3 A4 A2 A1 A4 is always added A3 is always added A2 is always subtracted A1 is always subtracted So the answer is (2N choose N)*sum(bigger half)-sum(smaller half) """ def solve(): N = getInt() A = getInts() A.sort() ans = 0 #print(A) for j in range(N): ans -= A[j] for j in range(N,2*N): ans += A[j] ans %= MOD curr = 1 for j in range(1,2*N+1): curr *= j curr %= MOD if j == N: n_fac = curr if j == 2*N: tn_fac = curr #print(ans,n_fac,tn_fac) div = pow(n_fac,MOD-2,MOD) return (ans*tn_fac*div*div) % MOD #for _ in range(getInt()): print(solve()) ```
output
1
48,104
12
96,209
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≤ n ≤ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a. Output Print one integer — the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
48,105
12
96,210
Tags: combinatorics, math, sortings Correct Solution: ``` import os,io input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def ncrmod(n,r): num = den = 1 for i in range(r): num = (num * (n - i)) % mod den = (den * (i + 1)) % mod return (num * pow(den, mod - 2, mod)) % mod n = int(input()) a = list(map(int,input().split())) mod = 998244353 l1 = sorted(a[:n]) l2 = sorted(a[n:],reverse=True) diff = sum([abs(l1[i]-l2[i]) for i in range(n)]) out = (ncrmod(2*n,n)*(diff%mod))%mod print(out) ```
output
1
48,105
12
96,211
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≤ n ≤ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a. Output Print one integer — the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
48,106
12
96,212
Tags: combinatorics, math, sortings Correct Solution: ``` def ncr(n,r,p): num = 1 den = 1 for i in range(r): num = num*(n-i)%p den = den*(i+1)%p return (num*pow(den,p-2,p))%p def solve(n,a): a.sort() M = 998244353 ss = sum(a[:n]) ls = sum(a[n:]) return (ls-ss)*ncr(2*n,n,M)%M n = int(input()) a = list(map(int,input().split())) print(solve(n,a)) ```
output
1
48,106
12
96,213
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≤ n ≤ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a. Output Print one integer — the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
48,107
12
96,214
Tags: combinatorics, math, sortings Correct Solution: ``` def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p;den = (den * (i + 1)) % p return (num * pow(den,p - 2, p)) % p n = int(input());a = sorted(list(map(int, input().split())));ans = 0 for i in range(n):ans += abs(a[i] - a[2*n-1-i]) % 998244353 print((ncr(2*n, n, 998244353) * ans) % 998244353) ```
output
1
48,107
12
96,215