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 a correct Python 3 solution for this coding contest problem.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element. | instruction | 0 | 24,785 | 12 | 49,570 |
"Correct Solution:
```
input() # array len
array = list(map(int, input().split(' ')))
sum_of_array = sum(array)
second_max, first_max = sorted(array)[-2:]
answer = []
for index, each in enumerate(array, 1):
existing_sum = sum_of_array - each
existing_max = second_max if each == first_max else first_max
if existing_sum == existing_max * 2:
answer.append(index)
print(len(answer))
print(*answer)
``` | output | 1 | 24,785 | 12 | 49,571 |
Provide a correct Python 3 solution for this coding contest problem.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element. | instruction | 0 | 24,786 | 12 | 49,572 |
"Correct Solution:
```
from sys import stdin,stdout
def main():
n=int(stdin.readline())
a=[(i,u) for i,u in enumerate(list(map(int,stdin.readline().split( ))))]
a.sort(key=lambda x:x[1])
ans=[]
p=[a[0][1]]*n
for i in range(1,n):
p[i]=p[i-1]+a[i][1]
for i in range(n):
if i==n-1:
try:
x=p[n-3]
if x==a[n-2][1]:
ans.append(a[n-1][0])
except:
pass
else:
try:
x=p[n-2]-a[i][1]
if x==a[n-1][1]:
ans.append(a[i][0])
except:
pass
if not ans:
stdout.write("%d\n"%(0))
else:
stdout.write("%d\n"%(len(ans)))
for u in ans:
stdout.write("%d "%(u+1))
stdout.write("\n")
main()
``` | output | 1 | 24,786 | 12 | 49,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
n = int(input())
a = [int(e) for e in input().split()]
ans = []
sm = 0
st = dict()
for i in a:
st[i * 2] = st.get(i * 2, 0) + 1
sm += i
for i, j in enumerate(a):
t = sm - j
if t == 2 * j and st.get(t, 0) <= 1:
continue
if st.get(t, 0) > 0:
ans.append(i + 1)
print(len(ans))
print(' '.join(str(e) for e in ans))
``` | instruction | 0 | 24,787 | 12 | 49,574 |
Yes | output | 1 | 24,787 | 12 | 49,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
'''
___ ___ ___ ___ ___ ___
/\__\ /\ \ _____ /\ \ /\ \ /\ \ /\__\
/:/ _/_ \:\ \ /::\ \ \:\ \ ___ /::\ \ |::\ \ ___ /:/ _/_
/:/ /\ \ \:\ \ /:/\:\ \ \:\ \ /\__\ /:/\:\__\ |:|:\ \ /\__\ /:/ /\ \
/:/ /::\ \ ___ \:\ \ /:/ \:\__\ ___ /::\ \ /:/__/ /:/ /:/ / __|:|\:\ \ /:/ / /:/ /::\ \
/:/_/:/\:\__\ /\ \ \:\__\ /:/__/ \:|__| /\ /:/\:\__\ /::\ \ /:/_/:/__/___ /::::|_\:\__\ /:/__/ /:/_/:/\:\__\
\:\/:/ /:/ / \:\ \ /:/ / \:\ \ /:/ / \:\/:/ \/__/ \/\:\ \__ \:\/:::::/ / \:\~~\ \/__/ /::\ \ \:\/:/ /:/ /
\::/ /:/ / \:\ /:/ / \:\ /:/ / \::/__/ ~~\:\/\__\ \::/~~/~~~~ \:\ \ /:/\:\ \ \::/ /:/ /
\/_/:/ / \:\/:/ / \:\/:/ / \:\ \ \::/ / \:\~~\ \:\ \ \/__\:\ \ \/_/:/ /
/:/ / \::/ / \::/ / \:\__\ /:/ / \:\__\ \:\__\ \:\__\ /:/ /
\/__/ \/__/ \/__/ \/__/ \/__/ \/__/ \/__/ \/__/ \/__/
'''
"""
βββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
ββββββββββββββββββββ
"""
import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2, floor, log
from bisect import bisect_right, bisect_left, bisect
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2**20)
def ncr(n, r):
r = min(r, n - r)
numer = reduce(op.mul, range(n, n - r, -1), 1)
denom = reduce(op.mul, range(1, r + 1), 1)
return numer // denom # or / in Python 2
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return (list(factors))
def isPowerOfTwo(x):
return (x and (not(x & (x - 1))))
MOD = 1000000007
PMOD = 998244353
N = 10**6
LOGN = 30
alp = 'abcdefghijklmnopqrstuvwxyz'
T = 1
# T = int(stdin.readline())
for _ in range(T):
# n, m = list(map(int, stdin.readline().rstrip().split()))
n = int(stdin.readline())
a = list(map(int, stdin.readline().rstrip().split()))
# b = list(map(int, stdin.readline().rstrip().split()))
# s = list(stdin.readline().strip('\n'))
# b = list(stdin.readline().strip('\n'))
# m = int(stdin.readline())
# c = list(map(int, stdin.readline().rstrip().split()))
# print(a)
A = a.copy()
A.sort()
b = []
s = sum(a)
for i in range(n):
b.append([a[i], i + 1])
b.sort()
ans = []
for i in range(n):
if (s - a[i]) % 2 == 0:
val = (s - a[i]) // 2
x = bisect_left(A, val)
# if x < n:
# print(A, val, s, i, x)
if x == n:
continue
if(A[x] == val and i + 1 != b[x][1]):
ans.append(i + 1)
elif x + 1 < n:
if A[x + 1] == val:
ans.append(i + 1)
print(len(ans))
print(*ans)
``` | instruction | 0 | 24,788 | 12 | 49,576 |
Yes | output | 1 | 24,788 | 12 | 49,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
n = int(input())
res = []
a = input().split()
for i in range(n):
a[i] = int(a[i])
asor = sorted(a)
run = True
su = sum(a)
m=max(a)
if run:
for i in range(n):
if a[i]==m:
if su-a[i]-asor[-2]==asor[-2]:
res.append(i+1)
else:
if su-a[i]-asor[-1]==asor[-1]:
res.append(i+1)
if len(res)>0:
print(len(res))
for r in res:
print(r,end=' ')
else:
print(0)
``` | instruction | 0 | 24,789 | 12 | 49,578 |
Yes | output | 1 | 24,789 | 12 | 49,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
n = input()
a=list(map(int, input().split()))
s=sum(a)
b=sorted(a)[-2:]
k= []
c=0
for d in a:
c=c+1
if (s-d==2*b[1] and (b[1]!=d or b[0]==d)) or (s-d==2*b[0] and b[0]!=d):
k.append(c)
print(len(k))
print(*k)
``` | instruction | 0 | 24,790 | 12 | 49,580 |
Yes | output | 1 | 24,790 | 12 | 49,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split(' ')))
rev_map = {}
for i, ai in enumerate(a):
if ai in rev_map:
rev_map[ai].append(i)
else:
rev_map[ai] = [i]
indices = set()
total = sum(a)
for ai in rev_map:
subtotal = total - ai
if subtotal % 2 == 0 and subtotal // 2 in rev_map and (not ai == subtotal // 2 or len(rev_map[subtotal // 2]) > 1):
print(ai, subtotal)
for index in rev_map[ai]:
indices.add(index + 1)
print(len(indices))
print(*list(indices))
``` | instruction | 0 | 24,791 | 12 | 49,582 |
No | output | 1 | 24,791 | 12 | 49,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
n = int(input())
a = [ int(s) for s in input().split()]
b = []
b = list(a)
b.sort()
print(b)
ids = []
r = sum(b) - 2*b[-1]
i = 0
if r in b:
if r == b[-1] and b[-2] != b[-1]:
print(0)
else:
while i < a.count(r):
i+=1
if a[i] == r:
ids.append(i)
if (sum(a) - b[-1])//2 == b[-2] and (sum(a) - b[-1])%2 == 0:
ids.append(a.index(b[-2]))
i+=1
print(i)
print(" ".join(map(str, ids)))
else:
print(0)
``` | instruction | 0 | 24,792 | 12 | 49,584 |
No | output | 1 | 24,792 | 12 | 49,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
from os import path
import sys,time
# mod = int(1e9 + 7)
# import re
from math import ceil, floor,gcd,log,log2 ,factorial
from collections import defaultdict , Counter,deque
from itertools import permutations
# from bisect import bisect_left, bisect_right
maxx = float('inf')
#----------------------------INPUT FUNCTIONS------------------------------------------#
I = lambda :int(sys.stdin.buffer.readline())
tup= lambda : map(int , sys.stdin.buffer.readline().split())
lint = lambda :[int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda: sys.stdin.readline().replace('\n', '').strip()
def grid(r, c): return [lint() for i in range(r)]
stpr = lambda x : sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
# input = sys.stdin.readline
localsys = 0
start_time = time.time()
if (path.exists('input.txt')):
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
#left shift --- num*(2**k) --(k - shift)
n = I()
ls = lint()
print(n-1)
print(*range(1, n))
if localsys:
print("\n\nTime Elased :",time.time() - start_time,"seconds")
``` | instruction | 0 | 24,793 | 12 | 49,586 |
No | output | 1 | 24,793 | 12 | 49,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1, 3, 3, 7] is good because there is the element a_4=7 which equals to the sum 1 + 3 + 3.
You are given an array a consisting of n integers. Your task is to print all indices j of this array such that after removing the j-th element from the array it will be good (let's call such indices nice).
For example, if a=[8, 3, 5, 2], the nice indices are 1 and 4:
* if you remove a_1, the array will look like [3, 5, 2] and it is good;
* if you remove a_4, the array will look like [8, 3, 5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β elements of the array a.
Output
In the first line print one integer k β the number of indices j of the array a such that after removing the j-th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print k distinct integers j_1, j_2, ..., j_k in any order β nice indices of the array a.
If there are no such indices in the array a, just print 0 in the first line and leave the second line empty or do not print it at all.
Examples
Input
5
2 5 1 2 2
Output
3
4 1 5
Input
4
8 3 5 2
Output
2
1 4
Input
5
2 1 2 4 3
Output
0
Note
In the first example you can remove any element with the value 2 so the array will look like [5, 1, 2, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 1 + 2 + 2).
In the second example you can remove 8 so the array will look like [3, 5, 2]. The sum of this array is 10 and there is an element equals to the sum of remaining elements (5 = 3 + 2). You can also remove 2 so the array will look like [8, 3, 5]. The sum of this array is 16 and there is an element equals to the sum of remaining elements (8 = 3 + 5).
In the third example you cannot make the given array good by removing exactly one element.
Submitted Solution:
```
cnt = lambda s, x: s.count(x)
ii = lambda: int(input())
si = lambda: input()
f = lambda: map(int, input().split())
dgl = lambda: list(map(int, input()))
il = lambda: list(map(int, input().split()))
n=ii()
l=il()
lenm={}
l1=[]
x=0
for i in range(1,n+1):
lenm[i]=l[i-1]
lenm=sorted(lenm.items(),key= lambda ky:(ky[1],ky[0]))
if n>2:
for i in range(n-2):
x+=lenm[i][1]
if x==lenm[n-2][1]:
l1.append(lenm[n-1][0])
x+=lenm[n-2][1]
if x==lenm[n-1][1]:
l1.append(lenm[n-2][0])
for i in range(n-1):
y=x-lenm[i][1]
if y==lenm[n-1][1]:
l1.append(lenm[i][0])
print(len(l1))
print(*l1)
else:
print(0)
``` | instruction | 0 | 24,794 | 12 | 49,588 |
No | output | 1 | 24,794 | 12 | 49,589 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,902 | 12 | 49,804 |
Tags: implementation, math
Correct Solution:
```
input=__import__('sys').stdin.readline
for _ in range(int(input())):
n = int(input())
lis = list(map(int,input().split()))
pre=[0]*(n+1)
has=[0]*(n+2)
j=0;k=0
for i in range(1,n+1):
if has[lis[i-1]]==1:
break
has[lis[i-1]]=1
j+=1
k=max(k,lis[i-1])
if k==j:
pre[i]=1
j=0;k=0
has=[0]*(n+2)
ans=[]
for i in range(n-1,0,-1):
if has[lis[i]]==1:
break
has[lis[i]]=1
k=max(k,lis[i])
j+=1
if k==j and pre[i]==1:
ans.append([i,n-i])
print(len(ans))
for i in ans:
print(*i)
``` | output | 1 | 24,902 | 12 | 49,805 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,903 | 12 | 49,806 |
Tags: implementation, math
Correct Solution:
```
import collections, math
local = False
if local:
file = open("inputt.txt", "r")
def inp():
if local:
return file.readline().rstrip()
else:
return input().rstrip()
def ints():
return [int(_) for _ in inp().split()]
t = int(inp())
for _ in range(t):
n = int(inp())
arr = ints()
ans = []
right = 0
visited = set()
validPermu = [False]*len(arr)
for i in range(len(arr)):
right = max(right, arr[-i-1])
if arr[-i-1] not in visited:
visited.add(arr[-i-1])
else:
break
if i+1==right:
validPermu[-i-1] = True
right = 0
visited.clear()
for i in range(len(arr)):
right = max(right, arr[i])
if arr[i] not in visited:
visited.add(arr[i])
else:
break
if i+1==right and validPermu[i+1]:
first, sec = i+1, len(arr)-i-1
ans.append([first,sec])
if len(ans)==0:
print(0)
else:
print(len(ans))
for first,sec in ans:
print("{0} {1}".format(first, sec))
``` | output | 1 | 24,903 | 12 | 49,807 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,904 | 12 | 49,808 |
Tags: implementation, math
Correct Solution:
```
def find(a):
n=len(a)
minn1,minn2,maxx1,maxx2=[10**9],[10**9],[0],[0]
k=[]
f1,f2=[],[]
k={}
for i in range(n):
if a[i] not in k:
k[a[i]]="A"
#k.append(a[i])
if len(k)==i+1:
f1.append(True)
else:
f1.append(False)
minn1.append(min(minn1[-1],a[i]))
maxx1.append(max(maxx1[-1],a[i]))
a=a[::-1]
kk={}
for i in range(n):
if a[i] not in kk:
kk[a[i]]="A"
if len(kk)==i+1:
f2.append(True)
else:
f2.append(False)
minn2.append(min(minn2[-1],a[i]))
maxx2.append(max(maxx2[-1],a[i]))
#print(k,kk)
return minn1[1:],minn2[::-1][1:-1],maxx1[1:],maxx2[::-1][1:-1],f1,f2[::-1][1:]
for _ in range(int(input())):
n=int(input())
a=[int(x) for x in input().split()]
minf,minb,maxf,maxb,fir,sec=find(a)
f=[]
for i in range(n-1):
if minf[i] == 1 and maxf[i] == i+1 and fir[i] and minb[i]==1 and maxb[i] == n-i-1 and sec[i]:
f.append([i+1,n-i-1])
if len(f)==0:
print(0)
else:
print(len(f))
for x in f:
print(x[0],x[1])
``` | output | 1 | 24,904 | 12 | 49,809 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,905 | 12 | 49,810 |
Tags: implementation, math
Correct Solution:
```
def ipr(s):
for n in range(len(s)):
if s[n]!=n+1:return 0
return 1
def chek(s):
mp1={}
mp2={}
pos=0
a=[]
b=[]
for n in range(len(s)):
if s[n] not in mp1 and pos==0:
mp1[s[n]]=1
a.append(s[n])
elif s[n] not in mp2:
pos=1
b.append(s[n])
mp2[s[n]]=1
else:
return [0,(0,0)]
if ipr(sorted(a)) and ipr(sorted(b)):
return [1,(len(a),len(b))]
return [0,(0,0)]
def solv():
x=int(input())
s=list(map(int,input().split()))
res=set()
v,b=chek(s)
if v:res.add(b)
s.reverse()
v,b=chek(s)
j=(b[1],b[0])
if v:res.add(j)
res=list(res)
print(len(res))
for n in res:
print(*n)
for _ in range(int(input())):solv()
``` | output | 1 | 24,905 | 12 | 49,811 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,906 | 12 | 49,812 |
Tags: implementation, math
Correct Solution:
```
import heapq
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
q=[]
s=set()
memo=[0]*n
ret=[]
for i in range(n):
heapq.heappush(q,-a[i])
cmax=heapq.heappop(q)
heapq.heappush(q,cmax)
s.add(a[i])
if len(s)==i+1 and -cmax==i+1:
memo[i]=1
q=[]
s=set()
for i in range(n-1,0,-1):
heapq.heappush(q,-a[i])
cmax=heapq.heappop(q)
heapq.heappush(q,cmax)
s.add(a[i])
if len(s)==n-i and -cmax==n-i and memo[i-1]==1:
ret.append(i)
print(len(ret))
for r in ret:
print("{} {}".format(r,n-r))
``` | output | 1 | 24,906 | 12 | 49,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,907 | 12 | 49,814 |
Tags: implementation, math
Correct Solution:
```
def fun1(l):
# print(l)
if len(l)==0:
return 0
flag1 = False
mx = max(l)
n = len(l)
# ind = l.index(mx)
s = [0 for i in range(mx)]
for i in range(n):
if s[l[i]-1]==0:
s[l[i]-1]=1
else:
flag1 = False
break
else:
if min(s)!=0:
flag1 = True
if flag1:
return mx
return 0
import sys
from collections import Counter
from copy import deepcopy
for t in range(int(sys.stdin.readline().strip())):
n = int(sys.stdin.readline().strip())
l = [int(j) for j in sys.stdin.readline().split()]
sol = []
d = dict()
flag = True
for i in range(n):
if l[i] not in d:
d[l[i]]=i
else:
flag = False
break
else:
if len(l)==1 and l[0]==0:
print(1)
else:
print(0)
if not flag:
# print(d)
brk = i-1
x = fun1(l[:brk+1])
y = fun1(l[brk+1:])
# print(brk, x, y)
if x!=0 and y!=0:
sol.append((x,y))
l = l[::-1]
d = dict()
for i in range(n):
if l[i] not in d:
d[l[i]]=i
else:
break
else:
# print(0)
pass
if l[i] in d:
brk = i-1
x = fun1(l[:brk+1])
y = fun1(l[brk+1:])
if x!=0 and y!=0:
sol.append((y,x))
if len(sol)==0:
print(0)
else:
if len(sol)==2:
if sol[0][0]==sol[1][0] and sol[0][1]==sol[1][1]:
print(1)
print(sol[0][0], sol[0][1])
else:
print(len(sol))
for i in range(len(sol)):
print(sol[i][0], sol[i][1])
else:
print(len(sol))
for i in range(len(sol)):
print(sol[i][0], sol[i][1])
# mx = max(l)
# if l.count(mx)>1:
# print(1)
# print(mx, mx)
# else:
# flag1 = False
# sol = []
# flag2 = False
# ind = l.index(mx)
# if ind == n-1:
# flag1 = fun1(l[::-1])
# flag2 = fun1(l[::-1][mx+ind-1:])
# nind
# if ind == 0:
# for i in range(0, ind-1):
# flag1 = True
# sol.append(mx)
``` | output | 1 | 24,907 | 12 | 49,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,908 | 12 | 49,816 |
Tags: implementation, math
Correct Solution:
```
import sys
from math import sqrt, floor
# from bisect import bisect
from collections import deque, Counter
inp = sys.stdin.readline
read = lambda: list(map(int, inp().split()))
def a():
ans = ""
for _ in range(int(inp())):
n, x = read()
arr = sorted(list(set(read())))
n = len(arr)
i = 0
while (i < n and arr[i] <= x+1):
x += 1
i += 1
ans += str(x)+"\n"
print(ans)
def b():
ans = ""
for _ in range(int(inp())):
n = int(inp())
arr = read(); total = sum(arr); s1 = 0
ans_lis = []
for i in range(n):
s1 += arr[i]
total -= arr[i]
p1, p2 = i+1, n-i-1
if (s1 == (p1)*(p1+1)//2) and (total == p2*(p2+1)//2) and len(set(arr[:p1]))==p1 and len(set(arr[p1:]))==p2:
ans_lis.append(str(p1)+" "+str(p2))
ans += str(len(ans_lis))+"\n"
for i in ans_lis:ans += str(i)+"\n"
print(ans)
if __name__ == "__main__":
# a()
b()
# c()
# d()
``` | output | 1 | 24,908 | 12 | 49,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,909 | 12 | 49,818 |
Tags: implementation, math
Correct Solution:
```
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
arr=list(map(int,input().split()))
d=dict()
demand=1
pre=[0]*n
post=[0]*n
for i in range(n):
d[arr[i]]=1
if(demand in d):
while(demand in d):
demand+=1
pre[i]=demand-1
d2=dict()
#print(pre)
demand=1
for i in range(n-1,-1,-1):
d2[arr[i]]=1
if(demand in d2):
while(demand in d2):
demand+=1
post[i]=demand-1
#print(post)
l=[]
for i in range(1,n):
if(post[i]+pre[i-1]==n):
l+=[[pre[i-1],post[i]]]
print(len(l))
for i in l:
print(*i)
``` | output | 1 | 24,909 | 12 | 49,819 |
Provide tags and a correct Python 2 solution for this coding contest problem.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways. | instruction | 0 | 24,910 | 12 | 49,820 |
Tags: implementation, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
for i in arr:
stdout.write(str(i)+' ')
stdout.write('\n')
range = xrange # not for python 3.0+
# main code
for t in range(input()):
ans=[]
n=int(raw_input())
l=in_arr()
dp1=[0]*n
dp2=[0]*n
d=Counter()
f=0
mx=0
for i in range(n):
d[l[i]]+=1
if d[l[i]]==2:
f=1
break
mx=max(mx,l[i])
dp1[i]=mx
d=Counter()
f=0
mx=0
for i in range(n-1,-1,-1):
d[l[i]]+=1
if d[l[i]]==2:
f=1
break
mx=max(mx,l[i])
dp2[i]=mx
for i in range(n-1):
if dp1[i]==i+1 and dp2[i+1]==n-i-1:
ans.append((i+1,n-i-1))
pr_num(len(ans))
for i in ans:
pr_arr(i)
``` | output | 1 | 24,910 | 12 | 49,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
from collections import Counter
for _ in range(int(input())):
n = int(input())
array = list(map(int, input().split()))
ma = max(array)
x = 0
res = []
if set(array[:ma]) == set(range(1,ma+1)) and set(array[ma:]) == set(range(1, n-ma+1)):
x += 1
res.append((ma, n-ma))
if ma * 2 != n:
if set(array[:n-ma]) == set(range(1,n-ma+1)) and set(array[-ma:]) == set(range(1, ma+1)):
x += 1
res.append((n-ma, ma))
if x == 0: print(0)
else:
print(x)
for r in res:
print(*r)
``` | instruction | 0 | 24,911 | 12 | 49,822 |
Yes | output | 1 | 24,911 | 12 | 49,823 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
import sys
import math
input=sys.stdin.readline
t=int(input())
#t=1
for _ in range(t):
#n,x=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
#print(l)
#arr=[[ 0 for i in range(col)] for i in range(rows)]
#arr=[0]*202
d=[0]*(n)
for i in range(n):
d[a[i]]+=1
l1,l2=0,0
for i in range(1,n):
if d[i]==2:
l1+=1
l2+=1
elif d[i]==1:
l1+=1
else:
break
if l1+l2==n:
cnt=0
left={}
right={}
fa=0
fa1=0
for i in range(l1):
left[a[i]]=1
j=n-1
for i in range(l2):
right[a[j]]=1
j-=1
for i in range(1,l1+1):
if left.get(i,-1)==-1:
fa=1
break
for i in range(1,l2+1):
if right.get(i,-1)==-1:
fa=1
break
left={}
right={}
for i in range(l2):
left[a[i]]=1
j=n-1
for i in range(l1):
right[a[j]]=1
j-=1
for i in range(1,l2+1):
if left.get(i,-1)==-1:
fa1=1
break
for i in range(1,l1+1):
if right.get(i,-1)==-1:
fa1=1
break
if fa*fa1==1:
print(0)
else:
if l1==l2:
print(1)
print(l1,l2)
elif fa==0:
if fa1==0:
print(2)
print(l1,l2)
print(l2,l1)
else:
print(1)
print(l1,l2)
else:
if fa==0:
print(2)
print(l1,l2)
print(l2,l1)
else:
print(1)
print(l2,l1)
else:
print(0)
``` | instruction | 0 | 24,912 | 12 | 49,824 |
Yes | output | 1 | 24,912 | 12 | 49,825 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
t = int(input())
for i in range(t):
n = int(input())
mass = [int(x) for x in input().split()]
answer = 0
q = []
Max = 0
ind = 0
for j in range(n):
if Max < mass[j]:
Max = mass[j]
ind = Max - 1
a1 = mass[:n - ind - 1]
a2 = mass[n - ind - 1:]
a3 = mass[:ind + 1]
a4 = mass[ind + 1:]
a1.sort()
a2.sort()
a3.sort()
a4.sort()
f1 = True
f2 = True
for i in range(n - ind - 1):
if a1[i] != i + 1:
f1 = False
if f1:
for i in range(ind + 1):
if a2[i] != i + 1:
f1 = False
if f1:
answer += 1
q.append((n - ind - 1, ind + 1))
for i in range(ind + 1):
if a3[i] != i + 1:
f2 = False
if f2:
for i in range(n - ind - 1):
if a4[i] != i + 1:
f2 = False
if f2 and n - ind - 1 != ind + 1:
answer += 1
q.append((ind + 1, n - ind - 1))
print(answer)
for l in q:
print(*l)
``` | instruction | 0 | 24,913 | 12 | 49,826 |
Yes | output | 1 | 24,913 | 12 | 49,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n=int(input())
*a,=map(int,input().split())
ok=[0]*n
okrev=[0]*n
ma=le=0
se=set()
for i,x in enumerate(a):
if x not in se:
se.add(x)
le+=1
if x>ma:
ma=x
if ma==le==i+1:
ok[i]=1
ma=le=0
se=set()
for i,x in enumerate(a[::-1]):
if x not in se:
se.add(x)
le+=1
if x>ma:
ma=x
if ma==le==i+1:
okrev[i]=1
c=0
ans=[]
for i in range(n-1):
if ok[i] and okrev[n-i-2]:
c+=1
ans.append("{} {}".format(i+1,n-i-1))
print(c)
if c:
print("\n".join(ans))
``` | instruction | 0 | 24,914 | 12 | 49,828 |
Yes | output | 1 | 24,914 | 12 | 49,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
import threading
from collections import Counter
mod=998244353
def main():
for _ in range(int(input())):
n=int(input())
s=set()
arr=list(map(int,input().split() ))
ma=max(arr)
ans=[]
a=[i for i in range(1,n+10)]
s1=sorted(arr[:ma])
s2=sorted(arr[ma:])
if s1==a[:ma] and s2==a[:n-ma]:
ans.append((len(s1),len(s2)))
s1=sorted(arr[:n-ma])
s2=sorted(arr[n-ma:])
if s1==a[:n-ma] and s2==a[:ma]:
ans.append( (len(s1),len(s2)) )
print(len(ans))
for i,j in ans:
print(i,j)
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")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 24,915 | 12 | 49,830 |
No | output | 1 | 24,915 | 12 | 49,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
#!/usr/bin/python -i
import fileinput
from collections import defaultdict
reader = fileinput.input()
cases = int(next(reader))
def counter(a):
max_element = -1
elements = set()
for e in a:
if e in elements:
break
else:
elements.add(e)
max_element = max(e, max_element)
if max_element == len(elements):
yield max_element
for case in range(cases):
n = int(next(reader))
a = [int(ai) for ai in next(reader).split(" ")]
L1 = set(counter(a))
L2 = counter(reversed(a))
print(L1)
solutions = [(l, n-l) for l in L2 if n-l in L1]
print(len(solutions))
for l1, l2 in solutions:
print(l1, l2)
``` | instruction | 0 | 24,916 | 12 | 49,832 |
No | output | 1 | 24,916 | 12 | 49,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
n = int(inp())
a = [int(x) for x in inp().split()]
maxi = max(a)
answers = 0
ans = []
p1 = n - maxi
p2 = maxi
needed1 = list(range(1, p1+1))
needed2 = list(range(1, p2+1))
found1 = []
found2 = []
flag = True
for i in range(n):
if i <= p1-1:
found1.append(a[i])
else:
found2.append(a[i])
if i == p1-1:
if list(sorted(found1)) == list(sorted(needed1)):
continue
else:
flag = False
break
elif i == n-1:
if list(sorted(found2)) == list(sorted(needed2)):
continue
else:
flag = False
break
if flag:
answers += 1
ans.append([p1, p2])
p1 = maxi
p2 = n- maxi
needed1 = list(range(1, p1+1))
needed2 = list(range(1, p2+1))
found1 = []
found2 = []
flag = True
for i in range(n):
if i <= p1-1:
found1.append(a[i])
else:
found2.append(a[i])
if i == p1-1:
if list(sorted(found1)) == list(sorted(needed1)):
continue
else:
flag = False
break
elif i == n-1:
if list(sorted(found2)) == list(sorted(needed2)):
continue
else:
flag = False
break
if flag:
answers += 1
ans.append([p1, p2])
print(answers)
for i in ans:
print(*i)
``` | instruction | 0 | 24,917 | 12 | 49,834 |
No | output | 1 | 24,917 | 12 | 49,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
Dreamoon has two permutations p_1 and p_2 of non-zero lengths l_1 and l_2.
Now Dreamoon concatenates these two permutations into another sequence a of length l_1 + l_2. First l_1 elements of a is the permutation p_1 and next l_2 elements of a is the permutation p_2.
You are given the sequence a, and you need to find two permutations p_1 and p_2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
Input
The first line contains an integer t (1 β€ t β€ 10 000) denoting the number of test cases in the input.
Each test case contains two lines. The first line contains one integer n (2 β€ n β€ 200 000): the length of a. The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n-1).
The total sum of n is less than 200 000.
Output
For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p_1 and p_2.
Each of the next k lines should contain two integers l_1 and l_2 (1 β€ l_1, l_2 β€ n, l_1 + l_2 = n), denoting, that it is possible to divide a into two permutations of length l_1 and l_2 (p_1 is the first l_1 elements of a, and p_2 is the last l_2 elements of a). You can print solutions in any order.
Example
Input
6
5
1 4 3 2 1
6
2 4 1 3 2 1
4
2 1 1 3
4
1 3 3 1
12
2 1 3 4 5 6 7 8 9 1 10 2
3
1 1 1
Output
2
1 4
4 1
1
4 2
0
0
1
2 10
0
Note
In the first example, two possible ways to divide a into permutations are \{1\} + \{4, 3, 2, 1\} and \{1,4,3,2\} + \{1\}.
In the second example, the only way to divide a into permutations is \{2,4,1,3\} + \{2,1\}.
In the third example, there are no possible ways.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
b = [int(i) for i in input().split()]
a = [0]
c = [0]
ans = []
for i in range(1, n + 1):
a += [a[-1] + i]
c += [c[-1] + b[i - 1]]
for i in range(1, n):
if c[i] == a[i] and c[n] - c[i] == a[n - i]:
ans += [[i, n - i]]
print(len(ans))
for x, y in ans:
print(x, y)
``` | instruction | 0 | 24,918 | 12 | 49,836 |
No | output | 1 | 24,918 | 12 | 49,837 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,114 | 12 | 50,228 |
Tags: greedy
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
count = [0]*100005
mx = 0
for i in range(n):
count[a[i]] += 1
mx = max(mx,count[a[i]])
a[i] = count[a[i]]
ans = True
for i in range(1,100004):
if (count[i]<count[i+1]):
ans = False
break
if (ans):
print(mx)
print(*a)
else:
print(-1)
``` | output | 1 | 25,114 | 12 | 50,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,115 | 12 | 50,230 |
Tags: greedy
Correct Solution:
```
n = int(input())
l = list(map(int, input().split()))
dic = {}
li = list(set(l))
a = len(li)
b = max(li)
if b > a:
print(-1)
else:
for i in range(a):
dic[i + 1] = 0
for i in range(n):
dic[l[i]] += 1
ls = []
for i in range(a):
s = [i + 1, dic[i + 1]]
ls.append(s)
jud = 0
for i in range(1, a):
if ls[i][1] > ls[i - 1][1]:
print(-1)
jud = 1
break
if jud == 0:
print(dic[1])
lt = []
for i in range(n):
ss = dic[l[i]]
dic[l[i]] -= 1
lt.append(str(ss))
print(' '.join(lt))
``` | output | 1 | 25,115 | 12 | 50,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,116 | 12 | 50,232 |
Tags: greedy
Correct Solution:
```
n = int(input())
arr = list(map(int,input().split()))
res = [0] * n
mx = 0
l = [0] * 100001
for i in range(n):
l[arr[i]] += 1
mx = max(mx,arr[i])
for i in range(1,mx):
if l[i+1] > l[i]:
print(-1)
break
else:
copy = l[:]
print(l[1])
for i in range(n):
l[arr[i]] -= 1
res[i] = copy[arr[i]] - l[arr[i]]
print(" ".join(list(map(str,res))))
``` | output | 1 | 25,116 | 12 | 50,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,117 | 12 | 50,234 |
Tags: greedy
Correct Solution:
```
n=int(input())
t=list(map(int,input().split()))
l,m=min(t),max(t)
if l<1 or m>n:print(-1)
else:
r,p=[0]*(m+1),[0]*n
for i,j in enumerate(t):
r[j]+=1
p[i]=r[j]
if any(r[j+1]>r[j] for j in range(1,m)):print(-1)
else:
print(r[1])
print(' '.join(map(str,p)))
``` | output | 1 | 25,117 | 12 | 50,235 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,118 | 12 | 50,236 |
Tags: greedy
Correct Solution:
```
n = int(input())
a = []
mx = -1
for x in input().split():
a.append(int(x))
mx = max(mx, int(x))
b = [0 for x in range(mx + 1)]
for x in a:
b[x] += 1
ck = True
for i in range(1, mx):
if b[i + 1] > b[i]:
ck = False
break
if ck:
c = [1 for x in range(mx + 1)]
ans = []
for x in a:
ans.append(c[x])
c[x] += 1
print(max(c) - 1)
print(*ans)
else:
print(-1)
``` | output | 1 | 25,118 | 12 | 50,237 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,119 | 12 | 50,238 |
Tags: greedy
Correct Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
#l.sort()
#cnt=l.count(1)
from collections import Counter
c=Counter(l)
prev=c[1]
cnt=c[1]
f=1
from collections import defaultdict
d=defaultdict(list)
for i in range(n):
d[l[i]].append(i)
for i in range(2,max(l)+1):
if c[i]>prev:
f=0
break
prev=c[i]
'''if any(c[i]==0 for i in range(1,max(l)+1)):
print(-1)
exit()'''
if not f:
print(-1)
exit()
l=[[] for i in range(10**5+3)]
for i in range(n,0,-1):
if c[i]>0:
for j in range(c[i]):
l[j].append(d[i].pop())
print(cnt)
#print(l)
ans=[0]*n
for i in range(len(l)):
for j in range(len(l[i])):
ans[l[i][j]]=i+1
print(*ans)
``` | output | 1 | 25,119 | 12 | 50,239 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,120 | 12 | 50,240 |
Tags: greedy
Correct Solution:
```
#!/usr/bin/env python3
'''exceeds time-limit on the 51 testcase'''
from collections import Counter
def main():
n = int(input())
numbers = list(map(int, input().split()))
permutations = []
cnt = Counter()
for number in numbers:
cnt[number] += 1
permutations.append(cnt[number])
if cnt[1] == 0: return "-1"
buff_k, buff_d = 0, 0
for d, k in sorted(cnt.items()):
if (k <= buff_k or buff_k == 0) and (d == buff_d + 1 or buff_d == 0):
buff_k, buff_d = k, d
else:
return "-1"
return "".join([str(cnt[1]), '\n', " ".join(list(map(str, permutations)))])
if __name__ == "__main__":
print(main())
``` | output | 1 | 25,120 | 12 | 50,241 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible. | instruction | 0 | 25,121 | 12 | 50,242 |
Tags: greedy
Correct Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
#l.sort()
#cnt=l.count(1)
from collections import Counter
c=Counter(l)
prev=c[1]
cnt=c[1]
f=1
from collections import defaultdict
d=defaultdict(list)
for i in range(n):
d[l[i]].append(i)
for i in range(2,max(l)+1):
if c[i]>prev:
f=0
break
prev=c[i]
if any(c[i]==0 for i in range(1,max(l)+1)):
print(-1)
exit()
if not f:
print(-1)
exit()
l=[[] for i in range(10**5+3)]
for i in range(n,0,-1):
if c[i]>0:
for j in range(c[i]):
l[j].append(d[i].pop())
print(cnt)
#print(l)
ans=[0]*n
for i in range(len(l)):
for j in range(len(l[i])):
ans[l[i][j]]=i+1
print(*ans)
``` | output | 1 | 25,121 | 12 | 50,243 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
def solve():
_ = input()
arr = list(map(int, input().split()))
frq = {k: 0 for k in range(1, 100001)}
inds = {k: 1 for k in range(1, 100001)}
for v in arr:
frq[v] += 1
for i in range(1, len(frq)):
if frq[i] < frq[i+1]:
print(-1)
return
print(frq[1])
for v in arr:
print(inds[v], end = ' ')
inds[v] += 1
solve()
``` | instruction | 0 | 25,122 | 12 | 50,244 |
Yes | output | 1 | 25,122 | 12 | 50,245 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
#check for permutation
count = [0]*(10**6)
f=1
m=max(a)
for i in range(n):
count[a[i]]+=1
for i in range(2,n+1):
if count[i]>count[i-1]:
f=0
break
for i in range(2,m+1):
if count[i]==0 or count[i]>count[i-1] :
f=0
break
if count[1]==0:
f=0
if f==0 :
print(-1)
else:
f=[0]*(10**6)
ans=[]
for i in range(n):
ans.append(f[a[i]]+1)
f[a[i]]+=1
print(count[1])
print(*ans)
``` | instruction | 0 | 25,123 | 12 | 50,246 |
Yes | output | 1 | 25,123 | 12 | 50,247 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
n = int(input())
t = list(map(int, input().split()))
a, b = min(t), max(t)
if a < 1 or b > n: print(-1)
else:
r, p = [0] * (b + 1), [0] * n
for i, j in enumerate(t):
r[j] += 1
p[i] = r[j]
if any(r[j + 1] > r[j] for j in range(1, b)): print(-1)
else:
print(r[1])
print(' '.join(map(str, p)))
``` | instruction | 0 | 25,124 | 12 | 50,248 |
Yes | output | 1 | 25,124 | 12 | 50,249 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
n = int(input())
cnt = [set() for i in range(10 ** 5 + 1)]
a = list(map(int, input().split()))
for i in range(n):
cnt[a[i] - 1].add(i)
res = [0] * n
ends = []
for i in range(len(cnt) - 1):
if len(cnt[i + 1]) - len(cnt[i]) > 0:
print(-1)
exit()
ends.append((i + 1, - len(cnt[i + 1]) + len(cnt[i])))
if len(cnt[-1]) > 0:
ends.append((10 ** 5, len(cnt[-1])))
new_cnt = 0
for i in ends:
for j in range(i[1]):
new_cnt += 1
cur = []
for k in range(i[0]):
res[cnt[k].pop()] = new_cnt
print(new_cnt)
print(" ".join(map(str, res)))
``` | instruction | 0 | 25,125 | 12 | 50,250 |
Yes | output | 1 | 25,125 | 12 | 50,251 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
from collections import Counter
def permutations(n, array):
perms = []
counter = Counter(array)
perms = [[] for x in range(counter.most_common(1)[0][1])]
original_a = array
array.sort()
prev = 0
j = 0
for i in range(0, len(array)):
if array[i] == prev:
j = j+1
elif array[i] == prev + 1:
j = 0
else:
print(-1)
return
perms[j].append(array[i])
prev = array[i]
if len(perms) > counter.get(1):
print(-1)
return
s = ""
for i in original_a:
for index, j in enumerate(perms):
if i in j:
s += str(index + 1) + " "
j.remove(i)
break
print(len(perms))
print(s)
return
nb = int(input())
numbers = [int(i) for i in input().split(' ')]
permutations(nb, numbers)
``` | instruction | 0 | 25,126 | 12 | 50,252 |
No | output | 1 | 25,126 | 12 | 50,253 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
#!/usr/bin/env python3
from collections import Counter
n = int(input())
numbers = list(map(int, input().split()))
cnt = Counter()
for i, number in enumerate(numbers):
cnt[number] += 1
buff = 0
permutations, checked = [], []
ok = True
for d, k in cnt.items():
if k <= buff or buff == 0:
buff = k
continue
else:
print("-1")
ok = False
if ok:
for i, number in enumerate(numbers):
permutations.append(checked.count(number) + 1)
checked.append(number)
print(cnt[1])
print(' '.join(list(map(str, permutations))))
``` | instruction | 0 | 25,127 | 12 | 50,254 |
No | output | 1 | 25,127 | 12 | 50,255 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
#!/usr/bin/env python3
from collections import Counter
n = int(input())
numbers = list(map(int, input().split()))
cnt = Counter()
for number in sorted(numbers):
cnt[number] += 1
buff_k, buff_d = 0, 0
permutations, checked = [], []
ok = True
if ok:
for d, k in cnt.items():
if (k <= buff_k or buff_k == 0) and (d == buff_d + 1 or buff_d == 0):
buff_k, buff_d = k, d
elif cnt[1] == 0:
print("-1")
ok = False
break
else:
print("-1")
ok = False
break
if ok:
for number in numbers:
permutations.append(checked.count(number) + 1)
checked.append(number)
print(cnt[1])
print(' '.join(list(map(str, permutations))))
``` | instruction | 0 | 25,128 | 12 | 50,256 |
No | output | 1 | 25,128 | 12 | 50,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations, and (1, 1), (4, 3, 1), (2, 3, 4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
Input
The first line contains an integer n (1 β€ n β€ 105). The next line contains the mixed array of n integers, divided with a single space. The numbers in the array are from 1 to 105.
Output
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain n numbers, corresponding to the elements of the given array. If the i-th element belongs to the first permutation, the i-th number should be 1, if it belongs to the second one, then its number should be 2 and so on. The order of the permutationsβ numbering is free.
If several solutions are possible, print any one of them. If thereβs no solution, print in the first line - 1.
Examples
Input
9
1 2 3 1 2 1 4 2 5
Output
3
3 1 2 1 2 2 2 3 2
Input
4
4 3 2 1
Output
1
1 1 1 1
Input
4
1 2 2 3
Output
-1
Note
In the first sample test the array is split into three permutations: (2, 1), (3, 2, 1, 4, 5), (1, 2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth elements. Clearly, there are other splitting variants possible.
Submitted Solution:
```
#!/usr/bin/env python3
'''exceeds time-limit on the 51 testcase'''
from collections import Counter
def main():
n = int(input())
numbers = list(map(int, input().split()))
permutations, checked = [], []
cnt = Counter()
for number in numbers:
cnt[number] += 1
permutations.append(cnt[number] + 1)
# checked.append(number)
buff_k, buff_d = 0, 0
if cnt[1] == 0:
return "-1"
for d, k in sorted(cnt.items()):
if (k <= buff_k or buff_k == 0) and (d == buff_d + 1 or buff_d == 0):
buff_k, buff_d = k, d
else:
return "-1"
return "".join([str(cnt[1]), '\n', " ".join(list(map(str, permutations)))])
if __name__ == "__main__":
print(main())
``` | instruction | 0 | 25,129 | 12 | 50,258 |
No | output | 1 | 25,129 | 12 | 50,259 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 β€ bi β€ ai for every 1 β€ i β€ n. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of elements in the Alyona's array.
The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.
Output
Print one positive integer β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
Examples
Input
5
1 3 3 3 6
Output
5
Input
2
2 1
Output
3
Note
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | instruction | 0 | 25,204 | 12 | 50,408 |
Tags: sortings
Correct Solution:
```
read = lambda: map(int, input().split())
n = int(input())
a = sorted(read())
k = 1
for i in range(n):
if a[i] >= k: k += 1
print(k)
``` | output | 1 | 25,204 | 12 | 50,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 β€ bi β€ ai for every 1 β€ i β€ n. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of elements in the Alyona's array.
The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.
Output
Print one positive integer β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
Examples
Input
5
1 3 3 3 6
Output
5
Input
2
2 1
Output
3
Note
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | instruction | 0 | 25,205 | 12 | 50,410 |
Tags: sortings
Correct Solution:
```
a = int(input())
arr = list(map(int,input().split()))
m = 1
for i in sorted(arr):
m+=i>=m
print(m)
``` | output | 1 | 25,205 | 12 | 50,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 β€ bi β€ ai for every 1 β€ i β€ n. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of elements in the Alyona's array.
The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.
Output
Print one positive integer β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
Examples
Input
5
1 3 3 3 6
Output
5
Input
2
2 1
Output
3
Note
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | instruction | 0 | 25,206 | 12 | 50,412 |
Tags: sortings
Correct Solution:
```
N = int(input())
A = list(map(int, input().split()))
A.sort()
now = 0
for i in range(N):
if A[i] > now:
now += 1
A[i] = now
elif A[i] == now:
continue
print(A[-1] + 1)
``` | output | 1 | 25,206 | 12 | 50,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 β€ bi β€ ai for every 1 β€ i β€ n. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of elements in the Alyona's array.
The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.
Output
Print one positive integer β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
Examples
Input
5
1 3 3 3 6
Output
5
Input
2
2 1
Output
3
Note
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | instruction | 0 | 25,207 | 12 | 50,414 |
Tags: sortings
Correct Solution:
```
# RawCoder : https://bit.ly/RCyouTube
# Author : MehulYK
n = int(input())
w = list(map(int,input().split()))
w.sort()
ans = 1
for i in w:
ans += i >= ans
print(ans)
``` | output | 1 | 25,207 | 12 | 50,415 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 β€ bi β€ ai for every 1 β€ i β€ n. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of elements in the Alyona's array.
The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.
Output
Print one positive integer β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
Examples
Input
5
1 3 3 3 6
Output
5
Input
2
2 1
Output
3
Note
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | instruction | 0 | 25,208 | 12 | 50,416 |
Tags: sortings
Correct Solution:
```
n = int(input())
a = sorted(list(map(int, input().split())))
m = 1
for i in a:
if i >= m:
m += 1
print(m)
``` | output | 1 | 25,208 | 12 | 50,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 β€ bi β€ ai for every 1 β€ i β€ n. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of elements in the Alyona's array.
The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.
Output
Print one positive integer β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
Examples
Input
5
1 3 3 3 6
Output
5
Input
2
2 1
Output
3
Note
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | instruction | 0 | 25,209 | 12 | 50,418 |
Tags: sortings
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
temp = 1
for i in range(n):
if a[i]!=temp and a[i]>temp:
a[i] = temp
temp += 1
elif a[i] == temp:
temp += 1
print(a[n-1]+1)
``` | output | 1 | 25,209 | 12 | 50,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 β€ bi β€ ai for every 1 β€ i β€ n. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of elements in the Alyona's array.
The second line of the input contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.
Output
Print one positive integer β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
Examples
Input
5
1 3 3 3 6
Output
5
Input
2
2 1
Output
3
Note
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | instruction | 0 | 25,210 | 12 | 50,420 |
Tags: sortings
Correct Solution:
```
n = int(input())
nums = list(map(int, input().split()))
nums.sort()
nums[0] = 1
for i in range(n-1):
if nums[i+1] - nums[i] > 1:
nums[i+1] = nums[i] + 1
print(nums[-1] + 1)
``` | output | 1 | 25,210 | 12 | 50,421 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.