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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!
You are given n nonnegative integers a_1, a_2, ..., a_n arranged in a circle, where n must be odd (ie. n-1 is divisible by 2). Formally, for all i such that 2 β€ i β€ n, the elements a_{i - 1} and a_i are considered to be adjacent, and a_n and a_1 are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.
Help Danny find the maximum possible circular value after some sequences of operations.
Input
The first line contains one odd integer n (1 β€ n < 2 β
10^5, n is odd) β the initial size of the circle.
The second line contains n integers a_{1},a_{2},...,a_{n} (0 β€ a_{i} β€ 10^9) β the initial numbers in the circle.
Output
Output the maximum possible circular value after applying some sequence of operations to the given circle.
Examples
Input
3
7 10 2
Output
17
Input
1
4
Output
4
Note
For the first test case, here's how a circular value of 17 is obtained:
Pick the number at index 3. The sum of adjacent elements equals 17. Delete 7 and 10 from the circle and replace 2 with 17.
Note that the answer may not fit in a 32-bit integer.
Submitted Solution:
```
import heapq
def solve(arr):
even_indicies = arr[0:len(arr):2]
odd_indicies = arr[1:len(arr):2]
modified_arr = even_indicies + odd_indicies + even_indicies
sum_len = (len(arr) + 1)//2
summation = sum(modified_arr[:sum_len])
max_sum = summation
for i in range(len(arr)-1):
summation = summation - modified_arr[i] + modified_arr[i+sum_len]
max_sum = max(summation, max_sum)
return max_sum
n = int(input())
arr = [int(i) for i in input().split(' ')]
print(solve(arr))
``` | instruction | 0 | 5,951 | 12 | 11,902 |
Yes | output | 1 | 5,951 | 12 | 11,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!
You are given n nonnegative integers a_1, a_2, ..., a_n arranged in a circle, where n must be odd (ie. n-1 is divisible by 2). Formally, for all i such that 2 β€ i β€ n, the elements a_{i - 1} and a_i are considered to be adjacent, and a_n and a_1 are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.
Help Danny find the maximum possible circular value after some sequences of operations.
Input
The first line contains one odd integer n (1 β€ n < 2 β
10^5, n is odd) β the initial size of the circle.
The second line contains n integers a_{1},a_{2},...,a_{n} (0 β€ a_{i} β€ 10^9) β the initial numbers in the circle.
Output
Output the maximum possible circular value after applying some sequence of operations to the given circle.
Examples
Input
3
7 10 2
Output
17
Input
1
4
Output
4
Note
For the first test case, here's how a circular value of 17 is obtained:
Pick the number at index 3. The sum of adjacent elements equals 17. Delete 7 and 10 from the circle and replace 2 with 17.
Note that the answer may not fit in a 32-bit integer.
Submitted 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
n=int(data())
a=mdata()
odd=[0]
even=[0]
for i in range(0,n-1,2):
odd.append(odd[-1]+a[i])
even.append(even[-1]+a[i+1])
odd.append(a[-1])
ans=sum(odd)
for i in range(1,n//2+1):
k=odd[i]+even[-1]-even[i-1]
ans=max(ans,k)
k=odd[-1]-odd[n//2-i-1]+even[n//2-i]
ans=max(ans,k)
out(ans)
``` | instruction | 0 | 5,954 | 12 | 11,908 |
No | output | 1 | 5,954 | 12 | 11,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!
You are given n nonnegative integers a_1, a_2, ..., a_n arranged in a circle, where n must be odd (ie. n-1 is divisible by 2). Formally, for all i such that 2 β€ i β€ n, the elements a_{i - 1} and a_i are considered to be adjacent, and a_n and a_1 are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.
Help Danny find the maximum possible circular value after some sequences of operations.
Input
The first line contains one odd integer n (1 β€ n < 2 β
10^5, n is odd) β the initial size of the circle.
The second line contains n integers a_{1},a_{2},...,a_{n} (0 β€ a_{i} β€ 10^9) β the initial numbers in the circle.
Output
Output the maximum possible circular value after applying some sequence of operations to the given circle.
Examples
Input
3
7 10 2
Output
17
Input
1
4
Output
4
Note
For the first test case, here's how a circular value of 17 is obtained:
Pick the number at index 3. The sum of adjacent elements equals 17. Delete 7 and 10 from the circle and replace 2 with 17.
Note that the answer may not fit in a 32-bit integer.
Submitted Solution:
```
def getn(arr , i):
if i == len(arr) -1:
i = -1
return arr[i-1] , arr[i+1]
n = int(input())
data = list(map(int , input().split()))
index = dict()
for i in range(len(data)):
index[data[i]] = i
sdata = sorted(data)
lisp = set()
total = 0
c = (n-1)//2
for i in range(len(sdata)):
if c>0:
if sdata[i] not in lisp:
ind = index[sdata[i]]
n1 , n2 = getn(data , ind)
lisp.add(n1)
lisp.add(n2)
total += sdata[i]
c-=1
print(sum(data) - total)
``` | instruction | 0 | 5,957 | 12 | 11,914 |
No | output | 1 | 5,957 | 12 | 11,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,172 | 12 | 12,344 |
Tags: dp, implementation, two pointers
Correct Solution:
```
import math;
from math import log2,sqrt;
import sys;
sys.setrecursionlimit(pow(10,6))
import collections
from collections import defaultdict
from statistics import median
inf = float("inf")
mod=pow(10,9)+7
def gen_left(a):
l=[0]*n;
for i in range(1,len(l)):
if a[i]>a[i-1]:
l[i]=l[i-1]+1;
return l;
def gen_right(a):
r=[0]*n;
for i in reversed(range(len(l)-1)):
if a[i]<a[i+1]:
r[i]=r[i+1]+1
return r;
def generate(a,l,r):
ans=0
for i in range(n):
if i==0:
ans=max(ans,r[i+1]+1+1)
elif i==n-1:
ans=max(ans,l[n-2]+1+1)
elif a[i-1]+1<a[i+1]:
ans=max(ans,r[i+1]+1+l[i-1]+1+1)
else:
ans=max(ans,r[i+1]+1+1,l[i-1]+1+1)
return ans;
t=1;
for i in range(t):
n=int(input())
a = list(map(int, input().split()))
if n==1:
print(1)
continue
if n==2:
print(2)
continue
l=gen_left(a)
r=gen_right(a)
ans=generate(a,l,r)
print(ans)
``` | output | 1 | 6,172 | 12 | 12,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,173 | 12 | 12,346 |
Tags: dp, implementation, two pointers
Correct Solution:
```
# import math
n = int(input())
arr = [int(var) for var in input().split()]
maxLeft = [1 for _ in range(n)]
maxRight = [1 for _ in range(n)]
ans = 1
# maxLeft[0], maxRight[-1] = 1, 1
for i in range(1, n):
if arr[i] > arr[i-1]:
maxLeft[i] = maxLeft[i-1]+1
for i in reversed(range(n-1)):
if arr[i] < arr[i+1]:
maxRight[i] = maxRight[i+1] + 1
# DEBUG
# print(arr)
# print(maxRight)
# print(maxLeft)
for i in range(n):
prev, next = arr[i-1] if i-1 >= 0 else -float("inf"), arr[i+1] if i+1 < n else float("inf")
leftSubArray = maxLeft[i-1] if i-1 >= 0 else 0
rightSubArray = maxRight[i+1] if i+1 < n else 0
if not(prev < arr[i] < next):
if next - prev >= 2:
ans = max(ans, rightSubArray+leftSubArray+1)
else:
ans = max(ans, leftSubArray+1, 1+rightSubArray)
else:
ans = max(ans, rightSubArray + leftSubArray + 1)
print(ans)
``` | output | 1 | 6,173 | 12 | 12,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,174 | 12 | 12,348 |
Tags: dp, implementation, two pointers
Correct Solution:
```
n=int(input())
l=list(map(int,input().split(" ")))
a=[1]*n
b=[1]*n
for i in range(1,n):
if l[i]>l[i-1]:
a[i]=a[i-1]+1
i=n-2
while(i>=0):
if l[i+1]>l[i]:
b[i]=b[i+1]+1
i=i-1
total=max(a)
if total<n:
total=total+1
for i in range(1,n-1):
if l[i-1]+1<l[i+1]:
total=max(total,a[i-1]+1+b[i+1])
else:
total=max(total,a[i-1]+1)
print(total)
# Made By Mostafa_Khaled
``` | output | 1 | 6,174 | 12 | 12,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,175 | 12 | 12,350 |
Tags: dp, implementation, two pointers
Correct Solution:
```
import sys
n = int(input())
s = list(map(int, input().split()))
a = [0] * n
b = [0] * n
a[0] = 1
b[0] = 1
if n == 1:
print(1)
sys.exit()
for i in range(1, n):
if s[i] > s[i-1]:
a[i] = a[i-1] + 1
else:
a[i] = 1
s = s[::-1]
for i in range(1, n):
if s[i] < s[i-1]:
b[i] = b[i-1] + 1
else:
b[i] = 1
s = s[::-1]
b = b[::-1]
ans = b[1] + 1
for i in range(1, n - 1):
if s[i-1] + 1 < s[i + 1]:
ans = max(ans, a[i - 1] + 1 + b[i + 1])
else:
ans = max(ans, a[i - 1] + 1, 1 + b[i + 1])
ans = max(ans, a[n - 2] + 1)
print(ans)
``` | output | 1 | 6,175 | 12 | 12,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,176 | 12 | 12,352 |
Tags: dp, implementation, two pointers
Correct Solution:
```
"""
pppppppppppppppppppp
ppppp ppppppppppppppppppp
ppppppp ppppppppppppppppppppp
pppppppp pppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppp pppppppp
ppppppppppppppppppppp ppppppp
ppppppppppppppppppp ppppp
pppppppppppppppppppp
"""
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(str(var))
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
n = int(data())
arr = l()
if len(arr) <= 2:
out(n)
exit()
suf, pref = [1] * n, [1] * n
for i in range(1, n):
if arr[i] > arr[i-1]:
suf[i] = suf[i-1] + 1
for i in range(n-2, -1, -1):
if arr[i] < arr[i+1]:
pref[i] = pref[i+1] + 1
answer = max(suf)
if answer < n:
answer += 1
for i in range(1, n-1):
if arr[i-1] + 1 < arr[i+1]:
answer = max(answer, suf[i-1] + 1 + pref[i+1])
out(answer)
``` | output | 1 | 6,176 | 12 | 12,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,177 | 12 | 12,354 |
Tags: dp, implementation, two pointers
Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int,input().split()))
subseg = []
start = 0
for i in range(N-1):
if A[i] >= A[i+1]:
subseg.append((A[start],A[i],i-start,start,i))
start = i+1
else:
if N-1 == 0:
subseg.append((A[start],A[start],N-start-1,start,start))
else:
subseg.append((A[start],A[i+1],N-start-1,start,i+1))
l = len(subseg)
ans = max(subseg,key=lambda x:x[2])[2] + 1
for i in range(l):
if i + 1 < l and subseg[i+1][2] == 0:
if i+2 < l and subseg[i][1] + 2 <= subseg[i+2][0]:
ans = max(ans,subseg[i][2] + subseg[i+2][2] + 3)
if i + 1 < l and subseg[i+1][3] + 1 < N and subseg[i][1] + 1 < A[subseg[i+1][3]+1]:
ans = max(ans,subseg[i][2] + subseg[i+1][2] + 2)
if i + 1 < l and 0 <= subseg[i][4] - 1 and A[subseg[i][4]-1] + 1 < subseg[i+1][0]:
ans = max(ans,subseg[i][2] + subseg[i+1][2] + 2)
if i != 0 or i != l -1:
ans = max(ans,subseg[i][2] + 2)
else:
ans = max(ans,subseg[i][2] + 1)
print(ans)
``` | output | 1 | 6,177 | 12 | 12,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,178 | 12 | 12,356 |
Tags: dp, implementation, two pointers
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def main():
n = I()
a = LI()
if n < 3:
return n
b = [1]
for i in range(1,n):
if a[i-1] < a[i]:
b.append(b[-1] + 1)
else:
b.append(1)
c = [1]
for i in range(n-2,-1,-1):
if a[i+1] > a[i]:
c.append(c[-1]+1)
else:
c.append(1)
c = c[::-1]
# print(b)
# print(c)
r = max(c[1], b[-2]) + 1
for i in range(1,n-1):
if a[i+1] - a[i-1] > 1:
t = b[i-1] + c[i+1] + 1
if r < t:
r = t
else:
t = max(b[i-1], c[i+1]) + 1
if r < t:
r = t
return r
print(main())
``` | output | 1 | 6,178 | 12 | 12,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 6,179 | 12 | 12,358 |
Tags: dp, implementation, two pointers
Correct Solution:
```
from heapq import heapify, heappush, heappop
from collections import Counter, defaultdict, deque, OrderedDict
from sys import setrecursionlimit, maxsize
from bisect import bisect_left, bisect, insort_left, insort
from math import ceil, log, factorial, hypot, pi
from fractions import gcd
from copy import deepcopy
from functools import reduce
from operator import mul
from itertools import product, permutations, combinations, accumulate, cycle
from string import ascii_uppercase, ascii_lowercase, ascii_letters, digits, hexdigits, octdigits
prod = lambda l: reduce(mul, l)
prodmod = lambda l, mod: reduce(lambda x, y: mul(x,y)%mod, l)
def read_list(t): return [t(x) for x in input().split()]
def read_line(t): return t(input())
def read_lines(t, N): return [t(input()) for _ in range(N)]
N = read_line(int)
array = read_list(int)
i = 0
inc_subsegments = []
while i < N:
start = i
while i+1 < N and array[i] < array[i+1]:
i += 1
if i != start:
inc_subsegments.append((start, i))
i += 1
ans = max(min(2, N), max(min(end-start+1+1, N) for start, end in inc_subsegments) if inc_subsegments else 0)
for i in range(len(inc_subsegments) - 1):
i_last = inc_subsegments[i][1]
i_next_first = inc_subsegments[i+1][0]
if i_last + 1 == i_next_first and (array[i_next_first + 1] - array[i_last] > 1 or array[i_next_first] - array[i_last - 1] > 1):
ans = max(ans, inc_subsegments[i+1][1] - inc_subsegments[i][0] + 1)
print(ans)
``` | output | 1 | 6,179 | 12 | 12,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
Submitted Solution:
```
###### ### ####### ####### ## # ##### ### #####
# # # # # # # # # # # # # ###
# # # # # # # # # # # # # ###
###### ######### # # # # # # ######### #
###### ######### # # # # # # ######### #
# # # # # # # # # # #### # # #
# # # # # # # ## # # # # #
###### # # ####### ####### # # ##### # # # #
from __future__ import print_function # for PyPy2
from collections import Counter, OrderedDict
from itertools import permutations as perm
from fractions import Fraction
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
# from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1e9)+7
inf = float("inf")
# range = xrange
n, = gil()
a = gil()
ans = 1
l, r = [1]*n, [1]*n
for i in range(1, n):
if a[i] > a[i-1]:
l[i] += l[i-1]
ans = max(ans, l[i]+1)
for i in reversed(range(n-1)):
if a[i] < a[i+1]:
r[i] += r[i+1]
ans = max(ans, r[i]+1)
ans = min(ans, n)
# print(a)
# print(l)
for i in range(1, n-1):
if a[i+1] - a[i-1] > 1:
ans = max(ans, r[i+1] + l[i-1] + 1)
print(ans)
``` | instruction | 0 | 6,181 | 12 | 12,362 |
Yes | output | 1 | 6,181 | 12 | 12,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
Submitted Solution:
```
import random
n = int(input())
a = list(map(int, input().split()))
'''
a = []
for i in range(10):
a.append(random.randint(1, 10))
print(a)
'''
inc = []
j = 0
for i in range(len(a)):
if i != len(a)-1 and a[i] < a[i+1]:
if j == 0:
first = a[i]
if j == 1:
second = a[i]
j += 1
else:
if j == 1:
second = a[i]
if j == 0:
inc.append((a[i], a[i], a[i], a[i], j+1))
else:
inc.append((first, second, a[i-1], a[i], j+1))
j = 0
if(len(inc) == 1):
print(inc[0][4])
exit()
inc2 = []
for i in range(len(inc)-1):
if inc[i][3] + 1 < inc[i+1][1]:
inc2.append(inc[i][4] + inc[i+1][4])
if inc[i][2] + 1 < inc[i+1][0]:
inc2.append(inc[i][4] + inc[i+1][4])
inc2.append(max(inc[i][4]+1, inc[i+1][4]+1))
print(max(inc2))
``` | instruction | 0 | 6,182 | 12 | 12,364 |
Yes | output | 1 | 6,182 | 12 | 12,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 β€ i β€ j β€ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109).
Output
In a single line print the answer to the problem β the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
Submitted Solution:
```
from heapq import heapify, heappush, heappop
from collections import Counter, defaultdict, deque, OrderedDict
from sys import setrecursionlimit, maxsize
from bisect import bisect_left, bisect, insort_left, insort
from math import ceil, log, factorial, hypot, pi
from fractions import gcd
from copy import deepcopy
from functools import reduce
from operator import mul
from itertools import product, permutations, combinations, accumulate, cycle
from string import ascii_uppercase, ascii_lowercase, ascii_letters, digits, hexdigits, octdigits
prod = lambda l: reduce(mul, l)
prodmod = lambda l, mod: reduce(lambda x, y: mul(x,y)%mod, l)
def read_list(t): return [t(x) for x in input().split()]
def read_line(t): return t(input())
def read_lines(t, N): return [t(input()) for _ in range(N)]
N = read_line(int)
array = read_list(int)
i = 0
inc_subsegments = []
while i < N:
start = i
while i+1 < N and array[i] < array[i+1]:
i += 1
if i != start:
inc_subsegments.append((start, i))
i += 1
ans = max(min(2, N), max(min(end-start+1+1, N) for start, end in inc_subsegments) if inc_subsegments else 0)
for i in range(len(inc_subsegments) - 1):
i_last = inc_subsegments[i][1]
i_next_first = inc_subsegments[i+1][0]
if i_last + 1 == i_next_first and array[i_last] < array[i_next_first + 1]:
ans = max(ans, inc_subsegments[i+1][1] - inc_subsegments[i][0] + 1)
print(ans)
``` | instruction | 0 | 6,187 | 12 | 12,374 |
No | output | 1 | 6,187 | 12 | 12,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mike has discovered a new way to encode permutations. If he has a permutation P = [p1, p2, ..., pn], he will encode it in the following way:
Denote by A = [a1, a2, ..., an] a sequence of length n which will represent the code of the permutation. For each i from 1 to n sequentially, he will choose the smallest unmarked j (1 β€ j β€ n) such that pi < pj and will assign to ai the number j (in other words he performs ai = j) and will mark j. If there is no such j, he'll assign to ai the number - 1 (he performs ai = - 1).
Mike forgot his original permutation but he remembers its code. Your task is simple: find any permutation such that its code is the same as the code of Mike's original permutation.
You may assume that there will always be at least one valid permutation.
Input
The first line contains single integer n (1 β€ n β€ 500 000) β length of permutation.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n or ai = - 1) β the code of Mike's permutation.
You may assume that all positive values from A are different.
Output
In first and only line print n numbers p1, p2, ..., pn (1 β€ pi β€ n) β a permutation P which has the same code as the given one. Note that numbers in permutation are distinct.
Examples
Input
6
2 -1 1 5 -1 4
Output
2 6 1 4 5 3
Input
8
2 -1 4 -1 6 -1 8 -1
Output
1 8 2 7 3 6 4 5
Note
For the permutation from the first example:
i = 1, the smallest j is 2 because p2 = 6 > p1 = 2.
i = 2, there is no j because p2 = 6 is the greatest element in the permutation.
i = 3, the smallest j is 1 because p1 = 2 > p3 = 1.
i = 4, the smallest j is 5 (2 was already marked) because p5 = 5 > p4 = 4.
i = 5, there is no j because 2 is already marked.
i = 6, the smallest j is 4 because p4 = 4 > p6 = 3.
Submitted Solution:
```
n = int(input())
p = list(map(int, input().split(' ')))
p = [None] + p
p = [(p[i], i) for i in range(len(p))]
maxx = n
ans = [0]*len(p)
todel = []
for i in range(1, n+1):
if p[i][0] == -1:
ans[i] = maxx
maxx -= 1
todel.insert(0, i)
for i in todel:
del p[i]
p = p[1:]
p = sorted(p)
maxx = 1
for x, i in p:
ans[i] = maxx
maxx += 1
ans = ans[1:]
print((" ").join(list(map(str, ans))))
``` | instruction | 0 | 6,273 | 12 | 12,546 |
No | output | 1 | 6,273 | 12 | 12,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!
Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.
This time Alice wants to form four melodies for her tracks.
Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Input
The first line contains one integer number n (4 β€ n β€ 3000).
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105) β notes written on a sheet.
Output
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Examples
Input
5
1 3 5 7 9
Output
4
Input
5
1 3 5 7 2
Output
5
Note
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).
In the second example it is possible to compose one melody with 2 notes β {1, 2}. Remaining notes are used in other three melodies (one note per each melody). | instruction | 0 | 6,276 | 12 | 12,552 |
Tags: flows, graphs
Correct Solution:
```
import sys
n = int(input()) + 1
a = [0] + list(map(int, input().split()))
mod7 = [x % 7 for x in a]
dp = [[0]*n for _ in range(n)]
maxnum = [0]*(10**5+10)
ans = 0
for i in range(n):
maxmod = [0]*7
for j in range(n):
maxnum[a[j]] = 0
for j in range(i):
maxnum[a[j]] = max(maxnum[a[j]], dp[j][i])
maxmod[mod7[j]] = max(maxmod[mod7[j]], dp[j][i])
for j in range(i+1, n):
dp[i][j] = max(
maxnum[a[j]-1],
maxnum[a[j]+1],
maxmod[mod7[j]],
dp[0][i]
) + 1
maxnum[a[j]] = max(maxnum[a[j]], dp[i][j])
maxmod[mod7[j]] = max(maxmod[mod7[j]], dp[i][j])
ans = max(ans, dp[i][j])
print(ans)
``` | output | 1 | 6,276 | 12 | 12,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!
Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.
This time Alice wants to form four melodies for her tracks.
Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Input
The first line contains one integer number n (4 β€ n β€ 3000).
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105) β notes written on a sheet.
Output
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Examples
Input
5
1 3 5 7 9
Output
4
Input
5
1 3 5 7 2
Output
5
Note
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).
In the second example it is possible to compose one melody with 2 notes β {1, 2}. Remaining notes are used in other three melodies (one note per each melody).
Submitted Solution:
```
import sys
n = int(input()) + 1
a = [0] + list(map(int, input().split()))
mod7 = [x % 7 for x in a]
inf = 10**9
next_i = [[[inf]*3 for _ in range(n+1)] for _ in range(n+1)]
for j in range(n-1, -1, -1):
next_i[0][j] = [j]*3
for i in range(1, n):
next_i[i][j][0] = j if a[i]-1 == a[j] else next_i[i][j+1][0]
next_i[i][j][1] = j if a[i]+1 == a[j] else next_i[i][j+1][1]
next_i[i][j][2] = j if mod7[i] == mod7[j] else next_i[i][j+1][2]
dp = [[1]+[2]*n for _ in range(n)]
dp[0] = [0] + [1]*n
for j in range(1, n):
for i in range(j):
for k in range(3):
if next_i[i][j+1][k] != inf:
dp[j][next_i[i][j+1][k]] = max(dp[j][next_i[i][j+1][k]], dp[i][j]+1)
if next_i[j][j+1][k] != inf:
dp[i][next_i[j][j+1][k]] = max(dp[i][next_i[j][j+1][k]], dp[i][j]+1)
dp[i][j+1] = max(dp[i][j+1], dp[i][j])
dp[j][j+1] = max(dp[j][j+1], dp[i][j])
print(max(max(row[1:]) for row in dp[1:]))
``` | instruction | 0 | 6,277 | 12 | 12,554 |
No | output | 1 | 6,277 | 12 | 12,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!
Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.
This time Alice wants to form four melodies for her tracks.
Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Input
The first line contains one integer number n (4 β€ n β€ 3000).
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105) β notes written on a sheet.
Output
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Examples
Input
5
1 3 5 7 9
Output
4
Input
5
1 3 5 7 2
Output
5
Note
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).
In the second example it is possible to compose one melody with 2 notes β {1, 2}. Remaining notes are used in other three melodies (one note per each melody).
Submitted Solution:
```
import sys
n = int(input()) + 1
a = [0] + list(map(int, input().split()))
mod7 = [x % 7 for x in a]
inf = 10**9
next_i = [[[inf]*3 for _ in range(n+1)] for _ in range(n+1)]
for j in range(n-1, -1, -1):
next_i[0][j] = [j]*3
for i in range(1, n):
next_i[i][j][0] = j if a[i]-1 == a[j] else next_i[i][j+1][0]
next_i[i][j][1] = j if a[i]+1 == a[j] else next_i[i][j+1][1]
next_i[i][j][2] = j if mod7[i] == mod7[j] else next_i[i][j+1][2]
dp = [[0]*n for _ in range(n)]
i0_max = 0
for j in range(1, n-1):
dp[0][j] = max(dp[0][j], 1)
i0_max = max(i0_max, dp[0][j])
for k in range(3):
if next_i[j][j+1][k] != inf:
dp[0][next_i[j][j+1][k]] = max(
dp[0][next_i[j][j+1][k]], dp[0][j]+1
)
for k in range(j+1, n):
dp[j][k] = max(dp[j][k], i0_max+1)
for i in range(1, n):
for j in range(i+1, n):
for k in range(3):
if next_i[i][j+1][k] != inf:
dp[j][next_i[i][j+1][k]] = max(
dp[j][next_i[i][j+1][k]],
dp[i][j]+1
)
if next_i[j][j+1][k] != inf:
dp[i][next_i[j][j+1][k]] = max(
dp[i][next_i[j][j+1][k]],
dp[i][j]+1
)
print(max(max(row[1:]) for row in dp[1:]))
``` | instruction | 0 | 6,278 | 12 | 12,556 |
No | output | 1 | 6,278 | 12 | 12,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!
Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.
This time Alice wants to form four melodies for her tracks.
Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Input
The first line contains one integer number n (4 β€ n β€ 3000).
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105) β notes written on a sheet.
Output
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Examples
Input
5
1 3 5 7 9
Output
4
Input
5
1 3 5 7 2
Output
5
Note
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).
In the second example it is possible to compose one melody with 2 notes β {1, 2}. Remaining notes are used in other three melodies (one note per each melody).
Submitted Solution:
```
import sys
n = int(input())
a = list(map(int, input().split()))
mod7 = [x % 7 for x in a]
inf = 10**9
next_i = [[inf]*3 for _ in range(n)]
for i in range(n):
for j in range(i+1, n):
if a[i]-1 == a[j]:
next_i[i][0] = min(next_i[i][0], j)
if a[i]+1 == a[j]:
next_i[i][1] = min(next_i[i][1], j)
if mod7[i] == mod7[j]:
next_i[i][2] = min(next_i[i][2], j)
dp = [[2]*n for _ in range(n)]
for j in range(1, n):
for i in range(j):
for k in range(3):
if j < next_i[i][k] < inf:
dp[j][next_i[i][k]] = max(dp[j][next_i[i][k]], dp[i][j]+1)
if j < next_i[j][k] < inf:
dp[i][next_i[j][k]] = max(dp[i][next_i[j][k]], dp[i][j]+1)
print(max(max(row) for row in dp))
``` | instruction | 0 | 6,279 | 12 | 12,558 |
No | output | 1 | 6,279 | 12 | 12,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!
Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks.
This time Alice wants to form four melodies for her tracks.
Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.
You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Input
The first line contains one integer number n (4 β€ n β€ 3000).
The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105) β notes written on a sheet.
Output
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
Examples
Input
5
1 3 5 7 9
Output
4
Input
5
1 3 5 7 2
Output
5
Note
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).
In the second example it is possible to compose one melody with 2 notes β {1, 2}. Remaining notes are used in other three melodies (one note per each melody).
Submitted Solution:
```
import sys,math,itertools
from collections import Counter,deque,defaultdict
from bisect import bisect_left,bisect_right
from heapq import heappop,heappush,heapify
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_1(): return list(map(lambda x:int(x)-1, sys.stdin.readline().split()))
def inps(): return sys.stdin.readline()
def inpsl(x): tmp = sys.stdin.readline(); return list(tmp[:x])
def err(x): print(x); exit()
n = inp()
a = inpl()
g = [[] for _ in range(n)]
ny = [0]*n
for i in range(n):
for j in range(i+1,n):
if abs(a[i]-a[j]) == 1 or a[i]%7 == a[j]%7:
g[i].append(j)
ny[j] += 1
q = deque([])
su = [1]*n
seen = [0]*n
for i,x in enumerate(ny):
if x == 0:
q.append(i)
seen[i] = 1
while q:
u = q.popleft()
for v in g[u]:
if seen[v]: continue
su[v] = max(su[v], su[u]+1)
ny[v] -= 1
if ny[v] == 0:
seen[v] = 1
q.append(v)
su.sort(reverse=True)
print(su[0]+su[1])
``` | instruction | 0 | 6,280 | 12 | 12,560 |
No | output | 1 | 6,280 | 12 | 12,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,281 | 12 | 12,562 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
a = sorted(zip(map(int, input().split()), range(n)))
s = []
for i in range(n):
if a[i]:
s.append([])
while a[i]:
s[-1].append(i + 1)
a[i], i = None, a[i][1]
print(len(s))
for l in s:
print(len(l), *l)
``` | output | 1 | 6,281 | 12 | 12,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,282 | 12 | 12,564 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
a=list(map(int,input().split()))
a = sorted([(a[i],i) for i in range(n)])
p =[]
c = [False]*n
for i in range(n):
if not c[i]:
k=i
b=[]
while not c[k]:
c[k]=True
b.append(str(k+1))
k = a[k][1]
p.append(b)
print(len(p))
for i in p:
print(str(len(i))+" "+" ".join(i))
``` | output | 1 | 6,282 | 12 | 12,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,283 | 12 | 12,566 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
a = sorted((a[i], i) for i in range(n))
s = []
for i in range(n):
if a[i]:
l = []
s.append(l)
while a[i]:
l.append(i + 1)
a[i], i = None, a[i][1]
print(len(s))
for l in s:
print(len(l), *l)
``` | output | 1 | 6,283 | 12 | 12,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,284 | 12 | 12,568 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
s = list(map(int, input().split()))
s1 = sorted(s)
d = {s1[i] : i for i in range(n)}
ans = []
for i in range(n):
if d[s[i]] > -1:
curans = [i + 1]
cur = i
place = d[s[cur]]
while place != i:
curans.append(place + 1)
d[s[cur]] = -1
cur = place
place = d[s[cur]]
d[s[cur]] = -1
ans.append(curans)
print(len(ans))
for a in ans:
print(len(a), end=' ')
print(*a)
``` | output | 1 | 6,284 | 12 | 12,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,285 | 12 | 12,570 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
a = list(enumerate(a))
a = sorted(a, key=lambda x: x[1])
a = [(i, x, j) for j, (i, x) in enumerate(a)]
a = sorted(a, key=lambda x: x[0])
count_cycles = 0
cycle = [-1] * n
i = 0
while True:
while i < n and cycle[i] >= 0:
i += 1
if i == n:
break
k = i
cycle[i] = count_cycles
while a[k][2] != i:
k = a[k][2]
cycle[k] = count_cycles
count_cycles += 1
print(count_cycles)
cycles = [[] for i in range(count_cycles)]
for i in range(n):
cycles[cycle[i]].append(i + 1)
for i in range(count_cycles):
print(str(len(cycles[i])) + ' ' + ' '.join([str(x) for x in cycles[i]]))
``` | output | 1 | 6,285 | 12 | 12,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,286 | 12 | 12,572 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
ind = sorted(range(n), key=lambda i: a[i])
visited = [False] * n
s = []
for i in range(n):
if not visited[i]:
s.append([i + 1])
l = s[-1]
j = ind[i]
while j != i:
visited[j] = True
l.append(j + 1)
j = ind[j]
print(len(s))
for l in s:
print(len(l), *l)
``` | output | 1 | 6,286 | 12 | 12,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,287 | 12 | 12,574 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
sa = sorted(a)
sg = {}
for i in range(n):
sg[sa[i]] = i
vis = set()
ans = []
for i in range(n):
if i in vis:
continue
vis.add(i)
c = [i+1]
v = i
while 1:
nv = sg[a[v]]
if nv not in vis:
vis.add(nv)
c.append(nv+1)
v = nv
else:
break
ans.append(c)
print(len(ans))
for c in ans:
print(len(c), ' '.join([str(e) for e in c]))
``` | output | 1 | 6,287 | 12 | 12,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | instruction | 0 | 6,288 | 12 | 12,576 |
Tags: dfs and similar, dsu, implementation, math, sortings
Correct Solution:
```
n = int(input())
lis = list(map(int,input().split()))
a = sorted(lis)[:]
d={}
for i in range(n):
d[a[i]]=i
#print(d)
vis=[0]*(n)
ans=[]
for i in range(n):
tmp=[]
if vis[i]==0:
j=i
while vis[j]==0:
vis[j]=1
tmp.append(j+1)
j=d[lis[j]]
ans.append([len(tmp)]+tmp)
print(len(ans))
for i in ans:
print(*i)
``` | output | 1 | 6,288 | 12 | 12,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
n = int(input())
a = sorted(zip(map(int, input().split()), range(n)))
s = []
for i in range(n):
if a[i]:
l = []
s.append(l)
while a[i]:
l.append(i + 1)
a[i], i = None, a[i][1]
print(len(s))
for l in s:
print(len(l), *l)
``` | instruction | 0 | 6,289 | 12 | 12,578 |
Yes | output | 1 | 6,289 | 12 | 12,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
from __future__ import print_function
import sys
sys.setrecursionlimit(100005)
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
pos = {}
for i in range(n):
pos[b[i]] = i
g = [[] for _ in range(n)]
used = [False for _ in range(n)]
for i in range(n):
g[i] += [pos[a[i]]]
b = []
for i in range(n):
if used[i] == False:
w = []
q = [i]
while q:
v = q[-1]
q.pop()
w.append(v)
used[v] = True
for to in g[v]:
if used[to] == False:
q += [to]
b.append(list(w))
print(len(b))
for i in range(len(b)):
b[i] = [x + 1 for x in b[i]]
print(len(b[i]), *b[i], sep=' ')
``` | instruction | 0 | 6,290 | 12 | 12,580 |
Yes | output | 1 | 6,290 | 12 | 12,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
"""
created by shhuan at 2017/10/20 10:12
"""
N = int(input())
A = [int(x) for x in input().split()]
B = [x for x in A]
B.sort()
ind = {v: i for i,v in enumerate(B)}
order = [ind[A[i]] for i in range(N)]
done = [0] * N
ans = []
for i in range(N):
if not done[i]:
g = []
cur = i
while not done[cur]:
g.append(cur)
done[cur] = 1
cur = order[cur]
ans.append([len(g)] + [j+1 for j in g])
print(len(ans))
for row in ans:
print(' '.join(map(str, row)))
``` | instruction | 0 | 6,291 | 12 | 12,582 |
Yes | output | 1 | 6,291 | 12 | 12,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
a = sorted(range(n), key=lambda i: a[i])
s = []
for i in range(n):
if a[i] + 1:
l = []
s.append(l)
while a[i] + 1:
l.append(i + 1)
a[i], i = -1, a[i]
print(len(s))
for l in s:
print(len(l), *l)
``` | instruction | 0 | 6,292 | 12 | 12,584 |
Yes | output | 1 | 6,292 | 12 | 12,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
n=int(input())
anss=0
a=list(map(int,input().split()))
b=[]
for i in range(n):
b.append([i,a[i],0])
b=sorted(b,key = lambda a : a[1])
for i in range(n):
b[i].append(i)
b=sorted(b,key = lambda a : a[0])
c=[]
d=[]
for i in range(n):
if b[i][2]!=1:
j=i
c.append([])
d.append(0)
while b[j][2]!=1:
c[anss].append(b[j][0])
d[anss]=d[anss]+1
b[j][2]=b[j][2]+1
j=b[j][3]
anss=anss+1
print(anss)
for i in range(anss):
print(d[i],end=" ")
for j in c[i]:
print(j,end=" ")
print()
``` | instruction | 0 | 6,293 | 12 | 12,586 |
No | output | 1 | 6,293 | 12 | 12,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
"""
created by shhuan at 2017/10/20 10:12
"""
N = int(input())
A = [int(x) for x in input().split()]
B = [x for x in A]
B.sort()
ans = []
vis = [0]*N
def merge_sort(l, r):
if r > l+1:
m = (l+r)//2
merge_sort(l, m)
merge_sort(m, r)
# merge
A[l:r] = sorted(A[l:r])
if A[l:r] == B[l:r]:
ids = [i+1 for i in range(l, r) if not vis[i]]
if ids:
for i in ids:
vis[i-1] = 1
ans.append([len(ids)]+ids)
merge_sort(0, N)
print(len(ans))
if ans:
for row in ans:
print(' '.join(map(str, row)))
``` | instruction | 0 | 6,294 | 12 | 12,588 |
No | output | 1 | 6,294 | 12 | 12,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
n = int(input())
d = list(map(int, input().split()))
temp = d[:]
d.sort()
from bisect import bisect_left
A = [bisect_left(d,i) for i in temp]
del d,temp
f = [False]*len(A)
for i in range(n):
if not f[i]:
L = [i+1]
f[i] = True
j = A[i]
while j != i:
L.append(j+1)
f[j] = True
j = A[j]
print(len(L),' '.join(map(str,L)))
``` | instruction | 0 | 6,295 | 12 | 12,590 |
No | output | 1 | 6,295 | 12 | 12,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence.
Input
The first line of input data contains integer n (1 β€ n β€ 105) β the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
Output
In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.
In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci β€ n), then ci integers l1, l2, ..., lci (1 β€ lj β€ n) β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.
If there are several possible answers, print any of them.
Examples
Input
6
3 2 1 6 5 4
Output
4
2 1 3
1 2
2 4 6
1 5
Input
6
83 -75 -49 11 37 62
Output
1
6 1 2 3 4 5 6
Note
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
Submitted Solution:
```
n = int(input())
m = list(map(int, input().split()))
d = {}
for i, k in enumerate(sorted(m)):
d[k] = i
r = set()
ans = []
for k in m:
if k not in r:
r.add(k)
ans.append({k,})
j = k
while m[d[j]] != k:
r.add(m[d[j]])
ans[-1].add(m[d[j]])
j = m[d[j]]
print(len(ans))
for s in ans:
print(len(s), *s)
``` | instruction | 0 | 6,296 | 12 | 12,592 |
No | output | 1 | 6,296 | 12 | 12,593 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers from 1 to 200. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally:
a_{1} β€ a_{2},
a_{n} β€ a_{n-1} and
a_{i} β€ max(a_{i-1}, a_{i+1}) for all i from 2 to n-1.
Ivan does not remember the array and asks to find the number of ways to restore it. Restored elements also should be integers from 1 to 200. Since the number of ways can be big, print it modulo 998244353.
Input
First line of input contains one integer n (2 β€ n β€ 10^{5}) β size of the array.
Second line of input contains n integers a_{i} β elements of array. Either a_{i} = -1 or 1 β€ a_{i} β€ 200. a_{i} = -1 means that i-th element can't be read.
Output
Print number of ways to restore the array modulo 998244353.
Examples
Input
3
1 -1 2
Output
1
Input
2
-1 -1
Output
200
Note
In the first example, only possible value of a_{2} is 2.
In the second example, a_{1} = a_{2} so there are 200 different values because all restored elements should be integers between 1 and 200. | instruction | 0 | 6,604 | 12 | 13,208 |
Tags: dp
Correct Solution:
```
import os
from io import BytesIO
from math import trunc
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
MX = 201
MOD = 998244353
MODF = float(MOD)
quickmod = lambda x: x - MODF * trunc(x / MODF)
def main():
n = int(input())
a = map(int, input().split())
dp0 = [1.0] * MX
dp1 = [0.0] * MX
for x in a:
pomdp0 = [0.0] * MX
pomdp1 = [0.0] * MX
if x == -1:
for val in range(1, MX):
pomdp0[val] = quickmod(
pomdp0[val - 1] + dp1[val - 1] + dp0[val - 1])
pomdp1[val] = quickmod(
pomdp1[val - 1] + dp1[MX - 1] - dp1[val - 1] + dp0[val] -
dp0[val - 1])
else:
pomdp0[x] = quickmod(dp1[x - 1] + dp0[x - 1])
pomdp1[x] = quickmod(dp1[MX - 1] - dp1[x - 1] + dp0[x] - dp0[x - 1])
for val in range(x + 1, MX):
pomdp0[val] = pomdp0[val - 1]
pomdp1[val] = pomdp1[val - 1]
dp0, dp1 = pomdp0, pomdp1
print(int(dp1[MX - 1] if n > 1 else dp0[MX - 1]) % MOD)
if __name__ == "__main__":
main()
``` | output | 1 | 6,604 | 12 | 13,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers from 1 to 200. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally:
a_{1} β€ a_{2},
a_{n} β€ a_{n-1} and
a_{i} β€ max(a_{i-1}, a_{i+1}) for all i from 2 to n-1.
Ivan does not remember the array and asks to find the number of ways to restore it. Restored elements also should be integers from 1 to 200. Since the number of ways can be big, print it modulo 998244353.
Input
First line of input contains one integer n (2 β€ n β€ 10^{5}) β size of the array.
Second line of input contains n integers a_{i} β elements of array. Either a_{i} = -1 or 1 β€ a_{i} β€ 200. a_{i} = -1 means that i-th element can't be read.
Output
Print number of ways to restore the array modulo 998244353.
Examples
Input
3
1 -1 2
Output
1
Input
2
-1 -1
Output
200
Note
In the first example, only possible value of a_{2} is 2.
In the second example, a_{1} = a_{2} so there are 200 different values because all restored elements should be integers between 1 and 200. | instruction | 0 | 6,605 | 12 | 13,210 |
Tags: dp
Correct Solution:
```
import os
from io import BytesIO
from math import trunc
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
MX = 201
MOD = 998244353
MODF = MOD * 1.0
quickmod = lambda x: x - MODF * trunc(x / MODF)
def main():
n = int(input())
a = map(int, input().split())
dp0 = [1.0] * MX
dp1 = [0.0] * MX
for x in a:
pomdp0 = [0.0] * MX
pomdp1 = [0.0] * MX
if x == -1:
for val in range(1, MX):
pomdp0[val] = quickmod(
pomdp0[val - 1] + dp1[val - 1] + dp0[val - 1])
pomdp1[val] = quickmod(
pomdp1[val - 1] + dp1[MX - 1] - dp1[val - 1] + dp0[val] -
dp0[val - 1])
else:
pomdp0[x] = quickmod(dp1[x - 1] + dp0[x - 1])
pomdp1[x] = quickmod(dp1[MX - 1] - dp1[x - 1] + dp0[x] - dp0[x - 1])
for val in range(x + 1, MX):
pomdp0[val] = pomdp0[val - 1]
pomdp1[val] = pomdp1[val - 1]
dp0, dp1 = pomdp0, pomdp1
print(int(dp1[MX - 1] if n > 1 else dp0[MX - 1]) % MOD)
if __name__ == "__main__":
main()
``` | output | 1 | 6,605 | 12 | 13,211 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers from 1 to 200. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally:
a_{1} β€ a_{2},
a_{n} β€ a_{n-1} and
a_{i} β€ max(a_{i-1}, a_{i+1}) for all i from 2 to n-1.
Ivan does not remember the array and asks to find the number of ways to restore it. Restored elements also should be integers from 1 to 200. Since the number of ways can be big, print it modulo 998244353.
Input
First line of input contains one integer n (2 β€ n β€ 10^{5}) β size of the array.
Second line of input contains n integers a_{i} β elements of array. Either a_{i} = -1 or 1 β€ a_{i} β€ 200. a_{i} = -1 means that i-th element can't be read.
Output
Print number of ways to restore the array modulo 998244353.
Examples
Input
3
1 -1 2
Output
1
Input
2
-1 -1
Output
200
Note
In the first example, only possible value of a_{2} is 2.
In the second example, a_{1} = a_{2} so there are 200 different values because all restored elements should be integers between 1 and 200. | instruction | 0 | 6,606 | 12 | 13,212 |
Tags: dp
Correct Solution:
```
import os
from io import BytesIO
from math import trunc
if os.name == 'nt':
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
MX = 201
MOD = 998244353
MODF = float(MOD)
MODF_inv = 1.0 / MODF
quickmod1 = lambda x: x - MODF * trunc(x / MODF)
def quickmod(a):
return a - MODF * trunc(a * MODF_inv)
def main():
n = int(input())
a = map(int, input().split())
dp0 = [1.0] * MX
dp1 = [0.0] * MX
for x in a:
pomdp0 = [0.0] * MX
pomdp1 = [0.0] * MX
if x == -1:
for val in range(1, MX):
pomdp0[val] = quickmod(
pomdp0[val - 1] + dp1[val - 1] + dp0[val - 1])
pomdp1[val] = quickmod(
pomdp1[val - 1] + dp1[MX - 1] - dp1[val - 1] + dp0[val] -
dp0[val - 1])
else:
pomdp0[x] = quickmod(dp1[x - 1] + dp0[x - 1])
pomdp1[x] = quickmod(dp1[MX - 1] - dp1[x - 1] + dp0[x] - dp0[x - 1])
for val in range(x + 1, MX):
pomdp0[val] = pomdp0[val - 1]
pomdp1[val] = pomdp1[val - 1]
dp0, dp1 = pomdp0, pomdp1
print(int(dp1[MX - 1] if n > 1 else dp0[MX - 1]) % MOD)
if __name__ == "__main__":
main()
``` | output | 1 | 6,606 | 12 | 13,213 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers from 1 to 200. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally:
a_{1} β€ a_{2},
a_{n} β€ a_{n-1} and
a_{i} β€ max(a_{i-1}, a_{i+1}) for all i from 2 to n-1.
Ivan does not remember the array and asks to find the number of ways to restore it. Restored elements also should be integers from 1 to 200. Since the number of ways can be big, print it modulo 998244353.
Input
First line of input contains one integer n (2 β€ n β€ 10^{5}) β size of the array.
Second line of input contains n integers a_{i} β elements of array. Either a_{i} = -1 or 1 β€ a_{i} β€ 200. a_{i} = -1 means that i-th element can't be read.
Output
Print number of ways to restore the array modulo 998244353.
Examples
Input
3
1 -1 2
Output
1
Input
2
-1 -1
Output
200
Note
In the first example, only possible value of a_{2} is 2.
In the second example, a_{1} = a_{2} so there are 200 different values because all restored elements should be integers between 1 and 200. | instruction | 0 | 6,607 | 12 | 13,214 |
Tags: dp
Correct Solution:
```
from math import trunc
MX = 201
MOD = 998244353
MODF = MOD * 1.0
quickmod = lambda x: x - MODF * trunc(x / MODF)
def main():
n = int(input())
a = map(int, input().split())
dp0 = [1.0] * MX
dp1 = [0.0] * MX
for x in a:
pomdp0 = [0.0] * MX
pomdp1 = [0.0] * MX
if x == -1:
for val in range(1, MX):
pomdp0[val] = quickmod(
pomdp0[val - 1] + dp1[val - 1] + dp0[val - 1])
pomdp1[val] = quickmod(
pomdp1[val - 1] + dp1[MX - 1] - dp1[val - 1] + dp0[val] -
dp0[val - 1])
else:
pomdp0[x] = quickmod(dp1[x - 1] + dp0[x - 1])
pomdp1[x] = quickmod(dp1[MX - 1] - dp1[x - 1] + dp0[x] - dp0[x - 1])
for val in range(x + 1, MX):
pomdp0[val] = pomdp0[val - 1]
pomdp1[val] = pomdp1[val - 1]
dp0, dp1 = pomdp0, pomdp1
print(int(dp1[MX - 1] if n > 1 else dp0[MX - 1]) % MOD)
if __name__ == "__main__":
main()
``` | output | 1 | 6,607 | 12 | 13,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,628 | 12 | 13,256 |
Tags: greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
length = 1
for i in range(1, n):
if a[i] <= a[i-1]: break
length += 1
l = [0] * n
for i in range(length):
l[i] = length - i - 1
length = 1
for i in range(n-2, -1, -1):
if a[i] <= a[i+1]: break
length += 1
r = [0] * n
for i in range(n-1, n-1-length, -1):
r[i] = length - n + i
res = []
lo, hi = 0, n-1
last = 0
while lo <= hi:
if a[lo] <= last and a[hi] <= last: break
if a[lo] == a[hi]:
if l[lo] >= r[hi]:
res.append('L')
last = a[lo]
lo += 1
else:
res.append('R')
last = a[hi]
hi -= 1
elif a[lo] < a[hi]:
if a[lo] > last:
res.append('L')
last = a[lo]
lo += 1
elif a[hi] > last:
res.append('R')
last = a[hi]
hi -= 1
else:
if a[hi] > last:
res.append('R')
last = a[hi]
hi -= 1
elif a[lo] > last:
res.append('L')
last = a[lo]
lo += 1
print(len(res))
print(''.join(res))
``` | output | 1 | 6,628 | 12 | 13,257 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,629 | 12 | 13,258 |
Tags: greedy
Correct Solution:
```
# list input
def linp(): return list(map(int, input().split()))
# map input
def minp(): return map(int, input().split())
# int input
def iinp(): return int(input())
# no of testcase
def test(): return int(input())
# normal input
def inp(): return input()
def fun(l, curr, res):
while l:
if l[0]> curr and l[-1]>curr:
if l[0] < l[-1]:
curr = l.pop(0)
res+= 'L'
elif l[-1] > l[0]:
curr = l.pop()
res += 'R'
else:
pass
elif l[0]> curr:
curr = l.pop(0)
res += 'L'
elif l[-1] > curr:
curr = l.pop()
res += 'R'
else:
break
return res
def solve():
# write your solution for one testcase
n = iinp()
l = linp()
curr = -1
res = ''
while l:
if l[0]> curr and l[-1]>curr:
if l[0] < l[-1]:
curr = l.pop(0)
res+= 'L'
n-=1
elif l[0] > l[-1]:
curr = l.pop()
res += 'R'
n-=1
else:
flag = 'L'
i, j = 0, n-1
counti, countj = 0, 0
while i < n-1 and j >0:
flag1 = True
if l[i+1] > l[i]:
counti += 1
i+=1
flag1 =False
if l[j-1] > l[j]:
countj += 1
j-=1
flag1 = False
if counti>countj:
flag = 'L'
break
elif countj>counti:
flag = 'R'
break
if flag1:
break
# print(flag)
if flag == 'L':
curr = l.pop(0)
res += 'L'
else:
curr = l.pop()
res += 'R'
elif l[0]> curr:
curr = l.pop(0)
res += 'L'
n-=1
elif l[-1] > curr:
curr = l.pop()
res += 'R'
n-=1
else:
break
print(str(len(res)) + '\n' + res)
def main():
# for _ in range(test()):
solve()
if __name__ == '__main__':
main()
``` | output | 1 | 6,629 | 12 | 13,259 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,630 | 12 | 13,260 |
Tags: greedy
Correct Solution:
```
import sys
sys.setrecursionlimit(10**7)
N = int(input())
number = list(map(int, input().split()))
seq = []
ans = []
l = 0
r = N-1
def correct(ans, l, r, action):
for i in range(len(ans)-1, -1, -1):
if not ans[i] == 'X':
break
ans[i] = action
if action == 'L':
r += 1
else:
l -= 1
return l, r
def run(number, l, r, seq):
ans = []
while l <= r:
n1 = number[l]
n2 = number[r]
if n1 == n2:
if seq and n1 <= seq[-1]:
break
steps = 0
last = -1
if seq:
last = seq[-1]
while l+steps <= r-steps and number[l+steps] == number[r-steps] and number[l+steps] > last:
seq.append(number[l+steps])
ans.append('X')
last = number[l+steps]
steps += 1
s1 = run(number, l+steps, r, [seq[-1]])
s2 = run(number, l, r-steps, [seq[-1]])
if len(s1) > len(s2):
return ''.join(ans).replace('X', 'L') + s1
else:
return ''.join(ans).replace('X', 'R') + s2
elif not seq:
if n1 < n2:
seq.append(n1)
ans.append('L')
l += 1
else:
seq.append(n2)
ans.append('R')
r -= 1
else:
last = seq[-1]
if last < n1 < n2 or n2 <= last < n1:
l, r = correct(ans, l, r, 'L')
seq.append(n1)
ans.append('L')
l += 1
elif last < n2 < n1 or n1 <= last < n2:
l, r = correct(ans, l, r, 'R')
seq.append(n2)
ans.append('R')
r -= 1
else:
break
return ''.join(ans)
# correct(ans, 0, 0, 'R')
ans = run(number, 0, N-1, [])
print(len(ans))
print(ans)
``` | output | 1 | 6,630 | 12 | 13,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,631 | 12 | 13,262 |
Tags: greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
Lind = 0
Rind = n - 1
flag = True
ans = list()
ans.append(0)
ans2 = list()
flag5 = True
flag6 = True
while flag and Rind >= Lind and flag5 and flag6:
if a[Rind] > a[Lind]:
if a[Lind] > ans[-1]:
ans.append(a[Lind])
Lind += 1
ans2.append('L')
else:
flag5 = False
elif a[Lind] > a[Rind]:
if a[Rind] > ans[-1]:
ans.append(a[Rind])
Rind -= 1
ans2.append('R')
else:
flag6 = False
else:
flag = False
if not flag or not flag5 or not flag6:
lmax = 0
rmax = 0
for i in range(Lind, Rind):
if a[i] > ans[-1]:
if a[i+1] > a[i]:
lmax += 1
else:
lmax += 1
break
else:
break
for i in reversed(range(Lind+1, Rind+1)):
if a[i] > ans[-1]:
if a[i-1] > a[i]:
rmax += 1
else:
rmax += 1
break
else:
break
if lmax > rmax:
i = Lind
while Lind <= Rind:
if a[i] > ans[-1]:
ans.append(a[i])
ans2.append('L')
i += 1
else:
break
else:
i = Rind
while Rind >= Lind:
if a[i] > ans[-1]:
ans.append(a[i])
ans2.append('R')
i -= 1
else:
break
print(len(ans)-1)
print(*ans2, end='', sep='')
``` | output | 1 | 6,631 | 12 | 13,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,632 | 12 | 13,264 |
Tags: greedy
Correct Solution:
```
N = int(input())
a = list(map(int, input().split()))
pre = -1
L, R = 0, N - 1
d = []
while L < N and R >= 0 and L <= R:
l = a[L]
r = a[R]
if pre < l and pre < r:
if l > r:
d.append('R')
pre = a[R]
R -= 1
elif r > l:
d.append('L')
pre = a[L]
L += 1
else:
d1 = []
d2 = []
R1 = R
L1 = L
pre1 = pre
pre2 = pre
while L <= R1:
if pre1 < a[R1]:
d1.append('R')
pre1 = a[R1]
R1 -= 1
else:
break
while L1 <= R:
if pre2 < a[L1]:
d2.append('L')
pre2 = a[L1]
L1 += 1
else:
break
if len(d1) > len(d2):
d += d1
break
else:
d += d2
break
elif pre < l:
d.append('L')
pre = a[L]
L += 1
elif pre < r:
d.append('R')
pre = a[R]
R -= 1
else:
break
print(len(d))
print(''.join(d))
``` | output | 1 | 6,632 | 12 | 13,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,633 | 12 | 13,266 |
Tags: greedy
Correct Solution:
```
# -*- coding: utf-8 -*-
# @Date : 2019-04-27 15:37:29
# @Author : raj lath (oorja.halt@gmail.com)
# @Link : link
# @Version : 1.0.0
import sys
sys.setrecursionlimit(10**5+1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
#! Solution by xumouyi
def solve(p, q, r):
ans = ""
current = r
i = p
j = q
while True:
if j < i:
break
if current < num[i] < num[j] or num[j] <= current < num[i]:
ans += "L"
current = num[i]
i += 1
continue
if current < num[j] < num[i] or num[i] <= current < num[j]:
ans += "R"
current = num[j]
j -= 1
continue
if current < num[i] == num[j]:
ans1 = solve(i, j - 1, num[i])
ans2 = solve(i + 1, j, num[i])
if len(ans1) > len(ans2):
ans += "R" + ans1
else:
ans += "L" + ans2
break
return ans
n = int(input())
num = [*map(int, input().split())]
ans = solve(0, n - 1, -1)
print(len(ans))
print(ans)
``` | output | 1 | 6,633 | 12 | 13,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,634 | 12 | 13,268 |
Tags: greedy
Correct Solution:
```
def getHighAns(a, low, high):
count = 1
ans = ''
ans += 'R'
last = a[high]
high -= 1
while(low <= high):
if(last < a[high]):
count += 1
ans += 'R'
last = a[high]
high -= 1
else:
break
return count, ans
def getLowAns(a, low, high):
count = 1
ans = ''
ans += 'L'
last = a[low]
low += 1
while(low <= high):
if(last < a[low]):
count += 1
ans += 'L'
last = a[low]
low += 1
else:
break
return count, ans
n = int(input())
a = list(map(int, input().split()))
high = n-1
low = 0
last = 0
if(a[high] == a[low] and last < a[low]):
count = 0
ans = ""
ch, ah = getHighAns(a, low, high)
cl, al = getLowAns(a, low, high)
if(ch > cl):
count += ch
ans += ah
print(count)
print(ans)
else:
count += cl
ans += al
print(count)
print(ans)
else:
low = 0
high = n-1
last = 0
count = 1
ans = ""
if(a[low] < a[high]):
last = a[low]
ans += 'L'
low += 1
else:
last = a[high]
ans += 'R'
high -= 1
while(low <= high):
if(a[high] == a[low] and last < a[low]):
ch, ah = getHighAns(a, low, high)
cl, al = getLowAns(a, low, high)
if(ch > cl):
count += ch
ans += ah
break
else:
count += cl
ans += al
break
elif(last < a[low] < a[high]):
last = a[low]
ans += 'L'
count += 1
low += 1
elif(last < a[high] < a[low]):
last = a[high]
ans += 'R'
count += 1
high -= 1
elif(last < a[low]):
last = a[low]
ans += 'L'
count += 1
low += 1
elif(last < a[high]):
last = a[high]
ans += 'R'
count += 1
high -= 1
else:
break
print(count)
print(ans)
``` | output | 1 | 6,634 | 12 | 13,269 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement. | instruction | 0 | 6,635 | 12 | 13,270 |
Tags: greedy
Correct Solution:
```
def getLens(arr,low,high,last):
l1 = 1
last1 = last
for i in range(low+1,high):
if arr[i] > last:
last = arr[i]
#print(last,l1)
l1 += 1
else:
break
l2 = 1
last = last1
for i in range(high-1,low,-1):
if arr[i] > last:
last = arr[i]
l2 += 1
else:
break
#print(l1,l2,low,high)
return l1,l2
def main():
n = int(input())
arr = list(map(int,input().split()))
low = 0
high = n-1
k = 0
ans = []
last = 0
while low < high:
if arr[low] < arr[high]:
if arr[low] > last:
ans.append('L')
last = arr[low]
low += 1
elif arr[high] > last:
ans.append('R')
last = arr[high]
high -= 1
else:
break
elif arr[high] < arr[low]:
if arr[high] > last:
ans.append('R')
last = arr[high]
high -= 1
elif arr[low] > last:
ans.append('L')
last = arr[low]
low += 1
else:
break
else:
if arr[low] <= last:
break
last = arr[low]
l1,l2 = getLens(arr,low,high,last)
#print(l1,l2)
if l1 >= l2:
for i in range(l1):
ans.append('L')
else:
for i in range(l2):
ans.append('R')
break
if low == high:
if arr[low] > last:
ans.append('L')
print(len(ans))
for i in ans:
print(i,end = '')
main()
``` | output | 1 | 6,635 | 12 | 13,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement.
Submitted Solution:
```
n=int(input())
arr=[int(x) for x in input().split()]
ans=''
i=0
j=n-1
last=0
while i<=j:
#print(ans)
if arr[i]>last and arr[j]>last:
if arr[i]>arr[j]:
ans+='R'
last=arr[j]
j-=1
elif arr[j]>arr[i]:
ans+='L'
last=arr[i]
i+=1
else:
a1=1
a2=1
while i+a1<=j and arr[i+a1]>arr[i+a1-1]:
a1+=1
while j-a2>=i and arr[j-a2+1]<arr[j-a2]:
a2+=1
#print(a1,a2)
if a1>a2:
ans+='L'*a1
break
else:
ans+='R'*a2
break
elif arr[i]>last:
ans+='L'
last=arr[i]
i+=1
elif arr[j]>last:
ans+='R'
last=arr[j]
j-=1
else:
break
print(len(ans))
print(ans)
``` | instruction | 0 | 6,637 | 12 | 13,274 |
Yes | output | 1 | 6,637 | 12 | 13,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [1, 2, 4, 3, 2] the answer is 4 (you take 1 and the sequence becomes [2, 4, 3, 2], then you take the rightmost element 2 and the sequence becomes [2, 4, 3], then you take 3 and the sequence becomes [2, 4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1, 2, 3, 4]).
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 (1 β€ a_i β€ 2 β
10^5), where a_i is the i-th element of a.
Output
In the first line of the output print k β the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
Note
The first example is described in the problem statement.
Submitted Solution:
```
def getHighAns(a, low, high):
count = 1
ans = ''
ans += 'R'
last = a[high]
high -= 1
while(low <= high):
if(last < a[high]):
count += 1
ans += 'R'
last = a[high]
high -= 1
else:
break
return count, ans
def getLowAns(a, low, high):
count = 1
ans = ''
ans += 'L'
last = a[low]
low += 1
while(low <= high):
if(last < a[low]):
count += 1
ans += 'L'
last = a[low]
low += 1
else:
break
return count, ans
n = int(input())
a = list(map(int, input().split()))
high = n-1
low = 0
last = 0
if(a[high] == a[low] and last < a[low]):
count = 0
ans = ""
ch, ah = getHighAns(a, low, high)
cl, al = getLowAns(a, low, high)
if(ch > cl):
count += ch
ans += ah
print(count)
print(ans)
else:
count += cl
ans += al
print(count)
print(ans)
else:
low = 0
high = n-1
last = 0
count = 1
ans = ""
if(a[low] < a[high]):
last = a[low]
ans += 'L'
low += 1
else:
last = a[high]
ans += 'R'
high -= 1
while(low <= high):
if(a[high] == a[low] and last < a[low]):
ch, ah = getHighAns(a, low, high)
cl, al = getLowAns(a, low, high)
print(cl, ch)
if(ch > cl):
count += ch
ans += ah
break
else:
count += cl
ans += al
break
elif(last < a[low] < a[high]):
last = a[low]
ans += 'L'
count += 1
low += 1
elif(last < a[high] < a[low]):
last = a[high]
ans += 'R'
count += 1
high -= 1
elif(last < a[low]):
last = a[low]
ans += 'L'
count += 1
low += 1
elif(last < a[high]):
last = a[high]
ans += 'R'
count += 1
high -= 1
else:
break
print(count)
print(ans)
``` | instruction | 0 | 6,641 | 12 | 13,282 |
No | output | 1 | 6,641 | 12 | 13,283 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.