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. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO".
instruction
0
6,914
12
13,828
Tags: implementation, sortings Correct Solution: ``` from collections import Counter n=int(input()) a=list(map(int,input().split())) b=a.copy() a.sort() for i in range(n): a[i]=abs(a[i]-b[i]) k=Counter(a) if(k[0]>=n-2): print("YES") else: print("NO") ```
output
1
6,914
12
13,829
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO".
instruction
0
6,915
12
13,830
Tags: implementation, sortings Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) b = sorted(a) res = 0 for i in range(n): if a[i] != b[i]: res += 1 print('YES' if res <= 2 else 'NO') ```
output
1
6,915
12
13,831
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO".
instruction
0
6,916
12
13,832
Tags: implementation, sortings Correct Solution: ``` n=int(input()) from itertools import permutations as pem List=list(map(int, input().split())) newlist=sorted(List) count=0 for i in range(n): if newlist[i]!=List[i]: count+=1 if count<=2: print("YES") else: print("NO") ```
output
1
6,916
12
13,833
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO".
instruction
0
6,917
12
13,834
Tags: implementation, sortings Correct Solution: ``` import sys import math import itertools import functools import collections def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def wr(arr): return ' '.join(map(str, arr)) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) n = ii() a = li() b = sorted(a) ans = 0 for i in range(n): if a[i] != b[i]: ans += 1 if ans == 0 or ans == 2: print('YES') else: print('NO') ```
output
1
6,917
12
13,835
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` def issort(l): return all(l[i] <= l[i+1] for i in range(len(l)-1)) sa = int(input()) array = list(map(int, input().split(' '))) array2 = array[:] array2.sort() count = 0 mislist=[] for i in range(sa): if array[i] != array2[i]: count += 1 if count <= 2: mislist.append(i) if count > 2: print("NO") else: if mislist == []: print("YES") elif len(mislist) == 1: if issort(array): print("YES") else: print("NO") else: array[mislist[0]], array[mislist[1]] = array[mislist[1]], array[mislist[0]] if issort(array): print("YES") else: print("NO") ```
instruction
0
6,918
12
13,836
Yes
output
1
6,918
12
13,837
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) b = sorted(a) ans = 0 for i in range(n): if a[i] != b[i]: ans += 1 print('YES' if ans <= 2 else 'NO') ```
instruction
0
6,919
12
13,838
Yes
output
1
6,919
12
13,839
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` def arr_inp(n): if n == 1: return [int(x) for x in stdin.readline().split()] elif n == 2: return [float(x) for x in stdin.readline().split()] else: return [str(x) for x in stdin.readline().split()] from sys import stdin n, a = int(input()), arr_inp(1) a1, c, ix = sorted(a.copy()), 0, 0 for i in range(n): if a[i] != a1[i]: if c == 0: c += 1 ix = i elif c > 2: exit(print('NO')) else: if a1[ix] == a[i]: c += 1 else: exit(print('NO')) print('YES') ```
instruction
0
6,920
12
13,840
Yes
output
1
6,920
12
13,841
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) d=sorted(l) c=0 for i in range(n): if(l[i]!=d[i]): c=c+1 if(c>2): print("NO") else: print("YES") ```
instruction
0
6,921
12
13,842
Yes
output
1
6,921
12
13,843
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) ind1 = a.index(max(a)) ind2 = a.index(min(a)) k = 0 f = False for i in range(n - 1): if a[i] > a[i + 1]: k += 1 if k == 2: f = True break if f or ind1 != 0 and ind2 != n - 1: print('NO') else: print('YES') ```
instruction
0
6,922
12
13,844
No
output
1
6,922
12
13,845
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` #https://codeforces.com/contest/221/problem/C n=int(input()) a=list(map(int,input().split(' '))) b=[] for i in range(n): b.append(a[i]) b.sort() q=0 R=[] bhul=True for i in range(n): if a[i]!=b[i]: if q>=2: print('NO') bhul=False break else: q+=1 R.append(i) if bhul: if q!=2: print('NO') else: print('YES') ```
instruction
0
6,923
12
13,846
No
output
1
6,923
12
13,847
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` import sys input = sys.stdin.readline n = int(input()) a = list(map(int,input().split())) d = {} for i in range(n): if a[i] in d: d[a[i]].add(i) else: d[a[i]] = {i} ans = 0 b = a[:] a.sort() cur = set() for i in range(n): if i not in d[a[i]]: cur.add(b[i]) cur.add(a[i]) print('YES' if len(cur) <= 2 else 'NO') ```
instruction
0
6,924
12
13,848
No
output
1
6,924
12
13,849
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` from collections import Counter n=int(input()) a=list(map(int,input().split())) b=a.copy() a.sort() for i in range(n): a[i]=abs(a[i]-b[i]) k=Counter(a) print(k[0]) if(k[0]>=n-2): print("YES") else: print("NO") ```
instruction
0
6,925
12
13,850
No
output
1
6,925
12
13,851
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,249
12
14,498
Tags: implementation Correct Solution: ``` ''' //Abdurasul #include<bits/stdc++.h> using namespace std; /** ################################## ## ___________________ ## ## | | Abdurasul... | ## ## |▓| _ _ | ## ## |▓| |_||_| | ## ## |▓| |_||_| | ## ## |▓| Microsoft | ## ## |▓|_________________| ## ## |/\ <><><><><><><><> \ ## ## \ \ <><><><><><<<>>> \ ## ## \ \ <<><><><><><><>> \ ## ## \ \__________________\ ## ## \|___________________| ## ################################## **/ void seti(){ int n, m; cin >> n >> m; if(n % 2 == 0 && m % 2 == 0 && n % m == 0) cout << "YES"; else if(n % 2 == 1 && m % 2 == 1 && n % m == 0) cout << "YES"; else if(n % m == 0) cout << "YES"; else cout << "NO"; cout << endl; } int main(){ int n = 1; //freopen("a.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin >> n; for(int i = 0; i < n; i++){ seti(); } return 0; } ''' n = int(input()) a = list(map(int,input().split())) for i in range(n - 1): if max(a[i],a[i + 1]) - min(a[i], a[i + 1]) > 1: print("NO") exit() print("YES") ```
output
1
7,249
12
14,499
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,250
12
14,500
Tags: implementation Correct Solution: ``` _=input() l = list(map(int, input().split())) ans=True for i in range(1, len(l)): if abs(l[i]-l[i-1])>=2: ans=False break if ans: print('YES') else: print('NO') ```
output
1
7,250
12
14,501
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,251
12
14,502
Tags: implementation Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) b=list(sorted(a,reverse=True)) c=[] for i in range(n): for j in range(n-i-1): if abs(a[j]-a[j+1])>=2: print('NO') exit() q=max(a) qi=a.index(q) c.append(q) for j in range(i): if abs(c[j]-c[j+1])>=2: print('NO') exit() del a[qi] print('YES') ```
output
1
7,251
12
14,503
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,252
12
14,504
Tags: implementation Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) s=set() if n==1: print("YES") exit(0) for i in range(1,n): s.add(abs(a[i]-a[i-1])) if max(s) >=2: print("NO") else: print("YES") ```
output
1
7,252
12
14,505
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,253
12
14,506
Tags: implementation Correct Solution: ``` n=int(input()) l=[int(s) for s in input().split()] z=0 f=0 d=0 e=0 while(len(l)>0): a=max(l) if l.index(a)!=0: d=a-l[l.index(a)-1] if l.index(a)!=len(l)-1: e=a-l[l.index(a)+1] if d>=2: z=1 break if e>=2: f=1 break else: l.remove(a) if z==1 or f==1: print("NO") else: print("YES") ```
output
1
7,253
12
14,507
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,254
12
14,508
Tags: implementation Correct Solution: ``` n = int(input()) a = [int(n) for n in input().split()] answer = True if n != 1: for i in range(0, n-1): #print(a[i], a[i+1]) if max(a[i], a[i+1]) - min(a[i+1], a[i]) >= 2: answer = False break if answer: print("YES") else: print("NO") else: print("YES") ```
output
1
7,254
12
14,509
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,255
12
14,510
Tags: implementation Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) ans=True for i in range(1,n): if abs(a[i]-a[i-1])>1: ans=False if ans: print("YES") else: print("NO") ```
output
1
7,255
12
14,511
Provide tags and a correct Python 3 solution for this coding contest problem. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}.
instruction
0
7,256
12
14,512
Tags: implementation Correct Solution: ``` def sign(x): if x > 0: return 1 elif x == 0: return 0 return -1 def bober(a, b): return sign(a - b) * (a - b) n = int(input()) a = list(map(int, input().split())) mx = 0 for i in range(1, n): mx = max(mx, bober(a[i], a[i - 1])) if mx <= 1: print('YES') else: print('NO') ```
output
1
7,256
12
14,513
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) print('NO' if any(abs(a[i]-a[i+1])>1 for i in range(n-1))else 'YES') ```
instruction
0
7,257
12
14,514
Yes
output
1
7,257
12
14,515
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` def check(a): for i in range(len(a) - 1): if a[i] > 0 and abs(a[i] - a[i + 1]) > 1: return False return True n = int(input()) a = list(map(int, input().split())) b = [] for i in range(n): if not check(a): print("NO") break ind = a.index(max(a)) b.append(a.pop(ind)) else: print("YES") ```
instruction
0
7,258
12
14,516
Yes
output
1
7,258
12
14,517
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` n=int(input()) num=input().split() flag=1 for i in range(n-1): if abs((int(num[i]))-(int(num[i+1])))>=2: flag=0 if flag==1: print("YES") else: print("NO") ```
instruction
0
7,259
12
14,518
Yes
output
1
7,259
12
14,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` n = int(input()) stacks = list(map(int,input().strip().split(' '))) while n: for i in range(n-1): if abs(stacks[i] - stacks[i+1]) >= 2: print('NO') exit(0) stacks.remove(max(stacks)) n -= 1 print('YES') ```
instruction
0
7,260
12
14,520
Yes
output
1
7,260
12
14,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` def step(xs): i = xs.pop(xs.index(max(xs))) return xs + [i] def f(xs): while not all(xs[i] <= xs[i+1] for i in range(len(xs)-1)): for x, y in zip(xs, xs[1:]): if abs(y - x) > 1: return False xs = step(xs) return True if __name__ == "__main__": input() xs = list(map(int, input().split())) print("YES" if f(xs) else "NO") ```
instruction
0
7,261
12
14,522
No
output
1
7,261
12
14,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` n=int(input()) flag=0 a=[int(x) for x in input().split()] for i in range(0,len(a)): for j in range(0,len(a)-1): if(a[j]>=a[j+1]+2 or a[j]+2<=a[j+1]): print("NO") flag=1 break c=a.index(max(a)) del a[c] if(flag==0): print("YES") ```
instruction
0
7,262
12
14,524
No
output
1
7,262
12
14,525
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` import sys if __name__ == '__main__': n = int(input()) arr = list(map(int, input().split())) last_max = -1 for i in range(n): max = 0 max_index = 0 for j in range(len(arr)): if arr[j] > max: max = arr[j] max_index = j if max_index < len(arr) - 1 and max_index > 0: if abs(arr[max_index-1] - arr[max_index+1]) > 1: print("NO") sys.exit() if last_max != -1: if abs(max - last_max) > 1: print("NO") sys.exit() last_max = max arr.pop(max_index) print("YES") ```
instruction
0
7,263
12
14,526
No
output
1
7,263
12
14,527
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you... You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai = 4. <image> Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed. At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack. Given an input array, figure out whether the described algorithm will sort it correctly. Input The first line of input contains a single number n (1 ≤ n ≤ 10) — the size of the array. The second line of input contains n space-separated integers ai (1 ≤ ai ≤ 100) — the elements of the array. Output Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. Examples Input 3 1 2 3 Output YES Input 3 3 1 2 Output NO Note In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2, 2, 2}. Submitted Solution: ``` import cmath import sys n = int(input()) a = [int(i) for i in input().split()] ok = True maxi = -1; mini = 1000000; for x in a: maxi = max(maxi, x) mini = max(mini, x) if (maxi - mini < 2): print('YES') sys.exit() for i in range(1, n): if (a[i] < a[i - 1] or abs(a[i] - a[i - 1]) > 1): print('NO') ok = False break if (ok == True): print('YES') ```
instruction
0
7,264
12
14,528
No
output
1
7,264
12
14,529
Provide a correct Python 3 solution for this coding contest problem. Problem Define a function $ f $ that starts with $ 1 $ and takes a sequence of finite lengths as an argument as follows. $ \ displaystyle f (\\ {a_1, a_2, \ ldots, a_n \\}) = \ sum_ {i = 1} ^ n {a_i} ^ i $ Given a sequence of length $ N $, $ X = \\ {x_1, x_2, \ ldots, x_N \\} $, $ f (X) for all subsequences $ X'$ except empty columns. ') Find $ and output the sum of them divided by $ 998244353 $. However, the subsequence subsequences shall be renumbered starting from $ 1 $ while maintaining the relative order in the original sequence. Also, even if two subsequences are the same as columns, if they are taken out at different positions, they shall be counted separately. Constraints The input satisfies the following conditions. * $ 1 \ leq N \ leq 10 ^ 6 $ * $ 1 \ leq x_i \ leq 10 ^ 6 $ * All inputs are integers Input The input is given in the following format. $ N $ $ x_1 $ $ \ ldots $ $ x_N $ The first line is given the length $ N $. In the second row, the elements of the sequence $ X $ are given, separated by blanks. Output Find $ f (X') $ for all subsequences $ X'$ except empty columns, and divide the sum by $ 998244353 $ to output the remainder. Examples Input 3 1 2 3 Output 64 Input 5 100 200 300 400 500 Output 935740429
instruction
0
7,503
12
15,006
"Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 998244353 def solve(): n = I() x = LI() ans = 0 for i in range(n): xi = x[i] ans += pow(2,n-i-1,mod)*xi*pow(xi+1,i,mod) if ans >= mod: ans %= mod print(ans) return if __name__ == "__main__": solve() ```
output
1
7,503
12
15,007
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1
instruction
0
7,505
12
15,010
"Correct Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): n = I() a = IR(n) dp = [float("inf")]*n for i in a: j = bisect.bisect_left(dp,i) dp[j] = i print(n-dp.count(float("inf"))) return #Solve if __name__ == "__main__": solve() ```
output
1
7,505
12
15,011
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1
instruction
0
7,506
12
15,012
"Correct Solution: ``` import sys from bisect import bisect_left def solve(): n = int(input()) A = [int(input()) for i in range(n)] inf = 10**9 + 1 dp = [inf] * n for a in A: j = bisect_left(dp, a) dp[j] = a for i, v in enumerate(dp): if v == inf: print(i) return print(n) def debug(x, table): for name, val in table.items(): if x is val: print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) return None if __name__ == '__main__': solve() ```
output
1
7,506
12
15,013
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1
instruction
0
7,507
12
15,014
"Correct Solution: ``` import sys from bisect import bisect_left read = sys.stdin.read readline = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 8) INF = float('inf') MOD = 10 ** 9 + 7 def main(): N = int(readline()) A = list(int(readline()) for _ in range(N)) LIS = [INF]*(N+1) for a in A: i = bisect_left(LIS,a) LIS[i] = a print(LIS.index(INF)) if __name__ == '__main__': main() ```
output
1
7,507
12
15,015
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1
instruction
0
7,509
12
15,018
"Correct Solution: ``` import bisect n = int(input()) L = [] for i in range(n): L.append(int(input())) dp = [float('inf')]*n for i in range(n): k = bisect.bisect_left(dp,L[i]) dp[k] = L[i] ans = 0 for i in range(n): if dp[i] != float('inf'): ans += 1 else: break print(ans) ```
output
1
7,509
12
15,019
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1
instruction
0
7,510
12
15,020
"Correct Solution: ``` # -*- coding: utf-8 -*- import bisect if __name__ == '__main__': n = int(input()) a = [int(input()) for _ in range(n)] L = [None] * n L[0] = a[0] length = 1 for i in range(1, n): if L[length - 1] < a[i]: L[length] = a[i] length += 1 else: indx = bisect.bisect_left(L[:length], a[i]) L[indx] = a[i] print(length) ```
output
1
7,510
12
15,021
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1
instruction
0
7,511
12
15,022
"Correct Solution: ``` import bisect n = int(input()) A = [int(input()) for j in range(n)] dp = A[:1] for a_i in A[1:]: if dp[-1] < a_i: dp.append(a_i) else: dp[bisect.bisect_left(dp, a_i)] = a_i print(len(dp)) ```
output
1
7,511
12
15,023
Provide a correct Python 3 solution for this coding contest problem. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1
instruction
0
7,512
12
15,024
"Correct Solution: ``` import sys, bisect def solve(): A = list(map(int, sys.stdin.readlines())) n = A[0] A = A[1:] L = A[:1] for a_i in A[1:]: if a_i > L[-1]: L.append(a_i) else: j = bisect.bisect_left(L, a_i) L[j] = a_i print(len(L)) solve() ```
output
1
7,512
12
15,025
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` from bisect import bisect_left N = int(input()) A = [int(input()) for _ in range(N)] LIS = [A[0]] # i番目の成分 = 長さ i+1 の増加部分列の最後の要素の値 for a in A[1:]: if a > LIS[-1]: # 最大の長さの増加部分列の最後の要素より大きい値なら、これを用いて1つ長い増加部分列を作ることができる。 # 更新前の最長増加部分列 = [..., L[-1]]、更新後の最長増加部分列 = [..., L[-1], a] LIS.append(a) else: LIS[bisect_left(LIS, a)] = a # 最長ではない増加部分列の最後の要素を小さい値に更新。これにより、a > LIS[-1] を満たす可能性が増える。 print(len(LIS)) ```
instruction
0
7,513
12
15,026
Yes
output
1
7,513
12
15,027
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` import bisect def LIS(): L.append(a[0]) length[0] = 1 for i in range(n-1): if L[-1]<a[i+1]: L.append(a[i+1]) length[i+1] = length[i]+1 else: L[bisect.bisect_left(L,a[i+1])] = a[i+1] length[i+1] = length[i] return length[-1] n = int(input()) a = [int(input()) for _ in range(n)] L = [] length = [None]*n print(LIS()) ```
instruction
0
7,514
12
15,028
Yes
output
1
7,514
12
15,029
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` #最長増加部分列問題 (LIS) import bisect N = int(input()) seq = [0] * N for i in range(N): seq[i] = int(input()) LIS = [seq[0]] #print(LIS) for i in range(len(seq)): if seq[i] > LIS[-1]: LIS.append(seq[i]) else: LIS[bisect.bisect_left(LIS, seq[i])] = seq[i] #print(LIS) #print(LIS) print(len(LIS)) ```
instruction
0
7,515
12
15,030
Yes
output
1
7,515
12
15,031
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` from bisect import bisect_left n=int(input()) a=[int(input()) for _ in range(n)] dp=[float('inf') for _ in range(n)] l=[] l.append(a[0]) dp[0]=a[0] for i in a[1:]: if l[-1]<i: l.append(i) dp[len(l)-1]=i else: x=bisect_left(l,i) dp[x]=i l[x]=i print(len(l)) ```
instruction
0
7,516
12
15,032
Yes
output
1
7,516
12
15,033
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A. An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik. Constraints * 1 ≤ n ≤ 100000 * 0 ≤ ai ≤ 109 Input n a0 a1 : an-1 In the first line, an integer n is given. In the next n lines, elements of A are given. Output The length of the longest increasing subsequence of A. Examples Input 5 5 1 3 2 4 Output 3 Input 3 1 1 1 Output 1 Submitted Solution: ``` n = int(input()) in_list = [] for i in range(n): in_list.append(list(map(int, input().split()))) matrices = [["0" for _ in range(n)] for _ in range(n)] for i in range(n): inp = in_list[i] u = inp[0] k = inp[1] for j in range(k): m = inp[j+2] matrices[i][m-1] = "1" for i in range(n): print(" ".join(matrices[i])) ```
instruction
0
7,518
12
15,036
No
output
1
7,518
12
15,037
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,614
12
15,228
Tags: data structures, greedy Correct Solution: ``` """ #If FastIO not needed, used this and don't forget to strip #import sys, math #input = sys.stdin.readline """ import os import sys from io import BytesIO, IOBase import heapq as h from bisect import bisect_left, bisect_right from types import GeneratorType BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") from collections import defaultdict as dd, deque as dq import math, string def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) MOD = 10**9+7 """ we can use prefix sums to calculate every possible subarray - there are at most 1275 of these For each possible value we can greedily take the first index to finish? """ def solve(): N = getInt() A = getInts() P, curr = [0], 0 for a in A: curr += a P.append(curr) D = dd(list) for L in range(N): for R in range(L+1,N+1): D[P[R]-P[L]].append((R,L)) best = 0 best_key = -10**9 best_arr = [] for key, arr in D.items(): D[key].sort(reverse=True) tmp = [] while D[key]: R, L = D[key].pop() if not tmp or L >= prev_R: tmp.append((L,R)) prev_R = R if len(tmp) > best: best = len(tmp) best_key = key best_arr = tmp[:] print(best) for L, R in best_arr: print(L+1,R) return #for _ in range(getInt()): solve() ```
output
1
7,614
12
15,229
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,615
12
15,230
Tags: data structures, greedy Correct Solution: ``` from collections import defaultdict def main(): n = int(input()) values = list(map(int, input().split(' '))) # print(values) ans = defaultdict(list) for i in range(n): s = 0 # print("---------- i = {} ----------".format(i)) for j in range(i, -1, -1): s += values[j] # print("i = {}; j = {} s = {}".format(i, j, s)) # print("ans = {}; (i + 1) = {}".format(i + 1, ans[s][-1])) # if (i + 1) not in ans[s][-1]: ans[s].append((j + 1, i + 1)) # print(ans) answer = dict() max = 0 for key in ans: # print("key = {} ; ans[key] = {}".format(key, ans[key], len(ans[key]))) sum_pairs = ans[key] non_overlap_pairs = [sum_pairs[0]] previous_pair_second_value = sum_pairs[0][1] for each_pair in sum_pairs[1:]: # print("each_pair = {}".format(each_pair)) if previous_pair_second_value < each_pair[0]: # print(each_pair) non_overlap_pairs.append(each_pair) previous_pair_second_value = each_pair[1] # else: # print("Found overlapping pair for key = {}".format(each_pair)) # ans[key] = non_overlap_pairs if len(non_overlap_pairs) > max: max = len(non_overlap_pairs) answer = {max: non_overlap_pairs} # print(list(answer.keys())[0]) for key in answer.keys(): print(key) for value in answer[key]: print(str(value[0]) + ' ' + str(value[1])) if __name__=="__main__": main() ```
output
1
7,615
12
15,231
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,616
12
15,232
Tags: data structures, greedy Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) if n == 1: print(1) print('1 1') else: d = [[]]*(n-1) d[0] = a for i in range(1,n-1): d[i] = [d[i-1][j]+a[j+i] for j in range(0,n-i)] d2 = {} for i,d_ in enumerate(d): for j,x in enumerate(d_): if x in d2: d2[x].append([j+1,j+i+1]) else: d2[x]=[[j+1,j+i+1]] list_keys = list(d2.keys()) #res = 0 ma = 0 for key in list_keys: d2[key].sort(key=lambda x:x[1]) after = -1 cnt = 0 tmp = [] for y,z in d2[key]: if y > after: cnt += 1 after = z tmp.append([y,z]) if cnt > ma: ma = cnt res = tmp # for j in range(len(d2[key])): # after = -1 # cnt = 0 # tmp = [] # for y,z in d2[key][j:]: # if y > after: # cnt += 1 # after = z # tmp.append([y,z]) # if cnt > ma: # ma = cnt # res = tmp print(len(res)) for x in res: print(*x) ```
output
1
7,616
12
15,233
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,617
12
15,234
Tags: data structures, greedy Correct Solution: ``` n=int(input()) ans=[] l=[int(i) for i in input().split()] from collections import defaultdict d=defaultdict(list) for i in range(n): sm=0 for j in range(i,n): sm+=l[j] d[sm].append([i,j]) for sm in d: z=d[sm] #print(z) z.sort(key=lambda x:x[1])#activity selection t=[z[0]] #print(t) #print(t[-1]) for i in z: #print(i[0]) if i[0]<=t[-1][1]: continue t.append(i) if len(t)>len(ans): ans=t print(len(ans)) for i in range(len(ans)): print(ans[i][0]+1,ans[i][1]+1) ```
output
1
7,617
12
15,235
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,618
12
15,236
Tags: data structures, greedy Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from collections import defaultdict def main(): n=int(input()) a=list(map(int,input().split())) b,ma,ans=defaultdict(list),0,[] for i in range(n): su=0 for j in range(i,n): su+=a[j] b[su].append((i,j)) for i in b: z,c=b[i],0 for j in range(len(z)-1): if z[j+1][0]<=z[j][1]: if z[j][1]<=z[j+1][1]: z[j+1]=z[j] z[j]=[] else: c+=1 c+=(z[-1]!=[]) if ma<c: ma,ans=c,z print(ma) for i in ans: if i!=[]: print(i[0]+1,i[1]+1) # 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") if __name__ == "__main__": main() ```
output
1
7,618
12
15,237
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,619
12
15,238
Tags: data structures, greedy Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from collections import defaultdict def main(): n=int(input()) a=list(map(int,input().split())) b=defaultdict(list) for i in range(n): su=0 for j in range(i,n): su+=a[j] b[su].append((i,j)) ma,ans=0,set() for i in b: z,c=b[i],set() for j in range(len(z)-1): if z[j+1][0]<=z[j][1]: if z[j][1]<=z[j+1][1]: z[j+1]=z[j] z[j]=[] else: c.add(z[j]) if z[-1]: c.add(z[-1]) if ma<len(c): ma=len(c) ans=c print(ma) for i in ans: print(i[0]+1,i[1]+1) # 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") if __name__ == "__main__": main() ```
output
1
7,619
12
15,239
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,620
12
15,240
Tags: data structures, greedy Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os import sys from io import BytesIO, IOBase from collections import defaultdict def main(): n=int(input()) a=list(map(int,input().split())) b=defaultdict(list) for i in range(n): su=0 for j in range(i,n): su+=a[j] b[su].append([i,j]) ma,ans=0,set() for i in b: z,c=b[i],set() for j in range(len(z)-1): if z[j+1][0]<=z[j][1]: if z[j][1]<=z[j+1][1]: z[j+1]=z[j] z[j]=[] for j in range(len(z)): if z[j]: c.add(tuple(z[j])) if ma<len(c): ma=len(c) ans=c print(ma) for i in ans: print(i[0]+1,i[1]+1) # 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") if __name__ == "__main__": main() ```
output
1
7,620
12
15,241
Provide tags and a correct Python 3 solution for this coding contest problem. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3
instruction
0
7,621
12
15,242
Tags: data structures, greedy Correct Solution: ``` import sys from collections import defaultdict import math n=int(sys.stdin.readline()) arr=list(map(int,sys.stdin.readline().split())) dp=defaultdict(list) for i in range(n): s=0 for j in range(i,n): s+=arr[j] dp[s].append([j,i]) ans=0 #print(dp,'dp') rem=[] for s in dp: dp[s].sort() m=len(dp[s]) lastj=-1 count=0 temp=[] for k in range(m): j,i=dp[s][k] if i>lastj: temp.append([i,j]) count+=1 lastj=j #ans=max(ans,count) if ans<count: ans=count #print(temp,'temp') rem=[a for a in temp] print(ans) for i in range(ans): print(rem[i][0]+1,rem[i][1]+1) ```
output
1
7,621
12
15,243
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem is given in two editions, which differ exclusively in the constraints on the number n. You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r). Find a set of blocks (l_1, r_1), (l_2, r_2), ..., (l_k, r_k) such that: * They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (l_i, r_i) and (l_j, r_j) where i ≠ j either r_i < l_j or r_j < l_i. * For each block the sum of its elements is the same. Formally, $$$a[l_1]+a[l_1+1]+...+a[r_1]=a[l_2]+a[l_2+1]+...+a[r_2]= ... = a[l_k]+a[l_k+1]+...+a[r_k].$$$ * The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l_1', r_1'), (l_2', r_2'), ..., (l_{k'}', r_{k'}') satisfying the above two requirements with k' > k. <image> The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer n (1 ≤ n ≤ 1500) — the length of the given array. The second line contains the sequence of elements a[1], a[2], ..., a[n] (-10^5 ≤ a_i ≤ 10^5). Output In the first line print the integer k (1 ≤ k ≤ n). The following k lines should contain blocks, one per line. In each line print a pair of indices l_i, r_i (1 ≤ l_i ≤ r_i ≤ n) — the bounds of the i-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 7 4 1 2 2 1 5 3 Output 3 7 7 2 3 4 5 Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 Output 2 3 4 1 1 Input 4 1 1 1 1 Output 4 4 4 1 1 2 2 3 3 Submitted Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- # @oj: codeforces # @id: hitwanyang # @email: 296866643@qq.com # @date: 2020/12/17 17:03 # @url: https://codeforc.es/contest/1141/problem/F2 import sys, os from io import BytesIO, IOBase import collections, itertools, bisect, heapq, math, string from decimal import * # region fastio BUFSIZE = 8192 BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ ## 注意嵌套括号!!!!!! ## 先有思路,再写代码,别着急!!! ## 先有朴素解法,不要有思维定式,试着换思路解决 ## 精度 print("%.10f" % ans) ## sqrt:int(math.sqrt(n))+1 ## 字符串拼接不要用+操作,会超时 ## 二进制转换:bin(1)[2:].rjust(32,'0') ## array copy:cur=array[::] ## oeis:example 1, 3, _, 1260, _, _, _, _, _, 12164510040883200 ## sqrt:Decimal(x).sqrt()避免精度误差 ## 无穷大表示:float('inf') def main(): n = int(input()) a = list(map(int, input().split())) res = [] prefix = list(itertools.accumulate(a)) d = collections.defaultdict(list) for i in range(n): for j in range(i, n): if i == 0: d[prefix[j]].append((i, j)) # res.append((i, j, prefix[j])) else: v = prefix[j] - prefix[i - 1] d[v].append((i, j)) # res.append((i, j, prefix[j] - prefix[i - 1])) ans = [] for k in d.keys(): v = d[k] cnt = [] tmp = sorted(v,key=lambda x:(x[1],x[0])) if len(tmp) == 1: cnt.append((tmp[0][0] + 1, tmp[0][1] + 1)) else: pre = tmp[0] # 贪心求不相交区间的最大个数 cnt.append((pre[0] + 1, pre[1] + 1)) for i in range(1, len(tmp)): cur = tmp[i] if cur[0] > pre[1]: cnt.append((cur[0] + 1, cur[1] + 1)) pre = cur if len(cnt) > len(ans): ans = cnt ############## TLE code ############## ## 按区间右端点排序 # sr = sorted(res, key=lambda x: (x[2], x[1], x[0])) # print(time.time() - start) # l, r = 0, 0 # while r < len(sr): # pre = sr[l] # cnt = [(pre[0] + 1, pre[1] + 1)] # while r < len(sr) and sr[l][2] == sr[r][2]: # cur=sr[r] # if cur[0] > pre[1]: # cnt.append((cur[0] + 1, cur[1] + 1)) # pre = cur # r += 1 # l = r # if len(cnt) > len(ans): # ans = cnt # print (time.time()-start) print(len(ans)) for a in ans: print(a[0], a[1]) if __name__ == "__main__": main() ```
instruction
0
7,622
12
15,244
Yes
output
1
7,622
12
15,245