message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image> | instruction | 0 | 51,311 | 14 | 102,622 |
Tags: math, number theory
Correct Solution:
```
import math
def gcd(a, b):
"""Returns the greatest common divisor of a and b.
Should be implemented using recursion.
>>> gcd(34, 19)
1
>>> gcd(39, 91)
13
>>> gcd(20, 30)
10
>>> gcd(40, 40)
40
"""
if b > a:
return gcd(b, a)
if a % b == 0:
return b
return gcd(b, a % b)
(la, ra, ta) = [int(s) for s in input().split(" ")]
(lb, rb, tb) = [int(s) for s in input().split(" ")]
delta = gcd(ta, tb)
# print(delta)
def overlap(al, ar, bl, br):
return max(0, min(ar, br) - max(al, bl) + 1)
def eval(fst_, snd_):
result_ = 0
k1 = math.ceil(abs(fst_[0] - snd_[0])/delta)
k2 = abs(fst_[0] - snd_[0]) // delta
for k in (k1, k2):
pos_sndl = snd_[0]
pos_sndr = snd_[1]
pos_fstl = k * delta + fst_[0]
pos_fstr = k * delta + fst_[1]
intersection = overlap(pos_sndl, pos_sndr, pos_fstl, pos_fstr)
# print(intersection)
# print(pos_sndl, pos_sndr, pos_fstl, pos_fstr, intersection)
result_ = max(result_, intersection)
# pos_sndl = snd_[0]
# pos_sndr = snd_[1]
# pos_fstl = fst_[0]
# pos_fstr = fst_[1]
# intersection = overlap(pos_sndl, pos_sndr, pos_fstl, pos_fstr)
# result_ = max(result_, intersection)
# # print(delta)
# # print(intersection)
# snd_k = (snd_[1] - fst_[0]) // delta + 1
# fst_k = (snd_[0] - fst_[1]) // delta -1
#
# print(fst_k, snd_k)
#
# for k in range(fst_k, snd_k):
# pos_sndl = snd_[0]
# pos_sndr = snd_[1]
# pos_fstl = k * delta + fst_[0]
# pos_fstr = k * delta + fst_[1]
# intersection = overlap(pos_sndl, pos_sndr, pos_fstl, pos_fstr)
# # print(intersection)
# # print(pos_sndl, pos_sndr, pos_fstl, pos_fstr, intersection)
# result_ = max(result_, intersection)
return result_
fst_ = (la, ra, ta)
snd_ = (lb, rb, tb)
result = max(eval(fst_, snd_), eval(snd_, fst_))
print(result)
``` | output | 1 | 51,311 | 14 | 102,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
def inters(x1,y1,x2,y2):
return max(0, min(y1,y2) - max(x1,x2) + 1)
from math import gcd
la, ra, ta = map(int, input().split())
lb, rb, tb = map(int, input().split())
shift = gcd(ta, tb)
start = la - (la % shift)
la -= start
ra -= start
start = lb - (lb % shift)
lb -= start
rb -= start
res = max(inters(la, ra, lb+shift, rb+shift), inters(la+shift, ra+shift, lb, rb), inters(la, ra, lb, rb))
# print(la,ra, lb,rb)
print(res)
``` | instruction | 0 | 51,312 | 14 | 102,624 |
Yes | output | 1 | 51,312 | 14 | 102,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
import math
def main():
la, ra, ta = [int(c) for c in input().split()]
lb, rb, tb = [int(c) for c in input().split()]
if lb < la:
la, lb = lb, la
ra, rb = rb, ra
ta, tb = tb, ta
gcd = math.gcd(ta, tb)
lena, lenb = ra - la + 1, rb - lb + 1
d = lb - la
diff = d - (d // gcd) * gcd
cur = lena - diff
ans = max(0, min(cur, lenb))
diff -= gcd
diff = -diff
cur = lenb - diff
ans = max(ans, min(lena, cur))
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 51,313 | 14 | 102,626 |
Yes | output | 1 | 51,313 | 14 | 102,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
l1,r1,t1=map(int,input().split())
l2,r2,t2=map(int,input().split())
#a=list(map(int,input().split()))
def gcd(a,b):
if a>b: a,b=b,a
while a>0:
b=b%a
a,b=b,a
return b
d=gcd(t1,t2)
def answer(x1,d1,x2,d2):
if x1>x2:
x1,x2,d1,d2=x2,x1,d2,d1
d1=d1-(x2-x1)
if d1<0: d1=0
return min(d1,d2)
d1=r1-l1+1
d2=r2-l2+1
l1=l1%d
l2=l2%d
if l1>l2:
l1,l2,d1,d2=l2,l1,d2,d1
ans1=answer(l1,d1,l2,d2)
ans2=answer(l1+d,d1,l2,d2)
print(max(ans1,ans2))
``` | instruction | 0 | 51,314 | 14 | 102,628 |
Yes | output | 1 | 51,314 | 14 | 102,629 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
import math
a,b,c=map(int,input().split())
d,e,f=map(int,input().split())
g=math.gcd(c,f)
int1=[a%g,b%g]
in1=b//g-a//g
int2=[d%g,e%g]
in2=e//g-d//g
int1[1]+=g*in1
int2[1]+=g*in2
maybegood=max(min(int1[1],int2[1])-max(int1[0],int2[0])+1,0)
int2[0]+=g
int2[1]+=g
maybegood1=max(min(int1[1],int2[1])-max(int1[0],int2[0])+1,0)
int2[0]-=2*g
int2[1]-=2*g
maybegood2=max(min(int1[1],int2[1])-max(int1[0],int2[0])+1,0)
print(max(maybegood,maybegood1,maybegood2))
``` | instruction | 0 | 51,315 | 14 | 102,630 |
Yes | output | 1 | 51,315 | 14 | 102,631 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
import math
la,ra,ta=map(int,input().split())
lb,rb,tb=map(int,input().split())
ans=-1
for i in range(1000000//4+1):
k1=((tb*i+lb-la)//ta)
x1=la+k1*ta
x2=ra+k1*ta
y1=lb+i*tb
y2=rb+i*tb
ans=max(ans,min(x2,y2)-max(x1,y1)+1)
ans=max(ans,min(x2+ta,y2)-max(x1+ta,y1)+1)
ans=max(ans,min(x2-ta,y2)-max(x1-ta,y1)+1)
print(ans)
``` | instruction | 0 | 51,316 | 14 | 102,632 |
No | output | 1 | 51,316 | 14 | 102,633 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import itertools
import sys
"""
created by shhuan at 2018/11/10 22:33
"""
la, ra, ta = map(int, input().split())
lb, rb, tb = map(int, input().split())
def rdiff(l1, r1, l2, r2):
if r1 < l2 or r2 < l1:
return 0
if l1 <= l2 <= r2 <= r1:
return r2 - l2 + 1
elif l2 <= l1 <= r1 <= r2:
return r1 - l1 + 1
elif l1 <= l2 <= r1:
return r1 - l2 + 1
elif l2 <= l1 <= r2:
return r2 - l1 + 1
else:
return 0
if la + 2*ta <= ra + ta:
print(rb-lb+1)
exit(0)
elif lb+2*tb <= rb+tb:
print(ra-la+1)
exit(0)
ans = set()
for kb in range(100000):
kal = max((lb+kb*tb-ra)//ta - 2, 0)
kar = (rb+kb*tb-la)//ta + 1
found = False
ds = set()
for ka in range(kal, kar+2):
d = rdiff(la + ka * ta, ra + ka * ta, lb + kb * tb, rb + kb * tb)
if d > 0:
ds.add(d)
ans |= ds
kb += 1
print(max(ans or [0]))
``` | instruction | 0 | 51,317 | 14 | 102,634 |
No | output | 1 | 51,317 | 14 | 102,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
#sys.setrecursionlimit(300000)
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
#sys.setrecursionlimit(300000)
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
# -----------------------------------------------binary seacrh tree---------------------------------------
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: 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)
# --------------------------------------------------binary------------------------------------
la,ra,ta=map(int,input().split())
lb,rb,tb=map(int,input().split())
if ta==1 or tb==1:
print(max(ra-la+1,rb-lb+1))
elif math.gcd(ta,tb)==1:
print(min(ra-la+1,rb-lb+1))
else:
d=math.gcd(ta,tb)
mindif=(la-lb+d)%d
dif=ra-la
la=lb+mindif
ra=la+dif
ans=max(0,min(ra,rb)-la+1)
mindif = d-mindif
la = lb + mindif
ra = la + dif
ans1 = max(0, min(ra, rb) - la + 1)
print(max(ans1,ans))
``` | instruction | 0 | 51,318 | 14 | 102,636 |
No | output | 1 | 51,318 | 14 | 102,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [l_a; r_a] are lucky, then there are some unlucky days: [r_a + 1; l_a + t_a - 1], and then there are lucky days again: [l_a + t_a; r_a + t_a] and so on. In other words, the day is lucky for Alice if it lies in the segment [l_a + k t_a; r_a + k t_a] for some non-negative integer k.
The Bob's lucky day have similar structure, however the parameters of his sequence are different: l_b, r_b, t_b. So a day is a lucky for Bob if it lies in a segment [l_b + k t_b; r_b + k t_b], for some non-negative integer k.
Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.
Input
The first line contains three integers l_a, r_a, t_a (0 ≤ l_a ≤ r_a ≤ t_a - 1, 2 ≤ t_a ≤ 10^9) and describes Alice's lucky days.
The second line contains three integers l_b, r_b, t_b (0 ≤ l_b ≤ r_b ≤ t_b - 1, 2 ≤ t_b ≤ 10^9) and describes Bob's lucky days.
It is guaranteed that both Alice and Bob have some unlucky days.
Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.
Examples
Input
0 2 5
1 3 5
Output
2
Input
0 1 3
2 3 6
Output
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
<image>
<image>
Submitted Solution:
```
from math import ceil, floor
la, ra, ta = map(int, input().split())
lb, rb, tb = map(int, input().split())
ga = ra - la + 1
gb = rb - lb + 1
if max(ta, tb) % min(ta, tb) == 0:
max_count = 0
cur_count = 0
for i in range(1, ta+tb+1):
ka = floor(i / ta)
a = la + ka * ta <= i <= int(ra + ka * ta)
kb = floor(i / tb)
b = lb + kb * tb <= i <= int(rb + kb * tb)
if a and b:
cur_count += 1
if cur_count > max_count:
max_count = cur_count
else:
cur_count = 0
print(max_count)
else:
print(min(ga, gb))
``` | instruction | 0 | 51,319 | 14 | 102,638 |
No | output | 1 | 51,319 | 14 | 102,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,346 | 14 | 102,692 |
Tags: binary search, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
stdout = sys.stdout
rr = lambda: input().strip()
rri = lambda: int(input())
rrm = lambda: list(map(int, input().strip().split()))
from bisect import bisect
N = rri()
A = rrm()
Q = rri()
A.sort()
D = sorted(A[i+1] - A[i] for i in range(N-1))
P = [0]
for x in D:
P.append(P[-1] + x)
queries = []
for q in range(Q):
L, R = rrm()
queries.append((R-L+1, q))
queries.sort()
ans = [None] * len(queries)
i = 0
for q, ix in queries:
bns = q
# bns += sum(min(d, w) for d in D)
i = bisect(D, q, i)
bns += P[i] + q * (len(D) - i)
ans[ix] = bns
print(*ans)
``` | output | 1 | 51,346 | 14 | 102,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,347 | 14 | 102,694 |
Tags: binary search, sortings
Correct Solution:
```
from bisect import bisect
n = int(input())
a = sorted(set(map(int, input().split())))
dd = sorted(map(lambda x, y: x-y, a[1:], a))
ddd = [0]
for v in dd:
ddd.append(ddd[-1] + v)
s = len(a)
k = int(input())
for j in range(k):
l, r = map(int, input().split())
d = r - l + 1
i = bisect(dd, d)
print(" " if j else "", ddd[i] + (s-i)*d, end = "")
print()
``` | output | 1 | 51,347 | 14 | 102,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,348 | 14 | 102,696 |
Tags: binary search, sortings
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
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-----------------------------------------------------
from bisect import bisect_left as bl
N = int(input())
A = sorted([int(a) for a in input().split()])
Q = int(input())
B = sorted([A[i+1] - A[i] for i in range(N-1)])
C = [0] * N
for i in range(1, N):
C[i] = C[i-1] + B[i-1]
ANS = []
for q in range(Q):
l, r = map(int, input().split())
k = r - l + 1
i = bl(B, k)
ANS.append(k * (N - i) + C[i])
print(*ANS)
``` | output | 1 | 51,348 | 14 | 102,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,349 | 14 | 102,698 |
Tags: binary search, sortings
Correct Solution:
```
from sys import stdin, stdout
import bisect
n = int(input())
a = list(map(int, stdin.readline().split()))
a = sorted(a)
if n == 1:
q = int(input())
out = []
while q > 0:
l, r = map(int, stdin.readline().split())
txt = str(r - l + 1)
out.append(txt)
q -= 1
stdout.write(' '.join(out))
else:
d = [0 for i in range(n - 1)]
for i in range(1, n):
d[i - 1] = a[i] - a[i - 1]
d = sorted(d)
s = [0 for i in range(n - 1)]
s[0] = d[0]
for i in range(1, n - 1):
s[i] = s[i - 1] + d[i]
q = int(input())
out = []
while q > 0:
l, r = map(int, stdin.readline().split())
tot = r - l + 1
ans = tot * n
p = bisect.bisect_right(d, tot) - 1
if p >= 0:
ans -= tot * (p + 1) - s[p]
out.append(str(ans))
q -= 1
stdout.write(' '.join(out))
``` | output | 1 | 51,349 | 14 | 102,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,350 | 14 | 102,700 |
Tags: binary search, sortings
Correct Solution:
```
#!/usr/bin/env python3
import os, sys
from io import BytesIO
class FastI:
stream = BytesIO()
newlines = 0
def readline(self):
while self.newlines == 0:
b, ptr = os.read(0, (1 << 13) + os.fstat(0).st_size), self.stream.tell()
self.stream.seek(0, 2), self.stream.write(b), self.stream.seek(ptr)
self.newlines += b.count(b'\n') + (not b)
self.newlines -= 1
return self.stream.readline()
class FastO:
def __init__(self):
stream = BytesIO()
self.flush = lambda: os.write(1, stream.getvalue()) and not stream.truncate(0) and stream.seek(0)
self.write = lambda b: stream.write(b.encode())
class ostream:
def __lshift__(self, a):
if a == endl:
sys.stdout.write('\n')
sys.stdout.flush()
else:
sys.stdout.write(str(a))
return self
sys.stdin, sys.stdout = FastI(), FastO()
input, flush = sys.stdin.readline, sys.stdout.flush
cout, endl = ostream(), object()
rr = lambda: input().strip()
rri = lambda: int(input())
rrm = lambda: list(map(int, input().strip().split()))
def main():
from bisect import bisect
N = rri()
A = rrm()
Q = rri()
A.sort()
D = sorted(A[i+1] - A[i] for i in range(N-1))
P = [0]
for x in D:
P.append(P[-1] + x)
queries = []
for q in range(Q):
L, R = rrm()
queries.append((R-L+1, q))
queries.sort()
ans = [None] * len(queries)
i = 0
for q, ix in queries:
bns = q
# bns += sum(min(d, w) for d in D)
i = bisect(D, q, i)
bns += P[i] + q * (len(D) - i)
ans[ix] = bns
cout << " ".join(map(str, ans))
if __name__ == '__main__':
main()
``` | output | 1 | 51,350 | 14 | 102,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,351 | 14 | 102,702 |
Tags: binary search, sortings
Correct Solution:
```
import copy
import bisect
n = int(input())
s = list(map(int, input().split()))
sCopy = copy.deepcopy(s)
AP = []
mx = -1
sCopy.sort()
miss = []
for i in range(n-1):
mx = max(mx, sCopy[i+1]-sCopy[i]-1, 0)
miss.append(max(sCopy[i+1]-sCopy[i]-1, 0))
miss.sort()
if miss!=[]:
pSOfMiss = [miss[0]]
for i in range(1, len(miss)):
pSOfMiss.append(pSOfMiss[-1]+miss[i])
MAX = max(s)
MIN = min(s)
C = len(set(s))
ansList = []
#print(miss, mx)
#print(pSOfMiss)
mL = len(miss)
q = int(input())
for _ in range(q):
l, r = map(int, input().split())
if l==r:
ansList.append(C)
elif n==1:
ans = r-l+1
ansList.append(ans)
elif r-l>=mx:
ans = (MAX+r)-(MIN+l)+1
ansList.append(ans)
else:
idx = bisect.bisect_right(miss, r-l)
if idx>0:
notThere = pSOfMiss[-1]-pSOfMiss[idx-1]-((r-l)*(mL-idx))
else:
notThere = pSOfMiss[-1] - ((r-l)*(mL-idx))
#print(l, r, notThere, idx)
ans = (MAX+r)-(MIN+l)+1-notThere
ansList.append(ans)
print(*ansList)
``` | output | 1 | 51,351 | 14 | 102,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,352 | 14 | 102,704 |
Tags: binary search, sortings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import Counter
import string
import heapq
import bisect
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def get_int():
return int(input())
def get_ints():
return list(map(int, input().split(' ')))
def get_int_grid(n):
return [get_ints() for _ in range(n)]
def get_str():
return input().strip()
def get_strs():
return input().strip().split(' ')
def yes_no(b):
if b:
return "YES"
else:
return "NO"
def binary_search(good, left, right, delta=1, right_true=False):
"""
Performs binary search
----------
Parameters
----------
:param good: Function used to perform the binary search
:param left: Starting value of left limit
:param right: Starting value of the right limit
:param delta: Margin of error, defaults value of 1 for integer binary search
:param right_true: Boolean, for whether the right limit is the true invariant
:return: Returns the most extremal value interval [left, right] which is good function evaluates to True,
alternatively returns False if no such value found
"""
limits = [left, right]
while limits[1] - limits[0] > delta:
if delta == 1:
mid = sum(limits) // 2
else:
mid = sum(limits) / 2
if good(mid):
limits[int(right_true)] = mid
else:
limits[int(~right_true)] = mid
if good(limits[int(right_true)]):
return limits[int(right_true)]
else:
return False
def prefix_sums(a, drop_zero=False):
p = [0]
for x in a:
p.append(p[-1] + x)
if drop_zero:
return p[1:]
else:
return p
def prefix_mins(a, drop_zero=False):
p = [float('inf')]
for x in a:
p.append(min(p[-1], x))
if drop_zero:
return p[1:]
else:
return p
def solve():
n = get_int()
s = get_ints()
s.sort()
d = []
for i in range(1, n):
d.append(s[i] - s[i - 1])
d.append(float('inf'))
d_cnt = [(k, v) for k, v in Counter(d).items()]
d_cnt.sort()
d_idxs = [d for d, cnt in d_cnt]
d_prods = [d * cnt for d, cnt in d_cnt]
d_cnts = [cnt for d, cnt in d_cnt]
d_prods_pref = prefix_sums(d_prods)
d_cnts_pref = prefix_sums(d_cnts)
q = get_int()
def query():
l, r = get_ints()
w = r - l + 1
idx = bisect.bisect_left(d_idxs, w)
return d_prods_pref[idx] + w * (n - d_cnts_pref[idx])
return [query() for i in range(q)]
print(*solve())
``` | output | 1 | 51,352 | 14 | 102,705 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11. | instruction | 0 | 51,353 | 14 | 102,706 |
Tags: binary search, sortings
Correct Solution:
```
n=int(input())
s=list(map(int,input().split()))
if n==1:
q=int(input())
out=[0]*q
for u in range(q):
a,b=map(int,input().split())
out[u]=str(b-a+1)
print(" ".join(out))
exit()
s.sort()
diffs=[s[i+1]-s[i] for i in range(n-1)]
diffs.sort()
parts=[0]*(n-1)
parts[0]=diffs[0]*n
for i in range(n-2):
parts[i+1]=parts[i]+(diffs[i+1]-diffs[i])*(n-i-1)
q=int(input())
out=[0]*q
for u in range(q):
a,b=map(int,input().split())
size=b-a+1
if size<=diffs[0]:
out[u]=n*size
elif size>=diffs[-1]:
out[u]=parts[-1]+size-diffs[-1]
else:
big=n-2
sml=0
curr=(n-2)//2
while big-sml>1:
if size>diffs[curr]:
sml=curr
curr=(big+sml)//2
elif size<diffs[curr]:
big=curr
curr=(big+sml)//2
else:
big=sml=curr
if big==sml:
out[u]=parts[big]
else:
rat=(parts[big]-parts[sml])//(diffs[big]-diffs[sml])
out[u]=parts[sml]+rat*(size-diffs[sml])
out=[str(guy) for guy in out]
print(" ".join(out))
``` | output | 1 | 51,353 | 14 | 102,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
import bisect
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
a = set(a)
a = list(a)
n = len(a)
a.sort()
h = []
for i in range(n-1):
if a[i+1]-a[i] > 1:
h.append(a[i+1]-a[i]-1)
m = len(h)
h.sort()
rui = [0]*(m+1)
for i in range(m):
rui[i+1] = rui[i] + h[i]
q = int(input())
ans = [list(map(int,input().split())) for i in range(q)]
for i in range(q):
aa,bb = ans[i]
c = bb-aa
d = bisect.bisect_left(h, c)
print(rui[d]+c*(m-d)+n+c)
``` | instruction | 0 | 51,354 | 14 | 102,708 |
Yes | output | 1 | 51,354 | 14 | 102,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
n=int(input())
answer=[]
summa={}
summa[0]=0
def bin(p):
left=0
right=lena
while right-left>1:
mid=(left+right)//2
if res[mid]<=p:
left=mid
else:
right=mid
return left+1
uku=list(set([int(x) for x in input().split()]))
uku.sort()
res=[]
for i in range(1,len(uku)):
res.append(uku[i]-uku[i-1])
q=int(input())
res.append(10**100)
res.sort()
counter=0
i=1
for item in res:
counter+=item
summa[i]=counter
i+=1
lena=len(res)
for i in range(q):
l,r=[int(x) for x in input().split()]
s=r-l+1
counter=0
if s<res[0]:
bina=0
else:
bina=bin(s)
counter=summa[bina]+s*(lena-bina)
answer.append(counter)
print(*answer)
``` | instruction | 0 | 51,355 | 14 | 102,710 |
Yes | output | 1 | 51,355 | 14 | 102,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
#!/usr/bin/env python3
import os, sys
from io import BytesIO
class FastI:
stream = BytesIO()
newlines = 0
def readline(self):
while self.newlines == 0:
b, ptr = os.read(0, (1 << 13) + os.fstat(0).st_size), self.stream.tell()
self.stream.seek(0, 2), self.stream.write(b), self.stream.seek(ptr)
self.newlines += b.count(b'\n') + (not b)
self.newlines -= 1
return self.stream.readline()
class FastO:
def __init__(self):
stream = BytesIO()
self.flush = lambda: os.write(1, stream.getvalue()) and not stream.truncate(0) and stream.seek(0)
self.write = lambda b: stream.write(b.encode())
class ostream:
def __lshift__(self, a):
if a == endl:
sys.stdout.write('\n')
sys.stdout.flush()
else:
sys.stdout.write(str(a))
return self
sys.stdin, sys.stdout = FastI(), FastO()
input, flush = sys.stdin.readline, sys.stdout.flush
cout, endl = ostream(), object()
rr = lambda: input().strip()
rri = lambda: int(input())
rrm = lambda: list(map(int, input().strip().split()))
def main():
from bisect import bisect
N = rri()
A = rrm()
Q = rri()
A.sort()
D = sorted(A[i+1] - A[i] for i in range(N-1))
P = [0]
for x in D:
P.append(P[-1] + x)
queries = []
for q in range(Q):
L, R = rrm()
queries.append((R-L+1, q))
queries.sort()
ans = [None] * len(queries)
i = 0
for q, ix in queries:
bns = q
# bns += sum(min(d, w) for d in D)
i = bisect(D, q, i)
bns += P[i] + q * (len(D) - i)
ans[ix] = bns
print(*ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 51,356 | 14 | 102,712 |
Yes | output | 1 | 51,356 | 14 | 102,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
import math
def f5(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
def upper_bound(v, val):
l = 0
r = len(v)
while l+1<r :
mid = (l+r)//2
if v[mid] <= val:
l=mid
else:
r=mid
return r
N = map(int,input().split())
inp = [*map(int, input().split())]
inp = sorted(f5(inp))
N = len(inp)
mp = {}
v = []
i = 0
while i < N-1:
gap = inp[i+1]-inp[i]
v.append(gap)
mp[gap] = 0
i = i + 1
for gap in v:
mp[gap] = mp[gap] + 1
v.append(0)
v = sorted(f5(v))
psum1 = [0] * (len(mp) + 1)
psum0 = [0] * (len(mp) + 1)
i = 1
while i <= len(mp):
cur = v[i]
psum1[i] = psum1[i-1] + (psum0[i-1]*(cur-v[i-1])) + mp[cur]
psum0[i] = psum0[i-1] + mp[cur]
i = i+1
Q = int(input())
ansstring = ''
while Q>0:
l, r = map(int,input().split())
siz = r-l
idx = upper_bound(v,siz)-1
ans = N * (siz + 1)
remain = siz-v[idx]
ans -= psum1[idx]
ans -= psum0[idx] * remain
ansstring += str(ans) + ' '
Q = Q-1
print(ansstring)
``` | instruction | 0 | 51,357 | 14 | 102,714 |
Yes | output | 1 | 51,357 | 14 | 102,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
n = int(input())
A = [int(i) for i in input().split()]
A = sorted(list(set(A)))
delta = []
for i in range(len(A)-1):
delta.append(A[i+1] - A[i])
delta.sort()
finans = []
q = int(input())
sumdelta = [0]
for i in range(len(delta)):
sumdelta.append(delta[i]+sumdelta[-1])
for i in range(10):
sumdelta.append(sumdelta[-1])
for qq in range(q):
l, r = [int(i) for i in input().split()]
sz = r-l+1
ans = sz
lo = 0
hi = len(delta)
while lo+1<hi:
mid = (lo+hi)//2
if mid<len(delta) and delta[mid]<=sz:
lo = mid
else:
hi = mid-1
#ans = sz
#for i in delta:
# ans += min(i, sz)
ind = lo
'''
for ind in range(max(0, min(lo, hi)-5), min(len(delta), max(lo, hi)+5)):
if ind<len(delta) and delta[ind] <= sz:
continue
else:
ind-=1
break
'''
#ind = min(ind, len(sumdelta)-2)
finans.append(sz + sumdelta[ind+1] + sz*(len(delta)-ind-1))
#print(ans, finans[-1])
print(*finans)
``` | instruction | 0 | 51,358 | 14 | 102,716 |
No | output | 1 | 51,358 | 14 | 102,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
n = int(input())
arr = list(map(int,input().split()))
uni = list(set(arr))
uni.sort()
mn = min(uni)
for i in range(int(input())):
l,r = list(map(int,input().split()))
ans = mn*(r-l+1)
for i in range(1,len(uni)):
ans+=min(uni[i]-uni[i-1],r-l+1)
print(ans)
``` | instruction | 0 | 51,359 | 14 | 102,718 |
No | output | 1 | 51,359 | 14 | 102,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
import copy
n = int(input())
s = list(map(int, input().split()))
sCopy = copy.deepcopy(s)
AP = []
mx = -1
sCopy.sort()
#miss = []
for i in range(n-1):
mx = max(mx, sCopy[i+1]-sCopy[i]-1, 0)
#miss.append(max(sCopy[i+1]-sCopy[i]-1, 0))
MAX = max(s)
MIN = min(s)
C = len(set(s))
ansList = []
q = int(input())
for _ in range(q):
l, r = map(int, input().split())
if l==r:
ansList.append(C)
elif r-l>mx:
#print(mx)
ans = (MAX+r)-(MIN+l)+1
ansList.append(ans)
else:
notThere = mx-r+l
#print(l, r, notThere)
ans = (MAX+r)-(MIN+l)+1-notThere
ansList.append(ans)
print(*ansList)
``` | instruction | 0 | 51,360 | 14 | 102,720 |
No | output | 1 | 51,360 | 14 | 102,721 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has n strings, and each string has (10^{18} + 1) frets numerated from 0 to 10^{18}. The fleas use the array s_1, s_2, …, s_n to describe the ukulele's tuning, that is, the pitch of the j-th fret on the i-th string is the integer s_i + j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between l and r (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with n rows and (10^{18}+1) columns, where the cell in the i-th row and j-th column (0 ≤ j ≤ 10^{18}) contains the integer s_i + j. You are to answer q queries, in the k-th query you have to answer the number of distinct integers in the matrix from the l_k-th to the r_k-th columns, inclusive.
Input
The first line contains an integer n (1 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^{18}) — the tuning of the ukulele.
The third line contains an integer q (1 ≤ q ≤ 100 000) — the number of questions.
The k-th among the following q lines contains two integers l_k,r_k (0 ≤ l_k ≤ r_k ≤ 10^{18}) — a question from the fleas.
Output
Output one number for each question, separated by spaces — the number of different pitches.
Examples
Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Output
5 10 18
Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Output
2 1500000000000000000
Note
For the first example, the pitches on the 6 strings are as follows.
$$$ \begin{matrix} Fret & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & … \\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & ... \\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & ... \\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & ... \\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & ... \\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & ... \end{matrix} $$$
There are 5 different pitches on fret 7 — 8, 10, 11, 12, 16.
There are 10 different pitches on frets 0, 1, 2 — 1, 2, 3, 4, 5, 6, 7, 9, 10, 11.
Submitted Solution:
```
n = int(input())
ar = list(set(map(int,input().split())))
q = int(input())
ar.sort()
ps = []
m = len(ar)
for i in range(1 , m):
ps.append(ar[i] - ar[i - 1] - 1)
ps.sort()
ts = [i for i in ps]
m = len(ps)
for i in range(1 , len(ps)):
ps[i] += ps[i - 1]
#bs on ts
#cal sum using ps
ans = len(ar)
ans_ls = []
for _ in range(q):
l , r = map(int,input().split())
if l == r:
ans_ls.append(str(ans))
continue
x = r - l
low = -1
high = m
while low + 1 < high:
mid = (low + high) >> 1
# print("l " , low , " r " , high)
if ts[mid] <= x:
low = mid
else:
high = mid
ss = ans
if low != -1:
ss += ps[low]
ss += ((m - low)*x)
else:
ss += ((m)*x)
ans_ls.append(str(ss))
#print(ans_ls)
print(' '.join(ans_ls))
``` | instruction | 0 | 51,361 | 14 | 102,722 |
No | output | 1 | 51,361 | 14 | 102,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
«Polygon» is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index. As in most cases it is clear which index the next test will have, the system suggests the default value of the index. It is calculated as the smallest positive integer which is not used as an index for some previously added test.
You are to implement this feature. Create a program which determines the default index of the next test, given the indexes of the previously added tests.
Input
The first line contains one integer n (1 ≤ n ≤ 3000) — the amount of previously added tests. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 3000) — indexes of these tests.
Output
Output the required default value for the next test index.
Examples
Input
3
1 7 2
Output
3 | instruction | 0 | 51,625 | 14 | 103,250 |
Tags: implementation, sortings
Correct Solution:
```
#x,y = map(int, input().strip().split(' '))
n=int(input())
lst = list(map(int, input().strip().split(' ')))
lst.sort()
f=0
for i in range(n):
if i==0:
if lst[i]!=1:
print(1)
f=1
break
else:
if lst[i]!=lst[i-1]+1:
print(lst[i-1]+1)
f=1
break
if f==0:
print(lst[n-1]+1)
``` | output | 1 | 51,625 | 14 | 103,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,692 | 14 | 103,384 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
(n, m, k) = map(int, input().split(' '))
x = []
for i in range(0, m+1):
t = int(input())
x.append(t)
fedor = x[m]
count = 0
for i in range(0, m):
tmp = x[i] ^ fedor
c = bin(tmp).count('1')
if(c <= k):
count += 1
print(count)
``` | output | 1 | 51,692 | 14 | 103,385 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,693 | 14 | 103,386 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
n,m,k = map(int,input().split())
a = []
for i in range(m):
a.append(int(input()))
fedya = int(input())
fedya = str(bin(fedya)[2:])
fedya = '0'*(n-len(fedya))+fedya
ans = 0
for bb in a:
bbb = str(bin(bb)[2:])
bbb = '0'*(n-len(bbb))+bbb
delta = 0
for i in range(n):
if bbb[i]!=fedya[i]:
delta+=1
if delta<=k:
ans+=1
print(ans)
``` | output | 1 | 51,693 | 14 | 103,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,694 | 14 | 103,388 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
inp = input()
inp = inp.split(' ')
n = int(inp[0])
m = int(inp[1])
k = int(inp[2])
count = 0
bin_ints = []
for i in range(m):
bin_ints.append(int(input()))
fedor = int(input())
print(len(list(filter(lambda e: bin(e ^ fedor).count("1") <= k, bin_ints))))
``` | output | 1 | 51,694 | 14 | 103,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,695 | 14 | 103,390 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
n, m, k = list(map(int, input().split()))
c = 0
a = []
for i in range(m): a.append(int(input()))
d = int(input())
for i in range(m):
x = a[i]
x ^= d
r = 0
while x:
r += x & 1
x = x >> 1
if r <= k: c += 1
print(c)
``` | output | 1 | 51,695 | 14 | 103,391 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,696 | 14 | 103,392 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
n, m, k = map(int, input().split())
a = [int(input()) for __ in range(m)]
x = int(input())
ans = 0
for y in a:
if bin(x ^ y).count('1') <= k:
ans += 1
print(ans)
``` | output | 1 | 51,696 | 14 | 103,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,697 | 14 | 103,394 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
n, m, k = input().split()
n = int (n)
m = int (m)
k = int (k)
#n = int(input())
#a = list(map(int, input().split()))
#g = list(map(int, input().split()))
#x1, y1, x2, y2 =map(int,input().split())
#n = int(input())
# = [""]*n
#f = [0]*n
#t = [0]*n
#f = []
#h = [""] * n
#f1 = sorted(f, key = lambda tup: tup[0])
#f1 = sorted(t, key = lambda tup: tup[0])
a = [0]*m
for i in range (m):
a[i] = int(input())
f = int(input())
c = 0
for i in range (m):
cu = f ^ a[i]
count = 0
while cu > 0:
if (cu & 1 == 1): count += 1
cu >>= 1
if count <= k:
#print (i)
c += 1
print(c)
``` | output | 1 | 51,697 | 14 | 103,395 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,698 | 14 | 103,396 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
n, m, k = map(int, input().split())
L = []
N = []
M = 0
for i in range(m+1):
L += [bin(int(input())).replace('0b', '')]
M = max(M, len(L[i]))
for i in range(m+1):
p = ['0'*(M-len(L[i]))+L[i]]
N += p
f = N[-1]
t = 0
for i in N:
t += 1
s = 0
for j in range(len(f)):
if i[j] != f[j]:
s += 1
if s == k+1:
t -= 1
break
print(t-1)
``` | output | 1 | 51,698 | 14 | 103,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».
The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type.
Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.
Input
The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000).
The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.
Output
Print a single integer — the number of Fedor's potential friends.
Examples
Input
7 3 1
8
5
111
17
Output
0
Input
3 3 3
1
2
3
4
Output
3 | instruction | 0 | 51,699 | 14 | 103,398 |
Tags: bitmasks, brute force, constructive algorithms, implementation
Correct Solution:
```
def dec_to_bin(x):
return bin(x)[2:]
def compare(x, y):
charX = list(dec_to_bin(x))
charY = list(dec_to_bin(y))
differ = 0
while (len(charX) < len(charY)):
charX.insert(0, '0')
while (len(charY) < len(charX)):
charY.insert(0, '0')
for i in range(0, len(charX)):
if charX[i] != charY[i]:
differ += 1
# print(charX)
# print(charY)
# print(differ)
return differ
n, m, k = [int(x) for x in input().split()]
numbers = []
for i in range (0, m + 1):
numbers.append(int(input()))
res = 0
for i in range(0, m):
if compare(numbers[i], numbers[m]) <= k:
res += 1
print(res)
``` | output | 1 | 51,699 | 14 | 103,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.
When a garland is switched on, it periodically changes its state — sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds x, x + ki, x + 2ki, x + 3ki and so on.
Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1, x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one — during x2-th second, and the third one — during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit.
Help Mishka by telling him if it is possible to do this!
Input
The first line contains three integers k1, k2 and k3 (1 ≤ ki ≤ 1500) — time intervals of the garlands.
Output
If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.
Otherwise, print NO.
Examples
Input
2 2 3
Output
YES
Input
4 2 3
Output
NO
Note
In the first example Mishka can choose x1 = 1, x2 = 2, x3 = 1. The first garland will be lit during seconds 1, 3, 5, 7, ..., the second — 2, 4, 6, 8, ..., which already cover all the seconds after the 2-nd one. It doesn't even matter what x3 is chosen. Our choice will lead third to be lit during seconds 1, 4, 7, 10, ..., though.
In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit. | instruction | 0 | 51,864 | 14 | 103,728 |
Tags: brute force, constructive algorithms
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
from queue import Queue
import collections
import itertools
import bisect
import heapq
# sys.setrecursionlimit(100000)
# ^^^TAKE CARE FOR MEMORY LIMIT^^^
import random
def main():
pass
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 binary(n):
return (bin(n).replace("0b", ""))
def decimal(s):
return (int(s, 2))
def pow2(n):
p = 0
while (n > 1):
n //= 2
p += 1
return (p)
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
l.append(i)
n = n / i
if n > 2:
l.append(int(n))
return (l)
def primeFactorsCount(n):
cnt=0
while n % 2 == 0:
cnt+=1
n = n // 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
cnt+=1
n = n // i
if n > 2:
cnt+=1
return (cnt)
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
def maxPrimeFactors(n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
def countcon(s, i):
c = 0
ch = s[i]
for i in range(i, len(s)):
if (s[i] == ch):
c += 1
else:
break
return (c)
def lis(arr):
n = len(arr)
lis = [1] * n
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
maximum = 0
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
def isSubSequence(str1, str2):
m = len(str1)
n = len(str2)
j = 0
i = 0
while j < m and i < n:
if str1[j] == str2[i]:
j = j + 1
i = i + 1
return j == m
def maxfac(n):
root = int(n ** 0.5)
for i in range(2, root + 1):
if (n % i == 0):
return (n // i)
return (n)
def p2(n):
c = 0
while (n % 2 == 0):
n //= 2
c += 1
return c
def seive(n):
primes = [True] * (n + 1)
primes[1] = primes[0] = False
i = 2
while (i * i <= n):
if (primes[i] == True):
for j in range(i * i, n + 1, i):
primes[j] = False
i += 1
pr = []
for i in range(0, n + 1):
if (primes[i]):
pr.append(i)
return pr
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def denofactinverse(n, m):
fac = 1
for i in range(1, n + 1):
fac = (fac * i) % m
return (pow(fac, m - 2, m))
def numofact(n, m):
fac = 1
for i in range(1, n + 1):
fac = (fac * i) % m
return (fac)
def sod(n):
s = 0
while (n > 0):
s += n % 10
n //= 10
return s
a,b,c=map(int,input().split())
l=sorted([a,b,c])
if(l==[2,2,2] or l==[2,2,3] or l==[3,3,3] or l==[2,4,4]):
print("YES")
elif(l[0]==l[1]==2 or l[0]==1):
print("YES")
else:
print("NO")
``` | output | 1 | 51,864 | 14 | 103,729 |
Provide tags and a correct Python 2 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,874 | 14 | 103,748 |
Tags: dp
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
n=in_num()
l=in_arr()
dp=[0,0,0,0]
for i in l:
if i==1:
dp[0]+=1
dp[2]=max(dp[2]+1,dp[1]+1)
else:
dp[1]=max(dp[1]+1,dp[0]+1)
dp[3]=max(dp[3]+1,dp[2]+1)
pr_num(max(dp))
``` | output | 1 | 51,874 | 14 | 103,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,875 | 14 | 103,750 |
Tags: dp
Correct Solution:
```
# -*- coding: utf-8 -*-
# Watashi wa ARUGONEKO wo tsukatteru. [algoneko.github.io]
# I hate python --RedCat
from math import *
import re
n = int(input())
a = list(map(int, input().split()))
d1, d2, d3, d4 = [0]*n, [0]*n, [0]*n, [0]*n
d1[0] = d3[0] = (1 if a[0] == 1 else 0)
d2[0] = d4[0] = (1 if a[0] == 2 else 0)
for i in range(1,n):
d1[i] = d1[i-1]
d2[i] = d2[i-1]
d3[i] = d3[i-1]
d4[i] = d4[i-1]
if a[i] == 1:
# * -> 1
# 2 -> 3
d1[i] = d1[i - 1] + 1
d3[i] = max(d3[i - 1], d2[i - 1]) + 1
if a[i] == 2:
# 1 -> 2
# 3 -> 4
d2[i] = max(d2[i - 1], d1[i - 1]) + 1
d4[i] = max(d4[i - 1], d3[i - 1]) + 1
print(max(max(d1), max(d2), max(d3), max(d4)))
``` | output | 1 | 51,875 | 14 | 103,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,876 | 14 | 103,752 |
Tags: dp
Correct Solution:
```
import sys
def input():
return sys.stdin.buffer.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
ans = max(a.count(1), a.count(2))
#mixed case
prefix = [0]*(n + 10)
suffix = [0]*(n + 10)
for i in range(n):
if a[i] == 1:
prefix[i + 1] = 1
a.reverse()
for i in range(n):
if a[i] == 2:
suffix[i + 1] = 1
a.reverse()
for i in range(n):
prefix[i + 1] += prefix[i]
suffix[i + 1] += suffix[i]
INF = 1 << 60
a.reverse()
ans = -INF
for l in range(n):
max_case_1 = 0
max_case_2 = 0
for r in range(l, n):
if a[r] == 1:
max_case_1 += 1
elif a[r] == 2:
max_case_2 = max(max_case_2, max_case_1) + 1
K = max(max_case_1, max_case_2)
ans = max(ans, K + prefix[n - 1 - r] + suffix[l])
print(ans)
``` | output | 1 | 51,876 | 14 | 103,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,877 | 14 | 103,754 |
Tags: dp
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
pref = [0] * (n + 1)
for i in range(n):
pref[i + 1] = pref[i] + (1 if a[i] == 1 else 0)
suf = [0] * (n + 1)
for i in reversed(range(n)):
suf[i] = suf[i + 1] + (1 if a[i] == 2 else 0)
dp = [0, 0, 0, 0]
for i in range(n):
new_dp = [max(dp[i], dp[i - 1]) if i > 0 else dp[i] for i in range(4)]
if a[i] == 1:
new_dp[0] += 1
new_dp[2] += 1
else:
new_dp[1] += 1
new_dp[3] += 1
dp = new_dp
print(max(max([pref[i] + suf[i] for i in range(n + 1)]), max(dp)))
``` | output | 1 | 51,877 | 14 | 103,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,878 | 14 | 103,756 |
Tags: dp
Correct Solution:
```
a = [0] * 4
input()
for n in map(int, input().split()):
if n == 1:
a[0] += 1
a[2] = max(a[1] + 1, a[2] + 1)
else:
a[1] = max(a[0] + 1, a[1] + 1)
a[3] = max(a[2] + 1, a[3] + 1)
print(max(a))
``` | output | 1 | 51,878 | 14 | 103,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,879 | 14 | 103,758 |
Tags: dp
Correct Solution:
```
input()
a = list(map(int, input().split()))
p1,p2,p3,p4=0,0,0,0
for n in a:
if n == 1:
p1 += 1
p3 = max(p3 + 1, p2 + 1)
else:
p2 = max(p2 + 1, p1 + 1)
p4 = max(p4 + 1, p3 + 1)
print(max(p1,p2,p3,p4))
``` | output | 1 | 51,879 | 14 | 103,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,880 | 14 | 103,760 |
Tags: dp
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
d=[0]*(4)
for i in range(n):
if a[i]==1:
d[0]+=1
d[2]=max(d[1],d[2])+1
else:
d[1]=max(d[0],d[1])+1
d[3]=max(d[2],d[3])+1
print(max(d))
``` | output | 1 | 51,880 | 14 | 103,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,881 | 14 | 103,762 |
Tags: dp
Correct Solution:
```
def solve(lst):
a = b = c = d = 0
for i in range(len(lst)):
if lst[i] == 1:
a += 1
c = max(b, c) + 1
else:
b = max(a, b) + 1
d = max(c, d) + 1
return max(a, b, c, d)
n = input()
lst = list(map(int, input().split()))
print(solve(lst))
``` | output | 1 | 51,881 | 14 | 103,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9. | instruction | 0 | 51,882 | 14 | 103,764 |
Tags: dp
Correct Solution:
```
#fek mikoni aslan lozoomi dare kasi tu in donya dustet dashtet bashe ?!
#age are chera tanhayi ?!!
#age na chera namordi ?!!!
DP = [0] * 4
input()
for x in map(int, input().split()):
if x == 1 :
DP[0] += 1
DP[2] += 1
else :
DP[1] += 1
DP[3] += 1
for j in range ( 1 , 4 ) :
DP[j] = max ( DP[j] , DP[j-1] )
print (DP[3])
``` | output | 1 | 51,882 | 14 | 103,765 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
d = [0 for _ in range(4)]
for val in a:
if val == 1:
d[0] += 1
d[2] = max(d[2] + 1, d[1] + 1)
else:
d[1] = max(d[1] + 1, d[0] + 1)
d[3] = max(d[3] + 1, d[2] + 1)
print(max(d))
``` | instruction | 0 | 51,883 | 14 | 103,766 |
Yes | output | 1 | 51,883 | 14 | 103,767 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
one = [0]
two = [0]
for i in A:
one.append(one[-1])
two.append(two[-1])
if i == 1:
one[-1] += 1
else:
two[-1] += 1
rdp1 = [[1] * n for _ in range(n)]
for l in range(n):
for r in range(l + 1, n):
if A[r] == 2:
rdp1[l][r] = rdp1[l][r - 1] + 1
else:
if rdp1[l][r - 1] == one[r] - one[l]:
rdp1[l][r] = rdp1[l][r - 1] + 1
else:
rdp1[l][r] = rdp1[l][r - 1]
rdp2 = [[1] * n for _ in range(n)]
for l in range(n):
for r in range(l + 1, n):
if A[r] == 1:
rdp2[l][r] = rdp2[l][r - 1] + 1
else:
if rdp2[l][r - 1] == two[r] - two[l]:
rdp2[l][r] = rdp2[l][r - 1] + 1
else:
rdp2[l][r] = rdp2[l][r - 1]
dp = [0] * n
dp[0] = 1
for i in range(1, n):
if A[i] == 2:
dp[i] = dp[i - 1] + 1
else:
if dp[i - 1] == one[i]:
dp[i] = dp[i - 1] + 1
else:
dp[i] = dp[i - 1]
dp[i] = max(dp[i], rdp2[0][i])
for j in range(i):
if rdp1[0][j] == one[j + 1]:
dp[i] = max(dp[i], rdp1[0][j] + rdp2[j + 1][i])
dp[i] = max(dp[i], rdp1[0][j] + two[i + 1] - two[j + 1])
print(dp[-1])
``` | instruction | 0 | 51,884 | 14 | 103,768 |
Yes | output | 1 | 51,884 | 14 | 103,769 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
Submitted Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
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-----------------------------------------------------
n = int(input())
a = list(map(int,input().split()))
count = [0]*4
for i in range(n):
if (a[i] == 1):
count[0] += 1
count[2] = max(count[1]+1,count[2]+1)
else:
count[1] = max(count[1]+1,count[0]+1)
count[3] = max(count[3]+1,count[2]+1)
print(max(count))
``` | instruction | 0 | 51,885 | 14 | 103,770 |
Yes | output | 1 | 51,885 | 14 | 103,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
dp = [0] * 4
for j in range(n):
dp = [dp[i] if i == 0 else max(dp[i], dp[i - 1]) for i in range(4)]
dp = [dp[i] + (1 if i % 2 == a[j] - 1 else 0) for i in range(4)]
print(max(dp))
``` | instruction | 0 | 51,886 | 14 | 103,772 |
Yes | output | 1 | 51,886 | 14 | 103,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
l = 0
d = [0 for i in range(4)]
it = 0
mx = 1
if a[0] == 1:
d[0] = 1
else:
d[1] = 1
it += 1
for i in range(1, n):
if a[i] != a[i - 1]:
it += 1
if it == 4:
l = i
d[0] = d[3]
d[1] = d[2] = d[3] = 0
it = 0
else:
d[it] += 1
mx = max(mx, i - l + 1)
print(mx)
``` | instruction | 0 | 51,887 | 14 | 103,774 |
No | output | 1 | 51,887 | 14 | 103,775 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.
A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a1, a2, ..., an.
Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse al, al + 1, ..., ar so that the length of the longest non-decreasing subsequence of the new sequence is maximum.
A non-decreasing subsequence is a sequence of indices p1, p2, ..., pk, such that p1 < p2 < ... < pk and ap1 ≤ ap2 ≤ ... ≤ apk. The length of the subsequence is k.
Input
The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.
The second line contains n space-separated integers, describing the original sequence a1, a2, ..., an (1 ≤ ai ≤ 2, i = 1, 2, ..., n).
Output
Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.
Examples
Input
4
1 2 1 2
Output
4
Input
10
1 1 2 2 2 1 1 2 2 1
Output
9
Note
In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.
In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
pref = [0] * (n + 1)
for i in range(n):
pref[i + 1] = pref[i] + (1 if a[i] == 1 else 0)
suf = [0] * (n + 1)
for i in reversed(range(n)):
suf[i] = suf[i + 1] + (1 if a[i] == 2 else 0)
dp = [0, 0, 0, 0]
for i in range(n):
new_dp = [max(dp[i], dp[i - 1]) if i > 0 else dp[i] for i in range(4)]
if a[i] == 1:
new_dp[0] += 1
new_dp[2] += 1
else:
new_dp[1] += 1
new_dp[3] += 1
dp = new_dp
print(max(max([pref[i] + suf[i] for i in range(n + 1)]), dp[-1]))
``` | instruction | 0 | 51,888 | 14 | 103,776 |
No | output | 1 | 51,888 | 14 | 103,777 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.