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
Provide tags and a correct Python 3 solution for this coding contest problem. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.
instruction
0
66,879
12
133,758
Tags: implementation, sortings Correct Solution: ``` #!/usr/local/env python3 # -*- encoding: utf-8 -*- import sys def readnlines(f_in): n = int(f_in.readline().strip()) s = set() content = f_in.readline().strip() for i, line in zip(range(n), content.split(' ')): if line.isdigit(): s.add(int(line)) else: s.add(line) return s def print_args(): print("Recieved {} arguments = {}.".format(len(sys.argv), sys.argv)) def intersect(l1, r1, l2, r2): left = max(l1, l2) right = min(r1, r2) return left, right, max(0, right - left + 1) def solve(): m = readnlines(sys.stdin) uniques = sorted(m) #print("len(set(m)) = {}".format(len(set(m)))) #print(uniques) if len(uniques) > 3: return "NO" elif len(uniques) == 3: if uniques[2] + uniques[0] == 2 * uniques[1]: return "YES" return "NO" else: return "YES" if __name__ == "__main__": ans = solve() print(ans) ```
output
1
66,879
12
133,759
Provide tags and a correct Python 3 solution for this coding contest problem. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.
instruction
0
66,880
12
133,760
Tags: implementation, sortings Correct Solution: ``` n=int(input()) a=input().split() count=0 a_distinct=[None]*3 for i in a: cur_num=int(i) if cur_num not in a_distinct: if count==3: print("NO") exit() a_distinct[count]=cur_num count+=1 if count<3: print("YES") exit() sum=a_distinct[0]+a_distinct[1]+a_distinct[2] if (sum%3)!=0: print("NO") exit() avg=sum//3 if avg==a_distinct[0] or avg==a_distinct[1] or avg==a_distinct[2]: print("YES") exit() print("NO") ```
output
1
66,880
12
133,761
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` num = int(input()) lst = list(map(int, input().split())) s = sorted(set(lst)) if len(s) <= 2: print('YES') elif len(s) > 3: print('NO') elif s[0] + s[2] == s[1] * 2: print('YES') else: print('NO') ```
instruction
0
66,881
12
133,762
Yes
output
1
66,881
12
133,763
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` cnt = lambda s, x: s.count(x) ii = lambda: int(input()) si = lambda: input() f = lambda: map(int, input().split()) dgl = lambda: list(map(int, input())) il = lambda: list(map(int, input().split())) n = ii() l = sorted(list(set(il()))) sz=len(l) if sz<4: if (sz==3 and 2*l[1]==l[0]+l[2]) or sz==1 or sz==2: exit(print('YES')) print('NO') ```
instruction
0
66,882
12
133,764
Yes
output
1
66,882
12
133,765
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` n = int(input()) ll = list(map(int,input().split())) minn = min(ll) maxx = max(ll) t = maxx - minn if t % 2 == 0: nor = (minn + maxx) // 2 t = True l = [minn, nor,maxx] for i in ll: if i not in l: t = False break if t == True: print('YES') else: print('NO') else: t = True l = [minn, maxx] for i in ll: if i not in l: t = False break if t == True: print('YES') else: print('NO') ```
instruction
0
66,883
12
133,766
Yes
output
1
66,883
12
133,767
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` num=int(input()) a=[int(n) for n in input().split()] b=set(a) c=list(b) c.sort() if len(b)>3: print('NO') elif len(b)<3 : print('YES') else : if c[0]+c[2]==2*c[1] : print('YES') else : print('NO') ```
instruction
0
66,884
12
133,768
Yes
output
1
66,884
12
133,769
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` n = int(input()) a = [int(i) for i in input().split()] q = set() for x in a: if x not in q: q.add(x) print(q) if len(q) > 3: print("NO") elif len(q) < 3: print("YES") else: x, y, z = q if y - x == z - y: print("YES") else : print("NO") ```
instruction
0
66,885
12
133,770
No
output
1
66,885
12
133,771
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` import math from collections import defaultdict def input_ints(): return list(map(int, input().split())) def solve(): n = int(input()) a = input_ints() a = list(set(a)) a.sort() if len(a) == 1 or (len(a) == 3 and a[1] - a[0] == a[2] - a[1]): print('YES') else: print('NO') if __name__ == '__main__': solve() ```
instruction
0
66,886
12
133,772
No
output
1
66,886
12
133,773
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` n = int(input().strip()) arr = sorted(list(map(int, input().strip().split()))) print(['NO','YES'][len(set(arr))<=3]) ```
instruction
0
66,887
12
133,774
No
output
1
66,887
12
133,775
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal. Input The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Examples Input 5 1 3 3 2 1 Output YES Input 5 1 2 3 4 5 Output NO Note In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. Submitted Solution: ``` n =int(input()) lst=list(map(int , input().split())) lst=sorted(lst) mn=lst[0] mx=lst[0] for i in range(n): if mn>lst[i]: mn=lst[i] if mx<lst[i]: mx=lst[i] if (mx-mn)<=3 or n<=2: print("YES") elif (mx-mn)%2==0: x=int((mx+mn)/2) flag=0 for i in lst: if i!=mn and i!=mx and i!=x: flag+=1 if(flag==0): print("YES") else: print("NO") else: print("NO") ```
instruction
0
66,888
12
133,776
No
output
1
66,888
12
133,777
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,901
12
133,802
Tags: constructive algorithms Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) L,R=[0]*n,[0]*n m=-2*10**9 for i in range(n): if a[i]==0: L[i],m=0,i else: L[i]=i-m m=2*10**9 for i in range(n-1,-1,-1): if a[i]==0: R[i],m=0,i else: R[i]=m-i s="" for i in range(n): s+=str(min(R[i],L[i]))+' ' print(s) ```
output
1
66,901
12
133,803
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,902
12
133,804
Tags: constructive algorithms Correct Solution: ``` # @oj: codeforces # @id: hitwanyang # @email: 296866643@qq.com # @date: 2020-11-12 10:38 # @url:https://codeforc.es/contest/803/problem/B import sys,os from io import BytesIO, IOBase import collections,itertools,bisect,heapq,math,string from decimal import * # region fastio BUFSIZE = 8192 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") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ ## 注意嵌套括号!!!!!! ## 先有思路,再写代码,别着急!!! ## 先有朴素解法,不要有思维定式,试着换思路解决 ## 精度 print("%.10f" % ans) ## sqrt:int(math.sqrt(n))+1 ## 字符串拼接不要用+操作,会超时 ## 二进制转换:bin(1)[2:].rjust(32,'0') ## array copy:cur=array[::] ## oeis:example 1, 3, _, 1260, _, _, _, _, _, 12164510040883200 def main(): n=int(input()) a=list(map(int,input().split())) ans=[n]*n r=0 for i in range(n): if a[i]==0: ans[i]=0 j=r while j<i: if a[j]==0: j+=1 continue if r==0 and a[r]!=0: ans[j]=i-j else: ans[j]=min(i-j,j-r) j+=1 r=i if i==n-1 and a[i]!=0: j=r while j<n: ans[j]=j-r j+=1 print (" ".join([str(x) for x in ans])) if __name__ == "__main__": main() ```
output
1
66,902
12
133,805
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,903
12
133,806
Tags: constructive algorithms Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) s = [i for i in range(n) if a[i]==0] c=0 for i in range(n): if c+1<len(s) and (abs(s[c]-i)>abs(s[c+1]-i)): c+=1 print(abs(i-s[c])) ```
output
1
66,903
12
133,807
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,904
12
133,808
Tags: constructive algorithms Correct Solution: ``` n = int(input()) f = list(map(int, input().split())) zeroes = [] for i in range(len(f)): if f[i] == 0: zeroes.append(i) cur0 = 0 if len(zeroes) > 1: next0 = 1 else: next0 = None for i in range(len(f)): end = " " if i == len(f) - 1: end = "\n" if next0 is None: print(abs(i - zeroes[cur0]), end=end) else: if abs(i - zeroes[cur0]) < abs(i - zeroes[next0]): print(abs(i - zeroes[cur0]), end=end) else: print(abs(i - zeroes[next0]), end=end) cur0 = next0 if cur0 == len(zeroes) - 1: next0 = None else: next0 = next0 + 1 ```
output
1
66,904
12
133,809
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,905
12
133,810
Tags: constructive algorithms Correct Solution: ``` def zero(i): for j in range(i+1,n): if a[j] == 0: return j return -1 n = int(input()) a = list(map(int, input().split())) res = [] zerol, zeror = 2 * 10**5 + 1, zero(-1) for i in range(n): if a[i] != 0: res.append(min(abs(i-zeror), abs(i - zerol))) else: res.append(0) zerol, zeror = zeror, zero(i) print(' '.join(map(str, res))) ```
output
1
66,905
12
133,811
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,906
12
133,812
Tags: constructive algorithms Correct Solution: ``` import sys n = int(input()) arr = sys.stdin.readline().split() z = [None]*n j = k = 0 for i in range(n): if(arr[i] == '0'): "z contains zero's index of arr" z[j] = i j += 1 "remove extra space" z = z[0:j] #print(z) for i in range(n): if( arr[i] == '0' ): print(0,'',end='' ) if(i > z[k]): k += 1 else: if( i < z[k] ): """zero is in right side""" print(z[k]-i,'',end='' ) elif( i > z[k] and k < len(z)-1 and i < z[k+1] ): """zero is in both side""" print( (min(i-z[k], z[k+1]-i )),'',end='' ) elif( i > z[k] and k == len(z)-1): """zero is in left side""" print( i - z[k],'',end='') ```
output
1
66,906
12
133,813
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,907
12
133,814
Tags: constructive algorithms Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) l,r,ll,rr,c=[0]*n,[0]*n,-10**9,10**9,[] for i in range(n): if not a[i]:ll=i l[i]=i-ll for i in range(n-1,-1,-1): if not a[i]:rr=i r[i]=rr-i for i in range(n):c.append(str(min(l[i],r[i]))) print(' '.join(c)) ```
output
1
66,907
12
133,815
Provide tags and a correct Python 3 solution for this coding contest problem. You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array. Input The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109). Output Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4
instruction
0
66,908
12
133,816
Tags: constructive algorithms Correct Solution: ``` #!/usr/bin/env python3 MAX = 2*10**5 + 1 n = int(input().strip()) ais = list(map(int, input().strip().split())) dleft = [MAX for _ in range(n)] dright = [MAX for _ in range(n)] for i in range(n): if ais[i] == 0: dleft[i] = 0 else: dleft[i] = MAX if i == 0 else dleft[i - 1] + 1 for i in reversed(range(n)): if ais[i] == 0: dright[i] = 0 else: dright[i] = MAX if i == n - 1 else dright[i + 1] + 1 d = [min(dl, dr) for dl, dr in zip(dleft, dright)] print (' '.join(map(str, d))) ```
output
1
66,908
12
133,817
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After entering high school, Takeko, who joined the programming club, gradually became absorbed in the fun of algorithms. Now, when I'm in the second grade, I'd like to participate in Programming Koshien. At one point, Takeko, who learned about sorting algorithms, tried to design a sorting algorithm herself. The sort algorithm created by Takeko executes the following processing when a column consisting of one or more natural numbers with no duplication between elements is given as input. 1. First, select the first element of the column. 2. When there is an element immediately before the selected element, compare the selected element with the element immediately before it. If the previous element is larger, move it immediately after the end of the column (figure). Continue this operation until the selected element is at the beginning of the column or the previous element is smaller than the selected element. 3. Finish if the selected element is at the end of the column. If not, select a new element immediately after the selected element and return to 2. <image> Takeko decided to count the number of operations to move the element immediately after the end of the column in order to estimate how much computational time this algorithm would take. Write a program that takes column information as input and reports the number of operations that move an element immediately after the end of the column. Input The input is given in the following format. N a1 a2 ... aN The number of elements N (1 ≤ N ≤ 200000) contained in the column is given in the first row. In the second row, the column elements ai (1 ≤ ai ≤ 109) are given in order from the beginning. There is no duplication in element ai. Output Outputs the number of operations to move an element immediately after the end of a column in one row. Examples Input 6 1 3 6 5 8 2 Output 10 Input 4 4 3 2 1 Output 6 Submitted Solution: ``` n = int(input()) Num_lis = list(map(int,input().split())) cou = Num_lis.index(min(Num_lis)) Sort_lis = sorted(Num_lis) for i in range(len(Num_lis)): if Num_lis[i] != Sort_lis[i]: pos = i break if cou == 0: for j in range(1,n - pos): cou += j else: for j in range(1,n - pos - 1): cou += j print(cou) ```
instruction
0
67,162
12
134,324
No
output
1
67,162
12
134,325
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After entering high school, Takeko, who joined the programming club, gradually became absorbed in the fun of algorithms. Now, when I'm in the second grade, I'd like to participate in Programming Koshien. At one point, Takeko, who learned about sorting algorithms, tried to design a sorting algorithm herself. The sort algorithm created by Takeko executes the following processing when a column consisting of one or more natural numbers with no duplication between elements is given as input. 1. First, select the first element of the column. 2. When there is an element immediately before the selected element, compare the selected element with the element immediately before it. If the previous element is larger, move it immediately after the end of the column (figure). Continue this operation until the selected element is at the beginning of the column or the previous element is smaller than the selected element. 3. Finish if the selected element is at the end of the column. If not, select a new element immediately after the selected element and return to 2. <image> Takeko decided to count the number of operations to move the element immediately after the end of the column in order to estimate how much computational time this algorithm would take. Write a program that takes column information as input and reports the number of operations that move an element immediately after the end of the column. Input The input is given in the following format. N a1 a2 ... aN The number of elements N (1 ≤ N ≤ 200000) contained in the column is given in the first row. In the second row, the column elements ai (1 ≤ ai ≤ 109) are given in order from the beginning. There is no duplication in element ai. Output Outputs the number of operations to move an element immediately after the end of a column in one row. Examples Input 6 1 3 6 5 8 2 Output 10 Input 4 4 3 2 1 Output 6 Submitted Solution: ``` N = int(input()) num = list(map(int,input().split())) num1 = num.sort() n = 1 ans = 0 while 1>0: if num[n-1]>num[n]: num.append(num[n-1]) num.remove(num[n-1]) ans += 1 elif num[n-1]<num[n]: if n == N-1: if num == num1: print(ans) break else: n = 1 n += 1 ```
instruction
0
67,163
12
134,326
No
output
1
67,163
12
134,327
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After entering high school, Takeko, who joined the programming club, gradually became absorbed in the fun of algorithms. Now, when I'm in the second grade, I'd like to participate in Programming Koshien. At one point, Takeko, who learned about sorting algorithms, tried to design a sorting algorithm herself. The sort algorithm created by Takeko executes the following processing when a column consisting of one or more natural numbers with no duplication between elements is given as input. 1. First, select the first element of the column. 2. When there is an element immediately before the selected element, compare the selected element with the element immediately before it. If the previous element is larger, move it immediately after the end of the column (figure). Continue this operation until the selected element is at the beginning of the column or the previous element is smaller than the selected element. 3. Finish if the selected element is at the end of the column. If not, select a new element immediately after the selected element and return to 2. <image> Takeko decided to count the number of operations to move the element immediately after the end of the column in order to estimate how much computational time this algorithm would take. Write a program that takes column information as input and reports the number of operations that move an element immediately after the end of the column. Input The input is given in the following format. N a1 a2 ... aN The number of elements N (1 ≤ N ≤ 200000) contained in the column is given in the first row. In the second row, the column elements ai (1 ≤ ai ≤ 109) are given in order from the beginning. There is no duplication in element ai. Output Outputs the number of operations to move an element immediately after the end of a column in one row. Examples Input 6 1 3 6 5 8 2 Output 10 Input 4 4 3 2 1 Output 6 Submitted Solution: ``` n = int(input()) Num_lis = list(map(int,input().split())) cou = Num_lis.index(min(Num_lis)) Sort_lis = sorted(Num_lis) for i in range(len(Num_lis)): if Num_lis[i] != Sort_lis[i]: pos = i break for j in range(1,n - pos - 1): cou += j print(cou) ```
instruction
0
67,164
12
134,328
No
output
1
67,164
12
134,329
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After entering high school, Takeko, who joined the programming club, gradually became absorbed in the fun of algorithms. Now, when I'm in the second grade, I'd like to participate in Programming Koshien. At one point, Takeko, who learned about sorting algorithms, tried to design a sorting algorithm herself. The sort algorithm created by Takeko executes the following processing when a column consisting of one or more natural numbers with no duplication between elements is given as input. 1. First, select the first element of the column. 2. When there is an element immediately before the selected element, compare the selected element with the element immediately before it. If the previous element is larger, move it immediately after the end of the column (figure). Continue this operation until the selected element is at the beginning of the column or the previous element is smaller than the selected element. 3. Finish if the selected element is at the end of the column. If not, select a new element immediately after the selected element and return to 2. <image> Takeko decided to count the number of operations to move the element immediately after the end of the column in order to estimate how much computational time this algorithm would take. Write a program that takes column information as input and reports the number of operations that move an element immediately after the end of the column. Input The input is given in the following format. N a1 a2 ... aN The number of elements N (1 ≤ N ≤ 200000) contained in the column is given in the first row. In the second row, the column elements ai (1 ≤ ai ≤ 109) are given in order from the beginning. There is no duplication in element ai. Output Outputs the number of operations to move an element immediately after the end of a column in one row. Examples Input 6 1 3 6 5 8 2 Output 10 Input 4 4 3 2 1 Output 6 Submitted Solution: ``` n = int(input()) Num_lis = list(map(int,input().split())) cou = Num_lis.index(min(Num_lis)) Sort_lis = sorted(Num_lis) if Num_lis != Sort_lis: for i in range(len(Num_lis)): if Num_lis[i] != Sort_lis[i]: pos = i break if cou == 0: for j in range(1,n - pos): cou += j else: for j in range(1,n - pos - 1): cou += j print(cou) ```
instruction
0
67,165
12
134,330
No
output
1
67,165
12
134,331
Provide a correct Python 3 solution for this coding contest problem. problem Given the sequence $ A $ of length $ N $. The $ i $ item in $ A $ is $ A_i $. You can do the following for this sequence: * $ 1 \ leq i \ leq N --Choose the integer i that is 1 $. Swap the value of $ A_i $ with the value of $ A_ {i + 1} $. Find the minimum number of operations required to make $ A $ a sequence of bumps. A sequence of length $ N $ that satisfies the following conditions is defined as a sequence of irregularities. * For any $ i $ that satisfies $ 1 <i <N $, ​​$ A_ {i + 1}, A_ {i --1}> A_i $ or $ A_ {i + 1}, A_ {i --1} <A_i Satisfy $. Intuitively, increase, decrease, increase ... (decrease, like $ 1, \ 10, \ 2, \ 30, \ \ dots (10, \ 1, \ 30, \ 2, \ \ dots) $ It is a sequence that repeats increase, decrease ...). output Output the minimum number of operations required to make a sequence of bumps. Also, output a line break at the end. Example Input 5 1 2 3 4 5 Output 2
instruction
0
67,216
12
134,432
"Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) ans = n s = 0 b = [0 for i in range(n)] for i in range(n): b[i] = a[i] for i in range(1,n): if i%2 == 1: if b[i] > b[i-1]: flag = True if i < n-1: if b[i-1] > b[i+1] and b[i+1] < b[i]: v = b[i+1] b[i+1] = b[i] b[i] = v flag = False if flag: v = b[i-1] b[i-1] = b[i] b[i] = v s += 1 else: if b[i] < b[i-1]: flag = True if i < n-1: if b[i-1] < b[i+1] and b[i+1] > b[i]: v = b[i+1] b[i+1] = b[i] b[i] = v flag = False if flag: v = b[i-1] b[i-1] = b[i] b[i] = v s += 1 ans = min(ans, s) s = 0 for i in range(n): b[i] = a[i] for i in range(1,n): if i%2 == 0: if b[i] > b[i-1]: flag = True if i < n-1: if b[i-1] > b[i+1] and b[i+1] < b[i]: v = b[i+1] b[i+1] = b[i] b[i] = v flag = False if flag: v = b[i-1] b[i-1] = b[i] b[i] = v s += 1 else: if b[i] < b[i-1]: flag = True if i < n-1: if b[i-1] < b[i+1] and b[i+1] > b[i]: v = b[i+1] b[i+1] = b[i] b[i] = v flag = False if flag: v = b[i-1] b[i-1] = b[i] b[i] = v s += 1 ans = min(ans, s) print(ans) ```
output
1
67,216
12
134,433
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,581
12
135,162
Tags: greedy Correct Solution: ``` n = int(input()) l = list(map(int,input().split())) suf = [0] tot = 0 for i in range(1,n+1): tot += l[n - i] suf.append(min(0,tot,suf[i-1])) pre = [0] for i in range(1,n+1): pre.append(pre[i-1] + l[i-1]) ans = -9999999999999999999 for i in range(n+1): ans = max(ans,pre[n] - 2*pre[i] - 2*suf[n-i]) print(ans) ```
output
1
67,581
12
135,163
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,582
12
135,164
Tags: greedy Correct Solution: ``` n = int(input()) seq = list(map(int, input().split())) soma, melhor = 0, 0 for i in seq: soma = max(0, soma + i) melhor = max(melhor, soma) print(2*melhor - sum(seq)) ```
output
1
67,582
12
135,165
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,583
12
135,166
Tags: greedy Correct Solution: ``` n=int(input()) ans1=0 ans=0 sum=0 x=list(input().split()) for i in range(len(x)): sum+=int(x[i]) ans1+=int(x[i]) ans1=max(ans1,0) ans=max(ans,ans1) print(2*ans-sum) ```
output
1
67,583
12
135,167
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,584
12
135,168
Tags: greedy Correct Solution: ``` n, a = int(input()), list(map(int, input().split())) b = a[:: -1] for i in range(n - 1): a[i + 1] += a[i] for i in range(n - 1): b[i + 1] += b[i] s, a, b = a[-1], [0] + a, [0] + b for i in range(n): a[i + 1] = min(a[i], a[i + 1]) for i in range(n): b[i + 1] = min(b[i], b[i + 1]) b.reverse() print(s - 2 * min(a[i] + b[i] for i in range(n))) ```
output
1
67,584
12
135,169
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,585
12
135,170
Tags: greedy Correct Solution: ``` n = int(input()) values = list(map(int, input().split())) best_infix = infix = 0 for x in values: infix = max(0, infix + x) best_infix = max(best_infix, infix) print(2 * best_infix - sum(values)) ```
output
1
67,585
12
135,171
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,586
12
135,172
Tags: greedy Correct Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO,IOBase def main(): n = int(input()) a = list(map(int,input().split())) dp = [0 if a[-1]>0 else a[-1]] dp1 = [0 if a[-1]<0 else a[-1]] xx = a[-1] for i in range(n-2,0,-1): xx += a[i] dp.append(min(dp[-1],xx)) dp1.append(max(dp1[-1],xx)) dp.reverse() dp1.reverse() tot = sum(a) ans = abs(tot) x = 0 for i in range(n-1): x += a[i] ans = max(ans,tot+abs(x)-x+max(abs(dp[i])-dp[i],abs(dp1[i])-dp1[i])) print(ans) #Fast IO Region 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") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == '__main__': main() ```
output
1
67,586
12
135,173
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,587
12
135,174
Tags: greedy Correct Solution: ``` n = int(input()) arr = input().split() arr = [int(i) for i in arr] max_, sum_ = 0, 0 for i in range(len(arr)): sum_ += arr[i] if sum_ < 0: sum_ = 0 max_ = max(max_, sum_) print(2*max_ - sum(arr)) ```
output
1
67,587
12
135,175
Provide tags and a correct Python 3 solution for this coding contest problem. Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations? Input The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself. Output The first and the only line of the output should contain the answer to the problem. Examples Input 3 -1 -2 -3 Output 6 Input 5 -4 2 0 5 0 Output 11 Input 5 -1 10 -5 10 -2 Output 18
instruction
0
67,588
12
135,176
Tags: greedy Correct Solution: ``` n = int(input()) s = input().split() a = [] sum = 0 for i in range(0, n) : a.append(int(s[i])) sum += int(s[i]) maxx = 0 s = 0 for i in range(0, n) : if (s + a[i] < 0) : s = 0 else : s += a[i] maxx = max(maxx, s) maxx = max(maxx, s) print(maxx * 2 - sum) ```
output
1
67,588
12
135,177
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,133
12
136,266
Tags: sortings Correct Solution: ``` import math import sys from collections import defaultdict # input = sys.stdin.readline rt = lambda: map(int, input().split()) ri = lambda: int(input()) rl = lambda: list(map(int, input().split())) def main(): n, m = rt() a = rl() def greaterCount(m): sums = defaultdict(int) s = n sums[s] = 1 res = 0 add = 0 for i in range(n): if a[i] < m: s -= 1 add -= sums[s] else: add += sums[s] s += 1 res += add sums[s] += 1 return res print(greaterCount(m) - greaterCount(m+1)) if __name__ == '__main__': main() ```
output
1
68,133
12
136,267
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,134
12
136,268
Tags: sortings Correct Solution: ``` class BinaryIndexedTree: def __init__(self, n): self.bit = [0] * n def add(self, i, x): i += 1 while i <= len(self.bit): self.bit[i-1] += x i += i & -i def sum_sub(self, i): a = 0 i += 1 while i: a += self.bit[i-1] i -= i & -i return a def sum(self, i, j): a = 0 if j != 0: a += self.sum_sub(j-1) if i != 0: a -= self.sum_sub(i-1) return a def f(m): ans=0 bit=BinaryIndexedTree(2*n+7) f=0 for i in range(n): bit.add(n+f,1) if a[i]>m:f-=1 else:f+=1 ans+=bit.sum_sub(n+f) return ans n,m=map(int,input().split()) a=list(map(int,input().split())) print(f(m)-f(m-1)) ```
output
1
68,134
12
136,269
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,135
12
136,270
Tags: sortings Correct Solution: ``` L1=list(map(int, input().split())) numList=list(map(int, input().split())) length=L1[0] m=L1[1] def greaterCount(numList,m): countDic={0:1} sum=0 total=0 rem=0 for number in numList: if number>=m: sum+=1 rem+=countDic[sum-1] total+=rem else: sum-=1 if sum in countDic: rem-=countDic[sum] total+=rem if sum in countDic: countDic[sum] += 1 else: countDic[sum] = 1 #print("m=", m, "number=", number, "sum=", sum, "total=", total, "rem=", rem, "countDic=", countDic) return total print(greaterCount(numList,m)-greaterCount(numList,m+1)) ```
output
1
68,135
12
136,271
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,136
12
136,272
Tags: sortings Correct Solution: ``` n, m = map(int, input().split()) a = list(map(int, input().split())) class BIT: def __init__(self, n): self.n = n self.data = [0]*(n+1) def to_sum(self, i): s = 0 while i > 0: s += self.data[i] i -= (i & -i) return s def add(self, i, x): while i <= self.n: self.data[i] += x i += (i & -i) def get(self, i, j): #[i,j](1<=i<=j<=N) return self.to_sum(j) - self.to_sum(i - 1) def f(x, V): if x < V: return -1 return 1 def calc_median(M): b = [f(v, M) for v in a] res = 0 c = [0] for x in b: c.append(c[-1] + x) d = [(c[i], i) for i in range(n + 1)] bit = BIT(2*n + 10) for value, index in d: if index == 0: bit.add(value + n + 1, 1) continue res += bit.get(1, value + n) bit.add(value + n + 1, 1) return res print(calc_median(m) - calc_median(m + 1)) ```
output
1
68,136
12
136,273
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,137
12
136,274
Tags: sortings Correct Solution: ``` MAXN = 200001 def less_sum(s, m): n = len(s) a = 0 b = 0 res = 0 last = 0 count = [0 for i in range(-MAXN, MAXN+1)] count[0] = 1 x = 0 last = 1 for i in range(n): if s[i] > m: b += 1 else: a += 1 x = a-b #print(x) #print(count[-2], count[-1], count[0], count[1], count[2]) if s[i] > m: last -= count[x+1] else: last += count[x] #print(x, last) res += last count[x] += 1 last += 1 #print(res) return res n, m = map(int, input().split(' ')) s = list(map(int, input().split(' ')))[0:n] #print(m, s) print(less_sum(s, m) - less_sum(s, m-1)) ```
output
1
68,137
12
136,275
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,138
12
136,276
Tags: sortings Correct Solution: ``` class BIT(): def __init__(self,n): self.BIT=[0]*(n+1) self.num=n def query(self,idx): res_sum = 0 while idx > 0: res_sum += self.BIT[idx] idx -= idx&(-idx) return res_sum #Ai += x O(logN) def update(self,idx,x): while idx <= self.num: self.BIT[idx] += x idx += idx&(-idx) return n,m = map(int,input().split()) a = list(map(int,input().split())) def solve(x): tmp = [0 for i in range(n)] for i in range(n): if a[i]>x: tmp[i] = -1 else: tmp[i] = 1 tmp[i] += tmp[i-1] tmp = [0] + tmp val = list(set([tmp[j] for j in range(n+1)])) val.sort() comp = {i:e+1 for e,i in enumerate(val)} bit = BIT(n+1) res = 0 for i in range(n+1): res += bit.query(comp[tmp[i]]) bit.update(comp[tmp[i]],1) return res print(solve(m) - solve(m-1)) ```
output
1
68,138
12
136,277
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,139
12
136,278
Tags: sortings Correct Solution: ``` def ask(x): s={} s[0]=1 sum,cnt,res=0,0,0 for i in range(n): if(a[i]<x): sum-=1 cnt-=s.get(sum,0) else: cnt+=s.get(sum,0) sum+=1 s[sum]=s.get(sum,0)+1 res+=cnt return res n,m=map(int,input().split()) a=list(map(int,input().split())) print(ask(m)-ask(m+1)) ```
output
1
68,139
12
136,279
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5).
instruction
0
68,140
12
136,280
Tags: sortings Correct Solution: ``` def main(): n, m = map(int, input().split()) l = list(map(int, input().split())) res = [] for m in m, m - 1: r = c = 0 cnt = [0] * 400002 cnt[0] = last = 1 for a in l: if a > m: c -= 1 last -= cnt[c + 1] else: c += 1 last += cnt[c] r += last cnt[c] += 1 last += 1 res.append(r) print(res[0] - res[1]) if __name__ == '__main__': main() ```
output
1
68,140
12
136,281
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5). Submitted Solution: ``` def cv(r, n, d): s = [0]*(2*n + 1) q = n ans = 0 s[q] = 1 z = 0 for i in range(n): if d[i] < r: q -= 1 z -= s[q] else: z += s[q] q += 1 ans += z s[q] += 1 return ans n, r = map(int,input().split()) d = list(map(int,input().split())) print(cv(r, n, d) - cv(r + 1, n, d)) ```
instruction
0
68,141
12
136,282
Yes
output
1
68,141
12
136,283
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an integer sequence a_1, a_2, ..., a_n. Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if a=[4, 2, 7, 5] then its median is 4 since after sorting the sequence, it will look like [2, 4, 5, 7] and the left of two middle elements is equal to 4. The median of [7, 1, 2, 9, 6] equals 6 since after sorting, the value 6 will be in the middle of the sequence. Write a program to find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that the value of median of a_l, a_{l+1}, ..., a_r is exactly the given number m. Input The first line contains integers n and m (1 ≤ n,m ≤ 2⋅10^5) — the length of the given sequence and the required value of the median. The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2⋅10^5). Output Print the required number. Examples Input 5 4 1 4 5 60 4 Output 8 Input 3 1 1 1 1 Output 6 Input 15 2 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Output 97 Note In the first example, the suitable pairs of indices are: (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 5), (4, 5) and (5, 5). Submitted Solution: ``` def grCount(m, n, a): s = [0]*(2*n + 1) sx = n result = 0 s[sx] = 1 add = 0 for i in range(n): if a[i] < m: sx -= 1 add -= s[sx] else: add += s[sx] sx += 1 result += add s[sx] += 1 return result n, m = map(int,input().split()) a = list(map(int,input().split())) print(grCount(m, n, a) - grCount(m + 1, n, a)) ```
instruction
0
68,142
12
136,284
Yes
output
1
68,142
12
136,285
Provide tags and a correct Python 2 solution for this coding contest problem. You are given an array a, consisting of n positive integers. Let's call a concatenation of numbers x and y the number that is obtained by writing down numbers x and y one right after another without changing the order. For example, a concatenation of numbers 12 and 3456 is a number 123456. Count the number of ordered pairs of positions (i, j) (i ≠ j) in array a such that the concatenation of a_i and a_j is divisible by k. Input The first line contains two integers n and k (1 ≤ n ≤ 2 ⋅ 10^5, 2 ≤ k ≤ 10^9). The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). Output Print a single integer — the number of ordered pairs of positions (i, j) (i ≠ j) in array a such that the concatenation of a_i and a_j is divisible by k. Examples Input 6 11 45 1 10 12 11 7 Output 7 Input 4 2 2 78 4 10 Output 12 Input 5 2 3 7 19 3 3 Output 0 Note In the first example pairs (1, 2), (1, 3), (2, 3), (3, 1), (3, 4), (4, 2), (4, 3) suffice. They produce numbers 451, 4510, 110, 1045, 1012, 121, 1210, respectively, each of them is divisible by 11. In the second example all n(n - 1) pairs suffice. In the third example no pair is sufficient.
instruction
0
68,151
12
136,302
Tags: implementation, math Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_num(): return int(raw_input()) def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ # main code inp=inp() n,k = inp[0],inp[1] l=inp[2:] d=defaultdict(Counter) ans=0 for i in l: for j in range(1,11): d[j][(i*pow(10,j,k))%k]+=1 for i in l: tp=(k-(i%k))%k x=len(str(i)) tp1=(i*pow(10,x,k))%k d[x][tp1]-=1 ans+=d[x][tp] d[x][tp1]+=1 pr_num(ans) ```
output
1
68,151
12
136,303
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has got an array consisting of n integers, and two integers k and len in addition. All numbers in the array are either between 1 and k (inclusive), or equal to -1. The array is good if there is no segment of len consecutive equal numbers. Vasya will replace each -1 with some number from 1 to k (inclusive) in such a way that the resulting array is good. Tell him the number of ways to do this replacement. Since the answer may be large, print it modulo 998244353. Input The first line contains three integers n, k and len (1 ≤ n ≤ 10^5, 1 ≤ k ≤ 100, 1 ≤ len ≤ n). The second line contains n numbers — the array. Each number is either -1 or between 1 and k (inclusive). Output Print one integer — the number of ways to replace each -1 with some number from 1 to k (inclusive) so the array is good. The answer may be large, so print it modulo 998244353. Examples Input 5 2 3 1 -1 1 -1 2 Output 2 Input 6 3 2 1 1 -1 -1 -1 -1 Output 0 Input 10 42 7 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 Output 645711643 Note Possible answers in the first test: 1. [1, 2, 1, 1, 2]; 2. [1, 2, 1, 2, 2]. There is no way to make the array good in the second test, since first two elements are equal. There are too many answers in the third test, so we won't describe any of them.
instruction
0
68,176
12
136,352
Tags: dp Correct Solution: ``` def vasya_and_array(): n,k,leng = [int(x) for x in input().split()] if(leng==1): return 0 a = [int(x) for x in input().split()] mod = 998244353 a.insert(0,0) dp = [[0 for x in range((k+1))] for y in range(n+1)] sumdp = [0 for _ in range(n+1)] sumdp[0]=1 count = [0 for _ in range(k+1)] for i in range(1,n+1): for j in range(1,k+1): if(a[i]==-1 or a[i]==j): dp[i][j] = sumdp[i-1] count[j]+=1 if(count[j] >= leng): dp[i][j]-=(sumdp[i-leng] - dp[i-leng][j]) dp[i][j]%=mod sumdp[i]+=dp[i][j] sumdp[i]%=mod else: count[j]=0 return (sumdp[n]) print(vasya_and_array()) ```
output
1
68,176
12
136,353
Provide tags and a correct Python 3 solution for this coding contest problem. This is the harder version of the problem. In this version, 1 ≤ n, m ≤ 2⋅10^5. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 2⋅10^5) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 2⋅10^5) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
instruction
0
68,259
12
136,518
Tags: data structures, greedy Correct Solution: ``` import sys input=sys.stdin.readline from operator import itemgetter def updatebit(BITTree , n , i ,v): #print('n',n) while i <= n: #print('i',i) BITTree[i] += v i += i & (-i) #print(BITTree) def bisect_left( val): sum_ = 0 pos = 0 bit_length = (n+1).bit_length() for i in range(bit_length, -1, -1): k = pos + (1 << i) #print('a',sum_,val,k,pos) if k < n + 1 and sum_ + l[k] < val: sum_ += l[k] pos += 1 << i #print('b',sum_,self.bit[k],val,k,pos) return pos n=int(input()) l=[0]*(n+1) a=[int(i) for i in input().split() if i!='\n'] empty=[] for i in range(len(a)): empty.append([a[i],i+1]) empty.sort(key=lambda x:x[1]) empty.sort(key=lambda x:x[0], reverse=True) m = int(input()) info = [list(map(int, input().split())) + [i] for i in range(m) if i!='\n'] info = sorted(info, key = itemgetter(0)) #print(empty) bit,ans,cnt=[0]*(n+1),[0]*(m),0 for i in range(m): k, pos, ind = info[i] while cnt < k: updatebit(l,n,empty[cnt][1], 1) cnt += 1 ans[ind] = a[bisect_left(pos)] for i in ans: sys.stdout.write(str(i)+'\n') ```
output
1
68,259
12
136,519
Provide tags and a correct Python 3 solution for this coding contest problem. This is the harder version of the problem. In this version, 1 ≤ n, m ≤ 2⋅10^5. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 2⋅10^5) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 2⋅10^5) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
instruction
0
68,260
12
136,520
Tags: data structures, greedy Correct Solution: ``` # Binary Indexed Tree (Fenwick Tree) class BIT(): """一点加算、区間取得クエリをそれぞれO(logN)で答える add: i番目にvalを加える get_sum: 区間[l, r)の和を求める i, l, rは0-indexed """ def __init__(self, n): self.n = n self.bit = [0] * (n + 1) def _sum(self, i): s = 0 while i > 0: s += self.bit[i] i -= i & -i return s def add(self, i, val): """i番目にvalを加える""" i = i + 1 while i <= self.n: self.bit[i] += val i += i & -i def get_sum(self, l, r): """区間[l, r)の和を求める""" return self._sum(r) - self._sum(l) def bisect_left(self, val): """[0, r)の和がval以下になるときの最小のrを求める""" sum_ = 0 pos = 0 bit_length = (n+1).bit_length() for i in range(bit_length, -1, -1): k = pos + (1 << i) if k < self.n + 1 and sum_ + self.bit[k] < val: sum_ += self.bit[k] pos += 1 << i return pos + 1 from operator import itemgetter n = int(input()) a = list(map(int, input().split())) m = int(input()) info = [list(map(int, input().split())) + [i] for i in range(m)] b = list(zip(a, range(len(a)))) b = sorted(b, key = itemgetter(0), reverse = True) info = sorted(info, key = itemgetter(0)) bit = BIT(n) ans = [0]*m cnt = 0 for i in range(m): k, pos, ind = info[i] while cnt < k: bit.add(b[cnt][1], 1) cnt += 1 ans[ind] = a[bit.bisect_left(pos)-1] for i in ans: print(i) ```
output
1
68,260
12
136,521
Provide tags and a correct Python 3 solution for this coding contest problem. This is the harder version of the problem. In this version, 1 ≤ n, m ≤ 2⋅10^5. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 2⋅10^5) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 2⋅10^5) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
instruction
0
68,261
12
136,522
Tags: data structures, greedy Correct Solution: ``` from bisect import bisect_left, bisect_right, insort_right class SquareSkipList: def __init__(self, values=None, sorted_=False, square=1000, seed=42): inf = float("inf") self.square = square if values is None: self.rand_y = seed self.layer1 = [inf] self.layer0 = [[]] else: self.layer1 = layer1 = [] self.layer0 = layer0 = [] if not sorted_: values.sort() y = seed l0 = [] for v in values: y ^= (y & 0x7ffff) << 13 y ^= y >> 17 y ^= (y & 0x7ffffff) << 5 if y % square == 0: layer0.append(l0) l0 = [] layer1.append(v) else: l0.append(v) layer1.append(inf) layer0.append(l0) self.rand_y = y def add(self, x): y = self.rand_y y ^= (y & 0x7ffff) << 13 y ^= y >> 17 y ^= (y & 0x7ffffff) << 5 self.rand_y = y if y % self.square == 0: layer1, layer0 = self.layer1, self.layer0 idx1 = bisect_right(layer1, x) layer1.insert(idx1, x) layer0_idx1 = layer0[idx1] idx0 = bisect_right(layer0_idx1, x) layer0.insert(idx1+1, layer0_idx1[idx0:]) # layer0 は dict で管理した方が良いかもしれない # dict 微妙だった del layer0_idx1[idx0:] else: idx1 = bisect_right(self.layer1, x) insort_right(self.layer0[idx1], x) def remove(self, x): idx1 = bisect_left(self.layer1, x) layer0_idx1 = self.layer0[idx1] idx0 = bisect_left(layer0_idx1, x) if idx0 == len(layer0_idx1): del self.layer1[idx1] self.layer0[idx1] += self.layer0.pop(idx1+1) else: del layer0_idx1[idx0] def search_higher_equal(self, x): # x 以上の最小の値を返す O(log(n)) idx1 = bisect_left(self.layer1, x) layer0_idx1 = self.layer0[idx1] idx0 = bisect_left(layer0_idx1, x) if idx0 == len(layer0_idx1): return self.layer1[idx1] return layer0_idx1[idx0] def search_higher(self, x): # x を超える最小の値を返す O(log(n)) idx1 = bisect_right(self.layer1, x) layer0_idx1 = self.layer0[idx1] idx0 = bisect_right(layer0_idx1, x) if idx0 == len(layer0_idx1): return self.layer1[idx1] return layer0_idx1[idx0] def search_lower(self, x): # x 未満の最大の値を返す O(log(n)) idx1 = bisect_left(self.layer1, x) layer0_idx1 = self.layer0[idx1] idx0 = bisect_left(layer0_idx1, x) if idx0 == 0: # layer0_idx1 が空の場合とすべて x 以上の場合 return self.layer1[idx1-1] return layer0_idx1[idx0-1] def pop(self, idx): # 小さい方から idx 番目の要素を削除してその要素を返す(0-indexed) # O(sqrt(n)) # for を回すので重め、使うなら square パラメータを大きめにするべき layer0 = self.layer0 s = -1 for i, l0 in enumerate(layer0): s += len(l0) + 1 if s >= idx: break if s==idx: layer0[i] += layer0.pop(i+1) return self.layer1.pop(i) else: return layer0[i].pop(idx-s) def __getitem__(self, item): layer0 = self.layer0 s = -1 for i, l0 in enumerate(layer0): s += len(l0) + 1 if s >= item: break if s == item: return self.layer1[i] else: return layer0[i][item - s] def print(self): print(self.layer1) print(self.layer0) import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) M = int(input()) Ans = [-1] * M KP = [list(map(int, input().split())) for _ in range(M)] Idx_query = list(range(M)) Idx_query.sort(key=lambda x: KP[x][0]) Idx = list(range(N)) Idx.sort(key=lambda x: A[x], reverse=True) n = 0 st = SquareSkipList() for idx_query in Idx_query: k, pos = KP[idx_query] while n < k: idx = Idx[n] st.add(idx) n += 1 #st.print() #print(pos) Ans[idx_query] = A[st[pos-1]] #print(A[st[pos-1]]) print("\n".join(map(str, Ans))) ```
output
1
68,261
12
136,523
Provide tags and a correct Python 3 solution for this coding contest problem. Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible. More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution. Input The first line contains a single number n (1 ≤ n ≤ 2000) — the length of the permutations. The second line contains a sequence of n numbers from 1 to n — permutation p. Each number from 1 to n occurs exactly once in this line. The third line contains a sequence of n numbers from 1 to n — permutation s. Each number from 1 to n occurs once in this line. Output In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s. In the second line print number k (0 ≤ k ≤ 2·106) — the number of operations needed to get the solution. In the next k lines print the operations. Each line must contain two numbers i and j (1 ≤ i, j ≤ n, i ≠ j), which means that you need to swap pi and pj. It is guaranteed that the solution exists. Examples Input 4 4 2 1 3 3 2 4 1 Output 3 2 4 3 3 1 Note In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins.
instruction
0
68,563
12
137,126
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) c = {} for i in range(n): c[b[i]] = i b = [] for i in range(n): a[i] = c[a[i]] print(sum(abs(a[i] - i) for i in range(n)) >> 1) while True: for i in range(n): if a[i] < i: for j in range(a[i], i): if a[j] >= i: a[i], a[j] = a[j], a[i] b += [(i+1, j+1)] break break else: break print(len(b)) for e in b: print(*e) ```
output
1
68,563
12
137,127
Provide tags and a correct Python 3 solution for this coding contest problem. Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible. More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution. Input The first line contains a single number n (1 ≤ n ≤ 2000) — the length of the permutations. The second line contains a sequence of n numbers from 1 to n — permutation p. Each number from 1 to n occurs exactly once in this line. The third line contains a sequence of n numbers from 1 to n — permutation s. Each number from 1 to n occurs once in this line. Output In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s. In the second line print number k (0 ≤ k ≤ 2·106) — the number of operations needed to get the solution. In the next k lines print the operations. Each line must contain two numbers i and j (1 ≤ i, j ≤ n, i ≠ j), which means that you need to swap pi and pj. It is guaranteed that the solution exists. Examples Input 4 4 2 1 3 3 2 4 1 Output 3 2 4 3 3 1 Note In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins.
instruction
0
68,564
12
137,128
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = {} for i in range(n): c[b[i]] = i b = [] for i in range(n): a[i] = c[a[i]] print(sum(abs(a[i] - i) for i in range(n)) >> 1) while True: for i in range(n): if a[i] < i: for j in range(a[i], i): if a[j] >= i: a[i], a[j] = a[j], a[i] b.append((i + 1, j + 1)) break break else: break print(len(b)) for i in b: print(*i) ```
output
1
68,564
12
137,129
Provide tags and a correct Python 3 solution for this coding contest problem. Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible. More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution. Input The first line contains a single number n (1 ≤ n ≤ 2000) — the length of the permutations. The second line contains a sequence of n numbers from 1 to n — permutation p. Each number from 1 to n occurs exactly once in this line. The third line contains a sequence of n numbers from 1 to n — permutation s. Each number from 1 to n occurs once in this line. Output In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s. In the second line print number k (0 ≤ k ≤ 2·106) — the number of operations needed to get the solution. In the next k lines print the operations. Each line must contain two numbers i and j (1 ≤ i, j ≤ n, i ≠ j), which means that you need to swap pi and pj. It is guaranteed that the solution exists. Examples Input 4 4 2 1 3 3 2 4 1 Output 3 2 4 3 3 1 Note In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins.
instruction
0
68,565
12
137,130
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) p = [int(x) for x in input().split()] s = [int(x) for x in input().split()] list = [] for i in range(n): list.append(s.index(p[i])-i) cost = 0 output = [] while list != [0]*n: for i in range(n): if list[i] != 0: increment = 1 if list[i] < 0: increment = -1 for j in range(i+increment,i + list[i]+increment, increment): if list[j] <= i-j: output.append([i+1,j+1]) change = abs(i-j) cost += change temp = list[i] - change*increment list[i] = list[j] + change*increment list[j] = temp break print(cost) print(len(output)) for i in output: print(i[0],i[1]) ```
output
1
68,565
12
137,131
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible. More formally, we have two permutations, p and s of numbers from 1 to n. We can swap pi and pj, by paying |i - j| coins for it. Find and print the smallest number of coins required to obtain permutation s from permutation p. Also print the sequence of swap operations at which we obtain a solution. Input The first line contains a single number n (1 ≤ n ≤ 2000) — the length of the permutations. The second line contains a sequence of n numbers from 1 to n — permutation p. Each number from 1 to n occurs exactly once in this line. The third line contains a sequence of n numbers from 1 to n — permutation s. Each number from 1 to n occurs once in this line. Output In the first line print the minimum number of coins that you need to spend to transform permutation p into permutation s. In the second line print number k (0 ≤ k ≤ 2·106) — the number of operations needed to get the solution. In the next k lines print the operations. Each line must contain two numbers i and j (1 ≤ i, j ≤ n, i ≠ j), which means that you need to swap pi and pj. It is guaranteed that the solution exists. Examples Input 4 4 2 1 3 3 2 4 1 Output 3 2 4 3 3 1 Note In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation 3241 equal to s. We pay |3 - 1| = 2 coins for that. In total we pay three coins. Submitted Solution: ``` n = int(input()) a = list(map(int,input().split())) b= list(map(int,input().split())) ans = [] for i in range(n): for k in range(n): if a[i] == b[k]: if i == k: ans.append(-1) break ans.append(k+1) break j = 0 otv = [] t = 0 for i in range(n): if t == 1: t = 0 i -= 1 for k in range(n-i-1): if ans[k] > ans[k+1+i] and min(ans[k],ans[k+1+i]) != -1: otv.append([k+1,k+i+2]) ans[k],ans[k+i+1] = ans[k+1+i],ans[k] j += (abs(k-i)) t = 1 print(j) print(len(otv)) for i in range(len(otv)): print(*otv[i]) ```
instruction
0
68,566
12
137,132
No
output
1
68,566
12
137,133