text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 β€ q β€ 1000
* -10000 β€ xi, yi β€ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
#! /usr/bin/env python3
from typing import List, Tuple
from math import sqrt
from enum import Enum
EPS = 1e-10
def float_equal(x: float, y: float) -> bool:
return abs(x - y) < EPS
class PointLocation(Enum):
COUNTER_CLOCKWISE = 1
CLOCKWISE = 2
ONLINE_BACK = 3
ONLINE_FRONT = 4
ON_SEGMENT = 5
class Point:
def __init__(self, x: float=0.0, y: float=0.0) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
return "Point({}, {})".format(self.x, self.y)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Point):
# print("NotImplemented in Point")
return NotImplemented
return float_equal(self.x, other.x) and \
float_equal(self.y, other.y)
def __add__(self, other: 'Point') -> 'Point':
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other: 'Point') -> 'Point':
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, k: float) -> 'Point':
return Point(self.x * k, self.y * k)
def __rmul__(self, k: float) -> 'Point':
return self * k
def __truediv__(self, k: float) -> 'Point':
return Point(self.x / k, self.y / k)
def __lt__(self, other: 'Point') -> bool:
return self.y < other.y \
if abs(self.x - other.x) < EPS \
else self.x < other.x
def norm(self):
return self.x * self.x + self.y * self.y
def abs(self):
return sqrt(self.norm())
def dot(self, other: 'Point') -> float:
return self.x * other.x + self.y * other.y
def cross(self, other: 'Point') -> float:
return self.x * other.y - self.y * other.x
def is_orthogonal(self, other: 'Point') -> bool:
return float_equal(self.dot(other), 0.0)
def is_parallel(self, other: 'Point') -> bool:
return float_equal(self.cross(other), 0.0)
def distance(self, other: 'Point') -> float:
return (self - other).abs()
def in_side_of(self, seg: 'Segment') -> bool:
return seg.vector().dot(
Segment(seg.p1, self).vector()) >= 0
def in_width_of(self, seg: 'Segment') -> bool:
return \
self.in_side_of(seg) and \
self.in_side_of(seg.reverse())
def distance_to_line(self, seg: 'Segment') -> float:
return \
abs((self - seg.p1).cross(seg.vector())) / \
seg.length()
def distance_to_segment(self, seg: 'Segment') -> float:
if not self.in_side_of(seg):
return self.distance(seg.p1)
if not self.in_side_of(seg.reverse()):
return self.distance(seg.p2)
else:
return self.distance_to_line(seg)
def location(self, seg: 'Segment') -> PointLocation:
p = self - seg.p1
d = seg.vector().cross(p)
if d < 0:
return PointLocation.CLOCKWISE
elif d > 0:
return PointLocation.COUNTER_CLOCKWISE
else:
r = (self.x - seg.p1.x) / (seg.p2.x - seg.p1.x)
if r < 0:
return PointLocation.ONLINE_BACK
elif r > 1:
return PointLocation.ONLINE__FRONT
else:
return PointLocation.ON_SEGMENT
Vector = Point
class Segment:
def __init__(self, p1: Point = None, p2: Point = None) -> None:
self.p1: Point = Point() if p1 is None else p1
self.p2: Point = Point() if p2 is None else p2
def __repr__(self) -> str:
return "Segment({}, {})".format(self.p1, self.p2)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Segment):
# print("NotImplemented in Segment")
return NotImplemented
return self.p1 == other.p1 and self.p2 == other.p2
def vector(self) -> Vector:
return self.p2 - self.p1
def reverse(self) -> 'Segment':
return Segment(self.p2, self.p1)
def length(self) -> float:
return self.p1.distance(self.p2)
def is_orthogonal(self, other: 'Segment') -> bool:
return self.vector().is_orthogonal(other.vector())
def is_parallel(self, other: 'Segment') -> bool:
return self.vector().is_parallel(other.vector())
def projection(self, p: Point) -> Point:
v = self.vector()
vp = p - self.p1
return v.dot(vp) / v.norm() * v + self.p1
def reflection(self, p: Point) -> Point:
x = self.projection(p)
return p + 2 * (x - p)
def intersect_ratio(self, other: 'Segment') -> Tuple[float, float]:
a = self.vector()
b = other.vector()
c = self.p1 - other.p1
s = b.cross(c) / a.cross(b)
t = a.cross(c) / a.cross(b)
return s, t
def intersects(self, other: 'Segment') -> bool:
s, t = self.intersect_ratio(other)
return (0 <= s <= 1) and (0 <= t <= 1)
def intersection(self, other: 'Segment') -> Point:
s, _ = self.intersect_ratio(other)
return self.p1 + s * self.vector()
def distance_with_segment(self, other: 'Segment') -> float:
if not self.is_parallel(other) and \
self.intersects(other):
return 0
else:
return min(
self.p1.distance_to_segment(other),
self.p2.distance_to_segment(other),
other.p1.distance_to_segment(self),
other.p2.distance_to_segment(self))
Line = Segment
class Circle:
def __init__(self, c: Point=None, r: float=0.0) -> None:
self.c: Point = Point() if c is None else c
self.r: float = r
def __eq__(self, other: object) -> bool:
if not isinstance(other, Circle):
return NotImplemented
return self.c == other.c and self.r == other.r
def __repr__(self) -> str:
return "Circle({}, {})".format(self.c, self.r)
def main() -> None:
x0, y0, x1, y1 = [int(x) for x in input().split()]
s = Segment(Point(x0, y0), Point(x1, y1))
q = int(input())
for _ in range(q):
x2, y2 = [int(x) for x in input().split()]
print(Point(x2, y2).location(s).name)
if __name__ == "__main__":
main()
```
No
| 85,700 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 β€ q β€ 1000
* -10000 β€ xi, yi β€ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
class Point:
def __init__(self, x , y):
self.x = x
self.y = y
def __sub__(self, p):
x_sub = self.x - p.x
y_sub = self.y - p.y
return Point(x_sub, y_sub)
class Vector:
def __init__(self, p):
self.x = p.x
self.y = p.y
def norm(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def cross(v1, v2):
return v1.x * v2.y - v1.y * v2.x
def dot(v1, v2):
return v1.x * v2.x + v1.x * v2.x
def ccw(p0, p1, p2):
a = Vector(p1 - p0)
b = Vector(p2 - p0)
cross_ab = cross(a, b)
if cross_ab > 0:
print("COUNTER_CLOCKWISE")
elif cross_ab < 0:
print("CLOCKWISE")
elif dot(a, b) < 0:
print("ONLINE_BACK")
elif a.norm() < b.norm():
print("ONLINE_FRONT")
else:
print("ON_SEGMENT")
import sys
file_input = sys.stdin
x_p0, y_p0, x_p1, y_p1 = map(int, file_input.readline().split())
p0 = Point(x_p0, y_p0)
p1 = Point(x_p1, y_p1)
q = map(int, file_input.readline())
for line in file_input:
x_p2, y_p2 = map(int, line.split())
p2 = Point(x_p2, y_p2)
ccw(p0, p1, p2)
```
No
| 85,701 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 β€ q β€ 1000
* -10000 β€ xi, yi β€ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
import math
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
class Vector():
def __init__(self, start, end):
self.x = end.x - start.x
self.y = end.y - start.y
self.r = math.sqrt(pow(self.x, 2) + pow(self.y, 2))
self.theta = math.atan2(self.y, self.x)
x0, y0, x1, y1 = list(map(int, input().split(' ')))
p0, p1 = Point(x0, y0), Point(x1, y1)
vec1 = Vector(p0, p1)
q = int(input())
for i in range(q):
x2, y2 = list(map(int, input().split(' ')))
p2 = Point(x2, y2)
vec2 = Vector(p0, p2)
if vec1.theta == vec2.theta:
if vec1.r < vec2.r:
print('ONLINE_FRONT')
else:
print('ON_SEGMENT')
elif abs(vec1.theta - vec2.theta) == math.pi:
print('ONLINE_BACK')
elif vec2.theta - vec1.theta > 0:
print('COUNTER_CLOCKWISE')
elif vec2.theta - vec1.theta < 0:
print('CLOCKWISE')
```
No
| 85,702 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 β€ q β€ 1000
* -10000 β€ xi, yi β€ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
#! /usr/bin/env python3
from typing import List, Tuple
from math import sqrt
from enum import Enum
EPS = 1e-10
def float_equal(x: float, y: float) -> bool:
return abs(x - y) < EPS
class PointLocation(Enum):
COUNTER_CLOCKWISE = 1
CLOCKWISE = 2
ONLINE_BACK = 3
ONLINE_FRONT = 4
ON_SEGMENT = 5
class Point:
def __init__(self, x: float=0.0, y: float=0.0) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
return "Point({}, {})".format(self.x, self.y)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Point):
# print("NotImplemented in Point")
return NotImplemented
return float_equal(self.x, other.x) and \
float_equal(self.y, other.y)
def __add__(self, other: 'Point') -> 'Point':
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other: 'Point') -> 'Point':
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, k: float) -> 'Point':
return Point(self.x * k, self.y * k)
def __rmul__(self, k: float) -> 'Point':
return self * k
def __truediv__(self, k: float) -> 'Point':
return Point(self.x / k, self.y / k)
def __lt__(self, other: 'Point') -> bool:
return self.y < other.y \
if abs(self.x - other.x) < EPS \
else self.x < other.x
def norm(self):
return self.x * self.x + self.y * self.y
def abs(self):
return sqrt(self.norm())
def dot(self, other: 'Point') -> float:
return self.x * other.x + self.y * other.y
def cross(self, other: 'Point') -> float:
return self.x * other.y - self.y * other.x
def is_orthogonal(self, other: 'Point') -> bool:
return float_equal(self.dot(other), 0.0)
def is_parallel(self, other: 'Point') -> bool:
return float_equal(self.cross(other), 0.0)
def distance(self, other: 'Point') -> float:
return (self - other).abs()
def in_side_of(self, seg: 'Segment') -> bool:
return seg.vector().dot(
Segment(seg.p1, self).vector()) >= 0
def in_width_of(self, seg: 'Segment') -> bool:
return \
self.in_side_of(seg) and \
self.in_side_of(seg.reverse())
def distance_to_line(self, seg: 'Segment') -> float:
return \
abs((self - seg.p1).cross(seg.vector())) / \
seg.length()
def distance_to_segment(self, seg: 'Segment') -> float:
if not self.in_side_of(seg):
return self.distance(seg.p1)
if not self.in_side_of(seg.reverse()):
return self.distance(seg.p2)
else:
return self.distance_to_line(seg)
def location(self, seg: 'Segment') -> PointLocation:
p = self - seg.p1
d = seg.vector().cross(p)
if d < 0:
return PointLocation.CLOCKWISE
elif d > 0:
return PointLocation.COUNTER_CLOCKWISE
else:
r = (self.x - seg.p1.x) / (seg.p2.x - seg.p1.x)
if r < 0:
return PointLocation.ONLINE_BACK
elif r > 1:
return PointLocation.ONLINE_FRONT
else:
return PointLocation.ON_SEGMENT
Vector = Point
class Segment:
def __init__(self, p1: Point = None, p2: Point = None) -> None:
self.p1: Point = Point() if p1 is None else p1
self.p2: Point = Point() if p2 is None else p2
def __repr__(self) -> str:
return "Segment({}, {})".format(self.p1, self.p2)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Segment):
# print("NotImplemented in Segment")
return NotImplemented
return self.p1 == other.p1 and self.p2 == other.p2
def vector(self) -> Vector:
return self.p2 - self.p1
def reverse(self) -> 'Segment':
return Segment(self.p2, self.p1)
def length(self) -> float:
return self.p1.distance(self.p2)
def is_orthogonal(self, other: 'Segment') -> bool:
return self.vector().is_orthogonal(other.vector())
def is_parallel(self, other: 'Segment') -> bool:
return self.vector().is_parallel(other.vector())
def projection(self, p: Point) -> Point:
v = self.vector()
vp = p - self.p1
return v.dot(vp) / v.norm() * v + self.p1
def reflection(self, p: Point) -> Point:
x = self.projection(p)
return p + 2 * (x - p)
def intersect_ratio(self, other: 'Segment') -> Tuple[float, float]:
a = self.vector()
b = other.vector()
c = self.p1 - other.p1
s = b.cross(c) / a.cross(b)
t = a.cross(c) / a.cross(b)
return s, t
def intersects(self, other: 'Segment') -> bool:
s, t = self.intersect_ratio(other)
return (0 <= s <= 1) and (0 <= t <= 1)
def intersection(self, other: 'Segment') -> Point:
s, _ = self.intersect_ratio(other)
return self.p1 + s * self.vector()
def distance_with_segment(self, other: 'Segment') -> float:
if not self.is_parallel(other) and \
self.intersects(other):
return 0
else:
return min(
self.p1.distance_to_segment(other),
self.p2.distance_to_segment(other),
other.p1.distance_to_segment(self),
other.p2.distance_to_segment(self))
Line = Segment
class Circle:
def __init__(self, c: Point=None, r: float=0.0) -> None:
self.c: Point = Point() if c is None else c
self.r: float = r
def __eq__(self, other: object) -> bool:
if not isinstance(other, Circle):
return NotImplemented
return self.c == other.c and self.r == other.r
def __repr__(self) -> str:
return "Circle({}, {})".format(self.c, self.r)
def main() -> None:
x0, y0, x1, y1 = [int(x) for x in input().split()]
s = Segment(Point(x0, y0), Point(x1, y1))
q = int(input())
for _ in range(q):
x2, y2 = [int(x) for x in input().split()]
print(Point(x2, y2).location(s).name)
if __name__ == "__main__":
main()
```
No
| 85,703 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
# coding=utf-8
A = list(map(int, input().split()))
print("{0} {1}".format(min(A), max(A)))
```
| 85,704 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_3_A&lang=jp
# Min-Max
from functools import reduce
from collections import deque
import sys
input = sys.stdin.readline
def main():
l = list ( map(int,input().split()))
max = reduce(lambda a,b : a if a > b else b,l)
min = reduce(lambda a,b : a if a < b else b,l)
print (min,max)
if __name__ == '__main__':
main()
```
| 85,705 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
if __name__ == '__main__':
a,b,c = map(int,input().split())
print(min(a,b,c),max(a,b,c))
```
| 85,706 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
a= list(map(int, input().split()))
print('{} {}'.format(min(a), max(a)))
```
| 85,707 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
inp = list(map(int, input().split()))
print("{} {}".format(min(inp), max(inp)))
```
| 85,708 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
temp = list(map(int, input().split()))
print(min(temp), max(temp))
```
| 85,709 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
l = list(map(int, input().split()))
print(min(l),max(l))
```
| 85,710 |
Provide a correct Python 3 solution for this coding contest problem.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
"Correct Solution:
```
a=list(map(int,input().split()))
print("{} {}".format(min(a),max(a)))
```
| 85,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
numbers = list(map(int, input().split()))
print(str(min(numbers))+" "+str(max(numbers)))
```
Yes
| 85,712 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
a=[int(i) for i in input().split()]
print(min(a),max(a))
```
Yes
| 85,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
a = list(map(int, input().split()))
print('%d %d' % (min(a), max(a)))
```
Yes
| 85,714 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
# coding: utf-8
# Your code here!
a,b,c=map(int,input().split())
print(min(a,b,c),max(a,b,c))
```
Yes
| 85,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
a, b, c = list(map(int, input().split()))
print('{} {}'.format(min([a,b,c], max([a,b,c])))
```
No
| 85,716 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
nums = list(map(int, input().split(' ')))
max = -float('inf')
min = flaot('inf')
for num in nums:
if num > max: max = num
if num < min: min = num
print(min, max)
```
No
| 85,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
a, b, c = list(map(int, input().split()))
print('{} {}'.format(min([a,b,c], max([a,b,c]))))
```
No
| 85,718 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given three integers $a, b, c$, print the minimum value and the maximum value.
Constraints
* $-1,000,000,000 \leq a, b, c \leq 1,000,000,000$
Input
The input is given in the following format.
$a \; b \; c\;$
Three integers $a, b, c$ are given in a line.
Output
Print the minimum and maximum values separated by a space in a line.
Example
Input
4 5 3
Output
3 5
Submitted Solution:
```
a, b, c = list(map(int, input().splist()))
print('{} {}'.format(min([a,b,c], max([a,b,c]))))
```
No
| 85,719 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 β€ n β€ 2 β
10^5, 1 β€ m β€ 2 β
10^5, 1 β€ k < n) β the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 β€ i β€ m) of the next m lines contains two integers x and y (1β€ x, yβ€ n, xβ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1β€ iβ€ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7.
Tags: graphs
Correct Solution:
```
from collections import deque
def solve(adj, m, k, uv):
n = len(adj)
nn = [len(a) for a in adj]
q = deque()
for i in range(n):
if nn[i] < k:
q.append(i)
while q:
v = q.popleft()
for u in adj[v]:
nn[u] -= 1
if nn[u] == k-1:
q.append(u)
res = [0]*m
nk = len([1 for i in nn if i >= k])
res[-1] = nk
for i in range(m-1, 0, -1):
u1, v1 = uv[i]
if nn[u1] < k or nn[v1] < k:
res[i - 1] = nk
continue
if nn[u1] == k:
q.append(u1)
nn[u1] -= 1
if not q and nn[v1] == k:
q.append(v1)
nn[v1] -= 1
if not q:
nn[u1] -= 1
nn[v1] -= 1
adj[u1].remove(v1)
adj[v1].remove(u1)
while q:
v = q.popleft()
nk -= 1
for u in adj[v]:
nn[u] -= 1
if nn[u] == k - 1:
q.append(u)
res[i - 1] = nk
return res
n, m, k = map(int, input().split())
a = [set() for i in range(n)]
uv = []
for i in range(m):
u, v = map(int, input().split())
a[u - 1].add(v - 1)
a[v - 1].add(u - 1)
uv.append((u-1, v-1))
res = solve(a, m, k, uv)
print(str(res)[1:-1].replace(' ', '').replace(',', '\n'))
```
| 85,720 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 β€ n β€ 2 β
10^5, 1 β€ m β€ 2 β
10^5, 1 β€ k < n) β the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 β€ i β€ m) of the next m lines contains two integers x and y (1β€ x, yβ€ n, xβ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1β€ iβ€ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7.
Submitted Solution:
```
maxN =200005
G = [None] * maxN
s = set()
k = [0] * 1
def delete(v):
if len(G[v]) < k[0] and (v in s):
s.remove(v)
for u in G[v]:
G[u].discard(v)
delete(u)
def main():
n,m,k[0] = map(int,input().split())
edges = [None] * (m + 1)
ans = [0] * m
for i in range(m):
u,v = map(int,input().split())
if G[u] is None:
G[u] = set()
if G[v] is None:
G[v] = set()
G[u].add(v)
G[v].add(u)
edges[i+1] = (u,v)
for i in range(1,n+1):
s.add(i)
for i in range(1,n+1):
delete(i)
i = m
while i > 0:
ans[i-1] = len(s)
e = edges[i]
G[e[0]].discard(e[1])
G[e[1]].discard(e[0])
delete(e[0])
delete(e[1])
i-=1
for i in range(m):
print(ans[i])
```
No
| 85,721 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 β€ n β€ 2 β
10^5, 1 β€ m β€ 2 β
10^5, 1 β€ k < n) β the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 β€ i β€ m) of the next m lines contains two integers x and y (1β€ x, yβ€ n, xβ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1β€ iβ€ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7.
Submitted Solution:
```
maxN =200005
G = [None] * maxN
s = set()
k = [0] * 1
def delete(v):
if len(G[v]) < k[0] and (v in s):
s.remove(v)
for u in G[v]:
G[u].discard(v)
delete(u)
def main():
n,m,k[0] = map(int,input().split())
edges = [None] * (m + 1)
ans = [0] * (m + 1)
for i in range(m):
u,v = map(int,input().split())
if G[u] is None:
G[u] = set()
if G[v] is None:
G[v] = set()
G[u].add(v)
G[v].add(u)
edges[i+1] = (u,v)
for i in range(1,n+1):
s.add(i)
for i in range(1,n+1):
delete(i)
i = m
while i > 0:
ans[i] = len(s)
e = edges[i]
G[e[0]].discard(e[1])
G[e[1]].discard(e[0])
delete(e[0])
delete(e[1])
i-=1
for i in range(1,m+1):
print(ans[i])
```
No
| 85,722 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 β€ n β€ 2 β
10^5, 1 β€ m β€ 2 β
10^5, 1 β€ k < n) β the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 β€ i β€ m) of the next m lines contains two integers x and y (1β€ x, yβ€ n, xβ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1β€ iβ€ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7.
Submitted Solution:
```
first = input()
first = first.split()
n = int(first[0])
m = int(first[1])
k = int(first[2])
d= {}
for i in range(m):
new = input()
new = new.split()
f1 = int(new[0])
f2 = int(new[1])
if f1 in d:
d[f1].append(f2)
else:
d[f1] = [f2]
if f2 in d:
d[f2].append(f1)
else:
d[f2] = [f1]
count = 0
for friend in d:
if len(d[friend])>=k:
val = len(d[friend])
for person in d[friend]:
if len(d[person])<k:
val -=1
if val>=k:
count+=1
print(count)
```
No
| 85,723 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
* Either this person does not go on the trip,
* Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 β€ n β€ 2 β
10^5, 1 β€ m β€ 2 β
10^5, 1 β€ k < n) β the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 β€ i β€ m) of the next m lines contains two integers x and y (1β€ x, yβ€ n, xβ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1β€ iβ€ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
* 1,2,3 can go on day 3 and 4.
In the second example,
* 2,4,5 can go on day 4 and 5.
* 1,2,4,5 can go on day 6 and 7.
* 1,2,3,4,5 can go on day 8.
In the third example,
* 1,2,5 can go on day 5.
* 1,2,3,5 can go on day 6 and 7.
Submitted Solution:
```
maxN =200005
G = [None] * maxN
s = set()
k = [0] * 1
def delete(v):
if len(G[v]) < k[0] and (v in s):
s.remove(v)
for u in G[v]:
G[u].discard(v)
delete(u)
def main():
n,m,k[0] = map(int,input().split())
edges = [None] * (m + 1)
ans = [0] * m
for i in range(m):
u,v = map(int,input().split())
if G[u] is None:
G[u] = set()
if G[v] is None:
G[v] = set()
G[u].add(v)
G[v].add(u)
edges[i+1] = (u,v)
for i in range(1,n+1):
s.add(i)
for i in range(1,n+1):
delete(i)
i = m
while i > 0:
ans[i-1] = len(s)
e = edges[i]
G[e[0]].discard(e[1])
G[e[1]].discard(e[0])
delete(e[0])
delete(e[1])
i-=1
print(str(ans)[1:-1].replace(' ', '').replace(',', '\n'))
```
No
| 85,724 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
n=int(input())
s=input()
con=n//11
if (s.count('8')>=con) and (n>=11):
print(con)
elif (s.count('8')!=0) and (n>=11):
print(s.count('8'))
else:
print(0)
```
| 85,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
n=int(input())
k=0
p=n//11
s=input()
for i in range(len(s)):
if s[i]=='8':
k+=1
else:
pass
if p<=k:
print(p)
elif p>k:
print(k)
```
| 85,726 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
def read():
return int(input())
def read_cards():
s = input()
return list(int(e) for e in s)
n = read()
arr = read_cards()
print(min(len(arr) // 11, arr.count(8)))
```
| 85,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
n = int(input())
c = sum(map(lambda x: x == '8', input()))
print(min(n//11, c))
```
| 85,728 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
n = int(input())
s = input()
n8 = 0
for i in range(n):
if s[i] == '8':
n8 += 1
ans = 0
for i in range(n8, 0, -1):
if (n - i) // 10 >= i:
ans = i
break
print(ans)
```
| 85,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
n=int(input())
num=input()
e=0
for x in num:
if x == '8':
e+=1
print(min(e,n//11))
```
| 85,730 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
n = int(input())
s = input()
ans = 0
for i in range(0, n) :
if s[i] == '8' :
ans = ans + 1
print(int(min(n / 11, ans)))
```
| 85,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Tags: brute force
Correct Solution:
```
n = int(input())
s = str(input())
n8 = s.count("8")
print(min(n // 11 , n8))
```
| 85,732 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
a=int(input())
b=input()
cnt=0
for i in b:
if i=='8':
cnt+=1
if a>=11:
if b.find('8')>=0:
x=a//11
print(min(cnt, x))
else:
print("0")
else:
print("0")
```
Yes
| 85,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
import math
n=int(input())
s=input()
print(min(s.count('8'), n//11))
```
Yes
| 85,734 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
n=int(input())
s=input()
res1=0
res2=0
l='8'
if l not in s or n<11:
print(0)
else:
res1=s.count(l)
res2=n//11
print(min(res1, res2))
```
Yes
| 85,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
a=int(input())//11
b=input()
b = b.count("8")
c=min(a,b)
print(c )
```
Yes
| 85,736 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
n=int(input())
s=input()
i=s.count('8')
if(s.count('8')==0):
print(0)
else:
while(i>0):
if (n-i)>=10*i:
print(i)
break
else:
i-=1
```
No
| 85,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
n = int(input())
s = input()
c = 0
for i in range(s.count('8')):
n -= 10
if n < 0:
break
c += 1
print(c)
```
No
| 85,738 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
n=int(input())
arr=input()
d=[0]*10
for i in arr:
d[int(i)]+=1
if(d[8]==0 or len(arr)<11):
print(0)
else:
count=0
for i in range(len(d)):
if(d[i]>=10 and d[8]>=1):
count+=1
d[i]-=10
d[8]-=1
z=d[8]
z=int(z)
d[8]=10000000
x=min(d)
if(z>=2):
if(x*2==z):
count+=x
else:
z=z//2
count+=abs(x-z)
print(count)
else:
print(count)
```
No
| 85,739 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit.
For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not.
You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct.
Input
The first line contains an integer n β the number of cards with digits that you have (1 β€ n β€ 100).
The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, β¦, s_n. The string will not contain any other characters, such as leading or trailing spaces.
Output
If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0.
Examples
Input
11
00000000008
Output
1
Input
22
0011223344556677889988
Output
2
Input
11
31415926535
Output
0
Note
In the first example, one phone number, "8000000000", can be made from these cards.
In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789".
In the third example you can't make any phone number from the given cards.
Submitted Solution:
```
a = int(input())
st = str(input())
if (a < 11):
print(0)
exit()
map = {}
for i in range(10):
map[i]=0
for i in range(a):
map[int(st[i])] += 1
if map[8] == 0:
print(0)
exit()
map[8] -= 1
ans = 0
print(int(a / 11))
```
No
| 85,740 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
n = int(input())
for i in range(n):
a = list(map(int,input().split(" ")))
if a[1] >= a[2] >= a[0] or a[0] >= a[2] >= a[1]:
if a[1] > a[0]:
a[0],a[1] = a[1], a[0]
print(a[0]//a[2]*a[2]+a[2])
else:
print(a[2])
```
| 85,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
def f(l, r, d):
if d>r:
return d
for i in range(d,l,d):
if i%d ==0:
return i
return (int(r/d)+1)*d
n = int(input())
for i in range(n):
row = input().split(' ')
l = int(row[0])
r = int(row[1])
d = int(row[2])
print(str(int(f(l,r,d))))
```
| 85,742 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
test_cases = int(input())
for i in range(test_cases) :
left, right, divisor = map(int, input().split(" "))
if divisor < left :
print(divisor)
else :
print(divisor * ((right // divisor) + 1))
```
| 85,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
q = int(input())
L = [0]*q
R = [0]*q
D = [0]*q
for i in range(q):
entrada = str(input()).split()
L[i] = int(entrada[0])
R[i] = int(entrada[1])
D[i] = int(entrada[2])
def find_x(l,r,d):
result = d
if l <= d <= r:
if r % d == 0:
result = r + d
else:
result = int(r/d)*d + d
return result
for i in range(q):
print( find_x(L[i], R[i], D[i]))
```
| 85,744 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
n = int(input())
for _ in range(n):
l,r,d = map(int,input().split())
if d<l:
print(d)
else:
a = r//d
print((a+1)*d)
```
| 85,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
q=int(input())
for i in range(q):
p=input().split()
l=int(p[0])
r=int(p[1])
d=int(p[2])
if d<l:
print(d)
else:
print(r+d-r%d)
```
| 85,746 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
for i in range(int(input())):
l, r, d = map(int, input().split())
if l <= d <= r:
print((r // d + 1)*d)
else:
print(d)
```
| 85,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Tags: math
Correct Solution:
```
q = int(input())
M = [list(map(int, input().split())) for i in range(q)]
S =[]
#x = 1
l = len(S)
for i in range(q):
x = 1
while True:
if x < M[i][0] or x > M[i][1]:
if x % M[i][2] == 0:
S.append(x)
break
else:
if x < M[i][2]:
x = M[i][2]
else:
x = x+(M[i][2]-x % M[i][2])
else:
if M[i][2] >= M[i][0]:
x = M[i][1]+1
elif M[i][2] < M[i][0]:
x = 2
for i in S:
print(i)
```
| 85,748 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
import math
import os
import random
import re
import sys
from collections import Counter
a=[]
l=[]
n=int(input())
for i in range(n):
l+=[list(map(int,input().split()))]
for i in range(n):
if l[i][2]<=l[i][1] and l[i][2]>=l[i][0]:
print(l[i][2]*((l[i][1]//l[i][2])+1))
else:
print(l[i][2])
```
Yes
| 85,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
def func():
q = int(input())
while q:
l , r , d = map(int , input().split())
if d>=l and d<=r:
k = r//d
ans = d*(k+1)
else:
ans = d
print(ans)
q-=1
if __name__ == '__main__':
func()
```
Yes
| 85,750 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
import math
sa=lambda :input()
sb=lambda:int(input())
sc=lambda:input().split()
sd=lambda:list(map(int,input().split()))
se=lambda:float(input())
sf=lambda:list(input())
#10101001
def hnbhai():
l,r,d=sd()
temp=d
while(temp>=l and temp<=r):
temp+=d
if temp>=l and temp<=r:
temp=(r//d+1)*d
break
print(temp)
for _ in range(sb()):
hnbhai()
```
Yes
| 85,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
t = int(input())
for case in range(t):
l, r, d = [int(x) for x in input().split(' ')]
if d < l:
ans = d
else:
ans = d * (r // d + 1)
print(ans)
```
Yes
| 85,752 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
from math import ceil
q = int(input())
for i in range(q):
l, r, d = list(map(int, input().split()))
x = (ceil(l / d) - 1) * d
y = (r // d +1)* d
s = min(x,y)
if s == 0:
print(max(x,y))
else:
print(s)
```
No
| 85,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
for i in range(int(input())):
l, r, d = map(int, input().split())
ans = False
p = 0
while ans == False:
if l >= 1:
l -= 1
r += 1
if r%d == 0:
ans = True
p = r
break
elif l%d == 0:
ans = True
p = l
break
print(p)
```
No
| 85,754 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
for _ in range(int(input())):
l,r,d = [int(_) for _ in input().strip().split()]
if d < l:
print(d)
elif d > l:
print(d)
else:
print(r+1 + r+1%d)
```
No
| 85,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
Submitted Solution:
```
a=int(input())
k=[]
for i in range(a):
b,c,d=list(map(int,input().split()))
o=True
for j in range(1,b):
if j%d==0:
k.append(j)
o=False
break
if (o==True)and(c<=999999999):
for j in range(c+1,1000000001):
if j%d==0:
k.append(j)
break
print(k)
for i in range(len(k)):
print(k[i])
```
No
| 85,756 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
Submitted Solution:
```
print (16)
```
No
| 85,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
Submitted Solution:
```
print(1)
```
No
| 85,758 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
import sys
input = sys.stdin.readline
def getN():
return int(input())
def getList():
return list(map(int, input().split()))
from sys import exit
n = getN()
vertex = [[] for i in range(n)]
for i in range(n-1):
a, b = getList()
vertex[a-1].append(b-1)
vertex[b - 1].append(a - 1)
for v in vertex:
if len(v) == 2:
print("NO")
exit()
print("YES")
```
| 85,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
n = int(input())
deg = [0]*n
for i in range(n-1):
u, v = map(int, input().split())
deg[u-1] += 1
deg[v-1] += 1
if all(i != 2 for i in deg):
print("YES")
else:
print("NO")
# cnt = sum(1 for i in deg if i == 1)
# if cnt*(cnt-1)//2 >= n - 1:
# print("YES")
# else:
# print("NO")
```
| 85,760 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
n = int(input())
a = []
for i in range(0,n+9):
a.append(0)
for i in range(1,n):
u,v = map(int,input().split())
a[u] = a[u]+1
a[v] = a[v]+1
flag = 1;
for i in range(1,n+1):
if a[i]==2:
flag = 0
if flag==0:
print("NO")
else:
print("YES")
```
| 85,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7
import threading
threading.stack_size(10**8)
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
def solve():
n = int(ri())
g = [ [] for i in range(n)]
for i in range(n-1):
a,b = Ri();a-=1;b-=1
g[a].append(b);g[b].append(a)
# child = [0]*n
# print(g)
flag = [True]
def dfs1(cur, par):
if len(g[cur]) == 2:
flag[0] = False
return
for child in g[cur]:
if child == par: continue
dfs1(child, cur)
return
dfs1(0, -1)
if flag[0]:
YES()
else:
NO()
threading.Thread(target= solve).start()
```
| 85,762 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
n = int(input())
g = [[] for i in range(n+1)]
d = [0]*100001
for i in range(n-1):
u, v = [int(i) for i in input().split()]
g[u].append(v)
g[v].append(u)
d[u] += 1
d[v] += 1
for i in d:
if i == 2:
print("NO")
break
else:
print("YES")
```
| 85,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
n=int(input())
deg=[0]*n
for i in range(n-1):
u,v=map(int,input().split())
u-=1
v-=1
deg[u]+=1
deg[v]+=1
for d in deg:
if d==2:
print("NO")
exit(0)
print("YES")
```
| 85,764 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
from sys import stdin
input = stdin.readline
n = int(input())
degree = [0 for i in range(n+1)]
for _ in range(n-1):
i, j = [int(i) for i in input().split()]
degree[i] += 1
degree[j] += 1
res = False
for i in range(1, n+1):
if degree[i] == 2: res = True
if res: print("NO")
else: print("YES")
```
| 85,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Tags: trees
Correct Solution:
```
# https://codeforces.com/contest/1189/problem/D1
n = int(input())
g = {}
p = {}
path = {}
flg = True
for _ in range(n-1):
u,v = map(int, input().split())
if u not in g:
g[u] = []
g[u].append(v)
if v not in g:
g[v] = []
g[v].append(u)
flg = 'YES'
for x in g:
if len(g[x]) == 1:continue
if len(g[x]) == 2:
flg = 'NO'
break
print(flg)
```
| 85,766 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
n = int(input())
g = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
g[u].append(v)
g[v].append(u)
for v in range(n):
if len(g[v]) == 2:
print('NO')
exit(0)
print('YES')
```
Yes
| 85,767 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
n=int(input())
tree={}
for i in range(n-1):
a,b=[int(x) for x in input().split()]
if a not in tree:
tree[a]=1
else:
tree[a]+=1
if b not in tree:
tree[b]=1
else:
tree[b]+=1
for item in tree:
if tree[item]==2:
print('NO')
break
else:
print('YES')
```
Yes
| 85,768 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
import sys
input=sys.stdin.readline
from collections import deque
n=int(input())
if n==2:
print('YES')
exit()
if n==3:
print('NO')
exit()
Edges=[[] for _ in range(n)]
for _ in range(n-1):
u,v=map(lambda x: int(x)-1,input().split())
Edges[u].append(v)
Edges[v].append(u)
for i,E in enumerate(Edges):
if len(E)>=3:
root=i
break
else:
print('NO')
exit()
Chi=[[] for _ in range(n)]
Par=[0]*n
q=deque()
q.append(root)
Used=[False]*n
Used[root]=True
while q:
v=q.popleft()
for c in Edges[v]:
if Used[c]:
continue
Chi[v].append(c)
Par[c]=v
Used[c]=True
q.append(c)
Leaf=[]
for v,l in enumerate(Chi):
if not l:
Leaf.append(v)
for l in Leaf:
while True:
p=Par[l]
if p==root:
break
if len(Chi[p])==1:
print('NO')
exit()
l=p
print('YES')
```
Yes
| 85,769 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())
N = inp()
cnt = [0]*N
for _ in range(N-1):
x,y = inpl()
cnt[x-1] += 1
cnt[y-1] += 1
for c in cnt:
if c == 2:
print('NO')
break
else:
print('YES')
```
Yes
| 85,770 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
#! /usr/bin/env python3
import sys
from math import factorial
class Node:
def __init__(self, num):
self.num = num
self.binds = []
# for dijkstra
self.marker = False
self.val = None
def add_bind(self, oth):
self.binds.append(oth)
def __repr__(self):
return '<{}: {}{}>'.format(
self.num,
[i.num for i in self.binds],
', \tval: {}'.format(self.val) if self.val != None else ''
)
class Graph:
def __init__(self, size):
self.size = size
self.nodes = [None] + [Node(num) for num in range(1, size+1)]
def read_input(self):
for _ in range(1, self.size):
i, j = (int(x) for x in sys.stdin.readline().split())
self.nodes[i].add_bind(self.nodes[j])
self.nodes[j].add_bind(self.nodes[i])
def __repr__(self):
return '\n'.join(str(node) for node in self.nodes[1:])
def pairs(n):
return factorial(n) // ( factorial(n-2) * 2 )
N = int(sys.stdin.readline())
g = Graph(N)
g.read_input()
#print(g)
ends = [node for node in g.nodes[1:] if len(node.binds) == 1]
#print(pairs(len(ends)), , len(ends))
print('YES' if pairs(len(ends)) >= N-1 else 'NO')
```
No
| 85,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
class Graph(object):
"""docstring for Graph"""
def __init__(self,n,d): # Number of nodes and d is True if directed
self.n = n
self.graph = [[] for i in range(n)]
self.parent = [-1 for i in range(n)]
self.directed = d
def addEdge(self,x,y):
self.graph[x].append(y)
if not self.directed:
self.graph[y].append(x)
def bfs(self, root): # NORMAL BFS
queue = [root]
queue = deque(queue)
vis = [0]*self.n
while len(queue)!=0:
element = queue.popleft()
vis[element] = 1
count = 0
for i in self.graph[element]:
if vis[i]==0:
queue.append(i)
self.parent[i] = element
vis[i] = 1
count += 1
if count==1 and element!=0:
return False
return True
def dfs(self, root, ans): # Iterative DFS
stack=[root]
vis=[0]*self.n
stack2=[]
while len(stack)!=0: # INITIAL TRAVERSAL
element = stack.pop()
if vis[element]:
continue
vis[element] = 1
stack2.append(element)
for i in self.graph[element]:
if vis[i]==0:
self.parent[i] = element
stack.append(i)
while len(stack2)!=0: # BACKTRACING. Modify the loop according to the question
element = stack2.pop()
m = 0
for i in self.graph[element]:
if i!=self.parent[element]:
m += ans[i]
ans[element] = m
return ans
def shortestpath(self, source, dest): # Calculate Shortest Path between two nodes
self.bfs(source)
path = [dest]
while self.parent[path[-1]]!=-1:
path.append(parent[path[-1]])
return path[::-1]
def detect_cycle(self):
indeg = [0]*self.n
for i in range(self.n):
for j in self.graph[i]:
indeg[j] += 1
q = deque()
vis = 0
for i in range(self.n):
if indeg[i]==0:
q.append(i)
while len(q)!=0:
e = q.popleft()
vis += 1
for i in self.graph[e]:
indeg[i] -= 1
if indeg[i]==0:
q.append(i)
if vis!=self.n:
return True
return False
def reroot(self, root, ans):
stack = [root]
vis = [0]*n
while len(stack)!=0:
e = stack[-1]
if vis[e]:
stack.pop()
# Reverse_The_Change()
continue
vis[e] = 1
for i in graph[e]:
if not vis[e]:
stack.append(i)
if self.parent[e]==-1:
continue
# Change_The_Answers()
n = int(input())
g = Graph(n,False)
for i in range(n-1):
u,v = map(int,input().split())
g.addEdge(u-1,v-1)
for i in range(n):
if len(g.graph[i])==1:
leaf = i
break
if not g.bfs(leaf):
print ("NO")
else:
print ("YES")
```
No
| 85,772 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
n = int(input())
tree = [[] for i in range(n)]
for i in range(n - 1):
a1, a2 = map(int, input().split())
tree[a1 - 1].append(a2 - 1)
tree[a2 - 1].append(a1 - 1)
if n == 2:
print('YES')
elif n == 3:
print('NO')
else:
tree_leafs = [False for i in range(n)]
for a in range(n):
if len(tree[a]) == 1:
tree_leafs[a] = True
no_var = False
for a0 in range(n):
if tree_leafs[a0] == False:
k = 0
for a in tree[a0]:
if tree_leafs[a] == True:
k += 1
if k == 2:
break
if k == 1:
print('NO')
no_var = True
break
if not no_var:
print('YES')
```
No
| 85,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
# @author
import sys
class D1AddOnATree:
def dfs(self, start):
self.done[start] = 1
for x in self.adj[start]:
if self.done[x]:
continue
self.par[x] = start
self.dfs(x)
def solve(self):
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 5)
n = int(input())
self.adj = defaultdict(list)
self.par = defaultdict(int)
self.done = [0] * (n + 1)
for i in range(n - 1):
u, v = [int(_) for _ in input().split()]
self.adj[u].append(v)
v = max(len(self.adj[p]) for p in self.adj)
start = -1
for p in self.adj:
if len(self.adj[p]) == v:
start = p
break
assert(start != -1)
self.dfs(start)
cnt = [0] * (n + 1)
for k in self.adj:
if self.par[k] == 0:
continue
if len(self.adj[k]) == 1:
cnt[self.par[k]] += 1
for x in cnt:
if x == 1:
print("NO")
break
else:
print("YES")
solver = D1AddOnATree()
input = sys.stdin.readline
solver.solve()
```
No
| 85,774 |
Provide tags and a correct Python 2 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(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+
#main code
n=ni()
BITTree=[0]*(n+1)
def gs(i):
s = 0
i = i+1
while i > 0:
s += BITTree[i]
i -= i & (-i)
return s
def ub(i ,v):
i += 1
while i <= n:
BITTree[i] += v
i += i & (-i)
def fun(x):
ret=0
sm=0
for i in range(21,-1,-1):
pw=1<<i
if ret+pw<=n and sm+BITTree[ret+pw]<=x:
ret+=pw
sm+=BITTree[ret]
return ret
l=li()
for i in range(n):
ub(i,i)
ans=[0]*n
for i in range(n-1,-1,-1):
ans[i]=fun(l[i])
ub(ans[i],-ans[i])
pa(ans)
```
| 85,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
# 1208D
class segTree():
def __init__(self, n):
self.t = [0] * (n << 2)
def update(self, node, l, r, index, value):
if l == r:
self.t[node] = value
return
mid = (l + r) >> 1
if index <= mid:
self.update(node*2, l, mid, index, value)
else:
self.update(node*2 + 1, mid + 1, r, index, value)
self.t[node] = self.t[node*2] + self.t[node*2 + 1]
def query(self, node, l, r, value):
if l == r:
return self.t[node]
mid = (l + r) >> 1
if self.t[node*2] >= value:
return self.query(node*2, l, mid, value)
return self.query(node*2 + 1, mid + 1, r, value - self.t[node*2])
def do():
n = int(input())
nums = [int(i) for i in input().split(" ")]
res = [0]*n
weightTree = segTree(n)
for i in range(1, n+1):
weightTree.update(1, 1, n, i, i)
# print(weightTree.t)
for i in range(n-1, -1, -1):
res[i] = weightTree.query(1, 1, n, nums[i] + 1)
weightTree.update(1, 1, n, res[i], 0)
return " ".join([str(c) for c in res])
print(do())
```
| 85,776 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
# https://codeforces.com/contest/1208/problem/D
from sys import stdin, stdout
input = stdin.readline
# print = stdout.write
# si is the sum of elements before the i-th element that are smaller than the i-th element.
# For every i from N to 1, let's say the value of the si is x.
# So it means there are k smallest unused numbers whose sum is x.
# We simply put the k+1st number in the output permutation at this i, and continue to move left.
# BIT and binary lifting
# https://codeforces.com/contest/1208/submission/59526098
class BIT:
def __init__(self, nums):
# we store the sum information in bit 1.
# so the indices should be 1 based.
# here we assume nums[0] = 0
self.nums = [0] * (len(nums))
for i, x in enumerate(nums):
if i == 0:
continue
self.update(i, x)
def low_bit(self, x):
return x & (-x)
def update(self, i, diff):
while i < len(self.nums):
self.nums[i] += diff
i += self.low_bit(i)
def prefix_sum(self, i):
ret = 0
while i != 0:
ret += self.nums[i]
i -= self.low_bit(i)
return ret
def search(self, x):
# find the index i such that prefix_sum(i) == x
cur_index, cur_sum = 0, 0
delta = len(self.nums) - 1
while delta - self.low_bit(delta):
delta -= self.low_bit(delta)
while delta:
m = cur_index + delta
if m < len(self.nums):
sm = cur_sum + self.nums[m]
if sm <= x:
cur_index, cur_sum = m, sm
delta //= 2
return cur_index + 1
n = int(input())
bit = BIT(range(n+1))
ans = [0 for _ in range(n)]
nums = list(map(int, input().split()))
for i in range(n - 1, -1, -1):
index = bit.search(nums[i])
bit.update(index, -index)
ans[i] = index
print(*ans)
```
| 85,777 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
class FTree:
def __init__(self, f):
self.n = len(f)
self.ft = [0] * (self.n + 1)
for i in range(1, self.n + 1):
self.ft[i] += f[i - 1]
if i + self.lsone(i) <= self.n:
self.ft[i + self.lsone(i)] += self.ft[i]
def lsone(self, s):
return s & (-s)
def query(self, i, j):
if i > 1:
return self.query(1, j) - self.query(1, i - 1)
s = 0
while j > 0:
s += self.ft[j]
j -= self.lsone(j)
return s
def update(self, i, v):
while i <= self.n:
self.ft[i] += v
i += self.lsone(i)
def select(self, k):
lo = 1
hi = self.n
for i in range(19): ######## 30
mid = (lo + hi) // 2
if self.query(1, mid) < k:
lo = mid
else:
hi = mid
return hi
n = int(input())
data = [int(i) for i in input().split()]
ft = FTree(list(range(1, n+1)))
ans = [""]*n
for i in range(n-1, -1, -1):
val = data[i]
ind = ft.select(val+1)
ans[i] = str(ind)
ft.update(ind, -ind)
print(" ".join(ans))
```
| 85,778 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
# https://codeforces.com/contest/1208/problem/D
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
# For every i from N to 1, let's say the value of the si is x.
# So it means there are k smallest unused numbers whose sum is x.
# We simply put the (k+1)st number in the output permutation at this i, and continue to move left.
# segment tree and binary search
_ = input()
x = [int(i) for i in input().split()]
res = []
from math import log
class SegmentTree(object):
def __init__(self, nums):
self.arr = nums
self.l = len(nums)
self.tree = [0] * self.l + nums
for i in range(self.l - 1, 0, -1):
self.tree[i] = self.tree[i << 1] + self.tree[i << 1 | 1]
def update(self, i, val):
n = self.l + i
self.tree[n] = val
while n > 1:
self.tree[n >> 1] = self.tree[n] + self.tree[n ^ 1]
n >>= 1
def query(self, i, j):
m = self.l + i
n = self.l + j
res = 0
while m <= n:
if m & 1:
res += self.tree[m]
m += 1
m >>= 1
if n & 1 == 0:
res += self.tree[n]
n -= 1
n >>= 1
return res
tree = SegmentTree(list(range(1, len(x) + 1)))
org = len(x)
while x:
# from back to forth
q = x.pop()
lo = 0
hi = org - 1
while lo < hi:
mid = (lo + hi) // 2
# print(lo, hi, mid)
sm = tree.query(0, mid)
# print(sm, mid)
if sm > q:
hi = mid
else:
lo = mid + 1
# print(tree.arr, lo, hi)
idx = tree.arr[lo]
# print(idx)
tree.update(lo, 0)
# also from back to forth
res.append(idx)
print(' '.join(str(i) for i in res[::-1]))
```
| 85,779 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
BIT=[0]*(n+1)
def update(v,w):
while v<=n:
BIT[v]+=w
v+=(v&(-v))
def getvalue(v):
ANS=0
while v!=0:
ANS+=BIT[v]
v-=(v&(-v))
return ANS
for i in range(1,n+1):
update(i,i)
ANS=[-1]*n
for i in range(n-1,-1,-1):
MIN=0
MAX=n
k=A[i]
while True:
x=(MIN+MAX+1)//2
if getvalue(x)>k:
if getvalue(x-1)==k:
ANS[i]=x
break
else:
MAX=x
else:
MIN=x
update(x,-x)
print(*ANS)
```
| 85,780 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
from collections import defaultdict
from collections import deque
import math
import copy
#T = int(input())
N = int(input())
#s1 = input()
#s2 = input()
#N,Q = [int(x) for x in stdin.readline().split()]
arr = [int(x) for x in stdin.readline().split()]
bit = [0]*(N+1)
series = [0] + [x for x in range(N)]
def lowbit(x):
return x&(-x)
def update(idx,delta):
while idx<=N:
bit[idx] += delta
idx += lowbit(idx)
def query(x):
s = 0
while x>0:
s += bit[x]
x -= lowbit(x)
return s
# init
for i in range(1,N+1):
bit[i] += series[i]
y = i + lowbit(i)
if y<=N:
series[y] += series[i]
visited = [0]*(N+1)
ans = [0]*N
for i in range(N-1,-1,-1):
# find
left = 1
right = N
target = arr[i]
while left<=right:
mid = (left+right)//2
q = query(mid)
#print(mid,q)
if q<target:
left = mid + 1
elif q>target:
right = mid - 1
else:
if visited[mid]==1:
left = mid + 1
else:
visited[mid] = 1
ans[i] = mid
break
# update
update(mid+1,-mid)
print(*ans)
```
| 85,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
from operator import add
class Stree:
def __init__(self, f, n, default, init_data):
self.ln = 2**(n-1).bit_length()
self.data = [default] * (self.ln * 2)
self.f = f
for i, d in init_data.items():
self.data[self.ln + i] = d
for j in range(self.ln - 1, 0, -1):
self.data[j] = f(self.data[j*2], self.data[j*2+1])
def update(self, i, a):
p = self.ln + i
self.data[p] = a
while p > 1:
p = p // 2
self.data[p] = self.f(self.data[p*2], self.data[p*2+1])
def get(self, i, j):
def _get(l, r, p):
if i <= l and j >= r:
return self.data[p]
else:
m = (l+r)//2
if j <= m:
return _get(l, m, p*2)
elif i >= m:
return _get(m, r, p*2+1)
else:
return self.f(_get(l, m, p*2), _get(m, r, p*2+1))
return _get(0, self.ln, 1)
def find_value(self, v):
def _find_value(l, r, p, v):
if r == l+1:
return l
elif self.data[p*2] <= v:
return _find_value((l+r)//2, r, p*2+1, v - self.data[p*2])
else:
return _find_value(l, (l+r)//2, p*2, v)
return _find_value(0, self.ln, 1, v)
def main():
n = int(input())
sums = {i:i for i in range(n+1)}
stree = Stree(add, n+1, 0, sums)
ss = list(map(int, input().split()))
ss.reverse()
pp = []
for s in ss:
sval = stree.find_value(s)
pp.append(sval)
stree.update(sval,0)
print(*(reversed(pp)))
if __name__ == "__main__":
main()
```
| 85,782 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Tags: binary search, data structures, greedy, implementation
Correct Solution:
```
def update(x,val):
while x<=n:
BIT[x]+=val
x+=(x&-x)
def query(x):
s=0
while x>0:
s=(s+BIT[x])
x-=(x&-x)
return s
n=int(input())
BIT=[0]*(n+1)
for i in range(1,n+1):
update(i,i)
arr=list(map(int,input().split()))
answers=[0]*(n)
#print(BIT)
for i in range(n-1,-1,-1):
lol=arr[i]
low=0
fjf=0
high=n
# print(lol)
while True:
mid=(high+low+1)//2
j=query(mid)
# print(mid,j)
# print(answers)
# break
if j>lol:
if query(mid-1)==lol:
answers[i]=mid
update(mid,-mid)
break
else:
high=mid
else:
low=mid
print(*answers)
```
| 85,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
def sumsegtree(l,seg,st,en,x):
if st==en:
seg[x]=l[st]
else:
mid=(st+en)>>1
sumsegtree(l,seg,st,mid,2*x)
sumsegtree(l,seg,mid+1,en,2*x+1)
seg[x]=seg[2*x]+seg[2*x+1]
def query(seg,st,en,val,x):
if st==en:
return seg[x]
mid=(st+en)>>1
if seg[2*x]>=val:
return query(seg,st,mid,val,2*x)
return query(seg,mid+1,en,val-seg[2*x],2*x+1)
def upd(seg,st,en,ind,val,x):
if st==en:
seg[x]=val
return
mid=(st+en)>>1
if mid>=ind:
upd(seg,st,mid,ind,val,2*x)
else:
upd(seg,mid+1,en,ind,val,2*x+1)
seg[x]=seg[2*x]+seg[2*x+1]
n=int(input())
l=list(map(int,range(1,n+1)))
s=[0]*n
p=list(map(int,input().split()))
seg=["#"]*(n<<2)
sumsegtree(l,seg,0,len(l)-1,1)
for i in range(n-1,-1,-1):
s[i]=query(seg,1,n,p[i]+1,1)
upd(seg,1,n,s[i],0,1)
print (*s)
```
Yes
| 85,784 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
from sys import setrecursionlimit as SRL, stdin
SRL(10 ** 7)
rd = stdin.readline
rrd = lambda: map(int, rd().strip().split())
n = int(rd())
bit = [0] * 200005
def add(x, val):
while x <= n:
bit[x] += val
x += (x & -x)
def query(x):
num = 0
for i in range(30, -1, -1):
if num+(1 << i) <= n and bit[num + (1 << i)] <= x:
x -= bit[num + (1 << i)]
num += (1 << i)
return num + 1
for i in range(1, n + 1):
add(i, i)
s = list(rrd())
ans = []
for i in range(len(s) - 1, -1, -1):
q = query(s[i])
ans.append(q)
add(q, -q)
ans = ans[::-1]
print(*ans)
```
Yes
| 85,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------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
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------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------------------------------------
n=int(input())
l=list(map(int,input().split()))
f=[i for i in range(1,n+1)]
s=SegmentTree(f)
ans=[0]*n
for i in range(n-1,-1,-1):
st=1
end=n
while(st<=end):
mid=(st+end)//2
su=s.query(0,mid-2)
if su==l[i]:
an=mid
st=mid+1
elif su<l[i]:
st=mid+1
else:
end=mid-1
ans[i]=an
s.__setitem__(an-1,0)
print(*ans,sep=" ")
```
Yes
| 85,786 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
from sys import stdin,stdout
class Tree(object):
def __init__(self,n):
self.tree=[0]*(4*n+10)
def update(self,L,C,l,r,rt):
if l==r:
self.tree[rt]+=C
return
mid=(l+r)//2
if L<=mid:
self.update(L,C,l,mid,rt<<1)
else:
self.update(L,C,mid+1,r,rt<<1|1)
self.tree[rt]=self.tree[rt<<1]+self.tree[rt<<1|1]
def query(self,s,l,r,rt):
if l==r:
return l
mid=(l+r)//2
if self.tree[rt<<1]>s:
return self.query(s,l,mid,rt<<1)
else:
return self.query(s-self.tree[rt<<1],mid+1,r,rt<<1|1)
if __name__ == '__main__':
n=int(stdin.readline())
a=list(map(int,stdin.readline().split()))
b=[0]*(n+10)
seg=Tree(n)
for i in range(n):
seg.update(i+1,i+1,1,n,1)
for i in range(n,0,-1):
b[i]=seg.query(a[i-1],1,n,1)
seg.update(b[i],-b[i],1,n,1)
for i in range(n):
stdout.write('%d '%(b[i+1]))
```
Yes
| 85,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
n=int(input())
a=[]
b={}
for i in input().split():
a.append(int(i))
b[int(i)]=True
qq=list(b.keys())
qq=sorted(qq)
ii=1
b=[0 for i in range(n)]
for i in qq:
for i2 in range(n-1,-1,-1):
if a[i2]==i:
b[i2]=ii
ii+=1
for i in b:
print(i,end=' ')
```
No
| 85,788 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
res = [0]*n
for i in range(n-1,-1,-1):
le = int((2*arr[i])**0.5)
res[i] = le+1
index = n-1
while index >= 0:
temp = res[index]
l = 0
while res[index] == temp and index >= 0:
res[index] = str(temp+l)
l += 1
index -= 1
print(' '.join(res))
```
No
| 85,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
t=int(input())
l1=[int(num) for num in input().split()]
k=0
c=1
l3=[]
l=0
for i in range(1,t):
if(l1[i]==l1[i-1]) or (l1[i]==0):
c+=1
continue
else:
if(l1[i-1]==0):
if(i!=1):
l=c
c=c-1
p=int(((l1[i]-k)-(((c-1)*c)/2))/c)
for j in range(1,l+1):
l3.append(p+(c-j))
if(i==t-1):
l3.append(p+c)
else:
l=c
p=int(((l1[i]-k)-(((c-1)*c)/2))/c)
for j in range(1,l+1):
l3.append(p+(l-j))
if(i==t-1):
l3.append(p+c)
else:
l=c
p=int(((l1[i]-k)-(((c-1)*c)/2))/c)
for j in range(1,l+1):
l3.append(p+(l-j))
if(i==t-1):
l3.append(p+c)
c=1
k=l1[i]
if(c==t):
for i in range(1,t+1):
l3.append(t-i+1)
for i in range(0,t):
print(l3[i],end=" ")
```
No
| 85,790 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_{1},p_{2}, β¦,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].
There is a hidden permutation of length n.
For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.
Your task is to restore the permutation.
Input
The first line contains a single integer n (1 β€ n β€ 2 β
10^{5}) β the size of the permutation.
The second line contains n integers s_{1}, s_{2}, β¦, s_{n} (0 β€ s_{i} β€ (n(n-1))/(2)).
It is guaranteed that the array s corresponds to a valid permutation of length n.
Output
Print n integers p_{1}, p_{2}, β¦, p_{n} β the elements of the restored permutation. We can show that the answer is always unique.
Examples
Input
3
0 0 0
Output
3 2 1
Input
2
0 1
Output
1 2
Input
5
0 1 1 1 10
Output
1 4 3 2 5
Note
In the first example for each i there is no index j satisfying both conditions, hence s_i are always 0.
In the second example for i = 2 it happens that j = 1 satisfies the conditions, so s_2 = p_1.
In the third example for i = 2, 3, 4 only j = 1 satisfies the conditions, so s_2 = s_3 = s_4 = 1. For i = 5 all j = 1, 2, 3, 4 are possible, so s_5 = p_1 + p_2 + p_3 + p_4 = 10.
Submitted Solution:
```
n=int(input())
a=[]
b={}
for i in input().split():
a.append(int(i))
b[int(i)]=True
qq=list(b.keys())
ii=1
b=[0 for i in range(n)]
for i in qq:
for i2 in range(n-1,-1,-1):
if a[i2]==i:
b[i2]=ii
ii+=1
for i in b:
print(i,end=' ')
```
No
| 85,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
n=int(input())
s=sorted([[v,-i] for i,v in enumerate(map(int,input().split()))])
for _ in range(int(input())):
k,i=map(int,input().split())
ans=sorted(s[-k:],key=lambda x:-x[1])
print(ans[i-1][0])
```
| 85,792 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
def main():
n = int(input())
a = list(enumerate(map(int, (input().split()))))
a.sort(key = lambda item: (item[1], -item[0]))
#print(a)
m = int(input())
for i in range(m):
k, pos = map(int, input().split())
s = a[-k:]
s = sorted(s)
print(s[pos - 1][1])
main()
```
| 85,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
a_sorted = sorted(a, reverse=True)
m = int(input())
for i in range(m):
k, pos = [int(x) for x in input().split()]
vals = a_sorted[0:k]
result = []
for val in a:
if val in vals:
result.append(val)
vals.remove(val)
if len(vals) == 0:
break
print(result[pos - 1])
```
| 85,794 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
n = int(input())
b = list(map(int,input().split()))
a = [[0] * 2 for i in range(n)]
for i in range(n):
a[i][0] = b[i]
a[i][1] = i
for i in range(n-1):
for j in range(n-i-1):
if a[j][0] > a[j+1][0]:
a[j], a[j+1] = a[j+1], a[j]
elif (a[j][0] == a[j + 1][0]) and (a[j][1] < a[j + 1][1]):
a[j], a[j+1] = a[j+1], a[j]
m = int(input())
for k in range(m):
k, ind = map(int,input().split())
ans = [[0] * 2 for i in range(k)]
for i in range(k):
ans[i][0] = a[n - i - 1][1]
ans[i][1] = a[n - i - 1][0]
ans.sort()
print(ans[ind - 1][1])
```
| 85,795 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
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-----------------------------------------------------
n=int(input())
a1=list(map(int,input().split()))
a=list()
for i in range (n):
a.append((a1[i],n-i))
a.sort(reverse=True)
m=int(input())
for i in range (m):
k,pos=map(int,input().split())
c=list()
for j in range (k):
c.append(n-a[j][1])
c.sort()
res=a1[c[pos-1]]
print(res)
```
| 85,796 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a)
b.reverse()
m = int(input())
for _ in range(m):
k, pos = map(int, input().split())
x = 0
j = 0
d = dict()
ans = 0
for i in b[:k]:
d[i] = d.get(i, 0) + 1
while x != pos:
u = a[j]
if u in d:
d[u] -= 1
x += 1
if d[u] == 0:
del d[u]
j += 1
ans = u
print(ans)
```
| 85,797 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
n = int(input())
line = list(map(int, input().split()))
m = int(input())
for _ in range(m):
k, pos = map(int, input().split())
lf = line[:]
while len(lf) > k:
j = -1
x = min(lf)
while j > -len(lf)-1:
if lf[j] == x:
del lf[j]
if len(lf) == k:
break
else:
j -= 1
print(lf[pos-1])
```
| 85,798 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
Tags: data structures, greedy
Correct Solution:
```
def mergesort(l, r, arr, pos):
if r - l == 1:
return arr, pos
m = (l + r) // 2
arr, pos = mergesort(l, m, arr, pos)
arr, pos = mergesort(m, r, arr, pos)
c = [0 for i in range(r)]
d = [0 for i in range(r)]
poi_a = l
poi_b = m
for i in range(l, r):
if poi_a == m:
c[i] = arr[poi_b]
d[i] = pos[poi_b]
poi_b += 1
elif poi_b == r:
c[i] = arr[poi_a]
d[i] = pos[poi_a]
poi_a += 1
elif a[poi_a] > arr[poi_b]:
c[i] = arr[poi_a]
d[i] = pos[poi_a]
poi_a += 1
else:
c[i] = arr[poi_b]
d[i] = pos[poi_b]
poi_b += 1
for i in range(l, r):
arr[i] = c[i]
pos[i] = d[i]
return arr, pos
n = int(input())
a = list(map(int, input().split()))
p = [i for i in range(n)]
temp = a[:]
a, p = mergesort(0, n, a, p)
for m in range(int(input())):
k, pos = map(int, input().split())
j = k
while j < n and a[j - 1] == a[j]:
j += 1
i = k - 1
l = 1
while i > 0 and a[i - 1] == a[i]:
i -= 1
l += 1
m = sorted(p[i:j])
res = sorted(m[:l] + p[:i])
print(temp[res[pos - 1]])
```
| 85,799 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.