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. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
instruction
0
102,105
12
204,210
Tags: dp, greedy, two pointers Correct Solution: ``` from collections import defaultdict def main(): t=int(input()) allans=[] for _ in range(t): n=int(input()) a=readIntArr() b=sorted(a) if a==b: allans.append(0) continue minIndexes=defaultdict(lambda:inf) maxIndexes=defaultdict(lambda:-inf) for i in range(n): minIndexes[a[i]]=min(minIndexes[a[i]],i) maxIndexes[a[i]]=max(maxIndexes[a[i]],i) uniqueVals=list(minIndexes.keys()) uniqueVals.sort() # iterating from left, if maxIdx[smaller]>minIdx[larger], then everything less than smaller must be shifted left m=len(uniqueVals) prefixMaxOverlapIndex=[0]*m for i in range(1,m): if maxIndexes[uniqueVals[i-1]]>minIndexes[uniqueVals[i]]: prefixMaxOverlapIndex[i]=i prefixMaxOverlapIndex[i]=max(prefixMaxOverlapIndex[i],prefixMaxOverlapIndex[i-1]) #similar logic when iterating from right suffixMinOverlapIndex=[m-1]*m for i in range(m-2,-1,-1): if maxIndexes[uniqueVals[i]]>minIndexes[uniqueVals[i+1]]: suffixMinOverlapIndex[i]=i suffixMinOverlapIndex[i]=min(suffixMinOverlapIndex[i],suffixMinOverlapIndex[i+1]) preCnts=prefixMaxOverlapIndex sufCnts=[m-1-x for x in suffixMinOverlapIndex] ans=inf for i in range(m): # find the min if I don't move uniqueVals[i] ans=min(ans,preCnts[i]+sufCnts[i]) # print('minIndexes:{}'.format(minIndexes)) # print('maxIndexes:{}'.format(maxIndexes)) # print('uniqueVals:{}'.format(uniqueVals)) # print('pre:{}'.format(preCnts)) # print('suf:{}'.format(sufCnts)) allans.append(ans) multiLineArrayPrint(allans) return import sys input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) # input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] # def readFloatArr(): # return [float(x) for x in input().split()] def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m]) dv=defaultValFactory;da=dimensionArr if len(da)==1:return [dv() for _ in range(da[0])] else:return [makeArr(dv,da[1:]) for _ in range(da[0])] def queryInteractive(i,j): print('? {} {}'.format(i,j)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print('! {}'.format(' '.join([str(x) for x in ans]))) sys.stdout.flush() inf=float('inf') MOD=10**9+7 # MOD=998244353 for _abc in range(1): main() ```
output
1
102,105
12
204,211
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
instruction
0
102,106
12
204,212
Tags: dp, greedy, two pointers Correct Solution: ``` import sys input = sys.stdin.readline def solve(): n = int(input()) a = list(map(int,input().split())) s = set(a) s = sorted(list(s)) ref = {x:i for i,x in enumerate(list(s))} sz = len(s) L = [1<<32]*sz R = [-1<<32]*sz for i in range(n): k = ref[a[i]] L[k] = min(L[k], i) R[k] = max(R[k], i) dp = [0]*sz for k in range(sz): if k == 0 or L[k] < R[k-1]: dp[k] = 1 else: dp[k] = 1 + dp[k-1] ans = 1<<32 for k in range(sz): ans = min(ans, sz - dp[k]) print(ans) return 0 for nt in range(int(input())): solve() ```
output
1
102,106
12
204,213
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
instruction
0
102,107
12
204,214
Tags: dp, greedy, two pointers Correct Solution: ``` # | # _` | __ \ _` | __| _ \ __ \ _` | _` | # ( | | | ( | ( ( | | | ( | ( | # \__,_| _| _| \__,_| \___| \___/ _| _| \__,_| \__,_| import sys import math def read_line(): return sys.stdin.readline()[:-1] def read_int(): return int(sys.stdin.readline()) def read_int_line(): return [int(v) for v in sys.stdin.readline().split()] def read_float_line(): return [float(v) for v in sys.stdin.readline().split()] t = read_int() for i in range(t): n = read_int() a = read_int_line() d = {} for i in range(n): if a[i] in d: d[a[i]].append(i) else: d[a[i]] = [i] dp = [1]*len(list(d.keys())) s = list(d.keys()) s.sort() for i in range(len(s)-2,-1,-1): if d[s[i]][-1] < d[s[i+1]][0]: dp[i] = dp[i+1]+1 else: dp[i] = 1 ans = len(s)-max(dp) print(ans) ```
output
1
102,107
12
204,215
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
instruction
0
102,108
12
204,216
Tags: dp, greedy, two pointers Correct Solution: ``` # SHRi GANESHA author: Kunal Verma # import os import sys from bisect import bisect_left, bisect_right from collections import Counter, defaultdict from io import BytesIO, IOBase from math import gcd, inf, sqrt, ceil, floor #sys.setrecursionlimit(2*10**5) def lcm(a, b): return (a * b) // gcd(a, b) ''' mod = 10 ** 9 + 7 fac = [1] for i in range(1, 2 * 10 ** 5 + 1): fac.append((fac[-1] * i) % mod) fac_in = [pow(fac[-1], mod - 2, mod)] for i in range(2 * 10 ** 5, 0, -1): fac_in.append((fac_in[-1] * i) % mod) fac_in.reverse() def comb(a, b): if a < b: return 0 return (fac[a] * fac_in[b] * fac_in[a - b]) % mod ''' MAXN = 1000004 spf = [0 for i in range(MAXN)] def sieve(): spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(4, MAXN, 2): spf[i] = 2 for i in range(3, ceil(sqrt(MAXN))): if (spf[i] == i): for j in range(i * i, MAXN, i): if (spf[j] == j): spf[j] = i def getFactorization(x): ret = Counter() while (x != 1): ret[spf[x]] += 1 x = x // spf[x] return ret def printDivisors(n): i = 2 z = [1, n] while i <= sqrt(n): if (n % i == 0): if (n / i == i): z.append(i) else: z.append(i) z.append(n // i) i = i + 1 return z def create(n, x, f): pq = len(bin(n)[2:]) if f == 0: tt = min else: tt = max dp = [[inf] * n for _ in range(pq)] dp[0] = x for i in range(1, pq): for j in range(n - (1 << i) + 1): dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))]) return dp def enquiry(l, r, dp, f): if l > r: return inf if not f else -inf if f == 1: tt = max else: tt = min pq1 = len(bin(r - l + 1)[2:]) - 1 return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1]) def SieveOfEratosthenes(n): prime = [True for i in range(n + 1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n + 1, p): prime[i] = False p += 1 x = [] for i in range(2, n + 1): if prime[i]: x.append(i) return x def main(): for _ in range(int(input())): n = int(input()) a = [int(X) for X in input().split()] x = sorted(list(set(a))) y = [-1] * n mi, ma = [-1] * len(x), [0] * len(x) dp = [1] * len(x) for i in range(len(x)): y[x[i] - 1] = i for i in range(n): if mi[y[a[i] - 1]] == -1: mi[y[a[i] - 1]] = i ma[y[a[i] - 1]] = i # print(mi,ma) for i in range(1, len(x)): if mi[i] > ma[i - 1]: dp[i] += dp[i - 1] # print(dp) print(len(x) - max(dp)) # 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
102,108
12
204,217
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
instruction
0
102,109
12
204,218
Tags: dp, greedy, two pointers Correct Solution: ``` def main(): from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) inp1 = [-1] * (n + 1) inp2 = [-1] * (n + 1) for i, ai in enumerate(map(int, stdin.readline().split())): if inp1[ai] < 0: inp1[ai] = i inp2[ai] = i inp1 = tuple((inp1i for inp1i in inp1 if inp1i >= 0)) inp2 = tuple((inp2i for inp2i in inp2 if inp2i >= 0)) n = len(inp1) ans = 0 cur = 0 for i in range(n): if i and inp1[i] < inp2[i - 1]: cur = 1 else: cur += 1 ans = max(ans, cur) stdout.write(f'{n - ans}\n') main() ```
output
1
102,109
12
204,219
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
instruction
0
102,110
12
204,220
Tags: dp, greedy, two pointers Correct Solution: ``` from sys import stdin, stdout, setrecursionlimit input = stdin.readline # import string # characters = string.ascii_lowercase # digits = string.digits # setrecursionlimit(int(1e5)) # dir = [-1,0,1,0,-1] # moves = 'NESW' inf = float('inf') from functools import cmp_to_key from collections import defaultdict as dd from collections import Counter, deque from heapq import * import math from math import floor, ceil, sqrt def geti(): return map(int, input().strip().split()) def getl(): return list(map(int, input().strip().split())) def getis(): return map(str, input().strip().split()) def getls(): return list(map(str, input().strip().split())) def gets(): return input().strip() def geta(): return int(input()) def print_s(s): stdout.write(s+'\n') def solve(): for _ in range(geta()): n = geta() a = getl() mini = dd(int) maxi = dd(int) for i in range(n): if a[i] not in mini: mini[a[i]] = i maxi[a[i]] = i order = sorted(set(a)) good = 0 count = 1 for i in range(1, len(order)): if mini[order[i]] > maxi[order[i-1]]: count += 1 else: good = max(good, count) count = 1 good = max(good, count) print(len(order) - good) if __name__=='__main__': solve() ```
output
1
102,110
12
204,221
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning.
instruction
0
102,111
12
204,222
Tags: dp, greedy, two pointers Correct Solution: ``` from sys import stdin input = stdin.readline def main(): anses = [] for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) f = [0]*(n+1) d = sorted(list(set(a))) for q in range(1, len(d)+1): f[d[q-1]] = q for q in range(len(a)): a[q] = f[a[q]] n = len(d) starts, ends = [-1]*(n+1), [n+1]*(n+1) for q in range(len(a)): if starts[a[q]] == -1: starts[a[q]] = q ends[a[q]] = q s = [0]*(n+1) max1 = -float('inf') for q in range(1, n+1): s[q] = s[q-1]*(ends[q-1] < starts[q])+1 max1 = max(max1, s[q]) anses.append(str(len(d)-max1)) print('\n'.join(anses)) main() ```
output
1
102,111
12
204,223
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio 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") # ------------------------------ from math import factorial from collections import Counter, defaultdict, deque from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) def ctd(chr): return ord(chr)-ord("a") mod = 998244353 INF = float('inf') from bisect import bisect_left # ------------------------------ def main(): for _ in range(N()): n = N() arr = RLL() dic = defaultdict(list) for i in range(n): dic[arr[i]].append(i) nl = list(dic.keys()) nl.sort(reverse=1) le = len(nl) dp = [1]*le for i in range(1, le): if dic[nl[i]][-1]<dic[nl[i-1]][0]: dp[i] = dp[i-1]+1 print(le-max(dp)) if __name__ == "__main__": main() ```
instruction
0
102,112
12
204,224
Yes
output
1
102,112
12
204,225
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` from sys import stdin input = stdin.readline def Input(): global A, n, D ,F n = int(input()) A = list(map(int, input().split())) D = sorted(list(set(A))) F = [0] * (n + 1) def Ans(): for i in range(1, len(D) + 1): F[D[i - 1]] = i for i in range(n): A[i] = F[A[i]] m = len(D) Start = [-1] * (m + 1) End = [n + 1] * (m + 1) for i in range(n): if Start[A[i]] == -1: Start[A[i]] = i End[A[i]] = i S = [0] * (m + 1) Max = -float('inf') for i in range(1, m + 1): S[i] = S[i - 1] * (End[i - 1] < Start[i]) + 1 Max = max(Max, S[i]) Result.append(str(m-Max)) def main(): global Result Result=[] for _ in range(int(input())): Input() Ans() print('\n'.join(Result)) if __name__ == '__main__': main() ```
instruction
0
102,113
12
204,226
Yes
output
1
102,113
12
204,227
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` import copy def DeleteRepetitionsIn(Array): AlreadyRead = {} index = 0 ConstantArray = copy.deepcopy(Array) for a in range(len(ConstantArray)): if Array[index] not in AlreadyRead: AlreadyRead[Array[index]] = "" index += 1 continue Array = Array[0:index] + Array[index + 1:len(Array)] return Array def DeleteRepetitionsIn2(Array): AlreadyRead = {} for elem in Array: if elem in AlreadyRead: continue AlreadyRead[elem] = "" return list(AlreadyRead) Results = [] ArraysNumber = int(input()) for e in range(ArraysNumber): AbsolutelyUselessNumber = int(input()) Array = list(map(int, input().split())) if len(Array) == 1: Results.append(0) continue #print(Array) TheRightOrder = DeleteRepetitionsIn2(Array) TheRightOrder.sort() TheCurrentOrder = {} for i in range(len(Array)): if Array[i] not in TheCurrentOrder: TheCurrentOrder[Array[i]] = [i, i] continue TheCurrentOrder[Array[i]][1] = i #print(TheRightOrder) #print(TheCurrentOrder) #print(Array) TheCurrentResult = 1 TheMaxResult = 1 for i in range(len(TheRightOrder)): #print("a =", TheCurrentResult) #print("b =", TheMaxResult) if i == len(TheRightOrder) - 1: if TheCurrentResult >= TheMaxResult: TheMaxResult = TheCurrentResult continue if TheCurrentOrder[TheRightOrder[i]][1] > TheCurrentOrder[TheRightOrder[i + 1]][0]: if TheCurrentResult >= TheMaxResult: TheMaxResult = TheCurrentResult TheCurrentResult = 1 continue TheCurrentResult += 1 Results.append(len(TheRightOrder) - TheMaxResult) for i in Results: print(i) ```
instruction
0
102,114
12
204,228
Yes
output
1
102,114
12
204,229
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` import os import sys def solve(arr): items = sorted(set(arr)) min_max = [(float("inf"), float("-inf"))] * len(items) item_to_idx = {k: idx for idx, k in enumerate(items)} for idx, a in enumerate(arr): m, M = min_max[item_to_idx[a]] min_max[item_to_idx[a]] = (min(idx, m), max(idx, M)) best = 1 current = 1 for i in range(1, len(items)): _, prev_M = min_max[i - 1] m, _ = min_max[i] if prev_M <= m: current += 1 else: current = 1 best = max(best, current) return len(items) - best def pp(input): T = int(input()) for t in range(T): input() arr = list(map(int, input().strip().split())) print(solve(arr)) if "paalto" in os.getcwd(): from string_source import string_source, codeforces_parse pp( string_source( """3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7""" ) ) else: pp(sys.stdin.readline) ```
instruction
0
102,115
12
204,230
Yes
output
1
102,115
12
204,231
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` import copy def DeleteRepetitionsIn(Array): AlreadyRead = {} index = 0 ConstantArray = copy.deepcopy(Array) for a in range(len(ConstantArray)): if Array[index] not in AlreadyRead: AlreadyRead[Array[index]] = "" index += 1 continue Array = Array[0:index] + Array[index + 1:len(Array)] return Array def DeleteRepetitionsIn2(Array): AlreadyRead = {} for elem in Array: if elem in AlreadyRead: continue AlreadyRead[elem] = "" return list(AlreadyRead) Results = [] ArraysNumber = int(input()) for e in range(ArraysNumber): AbsolutelyUselessNumber = int(input()) Array = list(map(int, input().split())) if len(Array) == 1: Results.append(0) continue if len(Array) == 300000: Results.append(0) continue #print(Array) TheRightOrder = DeleteRepetitionsIn2(Array) TheRightOrder.sort() TheCurrentOrder = {} for i in range(len(Array)): if Array[i] not in TheCurrentOrder: TheCurrentOrder[Array[i]] = [i, i] continue TheCurrentOrder[Array[i]][1] = i #print(TheRightOrder) #print(TheCurrentOrder) #print(Array) TheCurrentResult = 1 TheMaxResult = 1 for i in range(len(TheRightOrder)): #print("a =", TheCurrentResult) #print("b =", TheMaxResult) if i == len(TheRightOrder) - 1: if TheCurrentResult >= TheMaxResult: TheMaxResult = TheCurrentResult continue if TheCurrentOrder[TheRightOrder[i]][1] > TheCurrentOrder[TheRightOrder[i + 1]][0]: if TheCurrentResult >= TheMaxResult: TheMaxResult = TheCurrentResult TheCurrentResult = 1 continue TheCurrentResult += 1 Results.append(len(TheRightOrder) - TheMaxResult) for i in Results: print(i) ```
instruction
0
102,116
12
204,232
No
output
1
102,116
12
204,233
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` import sys as _sys def main(): q = int(input()) for i_q in range(q): n, = _read_ints() a = tuple(_read_ints()) result = find_min_sorting_cost(sequence=a) print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split(" ")) def find_min_sorting_cost(sequence): sequence = tuple(sequence) if not sequence: return 0 indices_by_values = {x: [] for x in sequence} for i, x in enumerate(sequence): indices_by_values[x].append(i) borders_by_values = { x: (indices[0], indices[-1]) for x, indices in indices_by_values.items() } max_lengths_by_right_borders_tree = [0] * (len(sequence) + 1) for x in sorted(borders_by_values.keys()): left_border, right_border = borders_by_values[x] # max_prev_length = max(max_lengths_by_right_borders[:left_border+1]) max_prev_length = _prefix_max_fenwick_tree(max_lengths_by_right_borders_tree, left_border) max_new_length = max_prev_length + 1 _set_new_max_fenwick_tree(max_lengths_by_right_borders_tree, right_border, max_new_length) max_cost_can_keep_n = _prefix_max_fenwick_tree( max_lengths_by_right_borders_tree, len(sequence) - 1 ) return len(set(sequence)) - max_cost_can_keep_n def _prefix_max_fenwick_tree(tree, i): i += 1 result = tree[1] while i: if tree[i] > result: result = tree[i] i -= i & (-i) return result def _set_new_max_fenwick_tree(tree, i, x): i += 1 if x < tree[i]: return while i < len(tree): if x > tree[i]: tree[i] = x i += i & (-i) if __name__ == '__main__': main() ```
instruction
0
102,117
12
204,234
No
output
1
102,117
12
204,235
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` def solveForMovingItemsRight(a,n): # Preserve the Longest Increasing Subsequence containing min. Everything else must shift latestIdx=dict() for i in range(n): latestIdx[a[i]]=i numIdx=[] # [number, latest idx] for k,v in latestIdx.items(): numIdx.append([k,v]) numIdx.sort() # sort by number asc removedNumbers=set() j=0 for i in range(n): if a[i]!=numIdx[j][0]: removedNumbers.add(a[i]) else: if i==numIdx[j][1]: # last idx for this number j+=1 # print('a:{} latestIdx:{} numIdx:{} removedNumbers:{}'.format(a,latestIdx,numIdx,removedNumbers)) return len(removedNumbers) def solveForMovingItemsLeft(a,n): # equivalent to move right, with negated and reversed array b=a.copy() b.reverse() for i in range(n): b[i]*=-1 return solveForMovingItemsRight(b,n) def main(): t=int(input()) allans=[] for _ in range(t): n=int(input()) a=readIntArr() b=sorted(a) if b==a: allans.append(0) continue ans=min(solveForMovingItemsLeft(a,n),solveForMovingItemsRight(a,n)) allans.append(ans) multiLineArrayPrint(allans) return import sys input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) # input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] # def readFloatArr(): # return [float(x) for x in input().split()] def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m]) dv=defaultValFactory;da=dimensionArr if len(da)==1:return [dv() for _ in range(da[0])] else:return [makeArr(dv,da[1:]) for _ in range(da[0])] def queryInteractive(i,j): print('? {} {}'.format(i,j)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print('! {}'.format(' '.join([str(x) for x in ans]))) sys.stdout.flush() inf=float('inf') MOD=10**9+7 # MOD=998244353 for _abc in range(1): main() ```
instruction
0
102,118
12
204,236
No
output
1
102,118
12
204,237
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n, consisting of integers. You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation. For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements): * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning; * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end; * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning; * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end; * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning; * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end; You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≀ a_i is satisfied. Note that you have to answer q independent queries. Input The first line contains one integer q (1 ≀ q ≀ 3 β‹… 10^5) β€” the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer n (1 ≀ n ≀ 3 β‹… 10^5) β€” the number of elements. The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≀ a_i ≀ n) β€” the elements. It is guaranteed that the sum of all n does not exceed 3 β‹… 10^5. Output For each query print one integer β€” the minimum number of operation for sorting sequence a in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Output 2 0 1 Note In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all 2-elements to the beginning. Submitted Solution: ``` import sys as _sys ZERO = '0' ONE = '1' def main(): t = int(input()) for i_t in range(t): n, = _read_ints() s = _read_line() result = find_max_operations_n_can_make(s) print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def find_max_operations_n_can_make(s): assert isinstance(s, str) islands_lengths = [] curr_length = 1 for prev_x, curr_x in zip(s, s[1:]): if curr_x == prev_x: curr_length += 1 else: islands_lengths.append(curr_length) curr_length = 1 islands_lengths.append(curr_length) islands_n = len(islands_lengths) islands_powers = [length - 1 for length in islands_lengths] indexed_nonzero_islands_powers_rev = [ [i, power] for i, power in enumerate(islands_powers) if power != 0 ][::-1] i_next_island_to_eat = 0 while indexed_nonzero_islands_powers_rev: indexed_nonzero_islands_powers_rev[-1][1] -= 1 if indexed_nonzero_islands_powers_rev[-1][1] == 0: indexed_nonzero_islands_powers_rev.pop() while indexed_nonzero_islands_powers_rev \ and indexed_nonzero_islands_powers_rev[-1][0] <= i_next_island_to_eat: indexed_nonzero_islands_powers_rev.pop() i_next_island_to_eat += 1 islands_eaten = i_next_island_to_eat assert islands_eaten <= islands_n one_elem_islands_remain = islands_n - islands_eaten moves_used = islands_eaten moves_used += one_elem_islands_remain // 2 + one_elem_islands_remain % 2 return moves_used if __name__ == '__main__': main() ```
instruction
0
102,119
12
204,238
No
output
1
102,119
12
204,239
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,173
12
204,346
Tags: brute force, implementation, two pointers Correct Solution: ``` import sys,math #sys.setrecursionlimit(100000000) input = sys.stdin.readline ############ ---- USER DEFINED INPUT FUNCTIONS ---- ############ def inp(): return(int(input())) def inara(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) ################################################################ ############ ---- THE ACTUAL CODE STARTS BELOW ---- ############ t=inp() for _ in range(t): n=inp() ara=inara() fre=[0]*(n+2) special=set() for i in range(n): tot=ara[i] fre[ara[i]]+=1 for j in range(i+1,n): tot+=ara[j] if tot<=n: special.add(tot) ans=0 for element in special: ans+=fre[element] print(ans) ```
output
1
102,173
12
204,347
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,174
12
204,348
Tags: brute force, implementation, two pointers Correct Solution: ``` t = int(input()) for i in range(t): n = int(input()) ai = list(map(int,input().split())) ai3 = [0] * (n+1) ai3[1] = ai[0] for i in range(1,n): ai3[i+1] = ai[i] + ai3[i] ai2 = [0] * (n*2+1) i = 0 j = 1 while j <= n: for z in range(i+2,j+1): ai2[ai3[z] - ai3[i]] += 1 if ai3[j] - ai3[i] > n: i += 1 else: j += 1 while i < n: for z in range(i+2,n+1): ai2[ai3[z] - ai3[i]] += 1 i += 1 ans = 0 for i in range(n): ans += int(ai2[ai[i]] != 0) print(ans) ```
output
1
102,174
12
204,349
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,175
12
204,350
Tags: brute force, implementation, two pointers Correct Solution: ``` def main(): LMIIS = lambda: list(map(int,input().split())) II = lambda: int(input()) MOD = 10**9+7 from collections import defaultdict for _ in range(II()): n = II() A = LMIIS() d = [0]*(n+1) u = max(A) numcounts = [0]*(u+1) for i in range(n): numcounts[A[i]] += 1 d[i+1] = d[i] + A[i] ans = 0 for i in range(n-1): for j in range(i+2,n+1): if d[j] - d[i] <= u: ans += numcounts[d[j] - d[i]] numcounts[d[j] - d[i]] = 0 print(ans) if __name__ == '__main__': main() ```
output
1
102,175
12
204,351
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,176
12
204,352
Tags: brute force, implementation, two pointers Correct Solution: ``` t=int(input()) for w in range(t): n=int(input()) l=[int(i) for i in input().split()] l1=[0]*8001 for i in range(n): k=l[i] for j in range(i+1,n): k+=l[j] if(k<=n): l1[k]+=1 c=0 for i in range(n): if(l1[l[i]]!=0): c+=1 print(c) ```
output
1
102,176
12
204,353
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,177
12
204,354
Tags: brute force, implementation, two pointers Correct Solution: ``` from collections import Counter for _ in " "*int(input()): n=int(input()) a=list(map(int,input().split())) d=Counter(a) cnt=0 for i in range(n): s=a[i] for j in range(i+1,n): s+=a[j] if s in d: cnt+=d[s] d[s]=0 print(cnt) ```
output
1
102,177
12
204,355
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,178
12
204,356
Tags: brute force, implementation, two pointers Correct Solution: ``` from collections import defaultdict import sys def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() for _ in range(int(input())): n = int(input()) a = get_array() d = defaultdict(int) for i in range(len(a)): d[a[i]] += 1 sum1 = 0 for i in range(n): sum = a[i] for j in range(i + 1, n): sum += a[j] if sum > n: break sum1 += d[sum] d[sum]=0 print(sum1) ```
output
1
102,178
12
204,357
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,179
12
204,358
Tags: brute force, implementation, two pointers Correct Solution: ``` # Why do we fall ? So we can learn to pick ourselves up. t = int(input()) for _ in range(0,t): n = int(input()) aa = [int(i) for i in input().split()] dic = dict() ss = 0 for i in aa: if i not in dic: dic[i] = 1 else: dic[i] += 1 for i in range(0,n): temp = aa[i] for j in range(i+1,n): temp += aa[j] if temp in dic: # print(temp,"temp") ss += dic[temp] dic[temp] = 0 print(ss) """ 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 """ ```
output
1
102,179
12
204,359
Provide tags and a correct Python 3 solution for this coding contest problem. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0
instruction
0
102,180
12
204,360
Tags: brute force, implementation, two pointers Correct Solution: ``` t=int(input()) for i in range(t): n=int(input()) b=list(map(int,input().split())) d=dict() e=dict() for j in range(n): if b[j] in d.keys(): d[b[j]]+=1 else: d[b[j]]=1 for i in range(0,n): temp = b[i] for j in range(i+1, n): temp += b[j] if temp>n: break e[temp]=1 c=list(set(b)) p=0 for j in c: if j in e.keys(): p+=d[j] print(p) ```
output
1
102,180
12
204,361
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` tc=int(input()) while tc!=0: tc=tc-1 n=int(input()) a=list(map(int,input().split(' '))) c=0 b=[] m={} mx=0 for i in range(len(a)): c=c+a[i] b.append(c) mx=max(mx,a[i]) if a[i] in m: m[a[i]]=m[a[i]]+1 else: m[a[i]]=1 c=0 for i in range(len(a)): s=0 for j in range(i,len(a)): s=s+a[j] if s>mx: break if s in m and i!=j: if m[s]!=0: c=c+m[s] m[s]=0 print(c) ```
instruction
0
102,181
12
204,362
Yes
output
1
102,181
12
204,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` # Don't ask for what the world needs. Ask what makes you come alive, and go do it. BrenοΏ½ Brown # by : Blue Edge - Create some chaos for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=[0]*(n+1) i=0 for i in range(n): s=a[i] for j in range(i+1,n): s+=a[j] if s<=n: b[s]=1 else: break # print(b) print(sum([b[x] for x in a])) ```
instruction
0
102,182
12
204,364
Yes
output
1
102,182
12
204,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` from collections import Counter,defaultdict,deque #import heapq as hq #import itertools #from operator import itemgetter #from itertools import count, islice #from functools import reduce #alph = 'abcdefghijklmnopqrstuvwxyz' #from math import factorial as fact #a,b = [int(x) for x in input().split()] #sarr = [x for x in input().strip().split()] #import math import sys input=sys.stdin.readline #sys.setrecursionlimit(2**30) def solve(): n = int(input()) arr = [int(x) for x in input().split()] c = defaultdict(int) mx = 0 for el in arr: if el>mx: mx=el c[el]+=1 res = 0 for i in range(n): su = arr[i] p = i+1 while su<=mx and p<n: su+=arr[p] if c[su]: res+=c[su] c[su]=0 p+=1 print(res) tt = int(input()) for test in range(tt): solve() # ```
instruction
0
102,183
12
204,366
Yes
output
1
102,183
12
204,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` tc=int(input()) for _ in range(tc): n=int(input()) a=list(map(int,input().split())) present=[0 for _ in range(n+1)] is_seg_sum=[0 for _ in range(n+1)] for i in range(n): present[a[i]]+=1 for i in range(n): seg_sum=a[i] for j in range(i+1,n): seg_sum+=a[j] if seg_sum>n: break is_seg_sum[seg_sum]=1 ans=0 for i in range(1,n+1): if is_seg_sum[i]==1: ans+=present[i] print(ans) ```
instruction
0
102,184
12
204,368
Yes
output
1
102,184
12
204,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` from collections import Counter for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=Counter(a) c=list(a) for i in range(1,n): c[i]+=c[i-1] ans=0 for i in range(n): for j in range(i+1,n): v=c[j]-c[i]+a[i] if v in b: ans+=1 b[v]-=1 if b[v]==0: b.pop(v) print(ans) ```
instruction
0
102,185
12
204,370
No
output
1
102,185
12
204,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` n=int(input()) for i in range(n): a=int(input()) x=list(map(int,input().split())) count=0 for j in range(a): abc=0 aa=0 k=0 for k in range(a): # while k!=a: abc+=x[k] if abc>x[j]: abc-=x[aa] aa+=1 if x[j]==abc and (k-aa)>0: # print(x[j]) # print(aa,k) count+=1 break abc=0 aa=k+1 # k+=1 print(count) ```
instruction
0
102,186
12
204,372
No
output
1
102,186
12
204,373
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` from collections import deque from collections import OrderedDict import math #data = sys.stdin.readlines() import sys import os from io import BytesIO import threading import bisect #input = sys.stdin.readline for t in range(int(input())): n = int(input()) array = input().split() prefixSum = [0]*n hashS = set() prefS = set() for i in range(n): array[i] = int(array[i]) hashS.add(array[i]) prefixSum[0]=array[0] prefS.add(0) prefS.add(array[0]) for i in range(1,n): prefixSum[i]=array[i]+prefixSum[i-1] answer = 0 for i in range(1,n): for j in hashS: value = prefixSum[i]-j print(i, "++", value, j) if value in prefS and j!=array[i]: print("YES", prefS) answer+=1 prefS.add(prefixSum[i]) print(answer) ```
instruction
0
102,187
12
204,374
No
output
1
102,187
12
204,375
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array a=[a_1, a_2, …, a_n] (1 ≀ a_i ≀ n) is given. Its element a_i is called special if there exists a pair of indices l and r (1 ≀ l < r ≀ n) such that a_i = a_l + a_{l+1} + … + a_r. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array a. For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5: * a_3=4 is a special element, since a_3=4=a_1+a_2=3+1; * a_5=5 is a special element, since a_5=5=a_2+a_3=1+4; * a_6=9 is a special element, since a_6=9=a_1+a_2+a_3+a_4=3+1+4+1; * a_8=6 is a special element, since a_8=6=a_2+a_3+a_4=1+4+1; * a_9=5 is a special element, since a_9=5=a_2+a_3=1+4. Please note that some of the elements of the array a may be equal β€” if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases in the input. Then t test cases follow. Each test case is given in two lines. The first line contains an integer n (1 ≀ n ≀ 8000) β€” the length of the array a. The second line contains integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000. Output Print t numbers β€” the number of special elements for each of the given arrays. Example Input 5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1 Output 5 1 0 4 0 Submitted Solution: ``` def solution(t): for _ in range(t): n = int(input()) a = list(map(int, input().split())) o = set() c = 0 dic = {} if n == 7999: print(7999) else: for d in range(2, len(a) + 1): for i in range(len(a) - d + 1): temp = 0 for k in range(d): temp += a[i+k] o.add(temp) for i in a: if i in o: c += 1 print(c) solution(int(input())) ```
instruction
0
102,188
12
204,376
No
output
1
102,188
12
204,377
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,189
12
204,378
Tags: constructive algorithms, math Correct Solution: ``` t=int(input()) for _ in range(t): n=int(input()) arr=[int(x) for x in input().split()] a=-1 b=n for i in range(n): if arr[i]!=i+1: break a=i for i in range(n-1,-1,-1): if arr[i]!=i+1: break b=i if a==n-1: print(0) continue ans=1 for i in range(a+1,b): if arr[i]==i+1: ans+=1 break print(ans) ```
output
1
102,189
12
204,379
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,190
12
204,380
Tags: constructive algorithms, math Correct Solution: ``` # list(map(int, input().split())) rw = int(input()) for ewq in range(rw): n = int(input()) a = list(map(int, input().split())) asorted = sorted(a) b = [] s = 0 t = True for i in range(n): if a[i] == i + 1: b.append(1) else: b.append(0) i = 0 if b.count(1) == n: print(0) continue while i < n: if b[i] == 0: s += 1 while i < n and b[i] == 0: i += 1 i += 1 t = False if s == 1: b0 = b.index(0) for i in range(b0, n): bk = i if b[i] == 1: break else: bk = n c = {i + 1 for i in range(b0, bk)} d = set() for i in range(b0, bk): d.add(a[i]) t = False if c == d: t = True if t: print(1) else: print(2) ```
output
1
102,190
12
204,381
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,191
12
204,382
Tags: constructive algorithms, math Correct Solution: ``` for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) if(li == sorted(li)): print(0) continue else: x = sorted(li) xs = list(range(1, n+1)) li = li[::-1]; x = x[::-1]; xs = xs[::-1] while(li[-1] == x[-1]): li.pop(); x.pop(); xs.pop() li = li[::-1]; x = x[::-1]; xs = xs[::-1] while(li[-1] == x[-1]): li.pop(); x.pop(); xs.pop() fnd = 0 for i in range(len(li)): if(li[i] == xs[i]): fnd = 1 break if(fnd): print(2) continue else: print(1) continue ```
output
1
102,191
12
204,383
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,192
12
204,384
Tags: constructive algorithms, math Correct Solution: ``` from collections import Counter,defaultdict,deque #from heapq import * #from itertools import * #from operator import itemgetter #from itertools import count, islice #from functools import reduce #alph = 'abcdefghijklmnopqrstuvwxyz' #dirs = [[1,0],[0,1],[-1,0],[0,-1]] #from math import factorial as fact #a,b = [int(x) for x in input().split()] #sarr = [x for x in input().strip().split()] #import math #from math import * import sys input=sys.stdin.readline #sys.setrecursionlimit(2**30) #MOD = 10**9+7 def primes(n): arr = [] nn = n d = 2 while d*d<=nn and n!=1: if n%d==0: arr.append(d) while n%d==0: n//=d d+=1 if n>1: arr.append(n) return arr def solve(): n = int(input()) arr = [int(x) for x in input().split()] if arr == sorted(arr): print(0) else: flag = False i = 0 while arr[i] == i+1: i+=1 while i<n: if flag and arr[i]!=i+1: print(2) return if arr[i] == i+1: flag = True i+=1 print(1) tt = int(input()) for test in range(tt): solve() # ```
output
1
102,192
12
204,385
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,193
12
204,386
Tags: constructive algorithms, math Correct Solution: ``` ''' bug is : the 2134 case -> printed 2 it should be 1 ''' t = int(input()) for _ in range(t): n = int(input()) c, m = 0, 0 c_arr = [0 for _ in range(n)] a = [int(e) - 1 for e in input().split()] for i in range(n): if a[i] == i: c_arr[i] = 1 c = c + 1 if c != n: left, right = 0, 0 while a[left] == left: left = left + 1 while a[n - right - 1] == n - right - 1: right = right + 1 if c == n: ans = 0 elif (c == 0) or (c == left + right): ans = 1 else: ans = 2 print(ans) ```
output
1
102,193
12
204,387
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,194
12
204,388
Tags: constructive algorithms, math Correct Solution: ``` from sys import stdin input=stdin.readline for _ in range(int(input())): n=int(input()) lis=list(map(int,input().split())) lis.insert(0,0) if lis==sorted(lis): print(0) else: for i in range(1,n+1): if i!=lis[i]: break for j in range(n,0,-1): if j!=lis[j]: break for k in range(i,j+1): if lis[k]==k: print(2) break else: print(1) ```
output
1
102,194
12
204,389
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,195
12
204,390
Tags: constructive algorithms, math Correct Solution: ``` t=int(input()) for _ in range(t): n=int(input()) arr=list(map(int,input().split())) count=0 f=1 for i in range(n): if(arr[i]!=i+1): if(f==1): count+=1 f=0 else: f=1 print(min(2,count)) ```
output
1
102,195
12
204,391
Provide tags and a correct Python 3 solution for this coding contest problem. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7]
instruction
0
102,196
12
204,392
Tags: constructive algorithms, math Correct Solution: ``` #!/usr/bin/env python3 import sys input=sys.stdin.readline t=int(input()) for _ in range(t): n=int(input()) arr=list(map(int,input().split())) persuit=sorted(arr) l=0 for i in range(n): if arr[i]!=persuit[i]: l=i break else: print(0) continue r=0 for i in range(n-1,-1,-1): if arr[i]!=persuit[i]: r=i break for i in range(l,r+1): if arr[i]==persuit[i]: print(2) break else: print(1) ```
output
1
102,196
12
204,393
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` #!/usr/bin/env python3 from sys import stdin as cin from itertools import groupby from operator import eq lmap = lambda f, v: list(map(f, v)) def main(): t = int(next(cin).strip()) for i in range(t): n = int(next(cin).strip()) a = lmap(int, next(cin).strip().split()) inplace = [inplace for (inplace, _) in groupby(i == ai for (i, ai) in enumerate(a, 1))] if inplace == [True]: print(0) elif len(inplace) < 3 or inplace == [True, False, True]: print(1) else: print(2) main() ```
instruction
0
102,197
12
204,394
Yes
output
1
102,197
12
204,395
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` t = int(input()) for T in range(t): n = int(input()) a = [int(x) for x in input().split()] a1 = a[:] a1.sort() if a == a1: print(0) continue count = 0 for i in range(n): if i > 0 and a[i - 1] == i and a[i] != i + 1 and count == 1: count = 2 if a[i] != i + 1 and count == 0: count = 1 print(count) ```
instruction
0
102,198
12
204,396
Yes
output
1
102,198
12
204,397
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` import math from collections import deque import sys sys.setrecursionlimit(10**4) def Divisors(n) : l=[] i = 2 while i <= math.sqrt(n): if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) i = i + 1 return l def SieveOfEratosthenes(n): l=[] prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 for p in range(2, n+1): if prime[p]: l.append(p) return l def primeFactors(n): l=[] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: l.append(i) n = n / i if n > 2: l.append(n) return(l) def Factors(n) : result = [] for i in range(2,(int)(math.sqrt(n))+1) : if (n % i == 0) : if (i == (n/i)) : result.append(i) else : result.append(i) result.append(n//i) result.append(1) return result def maxSubArraySum(a): max_so_far = 0 max_ending_here = 0 size=len(a) for i in range(0, size): max_ending_here = max_ending_here + a[i] if (max_so_far < abs(max_ending_here)): max_so_far = max_ending_here return max_so_far def longestsubarray(arr, n, k): current_count = 0 # this will contain length of # longest subarray found max_count = 0 for i in range(0, n, 1): if (arr[i] % k != 0): current_count += 1 else: current_count = 0 max_count = max(current_count, max_count) return max_count #print(SieveOfEratosthenes(100)) #print(Divisors(100)) #print(primeFactors(100)) #print(Factors(100)) #print(maxSubArraySum(a)) def main(): n=int(input()) l=list(map(int,input().split())) l1=[] f=0 for i in range(1,n+1): if l[i-1]!=i: l1.append(0) f=1 else: l1.append(1) if f==0: print(0) return for i in range(0,n): if l1[i]!=1: break s=i for i in range(n-1,-1,-1): if l1[i]!=1: break p=i c=0 for i in range(s,p+1): if l1[i]==1: c+=1 if c==0: print(1) else: print(2) t=int(input()) for i in range(0,t): main() ```
instruction
0
102,199
12
204,398
Yes
output
1
102,199
12
204,399
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` t=int(input()) for _ in range(t): n=int(input()) ar=list(map(int,input().split())) a,b=0,0 st=[] j=1 for i in ar: if i==j: a+=1 st+=["1"] else: b+=1 st+=["0"] j+=1 if b==0: print(0) elif a==0: print(1) else: st="".join(st) if (b*"0" in st): print(1) else: print(2) ```
instruction
0
102,200
12
204,400
Yes
output
1
102,200
12
204,401
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` t = int(input()) for k in range(t): n = int(input()) a = list(map(int, input().split())) a += [pow(10, 20)] c = 0 for i in range(n): if a[i] > a[i + 1]: c += 1 print(c) ```
instruction
0
102,201
12
204,402
No
output
1
102,201
12
204,403
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` t=int(input()) for _ in range(t): n=int(input()) ar=list(map(int,input().split())) a,b=0,0 st="" for i in range(n): if ar[i]==i+1: a+=1 st+="1" else: b+=1 st+="0" if b==0: print(0) elif a==0: print(1) else: if ("101" in st)or("010" in st): print(2) else: print(1) ```
instruction
0
102,202
12
204,404
No
output
1
102,202
12
204,405
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) ok=[] for i in range(n): if(l[i]==i+1): ok.append(i) if(len(ok)>2 and len(ok)!=n):print(2) elif(len(ok)==0):print(1) else:print(0) ```
instruction
0
102,203
12
204,406
No
output
1
102,203
12
204,407
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10^{18}. An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the given permutation. The second line of each test case contains n integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ n) β€” the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. Example Input 2 5 1 2 3 4 5 7 3 2 4 5 1 6 7 Output 0 2 Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3, 2, 4, 5, 1, 6, 7] Perform special exchange on range (1, 5) [4, 1, 2, 3, 5, 6, 7] Perform special exchange on range (1, 4) [1, 2, 3, 4, 5, 6, 7] Submitted Solution: ``` for _ in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] mistakes = 0 b = [] for ind, sym in enumerate(a): if ind + 1 != sym: mistakes += 1 b.append('-') else: b.append('+') groups = -1 for ind, pos_neg in enumerate(b): if ind > 1: if pos_neg != b[ind - 1]: groups += 1 c = [] counter = 0 nexus = 1 for ind, neg in enumerate(b): if ind > 1: if neg == '-' and b[ind - 1] == '-': nexus += 1 elif b[ind - 1] == '-': c.append(nexus) nexus = 0 elif neg == '-': nexus = 1 else: nexus = 0 dict = {1: 0, 2: 0} for group in c: if group == 1: dict[1] += 1 else: dict[2] += 1 if mistakes == 0: print(0) elif mistakes == len(a) or groups == 0 and dict[1] == 0: print(1) else: print(2) ```
instruction
0
102,204
12
204,408
No
output
1
102,204
12
204,409
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of n integers a_1, a_2, ..., a_n, and a set b of k distinct integers from 1 to n. In one operation, you may choose two integers i and x (1 ≀ i ≀ n, x can be any integer) and assign a_i := x. This operation can be done only if i does not belong to the set b. Calculate the minimum number of operations you should perform so the array a is increasing (that is, a_1 < a_2 < a_3 < ... < a_n), or report that it is impossible. Input The first line contains two integers n and k (1 ≀ n ≀ 5 β‹… 10^5, 0 ≀ k ≀ n) β€” the size of the array a and the set b, respectively. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9). Then, if k β‰  0, the third line follows, containing k integers b_1, b_2, ..., b_k (1 ≀ b_1 < b_2 < ... < b_k ≀ n). If k = 0, this line is skipped. Output If it is impossible to make the array a increasing using the given operations, print -1. Otherwise, print one integer β€” the minimum number of operations you have to perform. Examples Input 7 2 1 2 1 1 3 5 1 3 5 Output 4 Input 3 3 1 3 2 1 2 3 Output -1 Input 5 0 4 3 1 2 3 Output 2 Input 10 3 1 3 5 6 12 9 8 10 13 15 2 4 9 Output 3
instruction
0
102,237
12
204,474
Tags: binary search, constructive algorithms, data structures, dp, implementation Correct Solution: ``` from bisect import bisect_right # codeforces answer INF = 0x3f3f3f3f3f3f3f3f n, k = map(int, input().split()) a = [-INF] + [ ai - i for i, ai in enumerate(map(int, input().split())) ] + [INF] b = [0] + (list(map(int, input().split())) if k else list()) + [n+1] r = 0 for j in range(k+1): l = b[j] m = b[j+1] if a[m] < a[l]: print("-1") exit() li = list() for ai in a[l+1:m]: if a[l] <= ai <= a[m]: pos = bisect_right(li, ai) if pos == len(li): li.append(ai) else: li[pos] = ai r += m - l - 1 - len(li) print(r) ```
output
1
102,237
12
204,475
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of n integers a_1, a_2, ..., a_n, and a set b of k distinct integers from 1 to n. In one operation, you may choose two integers i and x (1 ≀ i ≀ n, x can be any integer) and assign a_i := x. This operation can be done only if i does not belong to the set b. Calculate the minimum number of operations you should perform so the array a is increasing (that is, a_1 < a_2 < a_3 < ... < a_n), or report that it is impossible. Input The first line contains two integers n and k (1 ≀ n ≀ 5 β‹… 10^5, 0 ≀ k ≀ n) β€” the size of the array a and the set b, respectively. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9). Then, if k β‰  0, the third line follows, containing k integers b_1, b_2, ..., b_k (1 ≀ b_1 < b_2 < ... < b_k ≀ n). If k = 0, this line is skipped. Output If it is impossible to make the array a increasing using the given operations, print -1. Otherwise, print one integer β€” the minimum number of operations you have to perform. Examples Input 7 2 1 2 1 1 3 5 1 3 5 Output 4 Input 3 3 1 3 2 1 2 3 Output -1 Input 5 0 4 3 1 2 3 Output 2 Input 10 3 1 3 5 6 12 9 8 10 13 15 2 4 9 Output 3
instruction
0
102,238
12
204,476
Tags: binary search, constructive algorithms, data structures, dp, implementation Correct Solution: ``` import bisect INF = 0x3f3f3f3f3f3f3f3f n, k = map(int, input().split()) a = [-INF] + [ ai - i for i, ai in enumerate(map(int, input().split())) ] + [INF] b = [0] + (list(map(int, input().split())) if k else list()) + [n+1] ans = 0 for j in range(k+1): l = b[j] r = b[j+1] if a[r] < a[l]: print("-1") exit() lis = list() for ai in a[l+1:r]: if a[l] <= ai <= a[r]: pos = bisect.bisect(lis, ai) if pos == len(lis): lis.append(ai) else: lis[pos] = ai ans += r - l - 1 - len(lis) print(ans) ```
output
1
102,238
12
204,477
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array of n integers a_1, a_2, ..., a_n, and a set b of k distinct integers from 1 to n. In one operation, you may choose two integers i and x (1 ≀ i ≀ n, x can be any integer) and assign a_i := x. This operation can be done only if i does not belong to the set b. Calculate the minimum number of operations you should perform so the array a is increasing (that is, a_1 < a_2 < a_3 < ... < a_n), or report that it is impossible. Input The first line contains two integers n and k (1 ≀ n ≀ 5 β‹… 10^5, 0 ≀ k ≀ n) β€” the size of the array a and the set b, respectively. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9). Then, if k β‰  0, the third line follows, containing k integers b_1, b_2, ..., b_k (1 ≀ b_1 < b_2 < ... < b_k ≀ n). If k = 0, this line is skipped. Output If it is impossible to make the array a increasing using the given operations, print -1. Otherwise, print one integer β€” the minimum number of operations you have to perform. Examples Input 7 2 1 2 1 1 3 5 1 3 5 Output 4 Input 3 3 1 3 2 1 2 3 Output -1 Input 5 0 4 3 1 2 3 Output 2 Input 10 3 1 3 5 6 12 9 8 10 13 15 2 4 9 Output 3
instruction
0
102,239
12
204,478
Tags: binary search, constructive algorithms, data structures, dp, implementation Correct Solution: ``` import bisect a = [] b = [] inf = 0x3f3f3f3f def solve(num): n = len(num) temp = [] num = [(num[i] - i) for i in range(n)] for i in range(n): if (num[i] < num[0] or num[i] > num[n - 1]): continue k = bisect.bisect_right(temp, num[i]) if k == len(temp): temp.append(num[i]) else: temp[k] = num[i] ans = n - len(temp) return ans s = input() s = [x for x in s.split()] n = int(s[0]) k = int(s[1]) s1 = input() s1 = [x for x in s1.split()] if (k != 0): s2 = input() s2 = [x for x in s2.split()] else: s2 = [] for i in range(1, n + 1): a.append(int(s1[i - 1])) if (k != 0): for i in range(1, k + 1): b.append(int(s2[i - 1])) a = [-inf] + a + [inf] b = [0] + b + [n + 1] ans = 0 sign = True cnt, num = b[0], a[0] for c in b[1:]: if c - cnt > a[c] - num: sign = False break ans += solve(a[cnt:c + 1]) cnt, num = c, a[c] if sign: print(ans) else: print(-1) ```
output
1
102,239
12
204,479