message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,408 | 12 | 166,816 |
Tags: constructive algorithms, sortings
Correct Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineerin College
Date:08/06/2020
'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def solve():
# for _ in range(ii()):
n=ii()
a=li()
x=[0]*(max(a)+1)
for i in a:
x[i]+=1
if max(x)>2:
print("NO")
exit(0)
a=[]
b=[]
for i in range(len(x)):
if x[i]==1:
a.append(i)
if x[i]==2:
a.append(i)
b.append(i)
print("YES")
print(len(a))
print(*a)
b.reverse()
print(len(b))
print(*b)
if __name__ =="__main__":
solve()
``` | output | 1 | 83,408 | 12 | 166,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,409 | 12 | 166,818 |
Tags: constructive algorithms, sortings
Correct Solution:
```
import sys
# sys.stdin = open('E:/Competitive Programming/input.txt', 'r')
# sys.stdout = open('E:/Competitive Programming/output.txt', 'w')
# sys.setrecursionlimit(1000000)
def Ints(): return map(int, sys.stdin.readline().strip().split())
def Strs(): return map(str, sys.stdin.readline().strip().split())
def Array(): return list(map(int, sys.stdin.readline().strip().split()))
def Str(): return sys.stdin.readline().strip()
def Int(): return int(sys.stdin.readline().strip())
def MOD(): return 1000000007
def power(base, power):
MOD = 1000000007
result = 1
while power > 0:
if power % 2 == 1:
result = (result * base) % MOD
power = power // 2
base = (base * base) % MOD
return result
if __name__ == "__main__":
n = Int()
a = Array()
sin ,sde = set(),set()
increase , decrease = [],[]
status = "YES"
for i in range(n):
if a[i] not in sin:
sin.add(a[i])
increase.append(a[i])
elif a[i] not in sde:
sde.add(a[i])
decrease.append(a[i])
else:
status = "NO"
# print(increase)
# print(decrease)
if status == "YES":
increase = sorted(increase)
decrease = sorted(decrease,reverse = True)
print(status)
print(len(increase))
for i in increase:
print(i , end = ' ')
print()
print(len(decrease))
for i in decrease:
print(i , end = ' ')
else:
print(status)
``` | output | 1 | 83,409 | 12 | 166,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,410 | 12 | 166,820 |
Tags: constructive algorithms, sortings
Correct Solution:
```
n = int(input())
l = sorted(map(int, input().split()))
inc = []
dec = []
visited = [False]*n
for i in range(n):
if not inc or inc[-1] != l[i]:
inc.append(l[i])
visited[i] = True
for i in range(n):
if not visited[i] and (not dec or dec[-1] != l[i]):
dec.append(l[i])
visited[i] = True
for i in range(n):
if not visited[i]:
print("NO")
exit()
dec.reverse()
print("YES")
print(len(inc))
for i in inc: print(i, end=' ')
print()
print(len(dec))
for i in dec: print(i, end=' ')
``` | output | 1 | 83,410 | 12 | 166,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,411 | 12 | 166,822 |
Tags: constructive algorithms, sortings
Correct Solution:
```
##############--->>>>> Deepcoder Amit Kumar Bhuyan <<<<<---##############
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import log,sqrt,factorial,cos,tan,sin,radians
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *
#import threading
#from itertools import permutations
#Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy
import sys
input = sys.stdin.readline
scan_int = lambda: int(input())
scan_string = lambda: input().rstrip()
read_list = lambda: list(read())
read = lambda: map(int, input().split())
read_float = lambda: map(float, input().split())
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // math.gcd(a, b)
def is_integer(n):
return math.ceil(n) == math.floor(n)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
def next_prime(n, primes):
while primes[n] != True:
n += 1
return n
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
def binary_search(low, high, w, h, n):
while low < high:
mid = low + (high - low) // 2
# print(low, mid, high)
if check(mid, w, h, n):
low = mid + 1
else:
high = mid
return low
## for checking any conditions
def check(beauty, s, n, count):
pass
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
def solve():
n = scan_int()
a = read_list()
a.sort()
mp = {}
for x in a:
d = mp.get(x, 0)
mp[x] = d + 1
if mp[x] > 2:
print("NO")
return
val = list(mp.keys())
one = []
two = []
for i in range(0, n, 2):
one.append(a[i])
for i in range(1, n, 2):
two.append(a[i])
print("YES")
print(len(one))
print(*one)
print(len(two))
print(*two[::-1])
# region fastio
# template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py
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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
#read()
for i in range(1):
solve()
#dmain()
# Comment Read()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
``` | output | 1 | 83,411 | 12 | 166,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,412 | 12 | 166,824 |
Tags: constructive algorithms, sortings
Correct Solution:
```
import sys
n = int(input())
s = list(map(int, input().split()))
d = dict()
ok = True
for i in range(n):
if s[i] in d:
d[s[i]] += 1
else:
d[s[i]] = 1
if d[s[i]] > 2:
print('NO')
sys.exit()
ans1 = []
ans2 = []
for i in d:
ans1.append(i)
d[i] -= 1
for i in d:
if d[i] != 0:
ans2.append(i)
ans1.sort()
ans2.sort(reverse = True)
print('YES')
print(len(ans1))
print(*ans1)
print(len(ans2))
print(*ans2)
``` | output | 1 | 83,412 | 12 | 166,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,413 | 12 | 166,826 |
Tags: constructive algorithms, sortings
Correct Solution:
```
n=int(input())
a=[int(x) for x in input().strip().split()]
ub=[]
vs=[]
error=0
a.sort()
vs.append(a[0])
for i in a[1:]:
if not i==vs[-1]:
vs.append(i)
elif not ub:
ub.append(i)
elif not i==ub[-1]:
ub.append(i)
else:
print('NO')
error=1
break
'''
for i in a:
if i not in vs:
vs.append(i)
elif i not in ub:
ub.append(i)
else:
print('NO')
error=1
break'''
if error==0:
print('YES')
print(len(vs))
for i in vs:
print(i,end=' ')
print()
print(len(ub))
for i in range(len(ub)):
print(ub[len(ub)-1-i],end=' ')
``` | output | 1 | 83,413 | 12 | 166,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,414 | 12 | 166,828 |
Tags: constructive algorithms, sortings
Correct Solution:
```
GI = lambda: int(input()); GIS = lambda: map(int, input().split()); LGIS = lambda: list(GIS())
def main():
GI()
ctr = [0] * ((2 * 10 ** 5) + 1)
for x in GIS():
ctr[x] += 1
inc = []
dec = []
for i, c in enumerate(ctr):
if c == 0:
continue
if c > 2:
print("NO")
return
inc.append(i)
if c == 2:
dec.append(i)
dec.reverse()
print("YES")
for l in inc, dec:
print(len(l))
print(' '.join(map(str, l)))
main()
``` | output | 1 | 83,414 | 12 | 166,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0 | instruction | 0 | 83,415 | 12 | 166,830 |
Tags: constructive algorithms, sortings
Correct Solution:
```
# -*- coding: utf-8 -*-
if __name__ == "__main__":
_ = int(input())
array = sorted(list(map(int, input().split())))
inc = []
dec = []
for elem in array:
if elem in inc[-1:] and elem in dec[-1:]:
inc = None
dec = None
break;
if elem in inc[-1:]:
dec.append(elem)
else:
inc.append(elem)
if inc is None:
print("NO")
else:
print("YES")
print(len(inc))
print(" ".join(str(e) for e in inc))
print(len(dec))
print(" ".join(str(e) for e in dec[::-1]))
``` | output | 1 | 83,415 | 12 | 166,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
a = set()
b = set()
for i in arr:
if i not in a :
a.add(i)
elif i not in b :
b.add(i)
else:
print('NO')
exit()
print('YES')
print(len(a))
print(*sorted(a))
print(len(b))
print(*sorted(b , reverse = True))
``` | instruction | 0 | 83,416 | 12 | 166,832 |
Yes | output | 1 | 83,416 | 12 | 166,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
num = int(input())
array = sorted(list(map(int, input().split())))
a = []
d = []
counter = 1
for x in range(len(array) - 1):
if counter == 3:
print('NO')
break
else:
if array[x] == array[x + 1]:
counter += 1
d.append(array[x])
else:
counter = 1
a.append(array[x])
else:
a.append(array[-1])
a.sort()
d.sort(reverse=True)
print('YES')
print(len(a))
print(*a)
print(len(d))
print(*d)
``` | instruction | 0 | 83,417 | 12 | 166,834 |
Yes | output | 1 | 83,417 | 12 | 166,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
from collections import Counter
n = int(input())
numl = list(map(int,input().split()))
decrese, increse = [],[]
c = Counter(numl)
if c.most_common(1)[0][1] > 2:
print("NO")
else:
for cnt in c.items():
if cnt[1]==2:
decrese.append(int(cnt[0]))
for i in c.keys():
increse.append(int(i))
increse.sort()
decrese.sort(reverse=True)
print("YES")
print(len(increse))
print(" ".join(map(str,increse)))
print(len(decrese))
print(" ".join(map(str,decrese)))
``` | instruction | 0 | 83,418 | 12 | 166,836 |
Yes | output | 1 | 83,418 | 12 | 166,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
n=int(input())
l=list(map(int, input().split()))
freq=dict()
for i in range(n):
if l[i] not in freq:
freq[l[i]]=1
else:
freq[l[i]]+=1
flag=0
inc=[]
#print(freq)
for key,value in freq.items():
if value>=3:
flag=1
break
if value>1:
inc.append(key)
freq[key]-=1
#print(freq)
if flag==1:
print('No')
else:
print("Yes")
'''
if len(set(l))==len(l):
print("0\n")
print(n)
print(*sorted(l, reverse=True))
'''
print(len(inc))
print(*sorted(list(set(inc))))
print(len(set(l)))
print(*sorted(list(set(l)), reverse=True))
``` | instruction | 0 | 83,419 | 12 | 166,838 |
Yes | output | 1 | 83,419 | 12 | 166,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
def solve():
n = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr)
for i in range(n):
arr[i] = [arr[i], 1]
a = []
a.append(arr[0][0])
arr[0][1] = 0
b = []
for i in range(1, n):
if arr[i][0] > a[len(a) - 1]:
a.append(arr[i][0])
arr[i][1] = 0
for i in range(n - 1, 0, -1):
if (arr[i][1] == 1):
if len(b) == 0 or b[len(b) - 1] > arr[i][0]:
b.append(arr[i][0])
arr[i][1] = 0
else:
print("No")
return
#print(arr)
print(len(a))
for el in a:
print(el, end = " ")
print()
print(len(b))
for el in b:
print(el, end = " ")
solve()
``` | instruction | 0 | 83,420 | 12 | 166,840 |
No | output | 1 | 83,420 | 12 | 166,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
n = int(input())
a = input().split()
a.sort(key = lambda x : int(x))
d = {}
l1 = []
l2 = []
flag = 0
for i in a:
try:
if d[i] >= 2:
flag = 1
break
d[i] += 1
l2 = [i] + l2
except KeyError:
d[i] = 1
l1.append(i)
if flag == 0:
print('Yes')
print(len(l1))
if len(l1):
print(' '.join(l1))
print(len(l2))
if len(l2):
print(len(l2))
print(' '.join(l2))
else:
print('No')
``` | instruction | 0 | 83,421 | 12 | 166,842 |
No | output | 1 | 83,421 | 12 | 166,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
n = int(input())
a = sorted(map(int, input().split()))
j, k, a1, a2 = -1, -1, set(a), []
for i in a:
if i == j == k:
print('NO')
exit()
elif i == j:
a2.append(i)
j, k = i, j
print('YES')
print(len(a1), *a1)
print(len(a2), *reversed(a2))
``` | instruction | 0 | 83,422 | 12 | 166,844 |
No | output | 1 | 83,422 | 12 | 166,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two integer sequences existed initially β one of them was strictly increasing, and the other one β strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one β strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i β the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) β the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d β the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) β the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Submitted Solution:
```
from collections import defaultdict
def fun():
return 0
n=int(input())
arr=[int(x) for x in input().split()]
arr=sorted(arr)
d=defaultdict(fun)
flag=True
for i in arr:
d[i]+=1
if d[i]>2:
flag=False
break
if not flag:
print("NO")
else:
print("YES")
dec=[]
dd=0
arr=list(set(arr))
for i in arr:
if d[i]==2:
dd+=1
dec.append(i)
print(dd)
if dd==0:
print()
else:
for i in dec:
print(i,end=" ")
print()
a=len(arr)
print(a)
if a==0:
print()
else:
for i in arr[::-1]:
print(i,end=" ")
print()
``` | instruction | 0 | 83,423 | 12 | 166,846 |
No | output | 1 | 83,423 | 12 | 166,847 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, as a friendship gift, Bakry gave Badawy n integers a_1, a_2, ..., a_n and challenged him to choose an integer X such that the value \underset{1 β€ i β€ n}{max} (a_i β X) is minimum possible, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Input
The first line contains integer n (1β€ n β€ 10^5).
The second line contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2^{30}-1).
Output
Print one integer β the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Examples
Input
3
1 2 3
Output
2
Input
2
1 5
Output
4
Note
In the first sample, we can choose X = 3.
In the second sample, we can choose X = 5. | instruction | 0 | 83,505 | 12 | 167,010 |
Tags: bitmasks, brute force, dfs and similar, divide and conquer, dp, greedy, strings, trees
Correct Solution:
```
def maxxor(A,n):
if len(A)==1:
return 0
if n==-1:
return 0
one=0
zero=0
res=0
for a in A:
one+=(a>>n &1)
zero+=(1-(a>>n &1))
if one==0 or zero==0:
return maxxor(A,n-1)
else:
B=[A[i] for i in range(len(A)) if A[i]>>n &1==1]
C=[A[i] for i in range(len(A)) if A[i]>>n &1==0]
m=min(maxxor(B,n-1),maxxor(C,n-1))
return m+2**n
import random
N=int(input())
A=list(map(int,input().split()))
print(maxxor(A,30))
``` | output | 1 | 83,505 | 12 | 167,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, as a friendship gift, Bakry gave Badawy n integers a_1, a_2, ..., a_n and challenged him to choose an integer X such that the value \underset{1 β€ i β€ n}{max} (a_i β X) is minimum possible, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Input
The first line contains integer n (1β€ n β€ 10^5).
The second line contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2^{30}-1).
Output
Print one integer β the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Examples
Input
3
1 2 3
Output
2
Input
2
1 5
Output
4
Note
In the first sample, we can choose X = 3.
In the second sample, we can choose X = 5. | instruction | 0 | 83,506 | 12 | 167,012 |
Tags: bitmasks, brute force, dfs and similar, divide and conquer, dp, greedy, strings, trees
Correct Solution:
```
import sys
# inf = open('input.txt', 'r')
# reader = (line.rstrip() for line in inf)
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
a = list(map(int, input().split()))
a.sort()
# inf.close()
stack = [(0, n, 1 << 29, 0)]
ans = 1 << 30
while stack:
l, r, mask, curr = stack.pop()
if not mask:
ans = min(ans, curr)
elif not(a[l] & mask) and (a[r-1] & mask):
L = l
R = r-1
while L+1 < R:
m = (L + R) >> 1
if a[m] & mask:
R = m
else:
L = m
m = R
stack.append((l, m, mask >> 1, curr | mask))
stack.append((m, r, mask >> 1, curr | mask))
else:
stack.append((l, r, mask >> 1, curr))
print(ans)
``` | output | 1 | 83,506 | 12 | 167,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, as a friendship gift, Bakry gave Badawy n integers a_1, a_2, ..., a_n and challenged him to choose an integer X such that the value \underset{1 β€ i β€ n}{max} (a_i β X) is minimum possible, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Input
The first line contains integer n (1β€ n β€ 10^5).
The second line contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2^{30}-1).
Output
Print one integer β the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Examples
Input
3
1 2 3
Output
2
Input
2
1 5
Output
4
Note
In the first sample, we can choose X = 3.
In the second sample, we can choose X = 5. | instruction | 0 | 83,508 | 12 | 167,016 |
Tags: bitmasks, brute force, dfs and similar, divide and conquer, dp, greedy, strings, trees
Correct Solution:
```
from math import floor, log
def get_x(i, nums):
# split the group based on the i-th significant bit
get_ith_bit = lambda num: (num >> i) & 1
hi, lo = [], []
for num in nums:
(hi if get_ith_bit(num) == 1 else lo).append(num)
# set i-th significant bit of X to 1 if there is a choice to make
if hi and lo:
if i == 0: return 1 # base case
ith_bit = 1 << i
explore_hi = get_x(i-1, hi)
explore_lo = get_x(i-1, lo)
next_bits = min(explore_hi, explore_lo)
# Else Leave i-th sig bit of X at 0 if all nums have the same i-th sig bit
else:
if i == 0: return 0 # base case
ith_bit = 0
next_bits = get_x(i-1, nums)
# Merge ith bit with recursively computed next_bits
return ith_bit | next_bits
input()
nums = [int(n) for n in input().split()]
# start from most significant bit
print(get_x(29, nums))
``` | output | 1 | 83,508 | 12 | 167,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, as a friendship gift, Bakry gave Badawy n integers a_1, a_2, ..., a_n and challenged him to choose an integer X such that the value \underset{1 β€ i β€ n}{max} (a_i β X) is minimum possible, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Input
The first line contains integer n (1β€ n β€ 10^5).
The second line contains n integers a_1, a_2, ..., a_n (0 β€ a_i β€ 2^{30}-1).
Output
Print one integer β the minimum possible value of \underset{1 β€ i β€ n}{max} (a_i β X).
Examples
Input
3
1 2 3
Output
2
Input
2
1 5
Output
4
Note
In the first sample, we can choose X = 3.
In the second sample, we can choose X = 5. | instruction | 0 | 83,509 | 12 | 167,018 |
Tags: bitmasks, brute force, dfs and similar, divide and conquer, dp, greedy, strings, trees
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
def fun(arr,bit):
if bit<0: return 0
z = []
o = []
for i in range(0,len(arr)):
if (arr[i]&(1<<bit))==0:
z.append(arr[i])
else:
o.append(arr[i])
if len(z)==0:
return fun(arr,bit-1)
if len(o)==0:
return fun(arr,bit-1)
return min(fun(z,bit-1),fun(o,bit-1))+(1<<bit)
print(fun(a,30))
``` | output | 1 | 83,509 | 12 | 167,019 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,573 | 12 | 167,146 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
import io
import os
from collections import Counter, defaultdict, deque
def solve(R, C, grid):
if R == 1 or C == 1:
return 0
if R >= 4 and C >= 4:
# 4 2x2 impossible
return -1
colMasks = []
if R < C:
#print("transposed")
transposed = [[0 for r in range(R)] for c in range(C)]
for r in range(R):
for c in range(C):
transposed[c][r] = grid[r][c]
R, C = C, R
grid = transposed
# Narrow
assert C <= R
assert 2 <= C < 4
# print('colmask', R, C, grid)
colMasks = []
for r in range(R):
mask = 0
for c in range(C):
mask |= grid[r][c] << c
colMasks.append(mask)
#print(bin(mask))
numMasks = 1 << C
# prevMask to mask that have odd
graph = [[] for i in range(numMasks)]
# print('graph')
for prevMask in range(numMasks):
for mask in range(numMasks):
good = True
vert = mask ^ prevMask
for c in range(C - 1):
a = (vert >> c) & 1
b = (vert >> (c + 1)) & 1
if a ^ b == 0:
good = False
break
if good:
graph[prevMask].append(mask)
# print(bin(prevMask), bin(mask))
popcount = []
for mask in range(numMasks):
popcount.append(bin(mask).count("1"))
inf = 10000000
dp = [[inf for i in range(R)] for j in range(numMasks)] # dp[endingMask][last index]
# r = 0
for mask in range(numMasks):
dp[mask][0] = popcount[mask ^ colMasks[0]]
#print(dp)
for r in range(1, R):
for mask in range(numMasks):
best = inf
cost = popcount[mask ^ colMasks[r]]
for prevMask in graph[mask]:
#print(r, bin(prevMask), bin(mask), bin(colMasks[r]), cost, dp[prevMask][r - 1])
best = min(best, cost + dp[prevMask][r - 1])
dp[mask][r] = best
best = min(dp[mask][R - 1] for mask in range(1 << C))
if best >= inf:
return -1
return best
if False:
def isGood(grid):
for r in range(R - 1):
for c in range(C - 1):
count = 0
for dr in range(2):
for dc in range(2):
count += grid[r + dr][c + dc]
if count % 2 != 1:
return False
return True
R, C = 3, 4
for mask in range(1 << R * C):
grid = [[0 for c in range(C)] for r in range(R)]
for r in range(R):
for c in range(C):
grid[r][c] = int(bool(mask & (1 << (r * C + c))))
if isGood(grid):
print(mask)
for row in grid:
print(row)
exit()
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
N, M = [int(x) for x in input().split()]
grid = [[int(x) for x in input().decode().rstrip()] for i in range(N)]
ans = solve(N, M, grid)
print(ans)
``` | output | 1 | 83,573 | 12 | 167,147 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,574 | 12 | 167,148 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
n,m = map(int,input().split())
if min(n,m)==1:
print(0)
elif n>=4:
print(-1)
elif n==2:
lis=[]
c=d=0
for i in range(2):
s = list(input())
lis.append(s)
for i in range(m):
if (int(lis[0][i])+int(lis[1][i])+i)%2:
c+=1
else:
d+=1
print(min(c,d))
else:
lis=[]
c=d=0
ans=[0,0,0,0]
for i in range(3):
s = list(input())
lis.append(s)
for i in range(m):
c = (int(lis[0][i]) + int(lis[1][i]))%2
d = (int(lis[1][i]) + int(lis[2][i]))%2
if i%2==0:
if d!=0 or c!=0:
ans[0]+=1
if d==0 or c==0:
ans[1]+=1
if c!=0 or d!=1:
ans[2]+=1
if c!=1 or d!=0:
ans[3]+=1
else:
if d!=0 or c!=0:
ans[1]+=1
if d==0 or c==0:
ans[0]+=1
if c!=1 or d!=0:
ans[2]+=1
if c!=0 or d!=1:
ans[3]+=1
print(min(ans))
#3 10
#1010101010
#1111111111
#1010010010
``` | output | 1 | 83,574 | 12 | 167,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,575 | 12 | 167,150 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
#!/usr/bin/env python3
import collections
import math
def valid(x):
for i in range(len(x) - 1):
for j in range(len(x[i]) - 1):
if (x[i][j] + x[i][j + 1] + x[i + 1][j] + x[i + 1][j + 1]) % 2 == 0:
return False
return True
def main():
n, m = list(map(int, input().split()))
if n >= 4 and m >= 4:
print(-1)
return
a = tuple(tuple(map(int, input())) for _ in range(n))
if n < m:
n, m = m, n
a = tuple(zip(*a))
# m is smaller index, so m in [1, 2, 3]
if m == 1:
# Automatically good
print(0)
return
if m == 2:
choices = [(0, 0), (0, 1), (1, 0), (1, 1)]
else:
choices = [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
prev = []
for c in choices:
prev.append([i for (i, p) in enumerate(choices) if valid([p, c])])
cost = [[sum(i != j for (i, j) in zip(r, c)) for r in choices] for c in choices]
best = [0 for _ in choices]
# first row automatically works
# just stick 0 for free choice
for i in a:
i = choices.index(i)
new = []
for j in range(len(choices)):
new.append(cost[i][j] + min(best[p] for p in prev[j]))
best = new
# print(list(zip(choices, best)))
print(min(best))
if __name__ == "__main__":
main()
``` | output | 1 | 83,575 | 12 | 167,151 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,576 | 12 | 167,152 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
from sys import stdin, stdout
# 0 1 0 1
# 0 0 1 1
# 0 1 0 0 1 1 0 1
# 0 0 1 0 1 0 1 1
# 0 0 0 1 0 1 1 1
def five_O_five(n, m, a_2d):
if n > 3:
return -1
if n == 1:
return 0
MAX = 10**9
if n == 2:
preMask = [0, 0, 0, 0]
for i in range(m):
curMask = [MAX, MAX, MAX, MAX]
for j in range(4):
val = ord(a_2d[0][i]) - ord('0') + 2*(ord(a_2d[1][i]) - ord('0'))
cnt = 0
for k in range(2):
if ((j >> k) & 1) != ((val >> k) & 1):
cnt += 1
for k in range(4):
bits = (j & 1) + ((j >> 1) & 1) + (k & 1) + ((k >> 1) & 1)
if bits % 2 == 1:
curMask[j] = min(curMask[j], preMask[k] + cnt)
preMask = curMask
return min(preMask)
else:
preMask = [0, 0, 0, 0, 0, 0, 0, 0]
for i in range(m):
curMask = [MAX, MAX, MAX, MAX, MAX, MAX, MAX, MAX]
for j in range(8):
val = ord(a_2d[0][i]) - ord('0') + 2 * (ord(a_2d[1][i]) - ord('0')) + 4 * (ord(a_2d[2][i]) - ord('0'))
cnt = 0
for k in range(3):
if ((j >> k) & 1) != ((val >> k) & 1):
cnt += 1
for k in range(8):
bits_up = (j & 1) + ((j >> 1) & 1) + (k & 1) + ((k >> 1) & 1)
bits_down = ((j >> 1) & 1) + ((j >> 2) & 1) + ((k >> 1) & 1) + ((k >> 2) & 1)
if bits_up % 2 == 1 and bits_down % 2 == 1:
curMask[j] = min(curMask[j], preMask[k] + cnt)
#print(curMask)
preMask = curMask
return min(preMask)
n, m = map(int, stdin.readline().split())
a_2d = []
for _ in range(n):
a_2d.append(stdin.readline().strip())
stdout.write(str(five_O_five(n, m, a_2d)) + '\n')
``` | output | 1 | 83,576 | 12 | 167,153 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,577 | 12 | 167,154 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
# import sys
# input = sys.stdin.readline
n, m = map(int,input().split())
A = [list(map(int,list(input()))) for i in range(n)]
def diff3(X, i, j):
if i==1 and j==1:
k = [0,1,0]
elif i==0 and j==1:
k = [0,0,1]
elif i==1 and j==0:
k = [0,1,1]
else:
k = [0,0,0]
dif = 0
for ii in range(3):
if X[ii] != k[ii]:
dif += 1
return min(dif, 3-dif)
if n == 1 or m == 1:
print(0)
elif n >= 4 and m >= 4:
print(-1)
elif n == 2 or m == 2:
if m > n:
m,n = n,m
B = [[0] * m for i in range(n)]
for i in range(n):
for j in range(m):
B[i][j] = A[j][i]
else:
B = A
ans = float("inf")
C = [sum(B[i])%2 for i in range(n)]
ans0 = 0
p = 0
for i in range(n):
if C[i]!=p:
ans0 += 1
p=(p+1)%2
ans = min(ans,ans0)
ans0 = 0
p = 1
for i in range(n):
if C[i]!=p:
ans0 += 1
p=(p+1)%2
ans = min(ans,ans0)
print(ans)
else:
# print("case3")
if m > n:
m,n = n,m
B = [[0] * m for i in range(n)]
for i in range(n):
for j in range(m):
B[i][j] = A[j][i]
else:
B = A
# print(B)
ans = float("inf")
for i in range(2):
for j in range(2):
ans0 = 0
ii=i
jj=j
for k in range(n):
ans0 += diff3(B[k][:], ii, jj)
ii=(ii+1)%2
jj=(jj+1)%2
ans = min(ans,ans0)
print(ans)
``` | output | 1 | 83,577 | 12 | 167,155 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,578 | 12 | 167,156 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal
from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7
def cal(a,b):
o1, o2 = [], []
for i in range(m):
if (mat[a][i] + mat[b][i]) % 2 == 1:
if i % 2 == 0:
o2.append(i)
else:
o1.append(i)
else:
if i % 2 == 0:
o1.append(i)
else:
o2.append(i)
return o1,o2
n,m=mdata()
mat=[list(map(int,list(data()))) for i in range(n)]
if n>3 and m>3:
out(-1)
exit()
if n>m:
n, m = m, n
mat=[[mat[j][i] for j in range(m)] for i in range(n)]
if n==1:
out(0)
exit()
elif n==2:
o1,o2=cal(0,1)
out(min(len(o1),len(o2)))
else:
o1,o2=cal(0,1)
o4, o3 = cal(2,1)
out(min(len(set(o1+o3)),len(set(o1+o4)),len(set(o2+o3)),len(set(o2+o4))))
``` | output | 1 | 83,578 | 12 | 167,157 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,579 | 12 | 167,158 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
def power(x, p,m):
res = 1
while p:
if p & 1:
res = (res * x) % m
x = (x * x) % m
p >>= 1
return res
def inar():
return [int(k) for k in input().split()]
def lcm(num1,num2):
return (num1*num2)//gcd(num1,num2)
def main():
#t=int(input())
# t=1
# mod = 10 ** 9 + 7
# factorial=[1,1]
# n=int(input())
# for i in range(2,n+1):
# factorial.append((factorial[-1]*i)%mod)
# ans=factorial[n]-power(2,n-1,mod)
# print(ans%mod)
#
# #print((n*(n-1))%mod)
n,m=inar()
matrix=[]
for i in range(n):
take=input()
matrix.append(take)
if min(n,m)>3:
print(-1)
else:
if min(n,m)==1:
print(0)
else:
if n>m:
n,m=m,n
matrix=["".join([matrix[j][i] for j in range(m)]) for i in range(n)]
if n==2:
odd=0
even=0
for i in range(m):
check=0
if i%2==0:
if matrix[0][i]=='1':
check+=1
if matrix[1][i]=='1':
check+=1
if check % 2 !=0:
even+=1
else:
if matrix[0][i]=='1':
check+=1
if matrix[1][i]=='1':
check+=1
if check % 2 ==0:
even+=1
for i in range(m):
check=0
if i%2==0:
if matrix[0][i]=='1':
check+=1
if matrix[1][i]=='1':
check+=1
if check % 2 ==0:
odd+=1
else:
if matrix[0][i]=='1':
check+=1
if matrix[1][i]=='1':
check+=1
if check % 2 !=0:
odd+=1
print(min(even,odd))
else:
# even,odd
ans1=0
for i in range(m):
changes1=0
changes2=0
if matrix[0][i]=='1':
changes1+=1
if matrix[1][i]=='1':
changes1+=1
changes2+=1
if matrix[2][i]=='1':
changes2+=1
if i%2==0:
if changes1%2==0 and changes2%2!=0:
continue
else:
ans1+=1
else:
if changes1%2!=0 and changes2%2==0:
continue
else:
ans1+=1
#odd,even
ans2=0
for i in range(m):
changes1 = 0
changes2 = 0
if matrix[0][i] == '1':
changes1 += 1
if matrix[1][i] == '1':
changes1 += 1
changes2 += 1
if matrix[2][i] == '1':
changes2 += 1
if i % 2 == 0:
if changes1 % 2 != 0 and changes2 % 2 == 0:
continue
else:
ans2 += 1
else:
if changes1 % 2 == 0 and changes2 % 2 != 0:
continue
else:
ans2 += 1
ans3=0
#even,even
for i in range(m):
changes1 = 0
changes2 = 0
if matrix[0][i] == '1':
changes1 += 1
if matrix[1][i] == '1':
changes1 += 1
changes2 += 1
if matrix[2][i] == '1':
changes2 += 1
if i % 2 == 0:
if changes1 % 2 == 0 and changes2 % 2 == 0:
continue
else:
ans3 += 1
else:
if changes1 % 2 != 0 and changes2 % 2 != 0:
continue
else:
ans3 += 1
#odd,odd
ans4=0
for i in range(m):
changes1 = 0
changes2 = 0
if matrix[0][i] == '1':
changes1 += 1
if matrix[1][i] == '1':
changes1 += 1
changes2 += 1
if matrix[2][i] == '1':
changes2 += 1
if i % 2 == 0:
if changes1 % 2 != 0 and changes2 % 2 != 0:
continue
else:
ans4 += 1
else:
if changes1 % 2 == 0 and changes2 % 2 == 0:
continue
else:
ans4 += 1
print(min(ans1,ans2,ans3,ans4))
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
#threadin.Thread(target=main).start()
``` | output | 1 | 83,579 | 12 | 167,159 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 β€ n β€ m β€ 10^6 and nβ
m β€ 10^6) β the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions β
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters β r_1, r_2, c_1, and c_2; here, 1 β€ r_1 β€ r_2 β€ n and 1 β€ c_1 β€ c_2 β€ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 β€ i β€ r_2 and c_1 β€ j β€ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2. | instruction | 0 | 83,580 | 12 | 167,160 |
Tags: bitmasks, brute force, constructive algorithms, dp, greedy, implementation
Correct Solution:
```
from itertools import chain, product
def find_cells_to_fix_n(matrix):
if len(matrix) < 2 or len(matrix[0]) < 2:
return 0
if not (len(matrix) <= len(matrix[0])):
matrix = tuple( zip(*matrix) )
len_y, len_x = len(matrix), len(matrix[0]) # len_y <= len_x
if len_y >= 4:
result = None
elif len_y == 3:
result = _solve_for_3(matrix)
elif len_y == 2:
result = _solve_for_2(matrix)
else:
raise AssertionError("can't get here")
return result
def _compress_2xn(matrix):
assert len(matrix) == 2
parity_line = tuple(map(int.__add__, *matrix))
parity_line = map(int.__add__, parity_line, parity_line[1:])
parity_line = map((2).__rmod__, parity_line)
parity_line = tuple(parity_line)
return parity_line
def _compress_3xn(matrix):
assert len(matrix) == 3
first_row = _compress_2xn(matrix[0:2])
second_row = _compress_2xn(matrix[1:3])
result = (first_row, second_row)
return result
def _get_zeros_pairs(zeros_positions, line_length, is_first_towards_left):
if not zeros_positions:
return []
if is_first_towards_left:
zeros_pairs = [(-1, zeros_positions[0])]
else:
zeros_pairs = []
i_start_zero = 1 if is_first_towards_left else 0
for i_first_zero in range(i_start_zero, len(zeros_positions), 2):
i_second_zero = i_first_zero + 1
first_zero_pos = zeros_positions[i_first_zero]
if i_second_zero < len(zeros_positions):
second_zero_pos = zeros_positions[i_second_zero]
else:
second_zero_pos = line_length
zeros_pairs.append((first_zero_pos, second_zero_pos))
return zeros_pairs
def _find_zeros_pairs_costs(zeros_pairs):
return sum(second_pos - first_pos for first_pos, second_pos in zeros_pairs)
def _solve_for_2(matrix):
parity_line = _compress_2xn(matrix)
zeros_positions = tuple(i for i, value in enumerate(parity_line) if value == 0)
line_length = len(parity_line)
min_cost = min(
_how_much_costs_tactic_for_2xn(zeros_positions, line_length, is_first_towards_left)
for is_first_towards_left in (True, False)
)
return min_cost
def _how_much_costs_tactic_for_2xn(zeros_positions, line_length, is_first_towards_left):
zeros_pairs = _get_zeros_pairs(zeros_positions, line_length, is_first_towards_left)
return _find_zeros_pairs_costs(zeros_pairs)
def _solve_for_3(matrix):
parity_lines = _compress_3xn(matrix)
zeros_positions = tuple(
tuple(i for i, value in enumerate(parity_line) if value == 0)
for parity_line in parity_lines
)
line_length = len(parity_lines[0])
min_cost = min(
_how_much_costs_tactic_for_3xn(zeros_positions, line_length, is_first_towards_left)
for is_first_towards_left in product((True, False), repeat=2)
)
return min_cost
def _how_much_costs_tactic_for_3xn(zeros_positions, line_length, is_first_towards_left):
zeros_pairs = tuple(
_get_zeros_pairs(_zeros_positions, line_length, _is_first_towards_left)
for _zeros_positions, _is_first_towards_left in zip(zeros_positions, is_first_towards_left)
)
first_row_pairs, second_row_pairs = zeros_pairs
walked = [0] * (line_length+1)
for pair_first, pair_last in chain(first_row_pairs, second_row_pairs):
for i in range(pair_first, pair_last):
walked[i+1] = 1
return sum(walked)
def main():
n, m = map(int, input().split())
matrix = tuple(
tuple(map(int, input()))
for i in range(n)
)
cells_to_fix_n = find_cells_to_fix_n(matrix)
if cells_to_fix_n is None:
cells_to_fix_n = -1
print(cells_to_fix_n)
if __name__ == '__main__':
main()
``` | output | 1 | 83,580 | 12 | 167,161 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,589 | 12 | 167,178 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
input=sys.stdin.readline
INF=10**18
t=int(input())
for _ in range(t):
n=int(input())
arr=list(map(int,input().split()))
tmp=[[-1] for _ in range(n+1)]
for i in range(n):
tmp[arr[i]].append(i)
mins=[INF]*(n+1)
for i in range(1,n+1):
if tmp[i][-1]==-1:
continue
cnt=0
prev=tmp[i][0]
for val in tmp[i][1:]:
cnt=max(cnt,val-prev)
prev=val
cnt=max(cnt,n-prev)
mins[cnt]=min(mins[cnt],i)
tmin=INF
for i in range(1,n+1):
tmin=min(tmin,mins[i])
if tmin==INF:
mins[i]=-1
else:
mins[i]=tmin
print(*mins[1:])
``` | output | 1 | 83,589 | 12 | 167,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,590 | 12 | 167,180 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
from collections import defaultdict
for _ in range(int(input())):
n=int(input())
arr=list(map(int,input().split()))
d=defaultdict(list)
l=[]
for i in range(n):
d[arr[i]].append(i)
for i in d:
ma=d[i][0]+1
c=d[i][0]
for j in d[i][1:]:
ma=max(ma,j-c)
c=j
ma=max(ma,n-d[i][-1])
l.append([ma,i])
l.append([n+1,100000000000])
ans=[-1]*(n)
l.sort(key=lambda x:(x[0],-x[1]))
mi=10000000000000
for i in range(len(l)-1):
if l[i+1][0]==l[i][0]:
continue
mi = min(mi, l[i][1])
for j in range(l[i][0],l[i+1][0]):
ans[j-1]=mi
print(*ans)
``` | output | 1 | 83,590 | 12 | 167,181 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,591 | 12 | 167,182 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def output(*args):
sys.stdout.buffer.write(
('\n'.join(map(str, args)) + '\n').encode('utf-8')
)
def main():
t = int(input())
ans_a = [''] * t
inf = 10**9
for ti in range(t):
n = int(input())
a = list(map(int, input().split()))
longest = [-1] * (n + 1)
prev = [-1] * (n + 1)
for i, x in enumerate(a):
longest[x] = max(longest[x], i - prev[x])
prev[x] = i
ans = [inf] * (n + 2)
for i in range(n, 0, -1):
longest[i] = max(longest[i], n - prev[i])
ans[longest[i]] = i
for i in range(1, n + 1):
ans[i + 1] = min(ans[i], ans[i + 1])
if ans[i] == inf:
ans[i] = -1
ans_a[ti] = ' '.join(map(str, ans[1:-1]))
output(*ans_a)
if __name__ == '__main__':
main()
``` | output | 1 | 83,591 | 12 | 167,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,592 | 12 | 167,184 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
import sys
sys.setrecursionlimit(1000000)
T = int(input())
for _ in range(T):
n = int(input())
a = [int(x) - 1 for x in input().split()]
prev = [-1 for _ in range(n)]
val = [1 for _ in range(n)]
for i, x in enumerate(a):
delta = i - prev[x]
val[x] = max(val[x], delta)
prev[x] = i
for i in range(n):
val[i] = max(val[i], n - prev[i])
ans = [-1 for _ in range(n + 1)]
r = n + 1
for i in range(n):
if val[i] < r:
for j in range(val[i], r):
ans[j] = i + 1
r = val[i]
print(' '.join([str(x) for x in ans[1:]]))
``` | output | 1 | 83,592 | 12 | 167,185 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,593 | 12 | 167,186 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
# import numpy as npy
# idx=sorted(idx,key=functools.cmp_to_key(cmpx))
import bisect
import array
import functools
import math
T=int(input())
for Tid in range(T):
n=int(input())
a=array.array('i',map(int,input().split()))
b=array.array('i',[0 for i in range(n+2)])
la=array.array('i',[-1 for i in range(n+2)])
for i in range(n):
b[a[i]]=max(b[a[i]],i-la[a[i]]);
la[a[i]]=i
for i in range(1,n+1):
b[i]=max(b[i],n-la[i]);
ans=array.array('i',[1<<30 for i in range(n+2)])
for i in range(1,n+1):
ans[b[i]]=min(ans[b[i]],i);
for i in range(1,n+1):
ans[i]=min(ans[i],ans[i-1])
for i in range(1,n+1):
if ans[i]==1<<30:
ans[i]=-1;
print(ans[i],end=' ');
print()
``` | output | 1 | 83,593 | 12 | 167,187 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,594 | 12 | 167,188 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
fullans = ''
for _ in range(int(input())):
n = int(input())
ls = list(map(int, input().split()))
ar = [[] for i in range(n)]
ans = [None] * n
for i in range(n):
ar[ls[i] - 1].append(i)
for j in range(n):
if len(ar[j]) == 0:
continue
mn = ar[j][0]
for i in range(1, len(ar[j])):
mn = max(ar[j][i] - ar[j][i - 1] - 1, mn)
mn = max(n - 1 - ar[j][-1], mn)
if ans[mn] is None:
ans[mn] = j + 1
# print(ans)
curr = -1
for i in range(n):
if ans[i] is None:
ans[i] = curr
else:
if curr == -1:
curr = ans[i]
else:
curr = min(ans[i], curr)
ans[i] = curr
for i in ans:
print(i, end=' ')
print()
``` | output | 1 | 83,594 | 12 | 167,189 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,595 | 12 | 167,190 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
from bisect import *
from collections import *
from math import *
from heapq import *
from typing import List
from itertools import *
from operator import *
from functools import *
import sys
'''
@lru_cache(None)
def fact(x):
if x<2:
return 1
return fact(x-1)*x
@lru_cache(None)
def per(i,j):
return fact(i)//fact(i-j)
@lru_cache(None)
def com(i,j):
return per(i,j)//fact(j)
def linc(f,t,l,r):
while l<r:
mid=(l+r)//2
if t>f(mid):
l=mid+1
else:
r=mid
return l
def rinc(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t<f(mid):
r=mid-1
else:
l=mid
return l
def ldec(f,t,l,r):
while l<r:
mid=(l+r)//2
if t<f(mid):
l=mid+1
else:
r=mid
return l
def rdec(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t>f(mid):
r=mid-1
else:
l=mid
return l
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def power2(n):
while not n&1:
n>>=1
return n==1
'''
from time import time
#t=int(input())
start=time()
t=1000
'''
for i in range(t):
#n,m=map(int,input().split())
n,m=1,0
graph=defaultdict(set)
for i in range(m):
u,v=map(int,input().split())
graph[u-1].add(v-1)
visited=[0]*n
ans=[]
def delchild(u):
for child in graph[u]:
visited[child]=1
ans.append(child+1)
for i in range(n):
if not visited[i]:
children=graph[i]
if len(children)==1:
u=children.pop()
visited[u]=1
delchild(u)
elif len(children)==2:
u=children.pop()
v=children.pop()
if u in graph[v]:
delchild(v)
visited[v]=1
elif v in graph[u]:
delchild(u)
visited[u]=1
else:
delchild(u)
delchild(v)
visited[u]=visited[v]=1
print(len(ans))
sys.stdout.flush()
print(' '.join(map(str,ans)))
sys.stdout.flush()
'''
t=int(input())
for i in range(t):
n=int(input())
arr=list(map(int,input().split()))
last=defaultdict(lambda:-1)
d=Counter()
for i,a in enumerate(arr):
tmp=i-last[a]
last[a]=i
d[a]=max(d[a],tmp)
for a in last:
d[a]=max(d[a],n-last[a])
ans=[inf]*n
#print('d',d)
for a in d:
ans[d[a]-1]=min(ans[d[a]-1],a)
for j in range(1,n):
ans[j]=min(ans[j-1],ans[j])
for j in range(n):
if ans[j]==inf:
ans[j]=-1
ans=map(str,ans)
print(' '.join(ans))
``` | output | 1 | 83,595 | 12 | 167,191 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1 | instruction | 0 | 83,596 | 12 | 167,192 |
Tags: binary search, data structures, implementation, two pointers
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
last = [-1 for i in range(n+1)]
gap = [0 for i in range(n+1)]
for i in range(n):
gap[a[i]] = max(gap[a[i]],i-last[a[i]])
last[a[i]] = i
for v in range(1,n+1):
gap[v] = max(gap[v],n-last[v])
INF = 10**9
# smallest v such that gap[v] < k
candidates = [INF for i in range(n+2)]
for v in range(1,n+1):
candidates[gap[v]] = min(candidates[gap[v]], v)
ans = INF
for k in range(1,n+1):
ans = min(ans,candidates[k])
print(-1 if ans==INF else ans,end=' ')
print()
``` | output | 1 | 83,596 | 12 | 167,193 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
last = [-1 for _ in range(n)]
maxlen = [-1 for _ in range(n)]
for i in range(n):
x = a[i]-1
maxlen[x] = max(maxlen[x],i - last[x])
last[x] = i
maxlen = [max(maxlen[i], n-last[i]) for i in range(n)]
#print(maxlen)
sol = [-1 for _ in range(n+2)]
for i in range(n):
if sol[maxlen[i]] == -1:
sol[maxlen[i]] = i+1
#print(sol)
sol = sol[1:len(sol)-1]
if sol[0] == -1: sol[0] = n+2
for i in range(1,n):
if sol[i]==-1:
sol[i] = n+2
sol[i] = min(sol[i], sol[i-1])
for i in range(n):
if sol[i]==n+2:
sol[i]=-1
else: break
print(' '.join([str(i) for i in sol]))
``` | instruction | 0 | 83,597 | 12 | 167,194 |
Yes | output | 1 | 83,597 | 12 | 167,195 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
import math
T = int(input())
#lets = 'abcdefghijklmnopqrstuvwxyz'
#key = {lets[i]:i for i in range(26)}
for t in range(T):
n = int(input())
#n,k = map(int,input().split())
a = list(map(int,input().split()))
#a = input()
d = False
m = [0]*(n+1)
lm = [-1]*(n+1)
app = set(a)
for i in range(n):
m[a[i]] = max(m[a[i]],i-lm[a[i]])
lm[a[i]] = i
for i in app:
if m[i]!=0:
m[i] = max(m[i],n-lm[i])
ans = [-1]*n
for i in app:
if ans[m[i]-1] != -1:
ans[m[i]-1] = min(ans[m[i]-1],i)
else:
ans[m[i]-1] = i
#print(m)
#print(ans)
nn = 1
mmm = n+1
for i in range(n):
if ans[i] != -1:
nn = 0
if nn:
ans[i] = '-1'
else:
if ans[i] != -1:
mmm = min(mmm,ans[i])
#print(mmm)
ans[i] = str(mmm)
print(' '.join(ans))
``` | instruction | 0 | 83,598 | 12 | 167,196 |
Yes | output | 1 | 83,598 | 12 | 167,197 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
from sys import stdin
input=stdin.readline
def A():
t=int(input())
for _ in range(t):
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))
tot=0
for i in range(1, n):
tot += (k-a[i])//a[0]
print(tot)
def B():
from collections import defaultdict
t=int(input())
for _ in range(t):
n,T=map(int,input().split())
a=list(map(int,input().split()))
d=defaultdict()
colors=[0]*n
for i in range(n):
colors[i] = d.get(a[i],0)
d[T-a[i]] = (1 + colors[i]) % 2
print(*colors)
def C():
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
nums=[[] for i in range(n+1)]
for i in range(n):
nums[a[i]].append(i)
s=[300001]*(n+1)
for i in range(len(nums)):
if len(nums[i]) > 0:
maxdist=max(nums[i][0]+1,n-nums[i][-1])
for j in range(1,len(nums[i])):
maxdist=max(maxdist,nums[i][j]-nums[i][j-1])
s[maxdist] = min(s[maxdist],i)
ans=[s[1]]*n
for i in range(1,n):
ans[i] = min(ans[i-1],s[i+1])
for i in range(n):
if ans[i]==300001: ans[i] = -1
print(*ans)
C()
``` | instruction | 0 | 83,599 | 12 | 167,198 |
Yes | output | 1 | 83,599 | 12 | 167,199 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
from sys import stdin, stdout
outputs = []
t = int(stdin.readline().strip())
for __ in range(t):
n = int(stdin.readline().strip())
res = [n+2] * n
arr = [int(num) for num in stdin.readline().strip().split()]
req = [[-1, -1, -1] for i in range(n+1)]
#print(req)
for i in range(n):
if req[arr[i]][0] == -1:
req[arr[i]][0] = i
req[arr[i]][1] = max(i+1, n-i)
req[arr[i]][2] = 1
elif req[arr[i]][2] == 1:
req[arr[i]][2] += 1
req[arr[i]][1] = max(req[arr[i]][0]+1, i-req[arr[i]][0])
req[arr[i]][0] = i
else:
req[arr[i]][2] += 1
req[arr[i]][1] = max(req[arr[i]][1], i - req[arr[i]][0])
req[arr[i]][0] = i
#print(req)
for i in range(n):
if req[arr[i]][2] > 1:
req[arr[i]][1] = max(req[arr[i]][1], n - req[arr[i]][0])
for i in range(n):
res[req[arr[i]][1]-1] = min(arr[i], res[req[arr[i]][1]-1])
i = 0
while i<n and res[i] == n+2:
res[i] = '-1'
i += 1
cur_minm = -1 if i>n else res[i]
while i < n:
cur_minm = min(cur_minm, res[i])
res[i] = f'{cur_minm}'
i += 1
outputs.append(' '.join(res))
for output in outputs:
stdout.write(output+'\n')
``` | instruction | 0 | 83,600 | 12 | 167,200 |
Yes | output | 1 | 83,600 | 12 | 167,201 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
import sys
import os
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
def k_amz(list_size, input_list):
dists = dict()
last_index = dict()
min_dist = (list_size + 1 )/ 2 if list_size % 2 else (list_size / 2 + 1)
for ind, val in enumerate(input_list):
if val not in last_index:
dists[val] = max(list_size - ind - 1, ind)
last_index[val] = ind
else:
current_dist = ind - last_index[val] - 1
dists[val] = max(current_dist, list_size - ind - 1)
last_index[val] = ind
if min_dist > dists[val]:
min_dist = dists[val]
output = []
cur_min = -1
for k in range(1, list_size + 1):
if k < min_dist:
output.append(-1)
else:
for key, val in dists.items():
if val < k:
if cur_min == -1 or cur_min > key:
cur_min = key
output.append(cur_min)
out = ' '.join(list((map(str, output))))
print(out)
# get number of testcases
testcases = inp()
while testcases > 0:
# read each testcase
number_of_int = inp()
int_list = inlt()
k_amz(number_of_int, int_list)
testcases -= 1
``` | instruction | 0 | 83,601 | 12 | 167,202 |
No | output | 1 | 83,601 | 12 | 167,203 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
from collections import defaultdict
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
d = defaultdict(list)
for i in range(n):
d[a[i]].append(i+1)
s = [-1]*(n+1)
for i in d.keys():
for k in range(1,n+1):
l = set(d[i])
p = list(d[i])
if p[-1]<n-k+1:
s[k]=-1
else:
pre = 0
b = 0
size = len(l)
for j in l:
if k==1:
if size==n:
s[k]=j
break
else:
break
else:
val = j-pre
# print('*',val)
if val>k :
b = 1
# print('/')
break
pre = j
if b==0:
if s[k]==-1:
s[k]=i
elif s[k]>i:
s[k]=i
print(s[1:])
``` | instruction | 0 | 83,602 | 12 | 167,204 |
No | output | 1 | 83,602 | 12 | 167,205 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 28 06:18:46 2020
@author: Dark Soul
"""
t=int(input(''))
for _ in range(t):
n=int(input(''))
arr1=list(map(int,input().split()))
f=[0]*(n+1)
arr=[0]
for i in arr1:
arr.append(i)
last=[0]*(n+1)
ans=[-1]*(n+1)
for i in range(1,n+1):
x=arr[i]
f[x]=max(f[x],i-last[x])
last[x]=i
for x in range(1,n+1):
f[x]=max(f[x],n-last[x]+1)
print(f[x],x)
for i in range(f[x],n+1):
if ans[i]!=-1:
break
ans[i]=x
print(ans,f[x])
print(*ans[1:])
``` | instruction | 0 | 83,603 | 12 | 167,206 |
No | output | 1 | 83,603 | 12 | 167,207 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there is no integer occuring in all subsegments of length k for some value of k, then the k-amazing number is -1.
For each k from 1 to n calculate the k-amazing number of the array a.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 3 β
10^5.
Output
For each test case print n integers, where the i-th integer is equal to the i-amazing number of the array.
Example
Input
3
5
1 2 3 4 5
5
4 4 4 4 2
6
1 3 1 5 3 1
Output
-1 -1 3 2 1
-1 4 4 4 2
-1 -1 1 1 1 1
Submitted Solution:
```
def solve():
n = int(input().strip())
# a, b = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
last_occur = [-1 for i in range(n+1)]
max_dist = [0 for i in range(n+1)]
max_dist[0] = n
ans = [-1 for i in range(n)]
for i in range(n):
elem = a[i]
dist = i - last_occur[elem] - 1
max_dist[elem] = max(dist, max_dist[elem])
last_occur[elem] = i
for i in range(n):
elem = i+1
dist = n - last_occur[elem] - 1
max_dist[elem] = max(dist, max_dist[elem])
# print(*max_dist)
min_dist_elem = 0
for elem in range(1, n+1):
if(max_dist[elem]<max_dist[min_dist_elem]):
min_dist_elem = elem
for dist in range(max_dist[min_dist_elem], n):
ans[dist] = min_dist_elem
print(*ans)
t = int(input().strip())
for _ in range(t):
solve()
``` | instruction | 0 | 83,604 | 12 | 167,208 |
No | output | 1 | 83,604 | 12 | 167,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k subsegments.
Let's see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1, 2, 3, 4, 5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below:
* [1], [2], [3, 4, 5];
* [1], [2, 3], [4, 5];
* [1], [2, 3, 4], [5];
* [1, 2], [3], [4, 5];
* [1, 2], [3, 4], [5];
* [1, 2, 3], [4], [5].
Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each query, print the answer to it. If it is impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as k integers r_1, r_2, ..., r_k such that 1 β€ r_1 < r_2 < ... < r_k = n, where r_j is the right border of the j-th segment (the index of the last element that belongs to the j-th segment), so the array is divided into subsegments [1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], ..., [r_{k - 1} + 1, n]. Note that r_k is always n but you should print it anyway.
Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO | instruction | 0 | 84,305 | 12 | 168,610 |
Tags: constructive algorithms, math
Correct Solution:
```
(lambda q: print('\n'.join(map(str, ((lambda n, k: (
lambda r: "NO" if len(r) < k or ((k & 1) != (len(r) & 1)) else "YES\n" + ' '.join(r[:k - 1]) + ' ' + str(n))(
[str(i + 1) for i, x in enumerate(input().split()) if int(x) & 1]))(*map(int, input().split())) for _ in
range(q))))))(int(input()))
``` | output | 1 | 84,305 | 12 | 168,611 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k subsegments.
Let's see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1, 2, 3, 4, 5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below:
* [1], [2], [3, 4, 5];
* [1], [2, 3], [4, 5];
* [1], [2, 3, 4], [5];
* [1, 2], [3], [4, 5];
* [1, 2], [3, 4], [5];
* [1, 2, 3], [4], [5].
Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each query, print the answer to it. If it is impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as k integers r_1, r_2, ..., r_k such that 1 β€ r_1 < r_2 < ... < r_k = n, where r_j is the right border of the j-th segment (the index of the last element that belongs to the j-th segment), so the array is divided into subsegments [1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], ..., [r_{k - 1} + 1, n]. Note that r_k is always n but you should print it anyway.
Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO | instruction | 0 | 84,306 | 12 | 168,612 |
Tags: constructive algorithms, math
Correct Solution:
```
from itertools import accumulate
from sys import stdin, stdout
readline = stdin.readline
write = stdout.write
q = int(readline())
for query in range(q):
n, k = map(int, readline().split())
a = [int(x) % 2 for x in readline().split()]
sum = list(accumulate(a))
if sum[-1] >= k and (sum[-1] - k) % 2 == 0:
b = [i + 1 for i in range(n) if a[i]]
b[k - 1] = n
print('YES')
for i in range(k):
print(b[i], end=' ')
print()
else:
print('NO')
``` | output | 1 | 84,306 | 12 | 168,613 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k subsegments.
Let's see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1, 2, 3, 4, 5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below:
* [1], [2], [3, 4, 5];
* [1], [2, 3], [4, 5];
* [1], [2, 3, 4], [5];
* [1, 2], [3], [4, 5];
* [1, 2], [3, 4], [5];
* [1, 2, 3], [4], [5].
Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each query, print the answer to it. If it is impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as k integers r_1, r_2, ..., r_k such that 1 β€ r_1 < r_2 < ... < r_k = n, where r_j is the right border of the j-th segment (the index of the last element that belongs to the j-th segment), so the array is divided into subsegments [1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], ..., [r_{k - 1} + 1, n]. Note that r_k is always n but you should print it anyway.
Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO | instruction | 0 | 84,307 | 12 | 168,614 |
Tags: constructive algorithms, math
Correct Solution:
```
for _ in range(int(input())):
a,b = map(int,input().split())
l = list(map(int,input().split()))
v =[]
for i in range(len(l)):
if l[i]%2!=0:v.append(i+1)
if len(v)>=b and (len(v)-b)%2==0:
print("YES")
for i in range(b-1):
print(v[i],end =" ")
print(a)
else:print("NO")
``` | output | 1 | 84,307 | 12 | 168,615 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k subsegments.
Let's see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1, 2, 3, 4, 5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below:
* [1], [2], [3, 4, 5];
* [1], [2, 3], [4, 5];
* [1], [2, 3, 4], [5];
* [1, 2], [3], [4, 5];
* [1, 2], [3, 4], [5];
* [1, 2, 3], [4], [5].
Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each query, print the answer to it. If it is impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as k integers r_1, r_2, ..., r_k such that 1 β€ r_1 < r_2 < ... < r_k = n, where r_j is the right border of the j-th segment (the index of the last element that belongs to the j-th segment), so the array is divided into subsegments [1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], ..., [r_{k - 1} + 1, n]. Note that r_k is always n but you should print it anyway.
Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO | instruction | 0 | 84,308 | 12 | 168,616 |
Tags: constructive algorithms, math
Correct Solution:
```
def solve(arr,n,k,ans):
odd = []
for i in range(n):
if arr[i]%2 != 0:
odd.append(i)
if len(odd) < k or (len(odd)-k+1)%2 == 0:
ans.append(['NO'])
return
query = ['YES']
indices = ''
for i in range(k-1):
indices += str(odd[i]+1)+' '
indices += str(n)
query.append(indices)
ans.append(query)
def main():
ans = []
q = int(input())
for i in range(q):
n,k = map(int,input().split())
arr = list(map(int,input().split()))
solve(arr,n,k,ans)
for i in ans:
for j in i:
print(j)
main()
``` | output | 1 | 84,308 | 12 | 168,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k subsegments.
Let's see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1, 2, 3, 4, 5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below:
* [1], [2], [3, 4, 5];
* [1], [2, 3], [4, 5];
* [1], [2, 3, 4], [5];
* [1, 2], [3], [4, 5];
* [1, 2], [3, 4], [5];
* [1, 2, 3], [4], [5].
Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each query, print the answer to it. If it is impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as k integers r_1, r_2, ..., r_k such that 1 β€ r_1 < r_2 < ... < r_k = n, where r_j is the right border of the j-th segment (the index of the last element that belongs to the j-th segment), so the array is divided into subsegments [1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], ..., [r_{k - 1} + 1, n]. Note that r_k is always n but you should print it anyway.
Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO | instruction | 0 | 84,309 | 12 | 168,618 |
Tags: constructive algorithms, math
Correct Solution:
```
q = int(input())
for ii in range(q):
n, k = [int(y) for y in input().split()]
lst = [int(x) for x in input().split()]
count, result = 0, list()
for i in lst:
if i % 2 != 0:
count += 1
if k % 2 == 0:
if count % 2 != 0:
print("NO")
continue
if k % 2 != 0:
if count % 2 == 0:
print("NO")
continue
if k > count:
print("NO")
continue
print("YES")
if k == 1:
print(n)
continue
for i in range(n):
if lst[i] % 2 != 0:
result.append(i + 1)
k -= 1
if k == 1:
result.append(n)
break
print(*result)
``` | output | 1 | 84,309 | 12 | 168,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k subsegments.
Let's see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1, 2, 3, 4, 5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below:
* [1], [2], [3, 4, 5];
* [1], [2, 3], [4, 5];
* [1], [2, 3, 4], [5];
* [1, 2], [3], [4, 5];
* [1, 2], [3, 4], [5];
* [1, 2, 3], [4], [5].
Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 2 β
10^5) β the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the i-th element of a.
It is guaranteed that the sum of n over all queries does not exceed 2 β
10^5 (β n β€ 2 β
10^5).
Output
For each query, print the answer to it. If it is impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as k integers r_1, r_2, ..., r_k such that 1 β€ r_1 < r_2 < ... < r_k = n, where r_j is the right border of the j-th segment (the index of the last element that belongs to the j-th segment), so the array is divided into subsegments [1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], ..., [r_{k - 1} + 1, n]. Note that r_k is always n but you should print it anyway.
Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO | instruction | 0 | 84,310 | 12 | 168,620 |
Tags: constructive algorithms, math
Correct Solution:
```
import sys
input = sys.stdin.readline
q=int(input())
n,k=0,0
a=0
s,t=0,0
b=0
j=0
for i in range(q):
n,k=map(int,input().split())
#a=list(map(int,input().split()))
a=[int(j)%2 for j in input().split()]
s=sum(a)
b=[]
if(s>=k and ((s-k)%2)==0):
print('YES')
for j in range(n):
if(k==1):
break
if(a[j]==1):
k-=1
b.append(j+1)
b.append(n)
print(*b)
else:
print('NO')
``` | output | 1 | 84,310 | 12 | 168,621 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.