message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum. | instruction | 0 | 108,069 | 12 | 216,138 |
Tags: constructive algorithms, math
Correct Solution:
```
"""
ID: happyn61
LANG: PYTHON3
PROB: loan
"""
#from collections import defaultdict
import sys
import heapq
from collections import deque
#fin = open ('loan.in', 'r')
#fout = open ('loan.out', 'w')
#print(dic["4734"])
def find(parent,i):
if parent[i] != i:
parent[i]=find(parent,parent[i])
return parent[i]
# A utility function to do union of two subsets
def union(parent,rank,xx,yy):
x=find(parent,xx)
y=find(parent,yy)
if rank[x]>rank[y]:
parent[y]=x
elif rank[y]>rank[x]:
parent[x]=y
else:
parent[y]=x
rank[x]+=1
ans=0
#NQ=sys.stdin.readline().strip().split()
n=int(sys.stdin.readline().strip())
#N1=int(NQ[0])
#N2=int(NQ[1])
#N3=int(NQ[1])
for j in range(n):
AB=sys.stdin.readline().strip().split()
n=int(AB[0])
m=int(AB[1])
l=[str(i+1) for i in range(m)]
if 2*m-1-n==0:
l.reverse()
print(" ".join(l))
else:
ll=l[2*m-1-n:]
ll.reverse()
k=l[:2*m-1-n]+ll
print(" ".join(k))
#l=sys.stdin.readline().strip().split()
#s1=sys.stdin.readline().strip()
#s2=sys.stdin.readline().strip()
#print(pre,post,ll,rr,m1,m2,pre[ll],post[n-1-rr],post[n-1-rr][2])
#if F:
# print("yes")
#else:
# print("no")
#return True
#for x,y in occupy:
# l[x][y]="X"
#for ll in l:
# print("".join(ll))
``` | output | 1 | 108,069 | 12 | 216,139 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum. | instruction | 0 | 108,070 | 12 | 216,140 |
Tags: constructive algorithms, math
Correct Solution:
```
"""
Code of Ayush Tiwari
Codeforces: servermonk
Codechef: ayush572000
"""
#import sys
#input = sys.stdin.buffer.readline
#Fast IO
import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self,a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
def solution():
# This is the main code
n,k=map(int,input().split())
l=[]
for i in range(1,2*k-n):
l.append(i)
for i in range(k,2*k-n-1,-1):
l.append(i)
print(*l)
t=int(input())
for _ in range(t):
solution()
``` | output | 1 | 108,070 | 12 | 216,141 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum. | instruction | 0 | 108,071 | 12 | 216,142 |
Tags: constructive algorithms, math
Correct Solution:
```
for _ in range(int(input())):
n, k = map(int, input().split())
if n == 1 or n == 2 or n == k:
print(*[i for i in range(1, n+1)])
else:
a = [i+1 for i in range(n-2*(n-k)-1, k)]
a.reverse()
print(*[i+1 for i in range(n-2*(n-k)-1)], *a)
``` | output | 1 | 108,071 | 12 | 216,143 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum. | instruction | 0 | 108,072 | 12 | 216,144 |
Tags: constructive algorithms, math
Correct Solution:
```
for t in range(int(input())):n,k = map(int,input().split());print(*(list(range(1,k-(n-k))) + list(range(k,k-(n-k)-1,-1))))
``` | output | 1 | 108,072 | 12 | 216,145 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum. | instruction | 0 | 108,073 | 12 | 216,146 |
Tags: constructive algorithms, math
Correct Solution:
```
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n,k=map(int,input().split())
aa=k-(n-k)
#print(aa)
print(*list(range(1,aa))+list(range(k,aa-1,-1)))
``` | output | 1 | 108,073 | 12 | 216,147 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum. | instruction | 0 | 108,074 | 12 | 216,148 |
Tags: constructive algorithms, math
Correct Solution:
```
import sys
import os
import math
import copy
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt,floor,factorial,gcd,log,ceil
from collections import deque,Counter,defaultdict
from itertools import permutations,combinations,accumulate
def Int(): return int(sys.stdin.readline())
def Mint(): return map(int,sys.stdin.readline().split())
def Lstr(): return list(sys.stdin.readline().strip())
def Str(): return sys.stdin.readline().strip()
def Mstr(): return map(str,sys.stdin.readline().strip().split())
def List(): return list(map(int,sys.stdin.readline().split()))
def Hash(): return dict()
def Mod(): return 1000000007
def Mat2x2(n): return [List() for _ in range(n)]
def Lcm(x,y): return (x*y)//gcd(x,y)
def dtob(n): return bin(n).replace("0b","")
def btod(n): return int(n,2)
def watch(x): return print(x)
def common(l1, l2): return set(l1).intersection(l2)
def Most_frequent(list): return max(set(list), key = list.count)
def solution():
for _ in range(Int()):
n,k=Mint()
a=[]
for i in range(1,k+1):
a.append(i)
x=k-1
for i in range(k+1,n+1):
a.append(x)
x-=1
b=Counter(a)
ans=[]
for i in range(n):
if(b[a[i]]==2):
b[a[i]]=1
else:
ans.append(a[i])
print(*ans)
if __name__ == "__main__":
solution()
``` | output | 1 | 108,074 | 12 | 216,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum. | instruction | 0 | 108,075 | 12 | 216,150 |
Tags: constructive algorithms, math
Correct Solution:
```
T = int(input())
for t in range(T):
n, k = tuple([int(x) for x in input().split()])
last_num = k - (n-k)
result = []
start_reverse = False
cur_num = 1
for i in range(1, k+1):
if i < last_num:
result.append(cur_num)
cur_num += 1
else:
if start_reverse == False:
start_reverse = True
cur_num = k
result.append(cur_num)
cur_num -= 1
print(" ".join([str(x) for x in result]))
``` | output | 1 | 108,075 | 12 | 216,151 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n,k=map(int,input().split())
a=[i for i in range(1,k+1)]
print(*(a[:-(n-k)-1]+a[-1:-(n-k)-2:-1]))
``` | instruction | 0 | 108,076 | 12 | 216,152 |
Yes | output | 1 | 108,076 | 12 | 216,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
for _ in range(int(input())):
n, k = map(int, input().split())
for i in range(1, 2*k-n):print(i, end=' ')
for i in range(k, 2*k-n-1, -1):print(i, end=' ')
print()
``` | instruction | 0 | 108,077 | 12 | 216,154 |
Yes | output | 1 | 108,077 | 12 | 216,155 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
'''
4
1 1
2 2
3 2
4 3
'''
n=int(input())
for i in range(0,n):
o=input().rstrip().split(' ')
N=int(o[0])
K=int(o[1])
if N==K:
for j in range(1,K+1):
print(j,end=' ')
print()
else:
T=K-(N-K);
H=K-T;
L=[0]*K;
for j in range(len(L)-1,-1,-1):
if H>0:
H-=1;
else:
L[j]=K;
G=j;
break;
E=1;
for j in range(0,G):
L[j]=E;
E+=1;
E=K-1;
for j in range(G+1,len(L)):
L[j]=E;
E-=1;
print(*L)
``` | instruction | 0 | 108,078 | 12 | 216,156 |
Yes | output | 1 | 108,078 | 12 | 216,157 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
from collections import Counter
import string
import math
import sys
# sys.setrecursionlimit(10**6)
from fractions import Fraction
def array_int():
return [int(i) for i in sys.stdin.readline().split()]
def vary(arrber_of_variables):
if arrber_of_variables==1:
return int(sys.stdin.readline())
if arrber_of_variables>=2:
return map(int,sys.stdin.readline().split())
def makedict(var):
return dict(Counter(var))
testcases=vary(1)
for _ in range(testcases):
n,k=vary(2)
num=[i for i in range(1,k+1)]
if n==k:
print(*num)
else:
l = [i for i in range(1,2*k-n)]
for i in range(n-k+1):
l.append(k-i)
print(*l)
``` | instruction | 0 | 108,079 | 12 | 216,158 |
Yes | output | 1 | 108,079 | 12 | 216,159 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
for _ in range(int(input())):
n,k=map(int,input().split())
if n==k:
for i in range(1,k+1):
print(i,end=' ')
print('')
else:
ar=[]
for i in range(1,k+1):
ar.append(i)
if k>=2:
ar[k-1],ar[k-2]=ar[k-2],ar[k-1]
for i in ar:
print(i,end=' ')
print('')
``` | instruction | 0 | 108,080 | 12 | 216,160 |
No | output | 1 | 108,080 | 12 | 216,161 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
from sys import stdin
stdin.readline
def mp(): return list(map(int, stdin.readline().strip().split()))
def it():return int(stdin.readline().strip())
for _ in range(it()):
n,k=mp()
t=n-k+1
l=[i for i in range(1,k+1)]
p=l[t-1:]
p.reverse()
w=l[:t-1]+p
print(*w)
``` | instruction | 0 | 108,081 | 12 | 216,162 |
No | output | 1 | 108,081 | 12 | 216,163 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
for _ in range(int(input())):
n,k=input().split()
n=int(n)
k=int(k)
l=[]
a=2*k-n
for i in range(k):
if(2*k-n>=i+1):
l.append(i+1)
else:
break
for i in range(n-k+1):
l.append(k-i)
print(*l)
``` | instruction | 0 | 108,082 | 12 | 216,164 |
No | output | 1 | 108,082 | 12 | 216,165 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a sequence a with n elements 1, 2, 3, ..., k - 1, k, k - 1, k - 2, ..., k - (n - k) (k β€ n < 2k).
Let's call as inversion in a a pair of indices i < j such that a[i] > a[j].
Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i] = p[a[i]].
Your goal is to find such permutation p that the total number of inversions in b doesn't exceed the total number of inversions in a, and b is lexicographically maximum.
Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.
Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that s_i β t_i, s_i < t_i holds (in the first position that these sequences are different, s has smaller number than t).
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains two integers n and k (k β€ n < 2k; 1 β€ k β€ 10^5) β the length of the sequence a and its maximum.
It's guaranteed that the total sum of k over test cases doesn't exceed 10^5.
Output
For each test case, print k integers β the permutation p which maximizes b lexicographically without increasing the total number of inversions.
It can be proven that p exists and is unique.
Example
Input
4
1 1
2 2
3 2
4 3
Output
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence a = [1], there is only one permutation p = [1].
In the second test case, the sequence a = [1, 2]. There is no inversion in a, so there is only one permutation p = [1, 2] which doesn't increase the number of inversions.
In the third test case, a = [1, 2, 1] and has 1 inversion. If we use p = [2, 1], then b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 1 inversion.
In the fourth test case, a = [1, 2, 3, 2], and since p = [1, 3, 2] then b = [1, 3, 2, 3]. Both a and b have 1 inversion and b is the lexicographically maximum.
Submitted Solution:
```
# ------- main function -------
def solve():
n,k = map(int,input().split(' '))
inv = n-k
p = [i for i in range(1,k-inv)]
for i in range(k,k-inv-1,-1):
p.append(i)
print(p)
# ------- starting point of program -------
if __name__ == "__main__":
for _ in range(int(input())):
solve()
``` | instruction | 0 | 108,083 | 12 | 216,166 |
No | output | 1 | 108,083 | 12 | 216,167 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem!
Nastia has a hidden permutation p of length n consisting of integers from 1 to n. You, for some reason, want to figure out the permutation. To do that, you can give her an integer t (1 β€ t β€ 2), two different indices i and j (1 β€ i, j β€ n, i β j), and an integer x (1 β€ x β€ n - 1).
Depending on t, she will answer:
* t = 1: max{(min{(x, p_i)}, min{(x + 1, p_j)})};
* t = 2: min{(max{(x, p_i)}, max{(x + 1, p_j)})}.
You can ask Nastia at most β \frac {3 β
n} { 2} β + 30 times. It is guaranteed that she will not change her permutation depending on your queries. Can you guess the permutation?
Input
The input consists of several test cases. In the beginning, you receive the integer T (1 β€ T β€ 10 000) β the number of test cases.
At the beginning of each test case, you receive an integer n (3 β€ n β€ 10^4) β the length of the permutation p.
It's guaranteed that the permutation is fixed beforehand and that the sum of n in one test doesn't exceed 2 β
10^4.
Interaction
To ask a question, print "? t i j x" (t = 1 or t = 2, 1 β€ i, j β€ n, i β j, 1 β€ x β€ n - 1) Then, you should read the answer.
If we answer with β1 instead of a valid answer, that means you exceeded the number of queries or made an invalid query. Exit immediately after receiving β1 and you will see the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
To print the answer, print "! p_1 p_2 β¦ p_{n} (without quotes). Note that answering doesn't count as one of the β \frac {3 β
n} {2} β + 30 queries.
After printing a query or printing the answer, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* See the documentation for other languages.
Hacks
To hack the solution, use the following test format.
The first line should contain a single integer T (1 β€ T β€ 10 000) β the number of test cases.
For each test case in the first line print a single integer n (3 β€ n β€ 10^4) β the length of the hidden permutation p.
In the second line print n space-separated integers p_1, p_2, β¦, p_n (1 β€ p_i β€ n), where p is permutation.
Note that the sum of n over all test cases should not exceed 2 β
10^4.
Example
Input
2
4
3
2
5
3
Output
? 2 4 1 3
? 1 2 4 2
! 3 1 4 2
? 2 3 4 2
! 2 5 3 4 1
Note
Consider the first test case.
The hidden permutation is [3, 1, 4, 2].
We print: "? 2 4 1 3" and get back min{(max{(3, p_4}), max{(4, p_1)})} = 3.
We print: "? 1 2 4 2" and get back max{(min{(2, p_2)}, min{(3, p_4)})} = 2.
Consider the second test case.
The hidden permutation is [2, 5, 3, 4, 1].
We print: "? 2 3 4 2" and get back min{(max{(2, p_3}), max{(3, p_4)})} = 3. | instruction | 0 | 108,093 | 12 | 216,186 |
Tags: constructive algorithms, interactive
Correct Solution:
```
import math,sys
## from itertools import permutations, combinations;import heapq,random;
from collections import defaultdict,deque
import bisect as bi
def yes():print('YES')
def no():print('NO')
# sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
def I():return (int(sys.stdin.readline()))
def In():return(map(int,sys.stdin.readline().split()))
def Sn():return sys.stdin.readline().strip()
#sys.setrecursionlimit(1500)
def dict(a):
d={}
for x in a:
if d.get(x,-1)!=-1:
d[x]+=1
else:
d[x]=1
return d
def find_gt(a, x):
'Find leftmost value greater than x'
i = bi.bisect_left(a, x)
if i != len(a):
return i
else:
return -1
def query(type,st,end,x):
print('?',type,st,end,x,flush=True)
def main():
try:
global topi,ans
n=I()
d={}
st=-1
ans=[0]*(n+1)
topi=n-1
end = n-1 if n&1==1 else n
for i in range(0,end,2):
query(1,i+1,i+2,topi)
q=I()
if q==topi:
query(1,i+2,i+1,topi)
x=I()
if x==n:
st=i+1
break
elif q==n:
st=i+2
break
if st==-1:
st=n
ans[st]=n
for i in range(st-1,0,-1):
query(2,i,st,1)
q=I()
ans[i]=q
for i in range(st+1,n+1):
query(2,i,st,1)
q=I()
ans[i]=q
print('!',*ans[1:],flush=True)
except:
pass
M = 998244353
P = 1000000007
if __name__ == '__main__':
for _ in range(I()):main()
# for _ in range(1):main()
#End#
# ******************* All The Best ******************* #
``` | output | 1 | 108,093 | 12 | 216,187 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,098 | 12 | 216,196 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
def fuk(a):
if a[0]<=a[1]<=a[2]:
return 1
if a[0]>=a[1]>=a[2]:
return 1
return 0
def fukk(a):
if a[0]<=a[1]<=a[2]:
return 1
if a[1]<=a[2]<=a[3]:
return 1
if a[0]<=a[2]<=a[3]:
return 1
if a[0]<=a[1]<=a[3]:
return 1
if a[0]>=a[1]>=a[2]:
return 1
if a[1]>=a[2]>=a[3]:
return 1
if a[0]>=a[2]>=a[3]:
return 1
if a[0]>=a[1]>=a[3]:
return 1
return 0
for _ in range(int(input())):
n = int(input())
l = list(map(int,input().split()))
ans = 0
ll = []
if n>=3:
for i in range(3):
ll.append(l[i])
for i in range(3,n):
if fuk(ll):
ans+=1
ll.pop(0)
ll.append(l[i])
ans+=(fuk(ll))
ll = []
if n>3:
for i in range(4):
ll.append(l[i])
for i in range(4,n):
if fukk(ll):
ans+=1
ll.pop(0)
ll.append(l[i])
ans+=(fukk(ll))
q = 0
for i in range(n):
if i<=3:
q+=(n-i)
else:
break
print(q-ans)
``` | output | 1 | 108,098 | 12 | 216,197 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,099 | 12 | 216,198 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
if i+2 < n:
if a[i] == a[i+1]:
continue
elif a[i] < a[i+1]:
if a[i+1] <= a[i+2]:
continue
else:
ans += 1
if i+3 < n:
if a[i] <= a[i+2]:
continue
else:
if a[i+2] >= a[i+3] or a[i+1] <= a[i+3]:
continue
else:
ans += 1
else:
if a[i+1] >= a[i+2]:
continue
else:
ans += 1
if i+3 < n:
if a[i] >= a[i+2]:
continue
else:
if a[i+2] <= a[i+3] or a[i+1] >= a[i+3]:
continue
else:
ans += 1
print(ans + n + (n-1))
``` | output | 1 | 108,099 | 12 | 216,199 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,100 | 12 | 216,200 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq,bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=2*10**9, func=lambda a, b: min(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: max(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] < key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEndOfWord = False
class Trie:
def __init__(self):
self.root = self.getNode()
def getNode(self):
return TrieNode()
def _charToIndex(self, ch):
return ord(ch) - ord('a')
def insert(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
pCrawl.children[index] = self.getNode()
pCrawl = pCrawl.children[index]
pCrawl.isEndOfWord = True
def search(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
return False
pCrawl = pCrawl.children[index]
return pCrawl != None and pCrawl.isEndOfWord
#-----------------------------------------trie---------------------------------
class Node:
def __init__(self, data):
self.data = data
self.count=0
self.left = None # left node for 0
self.right = None # right node for 1
class BinaryTrie:
def __init__(self):
self.root = Node(0)
def insert(self, pre_xor):
self.temp = self.root
for i in range(31, -1, -1):
val = pre_xor & (1 << i)
if val:
if not self.temp.right:
self.temp.right = Node(0)
self.temp = self.temp.right
self.temp.count+=1
if not val:
if not self.temp.left:
self.temp.left = Node(0)
self.temp = self.temp.left
self.temp.count += 1
self.temp.data = pre_xor
def query(self, xor):
self.temp = self.root
for i in range(31, -1, -1):
val = xor & (1 << i)
if not val:
if self.temp.left and self.temp.left.count>0:
self.temp = self.temp.left
elif self.temp.right:
self.temp = self.temp.right
else:
if self.temp.right and self.temp.right.count>0:
self.temp = self.temp.right
elif self.temp.left:
self.temp = self.temp.left
self.temp.count-=1
return xor ^ self.temp.data
# --------------------------------------------------binary-----------------------------------
for ik in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
s=sorted(set(l+[]))
ind=defaultdict(int)
for i in range(len(s)):
ind[s[i]]=i
ma=0
for i in range(n):
l[i]=ind[l[i]]
ma=max(i,ma)
e=[n]*(ma+1)
e1=[n] * (ma + 1)
e2=[n]*(ma+1)
s = SegmentTree1(e)
s1=SegmentTree1(e1)
s2 = SegmentTree1(e2)
nextg=[n]*(n+1)
nexts=[n]*(n+1)
ind = [n] * (n + 1)
ind1 = [n] * (n + 1)
for i in range(n-1,-1,-1):
nextg[i]=s.query(l[i],ma)
nexts[i]=s.query(0,l[i])
s.__setitem__(l[i],i)
ind[i]=s1.query(0,l[i])
ind1[i]=s2.query(l[i],ma)
s1.__setitem__(l[i],nexts[i])
s2.__setitem__(l[i],nextg[i])
e[l[i]]=i
end=[0]*n
for i in range(n):
end[i]=min(ind[i],ind1[i])-1
ans=1
for i in range(n-2,-1,-1):
end[i]=min(end[i],end[i+1])
ans+=end[i]-i+1
print(ans)
``` | output | 1 | 108,100 | 12 | 216,201 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,101 | 12 | 216,202 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
def is_bad(l):
for i in range(2):
for j in range(i+1,3):
for k in range(j+1,4):
if l[i] >= l[j] >= l[k] or l[i] <= l[j] <= l[k]:
return True
return False
def solve():
n = int(input())
inp = list(map(int,input().split()))
ans = n + n - 1
for i in range(n-2):
if all(inp[i+x] <= inp[i+x+1] for x in range(2)) or all(inp[i+x] >= inp[i+x+1] for x in range(2)):
pass
else:
ans += 1
for i in range(n-3):
if is_bad(inp[i:i + 4]):
pass
else:
ans += 1
print(ans)
tests = int(input())
for _ in range(tests):
solve()
``` | output | 1 | 108,101 | 12 | 216,203 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,102 | 12 | 216,204 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
def is_bad(l):
if l[0]<=l[1]<=l[2] or l[0]>=l[1]>=l[2]:return True
if l[0]<=l[1]<=l[3] or l[0]>=l[1]>=l[3]:return True
if l[0]<=l[2]<=l[3] or l[0]>=l[2]>=l[3]:return True
if l[1]<=l[2]<=l[3] or l[1]>=l[2]>=l[3]:return True
return False
def solve():
n = int(input())
inp = list(map(int,input().split()))
ans = n + n - 1
for i in range(n-2):
#print(10000000000+i)
if all(inp[i+x] <= inp[i+x+1] for x in range(2)) or all(inp[i+x] >= inp[i+x+1] for x in range(2)):
pass
else:
ans += 1
for i in range(n-3):
#print("asd")
if is_bad(inp[i:i + 4]):
pass
else:
ans += 1
print(ans)
tests = int(input())
for _ in range(tests):
solve()
``` | output | 1 | 108,102 | 12 | 216,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,103 | 12 | 216,206 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#######################################
def f(p1,p2,p3):
l2=[p1,p2,p3]
l2.sort()
if len(set([p1[0],p2[0],p3[0]]))>1:
if l2[1][0]==l2[2][0]:
if l2[1][1]<l2[0][1]<l2[2][1]:
return True
else:
return False
if l2[0][0]==l2[1][0]:
if l2[0][1]<l2[2][1]<l2[1][1]:
return True
else:
return False
a=min(l2[0][1],l2[2][1])
b=max(l2[0][1],l2[2][1])
if not (a<=l2[1][1]<=b):
return True
return False
for t in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
ans=2*n-1
for i in range(n-2):
if f([l[i],i],[l[i+1],i+1],[l[i+2],i+2]):
ans+=1
for i in range(n-3):
p1=[l[i],i]
p2=[l[i+1],i+1]
p3=[l[i+2],i+2]
p4=[l[i+3],i+3]
if f(p1,p2,p3) and f(p1,p3,p4) and f(p1,p2,p4) and f(p2,p3,p4):
ans+=1
print(ans)
``` | output | 1 | 108,103 | 12 | 216,207 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,104 | 12 | 216,208 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
import sys
t = int(sys.stdin.readline())
while(t>0):
n = int(sys.stdin.readline())
a = list(map(int, input().split()))
c = 2*n-1
if n<=2:
print(c)
else:
for i in range(n-2):
j = i+1
k = j+1
if a[i]>=a[j]>=a[k] or a[i]<=a[j]<=a[k]:
c+=0
else:
c+=1
for i in range(n-3):
j = i+1
k = j+1
l = k+1
if a[i]>=a[j]>=a[k] or a[i]<=a[j]<=a[k]:
c+=0
elif a[j]>=a[k]>=a[l] or a[j]<=a[k]<=a[l]:
c+=0
elif a[i]>=a[j]>=a[l] or a[i]<=a[j]<=a[l]:
c+=0
elif a[i]>=a[k]>=a[l] or a[i]<=a[k]<=a[l]:
c+=0
else:
c+=1
print(c)
t-=1
``` | output | 1 | 108,104 | 12 | 216,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)). | instruction | 0 | 108,105 | 12 | 216,210 |
Tags: brute force, geometry, greedy, implementation
Correct Solution:
```
from collections import *
import os, sys
from io import BytesIO, IOBase
from itertools import combinations
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda dtype: dtype(input().strip())
inp_d = lambda dtype: [dtype(x) for x in input().split()]
inp_2d = lambda dtype, n: [inp(dtype) for _ in range(n)]
inp_2ds = lambda dtype, n: [inp_d(dtype) for _ in range(n)]
inp_enu = lambda dtype: [(i, x) for i, x in enumerate(inp_d(dtype))]
inp_enus = lambda dtype, n: [[i, inp_d(dtype)] for i in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
for _ in range(inp(int)):
n, a = inp(int), inp_d(int)
good = 2 * n - 1
for i in range(2, n):
good += not (min(a[i - 2], a[i]) <= a[i - 1] <= max(a[i], a[i - 2]))
for i in range(4, n + 1):
bad = False
for com in combinations(a[i - 4:i], 3):
bad |= min(com[0], com[2]) <= com[1] <= max(com[2], com[0])
good += 1 - bad
print(good)
``` | output | 1 | 108,105 | 12 | 216,211 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
from collections import *
import os, sys
from io import BytesIO, IOBase
from itertools import combinations
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda dtype: dtype(input().strip())
inp_d = lambda dtype: [dtype(x) for x in input().split()]
inp_2d = lambda dtype, n: [inp(dtype) for _ in range(n)]
inp_2ds = lambda dtype, n: [inp_d(dtype) for _ in range(n)]
inp_enu = lambda dtype: [(i, x) for i, x in enumerate(inp_d(dtype))]
inp_enus = lambda dtype, n: [[i, inp_d(dtype)] for i in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
for _ in range(inp(int)):
n, a = inp(int), inp_d(int)
good = 2 * n - 1
for i in range(2, n):
good += not (a[i - 2] >= a[i - 1] >= a[i] or a[i - 2] <= a[i - 1] <= a[i])
for i in range(4, n + 1):
bad = False
for com in combinations(a[i - 4:i], 3):
bad |= com[0] >= com[1] >= com[2] or com[0] <= com[1] <= com[2]
good += 1 - bad
print(good)
``` | instruction | 0 | 108,106 | 12 | 216,212 |
Yes | output | 1 | 108,106 | 12 | 216,213 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
from copy import *
import sys
def init(N,node,A,unit,func):
n=1
while n<N:
n<<=1
for i in range(n*2-1):
if len(node)<=i:
node.append(deepcopy(unit))
else:
node[i]=deepcopy(unit)
for i in range(len(A)):
node[n-1+i]=deepcopy(A[i])
for i in range(n-2,-1,-1):
node[i]=func(node[(i<<1)+1],node[(i<<1)+2])
node.append(func)
node.append(unit)
node.append(n)
def upd(node,x,a):
y=node[-1]+x
node[y-1]=a
while y>1:
y=y>>1
node[y-1]=node[-3](node[(y<<1)-1],node[y<<1])
def query(node,l,r):
x,y=l,r
z=node[-1]-1
rr=deepcopy(node[-2])
rl=deepcopy(node[-2])
while True:
if x==y:
return node[-3](rl,rr)
if x&1:
rl=node[-3](rl,node[x+z])
x+=1
if y&1:
rr=node[-3](node[y+z-1],rr)
if z==0:
return node[-3](rl,rr)
x>>=1
y>>=1
z>>=1
def bis_min_k(node,k,cond):
x=k+1
while True:
if node[-1]<=x:
return x-node[-1]
if cond(node[(x<<1)-1]):
x=x<<1
else:
x=(x<<1)+1
def bis_min(node,l,r,cond):
x,y=l,r
z=node[-1]-1
for i in range(30):
if x+(1<<i)>y:
break
if x&(1<<i):
if cond(node[z+(x>>i)]):
return bis_min_k(node,z+(x>>i),cond)
x+=(1<<i)
if z==0:
break
z>>=1
for i in range(29,-1,-1):
if i and ((node[-1]-1)>>(i-1))==0:
continue
if x+(1<<i)>y:
continue
if (y-x)&(1<<i):
if cond(node[((node[-1]-1)>>i)+(x>>i)]):
return bis_min_k(node,((node[-1]-1)>>i)+(x>>i),cond)
x+=(1<<i)
return node[-1]
input=sys.stdin.buffer.readline
INF=(10**9)+7
for t in range(int(input())):
sega=[]
segb=[]
segarev=[]
segbrev=[]
N=int(input())
A=list(map(int,input().split()))
init(N,sega,A[:],0,lambda x,y:max(x,y))
init(N,segb,A[:],INF,lambda x,y:min(x,y))
init(N,segarev,A[::-1],0,lambda x,y:max(x,y))
init(N,segbrev,A[::-1],INF,lambda x,y:min(x,y))
X=[INF]*N
for i in range(N):
ra=bis_min(sega,i+1,N,lambda x:x>=A[i])
rb=bis_min(segb,i+1,N,lambda x:x<=A[i])
la=N-1-bis_min(segarev,N-i,N,lambda x:x>=A[i])
lb=N-1-bis_min(segbrev,N-i,N,lambda x:x<=A[i])
if 0<=la<N and 0<=rb<N:
X[la]=min(X[la],rb)
if 0<=lb<N and 0<=ra<N:
X[lb]=min(X[lb],ra)
seg=[]
init(N,seg,[],INF,lambda x,y:min(x,y))
l=0
ANS=0
for r in range(N):
upd(seg,r,X[r])
while seg[0]<=r:
upd(seg,l,INF)
l+=1
ANS+=r-l+1
print(ANS)
``` | instruction | 0 | 108,107 | 12 | 216,214 |
Yes | output | 1 | 108,107 | 12 | 216,215 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
#DaRk DeveLopeR
import sys
#taking input as string
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
mod = 10**9+7; Mod = 998244353; INF = float('inf')
#______________________________________________________________________________________________________
import math
from bisect import *
from heapq import *
from collections import defaultdict as dd
from collections import OrderedDict as odict
from collections import Counter as cc
from collections import deque
from itertools import groupby
sys.setrecursionlimit(20*20*20*20+10) #this is must for dfs
def good(arr):
n=len(arr)
for i in range(n):
for j in range(i+1,n):
for k in range(j+1,n):
if max(arr[i],arr[k])>=arr[j] and min(arr[i],arr[k])<=arr[j]:
return False
return True
def solve():
n=takein()
arr=takeiar()
if n==1:
print(1)
return
if n==2:
print(3)
return
ans=n+n-1
for j in range(0,n-2):
if good(arr[j:j+3]):
ans+=1
if n==3:
print(ans)
return
for j in range(0,n-3):
if good(arr[j:j+4]):
ans+=1
print(ans)
return
def main():
global tt
if not ONLINE_JUDGE:
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
t = 1
t = takein()
#t = 1
for tt in range(1,t + 1):
solve()
if not ONLINE_JUDGE:
print("Time Elapsed :",time.time() - start_time,"seconds")
sys.stdout.close()
#---------------------- USER DEFINED INPUT FUNCTIONS ----------------------#
def takein():
return (int(sys.stdin.readline().rstrip("\r\n")))
# input the string
def takesr():
return (sys.stdin.readline().rstrip("\r\n"))
# input int array
def takeiar():
return (list(map(int, sys.stdin.readline().rstrip("\r\n").split())))
# input string array
def takesar():
return (list(map(str, sys.stdin.readline().rstrip("\r\n").split())))
# innut values for the diffrent variables
def takeivr():
return (map(int, sys.stdin.readline().rstrip("\r\n").split()))
def takesvr():
return (map(str, sys.stdin.readline().rstrip("\r\n").split()))
#------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
def ispalindrome(s):
return s==s[::-1]
def invert(bit_s):
# convert binary string
# into integer
temp = int(bit_s, 2)
# applying Ex-or operator
# b/w 10 and 31
inverse_s = temp ^ (2 ** (len(bit_s) + 1) - 1)
# convert the integer result
# into binary result and then
# slicing of the '0b1'
# binary indicator
rslt = bin(inverse_s)[3 : ]
return str(rslt)
def counter(a):
q = [0] * max(a)
for i in range(len(a)):
q[a[i] - 1] = q[a[i] - 1] + 1
return(q)
def counter_elements(a):
q = dict()
for i in range(len(a)):
if a[i] not in q:
q[a[i]] = 0
q[a[i]] = q[a[i]] + 1
return(q)
def string_counter(a):
q = [0] * 26
for i in range(len(a)):
q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
return(q)
def factorial(n,m = 1000000007):
q = 1
for i in range(n):
q = (q * (i + 1)) % m
return(q)
def factors(n):
q = []
for i in range(1,int(n ** 0.5) + 1):
if n % i == 0: q.append(i); q.append(n // i)
return(list(sorted(list(set(q)))))
def prime_factors(n):
q = []
while n % 2 == 0: q.append(2); n = n // 2
for i in range(3,int(n ** 0.5) + 1,2):
while n % i == 0: q.append(i); n = n // i
if n > 2: q.append(n)
return(list(sorted(q)))
def transpose(a):
n,m = len(a),len(a[0])
b = [[0] * n for i in range(m)]
for i in range(m):
for j in range(n):
b[i][j] = a[j][i]
return(b)
def power_two(x):
return (x and (not(x & (x - 1))))
def ceil(a, b):
return -(-a // b)
def seive(n):
a = [1]
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2,n + 1, p):
prime[i] = False
p = p + 1
for p in range(2,n + 1):
if prime[p]:
a.append(p)
return(a)
def pref(li):
pref_sum = [0]
for i in li:
pref_sum.append(pref_sum[-1]+i)
return pref_sum
def kadane(x): # maximum sum contiguous subarray
sum_so_far = 0
current_sum = 0
for i in x:
current_sum += i
if current_sum < 0:
current_sum = 0
else:
sum_so_far = max(sum_so_far, current_sum)
return sum_so_far
def binary_search(li, val):
# print(lb, ub, li)
ans = -1
lb = 0
ub = len(li)-1
while (lb <= ub):
mid = (lb+ub) // 2
# print('mid is',mid, li[mid])
if li[mid] > val:
ub = mid-1
elif val > li[mid]:
lb = mid+1
else:
ans = mid # return index
break
return ans
def upper_bound(li, num):
answer = -1
start = 0
end = len(li)-1
while (start <= end):
middle = (end+start) // 2
if li[middle] <= num:
answer = middle
start = middle+1
else:
end = middle-1
return answer # max index where x is not greater than num
def lower_bound(li, num):
answer = -1
start = 0
end = len(li)-1
while (start <= end):
middle = (end+start) // 2
if li[middle] >= num:
answer = middle
end = middle-1
else:
start = middle+1
return answer # min index where x is not less than num
#-----------------------------------------------------------------------#
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
input = sys.stdin.readline
main()
``` | instruction | 0 | 108,108 | 12 | 216,216 |
Yes | output | 1 | 108,108 | 12 | 216,217 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
for _ in range(int(input())):
n = int(input() )
a = list(map(int, input().split()))
#n, a, b = map(int, input().split())
#s = input()
h = []
# max 4 points?
i, j = 0, 1
ans = 1
while j < n:
d = []
if i+3 < j:
i += 1
notgood = True
while i+1 < j and notgood:
# if i+1 == j - stop
notgood = False
bad = i
for x in range(i, j+1):
for y in range(x+1, j+1):
for z in range(y+1, j+1):
d = sorted([abs(x-y)+abs(a[x]-a[y]), abs(y-z)+abs(a[y]-a[z]), abs(z-x)+abs(a[z]-a[x])])
if d[0] + d[1] == d[2]:
notgood = True
bad = x
if notgood:
i = bad + 1
ans += j-i+1
j += 1
print(ans)
``` | instruction | 0 | 108,109 | 12 | 216,218 |
Yes | output | 1 | 108,109 | 12 | 216,219 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
from decimal import Decimal
from decimal import *
from collections import defaultdict, deque
import heapq
from decimal import Decimal
getcontext().prec = 25
abcd='abcdefghijklmnopqrstuvwxyz'
MOD = 1000000007
BUFSIZE = 8192
from bisect import bisect_left, bisect_right
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# for _ in range(int(input())):
# n, k = map(int, input().split(" "))
# list(map(int, input().split(" ")))
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split(" ")))
ans = 2*n -1
for i in range(n-2):
if l[i+1] not in range(min(l[i], l[i+2]),max(l[i], l[i+2])+1):
ans+=1
for i in range(n-3):
if l[i+1] not in range(min(l[i], l[i+3]),max(l[i], l[i+3])+1) and l[i+2] not in range(min(l[i], l[i+3]),max(l[i], l[i+3])+1):
ans+=1
print(ans)
``` | instruction | 0 | 108,110 | 12 | 216,220 |
No | output | 1 | 108,110 | 12 | 216,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
def d(p1,p2):
return abs(p1[0]-p2[0])+abs(p1[1]-p2[1])
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
sol = 0
for i in range(n-2):
done = False
p1 = [a[i+0],1]
p2 = [a[i+1],2]
p3 = [a[i+2],3]
ds = [d(p1,p2),d(p2,p3),d(p1,p3)]
ds = sorted(ds)
if ds[2] != ds[1] + ds[0]:
sol += 1
if i < n-3:
al = a[i:i+4]
if len(set(al)) == 4:
if (a[i+1] == min(al) and a[i+2] == max(al)) or (a[i+1] == max(al) and a[i+2] == min(al)):
sol += 1
print(sol+n-1+n)
``` | instruction | 0 | 108,111 | 12 | 216,222 |
No | output | 1 | 108,111 | 12 | 216,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
from sys import stdin , stdout
from collections import defaultdict
import math
def get_list(): return list(map(int, stdin.readline().strip().split()))
def get_int(): return int(stdin.readline())
def get_ints(): return map(int, stdin.readline().strip().split())
def get_string(): return stdin.readline()
def printn(n) : stdout.write(str(n) + "\n")
def increasingSubseq(a, k, dp):
n = len(a)
for i in range(n) : dp[0][i] = i
for l in range(1,k):
for i in range(l,n):
for j in range(l-1,i):
if a[j] <= a[i]:
dp[l][i] = min(dp[l][i],dp[l-1][j])
return
def decreasingSubseq(a, k, dp):
n = len(a)
for i in range(n) : dp[0][i] = i
for l in range(1,k):
for i in range(l,n):
for j in range(l-1,i):
if a[j] >= a[i]:
dp[l][i] = min(dp[l][i],dp[l-1][j])
return
def solve() :
n = get_int()
a = get_list()
ans = 0
mx = 200002
dp1 = [[mx for i in range(n)] for i in range(3)]
dp2 = [[mx for i in range(n)] for i in range(3)]
increasingSubseq(a,3,dp1)
decreasingSubseq(a,3,dp2)
check = []
for i in range(n):
if dp1[2][i] != dp2[2][i] :
if dp1[2][i] == mx:
check.append((dp2[2][i], -1))
elif dp2[2][i] == mx:
check.append((dp1[2][i], -1))
else :
check.append((dp1[2][i], dp2[2][i]))
else : check.append((dp1[2][i], dp2[2][i]))
for i in range(n):
for j in range(i,n):
if i not in check[j]: ans += 1
else : break
printn(ans)
if __name__ == "__main__" :
t = get_int()
# t = 1
while t:
t-=1
solve()
``` | instruction | 0 | 108,112 | 12 | 216,224 |
No | output | 1 | 108,112 | 12 | 216,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Suppose you have two points p = (x_p, y_p) and q = (x_q, y_q). Let's denote the Manhattan distance between them as d(p, q) = |x_p - x_q| + |y_p - y_q|.
Let's say that three points p, q, r form a bad triple if d(p, r) = d(p, q) + d(q, r).
Let's say that an array b_1, b_2, ..., b_m is good if it is impossible to choose three distinct indices i, j, k such that the points (b_i, i), (b_j, j) and (b_k, k) form a bad triple.
You are given an array a_1, a_2, ..., a_n. Calculate the number of good subarrays of a. A subarray of the array a is the array a_l, a_{l + 1}, ..., a_r for some 1 β€ l β€ r β€ n.
Note that, according to the definition, subarrays of length 1 and 2 are good.
Input
The first line contains one integer t (1 β€ t β€ 5000) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
For each test case, print the number of good subarrays of array a.
Example
Input
3
4
2 4 1 3
5
6 9 1 9 6
2
13 37
Output
10
12
3
Note
In the first test case, it can be proven that any subarray of a is good. For example, subarray [a_2, a_3, a_4] is good since it contains only three elements and:
* d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3 < d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7;
* d((a_2, 2), (a_3, 3)) < d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3));
* d((a_3, 3), (a_4, 4)) < d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4));
In the second test case, for example, subarray [a_1, a_2, a_3, a_4] is not good, since it contains a bad triple (a_1, 1), (a_2, 2), (a_4, 4):
* d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6;
* d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4;
* d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2;
So, d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4)).
Submitted Solution:
```
import sys
input = sys.stdin.readline
def main():
t = int(input()); INF = 1001001001
for _ in range(t):
n = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(n):
S = set([])
mn1 = INF
mn2 = INF
for j in range(i,n):
if A[j] >= mn2: break
if j - i == 2 and A[j] == A[j-1]: break
if A[j] > mn1:
mn2 = min(mn2,A[j])
if A[j] < mn1:
mn1 = A[j]
if A[j] in S:
mn2 = 0
S.add(A[j])
#print(i,j)
ans += 1
print(ans)
if __name__ == "__main__":
main()
``` | instruction | 0 | 108,113 | 12 | 216,226 |
No | output | 1 | 108,113 | 12 | 216,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:
1. Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 β€ q < k). The given operation is correct β both subsegments do not touch unexistent elements.
2. Determine the value in position x of array b, that is, find value bx.
For each query of the second type print the result β the value of the corresponding element of array b.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| β€ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| β€ 109).
Next m lines contain the descriptions of the queries. The i-th line first contains integer ti β the type of the i-th query (1 β€ ti β€ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 β€ xi, yi, ki β€ n) β the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 β€ xi β€ n) β the position in array b.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
Output
For each second type query print the result on a single line.
Examples
Input
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2
Output
0
3
-1
3
2
3
-1 | instruction | 0 | 108,153 | 12 | 216,306 |
Tags: data structures
Correct Solution:
```
import sys
'''
SEGMENT TREE
Assign
'''
class SegmTree():
'''
- modify elements on interval
- get single element
'''
def __init__(self, size):
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [0] * (2*N)
def modify_range(self, l, r, value):
l += self.N
r += self.N
while l < r:
if l & 1:
self.tree[l] = value
l += 1
if r & 1:
r -= 1
self.tree[r] = value
l >>= 1
r >>= 1
def query(self, i):
i += self.N
latest_change = self.tree[i]
p = i
while p > 1:
p >>= 1
latest_change = max(latest_change, self.tree[p])
return latest_change
# inf = open('input.txt', 'r')
# reader = (map(int, line.split()) for line in inf)
reader = (map(int, line.split()) for line in sys.stdin)
input = reader.__next__
n, m = input()
a = list(input())
b = list(input())
st = SegmTree(n)
request = [None] * (m + 1)
for i in range(1, m+1):
t, *arg = input()
if t == 1:
x, y, k = request[i] = arg
st.modify_range(y-1, y-1+k, i)
else:
pos = arg[0] - 1
req_id = st.query(pos)
if req_id > 0:
x, y, k = request[req_id]
ans = a[x+(pos-y)]
else:
ans = b[pos]
sys.stdout.write(f'{ans}\n')
# inf.close()
``` | output | 1 | 108,153 | 12 | 216,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:
1. Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 β€ q < k). The given operation is correct β both subsegments do not touch unexistent elements.
2. Determine the value in position x of array b, that is, find value bx.
For each query of the second type print the result β the value of the corresponding element of array b.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| β€ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| β€ 109).
Next m lines contain the descriptions of the queries. The i-th line first contains integer ti β the type of the i-th query (1 β€ ti β€ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 β€ xi, yi, ki β€ n) β the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 β€ xi β€ n) β the position in array b.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
Output
For each second type query print the result on a single line.
Examples
Input
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2
Output
0
3
-1
3
2
3
-1
Submitted Solution:
```
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for _ in range(m):
temp=list(map(int,input().split()))
if len(temp)==4:
x,y,k=temp[1],temp[2],temp[3]
#b=b[:y-1]+a[x-1:x+k]+b[y-1:]
b=b[:y-1]+a[x-1:x+k]+b[x+k:]
else:
x=temp[1]
print(b[x-1])
``` | instruction | 0 | 108,154 | 12 | 216,308 |
No | output | 1 | 108,154 | 12 | 216,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:
1. Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 β€ q < k). The given operation is correct β both subsegments do not touch unexistent elements.
2. Determine the value in position x of array b, that is, find value bx.
For each query of the second type print the result β the value of the corresponding element of array b.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| β€ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| β€ 109).
Next m lines contain the descriptions of the queries. The i-th line first contains integer ti β the type of the i-th query (1 β€ ti β€ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 β€ xi, yi, ki β€ n) β the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 β€ xi β€ n) β the position in array b.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
Output
For each second type query print the result on a single line.
Examples
Input
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2
Output
0
3
-1
3
2
3
-1
Submitted Solution:
```
read_line = lambda: [int(x) for x in input().split(' ')]
n, m = read_line()
a = read_line()
b = read_line()
class Node(object):
def __init__(_, l=0, r=n):
_.l, _.r = l, r
_.delta = None
if l + 1 < r:
_.m = (l + r) // 2
_.lc, _.rc = Node(l, _.m), Node(_.m, r)
def set(_, l, r, delta):
if _.r <= l or r <= _.l:
return
if l <= _.l and _.r <= r:
_.delta = delta
return
_.lc.delta, _.rc.delta = _.delta, _.delta
_.delta = None
_.lc.set(l, r, delta)
_.rc.set(l, r, delta)
def get(_, i):
if _.delta is not None:
return a[i + _.delta]
if _.l + 1 == _.r:
return b[i]
return _.lc.get(i) if i < _.m else _.rc.get(i)
tree = Node()
while m:
c = read_line()
if c[0] == 1:
tree.set(c[2] - 1, c[2] - 1 + c[3], c[1] - c[2])
else:
print(tree.get(c[1] - 1))
m -= 1
``` | instruction | 0 | 108,155 | 12 | 216,310 |
No | output | 1 | 108,155 | 12 | 216,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:
1. Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 β€ q < k). The given operation is correct β both subsegments do not touch unexistent elements.
2. Determine the value in position x of array b, that is, find value bx.
For each query of the second type print the result β the value of the corresponding element of array b.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| β€ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| β€ 109).
Next m lines contain the descriptions of the queries. The i-th line first contains integer ti β the type of the i-th query (1 β€ ti β€ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 β€ xi, yi, ki β€ n) β the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 β€ xi β€ n) β the position in array b.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
Output
For each second type query print the result on a single line.
Examples
Input
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2
Output
0
3
-1
3
2
3
-1
Submitted Solution:
```
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for _ in range(m):
temp=list(map(int,input().split()))
if len(temp)==4:
x,y,k=temp[1],temp[2],temp[3]
b=b[:y-1]+a[x-1:x+k]+b[y-1:]
else:
x=temp[1]
print(b[x-1])
``` | instruction | 0 | 108,156 | 12 | 216,312 |
No | output | 1 | 108,156 | 12 | 216,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:
1. Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 β€ q < k). The given operation is correct β both subsegments do not touch unexistent elements.
2. Determine the value in position x of array b, that is, find value bx.
For each query of the second type print the result β the value of the corresponding element of array b.
Input
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| β€ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| β€ 109).
Next m lines contain the descriptions of the queries. The i-th line first contains integer ti β the type of the i-th query (1 β€ ti β€ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 β€ xi, yi, ki β€ n) β the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 β€ xi β€ n) β the position in array b.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
Output
For each second type query print the result on a single line.
Examples
Input
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2
Output
0
3
-1
3
2
3
-1
Submitted Solution:
```
from bisect import bisect_left, bisect_right
n, m = map(int, input().split())
a, b = [''] + input().split(), [''] + input().split()
c, q = [(0, 0, 0)], []
for i in range(m):
t = input()
if t[0] == '1':
x, y, d = map(int, t[2: ].split())
p = (y, y + d, x - y)
u = v = bisect_right(c, p)
x, y, d = c[u - 1]
if y > p[0]:
if y > p[1]:
c = c[: u - 1] + [(x, p[0], d), p, (p[1], y, d)] + c[u: ]
continue
c[u - 1] = (x, p[0], d)
while v < len(c) and c[v][1] <= p[1]: v += 1
if v == len(c): c = c[: u] + [p]
elif c[v][0] < p[1]: c = c[: u] + [p, (p[1], c[v][1], c[v][2])] + c[v + 1: ]
else: c = c[: u] + [p] + c[v: ]
else:
p = (int(t[2: ]), n, n)
u = bisect_right(c, p) - 1
x, y, d = c[u]
if p[0] < y: q.append(a[p[0] + d])
else: q.append(b[p[0]])
print('\n'.join(q))
``` | instruction | 0 | 108,157 | 12 | 216,314 |
No | output | 1 | 108,157 | 12 | 216,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,242 | 12 | 216,484 |
Tags: combinatorics, math
Correct Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
from math import gcd, ceil
def prod(a, mod=10**9+7):
ans = 1
for each in a:
ans = (ans * each) % mod
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def modInverse(a, m) :
a = a % m;
for x in range(1, m) :
if ((a * x) % m == 1) :
return x
return 1
for _ in range(int(input()) if not True else 1):
n = int(input())
#n, k = map(int, input().split())
#a, b = map(int, input().split())
#c, d = map(int, input().split())
#a = list(map(int, input().split()))
#b = list(map(int, input().split()))
#s = input()
mod = 10**9 + 7
if 1:
max_n = 250000
fact, inv_fact = [0] * (max_n + 1), [0] * (max_n + 1)
fact[0] = 1
for i in range(max_n):
fact[i + 1] = fact[i] * (i + 1) % mod
inv_fact[-1] = pow(fact[-1], mod - 2, mod)
for i in reversed(range(max_n)):
inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod
def nCr_mod(n, r):
res = 1
while n or r:
a, b = n % mod, r % mod
if a < b:
return 0
res = res * fact[a] % mod * inv_fact[b] % mod * inv_fact[a - b] % mod
n //= mod
r //= mod
return res
print((nCr_mod(2*n,n) - n)%mod)
``` | output | 1 | 108,242 | 12 | 216,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,243 | 12 | 216,486 |
Tags: combinatorics, math
Correct Solution:
```
def fact(n):
res=1
for i in range(2,n+1):
res=(res*i)%(10**9+7)
return res
def rev(a):
return pow(a,10**9+5,10**9+7)
def c2nn(n):
return (fact(2*n+1)*(rev(fact(n))*rev(fact(n+1))))%(10**9+7)
n=int(input())
print(((c2nn(n-1)*2-n)%(10**9+7)+10**9+7)%(10**9+7))
``` | output | 1 | 108,243 | 12 | 216,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,244 | 12 | 216,488 |
Tags: combinatorics, math
Correct Solution:
```
n,a,b,M,I=int(input()),1,1,1000000007,1000000005
for i in range(1,n):
a=a*i%M
a=pow(a,I,M)
for i in range(n+1,n*2):
b=b*i%M
print(((2*a*b)%M-n)%M)
``` | output | 1 | 108,244 | 12 | 216,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,245 | 12 | 216,490 |
Tags: combinatorics, math
Correct Solution:
```
#love python :)
#medo journy to icpc
n = int(input())
m = int(1e9 + 7)
p = 1
for i in range(1, n + 1):
p *= 2 * n - i
p *= pow(i, m - 2, m)
p %= m
print((2 * p - n) % m)
``` | output | 1 | 108,245 | 12 | 216,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,246 | 12 | 216,492 |
Tags: combinatorics, math
Correct Solution:
```
def fact(n):
res=1
for i in range(2,n+1):
res=(res*i)%(10**9+7)
return res
def rev(a):
return pow(a,10**9+5,10**9+7)
def c2nn(n):
return fact(2*n)*(rev(fact(n))**2)%(10**9+7)
n=int(input())
print(c2nn(n)-n)
``` | output | 1 | 108,246 | 12 | 216,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,247 | 12 | 216,494 |
Tags: combinatorics, math
Correct Solution:
```
MOD=10**9+7
def power(x, a):
if(a==0):
return(1)
z=power(x, a//2)
z=(z*z)%MOD
if(a%2):
z=(z*x)%MOD
return(z)
def fact(n):
factn=1
for i in range(2, n+1):
factn=(factn*i)%MOD
return(factn)
def ncr(n, r):
return((fact(n)*power(fact(r), MOD-2)*power(fact(n-r), MOD-2))%MOD)
n=int(input())
print((2*ncr(2*n-1,n)-n)%MOD)
``` | output | 1 | 108,247 | 12 | 216,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,248 | 12 | 216,496 |
Tags: combinatorics, math
Correct Solution:
```
prod = 1
n = int(input())
for i in range(n+1, 2*n+1):
prod *= i
prod %= (10**9+7)
for i in range(1,n+1):
prod *= pow(i, 10**9+5, 10**9+7)
prod %= 10**9+7
print((prod-n)%(10**9+7))
``` | output | 1 | 108,248 | 12 | 216,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17 | instruction | 0 | 108,249 | 12 | 216,498 |
Tags: combinatorics, math
Correct Solution:
```
n = int(input())
m = int(1e9 + 7)
# binom(2n - 1, n)
p = 1
for i in range(1, n + 1):
p *= 2 * n - i
p *= pow(i, m - 2, m)
p %= m
print((2 * p - n) % m)
# Made By Mostafa_Khaled
``` | output | 1 | 108,249 | 12 | 216,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17
Submitted Solution:
```
# from math import factorial
import re
def factorial(n):
if n == 0:
return 1
else:
for i in range(1, n):
n = (n * i) % 1000000007
return n
def exgcd(a, b, c):
if b == 0:
c[0] = 1
c[1] = 0
return a
else:
ret = exgcd(b, a % b, c)
tmp = c[0] - a // b * c[1]
c[0] = c[1]
c[1] = tmp
return ret
def inv(a, b):
c = [0, 1]
exgcd(a, b, c)
return c[0] % b
def slove(n):
f2n = factorial(2 * n - 1) % 1000000007
fn = factorial(n) % 1000000007
fn1 = factorial(n - 1) % 1000000007
div = (fn * fn1) % 1000000007
ninv = inv(div, 1000000007)
ret = (2 * f2n * ninv - n) % 1000000007
return ret
n = int(input())
print(slove(n))
``` | instruction | 0 | 108,250 | 12 | 216,500 |
Yes | output | 1 | 108,250 | 12 | 216,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17
Submitted Solution:
```
n = int(input())
m = int(1e9 + 7)
# binom(2n - 1, n)
p = 1
for i in range(1, n + 1):
p *= 2 * n - i
p *= pow(i, m - 2, m)
p %= m
print((2 * p - n) % m)
``` | instruction | 0 | 108,251 | 12 | 216,502 |
Yes | output | 1 | 108,251 | 12 | 216,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# import string
# characters = string.ascii_lowercase
# digits = string.digits
# sys.setrecursionlimit(int(1e6))
# dir = [-1,0,1,0,-1]
# moves = 'NESW'
inf = float('inf')
from functools import cmp_to_key
from collections import defaultdict as dd
from collections import Counter, deque
from heapq import *
import math
from math import floor, ceil, sqrt
def geti(): return map(int, input().strip().split())
def getl(): return list(map(int, input().strip().split()))
def getis(): return map(str, input().strip().split())
def getls(): return list(map(str, input().strip().split()))
def gets(): return input().strip()
def geta(): return int(input())
def print_s(s): stdout.write(s+'\n')
mod=int(1e9+7)
maxi=int(2e5+2)
factorial=[0]*maxi
factorial[0]=1
for i in range(1,maxi):
factorial[i]=(factorial[i-1]*i)%mod
inverse = [0]*(maxi)
inverse[0]=inverse[1]=1
for i in range(2,maxi):
inverse[i]=(mod-(mod//i))*inverse[mod%i]%mod
for i in range(1,maxi):
inverse[i]=(inverse[i-1]*inverse[i])%mod
def ncr(n,r):
return (((factorial[n]*inverse[r])%mod)*inverse[n-r])%mod
def solve():
n = geta()
# dp = [[0] * n for _ in range(n)]
# dp[0][:] = [1]*n
# for i in range(1, n):
# for j in range(n):
# dp[i][j] += dp[i-1][j] + (dp[i-1][j-1] if j else 0) + (dp[i-1][j+1] if j+1 < n else 0)
# dp[i][j] %= mod
# ans = 0
# for i in dp:
# print(i)
# for i in range(n):
# ans += dp[n-1][i]
# ans %= mod
print((ncr(2*n-1, n) * 2 - n) % mod)
# Fast IO region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__=='__main__':
solve()
``` | instruction | 0 | 108,252 | 12 | 216,504 |
Yes | output | 1 | 108,252 | 12 | 216,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17
Submitted Solution:
```
def po(a,b,m):
if b == 0:
return 1
if b % 2 == 0:
return (po(a, b // 2, m) ** 2) % m
return (po(a, b - 1, m) * a) % m
def rev(a,m):
return po(a, m - 2, m)
def fact(a, m):
t = a
for i in range(1, a):
t=(t * i) % m
return t
def main(n):
m = 10 ** 9 + 7
if n == 1:
return 1
return (2*(fact(2 * n - 1, m) * rev(fact(n, m) * fact(n - 1, m), m)) - n) % m
print(main(int(input())))
``` | instruction | 0 | 108,253 | 12 | 216,506 |
Yes | output | 1 | 108,253 | 12 | 216,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
* each elements, starting from the second one, is no more than the preceding one
* each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
Input
The single line contains an integer n which is the size of the array (1 β€ n β€ 105).
Output
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
Examples
Input
2
Output
4
Input
3
Output
17
Submitted Solution:
```
def ncr(n,r):
if(n==r):
return 1
if(n<r):
return 0
if(r==0):
return 1
return ncr(n-1,r)+ncr(n-1,r-1)
x=int(input())
print(ncr(2*x-1, x))
``` | instruction | 0 | 108,254 | 12 | 216,508 |
No | output | 1 | 108,254 | 12 | 216,509 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.