message stringlengths 2 44.5k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 276 109k | cluster float64 23 23 | __index_level_0__ int64 552 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2 | instruction | 0 | 49,543 | 23 | 99,086 |
"Correct Solution:
```
N=int(input())
A=[]
for _ in range(0,N):
a=list(map(int,input().split()))
a.sort()
if A.count(a)==0:
A.append(a)
print(N-len(A))
``` | output | 1 | 49,543 | 23 | 99,087 |
Provide a correct Python 3 solution for this coding contest problem.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2 | instruction | 0 | 49,544 | 23 | 99,088 |
"Correct Solution:
```
N = int(input())
l = []
count = 0
for _ in range(N):
p = set(map(int, input().split()))
if p in l: count+=1
else: l.append(p)
print(count)
``` | output | 1 | 49,544 | 23 | 99,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
n=int(input())
n_keep=[]
count=0
for i in range(n):
n_li=list(map(int,input().split()))
n_li.sort()
if n_li in n_keep:
count+=1
else:
n_keep.append(n_li)
print(count)
``` | instruction | 0 | 49,545 | 23 | 99,090 |
Yes | output | 1 | 49,545 | 23 | 99,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
N = int(input())
s = set()
for i in range(N):
s.add(tuple(sorted(map(int, input().split()))))
print(N-len(s))
``` | instruction | 0 | 49,546 | 23 | 99,092 |
Yes | output | 1 | 49,546 | 23 | 99,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
N = int(input())
V = []
n = 0
for _ in range(N):
p = set(input().split())
if p in V:
n += 1
else:
V.append(p)
print(n)
``` | instruction | 0 | 49,547 | 23 | 99,094 |
Yes | output | 1 | 49,547 | 23 | 99,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
n = int(input())
a = []
c = 0
for i in range(n):
tmp = sorted(list(map(int, input().split())))
if tmp in a:
c += 1
else:
a.append(tmp)
print(c)
``` | instruction | 0 | 49,548 | 23 | 99,096 |
Yes | output | 1 | 49,548 | 23 | 99,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
l = []
N = int(input())
for i in range(N):
a,b,c = map(int,input().split(' '))
d = a + b + c
l.append(d)
e = l.count(9)
f = l.count(8)
g = l.count(7)
h = l.count(6)
ans = 0
if e>1:
ans += e - 1
if f>1:
ans += f - 1
if g>1:
ans += g - 1
if h>1:
ans += h - 1
if e = 1 and f = 1 and g = 1 and h = 1:
ans = 4
print(ans)
``` | instruction | 0 | 49,549 | 23 | 99,098 |
No | output | 1 | 49,549 | 23 | 99,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
N = int(input())
s = set()
for i in range(N):
a,b,c = map(str,input().split(' '))
e = a + b + c
e = sorted(d)
s.add(e)
print(N - len(s))
``` | instruction | 0 | 49,550 | 23 | 99,100 |
No | output | 1 | 49,550 | 23 | 99,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
N = int(input())
l = []
for i in range(N):
a,b,c = map(int,input().split(' '))
l.append(a,b,c)
l.sort()
s.add(l)
l = []
print(N - len(s))
``` | instruction | 0 | 49,551 | 23 | 99,102 |
No | output | 1 | 49,551 | 23 | 99,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Computer graphics uses polygon models as a way to represent three-dimensional shapes. A polygon model is a model that creates a surface by giving the coordinates of vertices and how to connect those vertices.
A general polygon model can handle any polygon, but this time we will consider a polygon model consisting of triangles. Any polygon model can be represented as a collection of surface information that represents a triangle.
One surface information represents three vertices side by side. However, if the same 3 points are arranged only in different arrangements, the same surface information will be represented. For example, in the tetrahedron shown below, the faces formed by connecting vertices 1, 2, and 3 can be represented as vertices 2, 3, 1, and vertices 3, 2, 1. In this way, if there are multiple pieces of the same surface information, it will be useless, so it is better to combine them into one.
<image>
Given the surface information, write a program to find the number of surface information that must be erased to eliminate duplicate surfaces.
Input
The input is given in the following format.
N
p11 p12 p13
p21 p22 p23
::
pN1 pN2 pN3
The number N (1 ≤ N ≤ 1000) of the surface information of the polygon model is given in the first line. The next N lines are given the number of vertices pij (1 ≤ pij ≤ 1000) used to create the i-th face. However, the same vertex is never used more than once for one face (pi1 ≠ pi2 and pi2 ≠ pi3 and pi1 ≠ pi3).
Output
Output the number of surface information that must be erased in order to eliminate duplicate surfaces on one line.
Examples
Input
4
1 3 2
1 2 4
1 4 3
2 3 4
Output
0
Input
6
1 3 2
1 2 4
1 4 3
2 3 4
3 2 1
2 3 1
Output
2
Submitted Solution:
```
N=int(input())
c=set()
for i in range(N):
ls=input().split()
ls.sort()
ls_sum=ls[0]*1000000+ls[1]*1000+ls[2]
c.add(ls_sum)
print(N-len(c))
``` | instruction | 0 | 49,552 | 23 | 99,104 |
No | output | 1 | 49,552 | 23 | 99,105 |
Provide a correct Python 3 solution for this coding contest problem.
3D Printing
We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction.
First, using a CAD system, we prepare n (n ≥ k) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied.
* Each cube may overlap zero, one or two other cubes, but not three or more.
* When a cube overlap two other cubes, those two cubes do not overlap.
* Two non-overlapping cubes do not touch at their surfaces, edges or corners.
Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area.
Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones.
<image>
Figure E1. A polyhedron formed with connected identical cubes.
Input
The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format.
n k s
x1 y1 z1
...
xn yn zn
In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i-th line, there are three integers xi, yi and zi that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied.
The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×107 ≤ xi, yi, zi ≤ 4×107.
The end of the input is indicated by a line containing three zeros separated by a space.
Output
For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1.
Sample Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output for the Sample Input
60000
1856
-1
1632
1856
2796
1640
Example
Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output
60000
1856
-1
1632
1856
2796
1640 | instruction | 0 | 49,567 | 23 | 99,134 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
input = sys.stdin.readline
INF = 10**20
EPS = 1.0 / 10**10
MOD = 10**9 + 7
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def I(): return int(input())
def F(): return float(input())
def S(): return input()
class Graph:
def __init__(self, _n):
self.n = _n
self.m = 0
self.g = [[] for i in range(_n)]
def add_edge(self, s, t, c = 1):
self.g[s].append((t,c))
self.g[t].append((s,c))
def __getitem__(self, v):
return self.g[v]
def edge_cost(i,j):
L = [s - abs(ps[i][k]-ps[j][k]) for k in range(3)]
if len([x for x in L if x <= 0]) > 0:
return -1
return 2*(L[0]*L[1]+L[1]*L[2]+L[2]*L[0])
def dfs(v, pv, k):
if k == 0:
for e in G[v]:
if e[0] != pv and used[e[0]]:
return e[1]
return 0
if used[v]:
return INF
used[v] = True
res = INF
for e in G[v]:
if e[0] == pv:
continue
dd = dfs(e[0],v,k-1)
if dd < INF:
res = min(res,dd+e[1])
used[v] = False
return res
if __name__ == '__main__':
while True:
n,k,s = LI()
if n == 0:
break
ps = [LI() for i in range(n)]
G = Graph(n)
for i in range(n-1):
for j in range(i+1,n):
c = edge_cost(i,j)
if c > 0:
G.add_edge(i,j,-c)
used = [False] * n
sub = INF
for i in range(n):
sub = min(sub,dfs(i,-1,k-1))
if sub == INF:
print(-1)
else:
print(6*k*s*s+sub)
``` | output | 1 | 49,567 | 23 | 99,135 |
Provide a correct Python 3 solution for this coding contest problem.
3D Printing
We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction.
First, using a CAD system, we prepare n (n ≥ k) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied.
* Each cube may overlap zero, one or two other cubes, but not three or more.
* When a cube overlap two other cubes, those two cubes do not overlap.
* Two non-overlapping cubes do not touch at their surfaces, edges or corners.
Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area.
Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones.
<image>
Figure E1. A polyhedron formed with connected identical cubes.
Input
The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format.
n k s
x1 y1 z1
...
xn yn zn
In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i-th line, there are three integers xi, yi and zi that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied.
The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×107 ≤ xi, yi, zi ≤ 4×107.
The end of the input is indicated by a line containing three zeros separated by a space.
Output
For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1.
Sample Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output for the Sample Input
60000
1856
-1
1632
1856
2796
1640
Example
Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output
60000
1856
-1
1632
1856
2796
1640 | instruction | 0 | 49,568 | 23 | 99,136 |
"Correct Solution:
```
class Cube:
def __init__(self, x, y, z, s):
self.x, self.y, self.z = x, y, z
self.s = s
def is_in_cube(self, x, y, z):
return self.x <= x <= self.x + self.s and self.y <= y <= self.y + self.s and self.z <= z <= self.z + self.s
def intersect(self, C):
dxyz = [(0, 0, 0),
(C.s, 0, 0), (0, C.s, 0), (0, 0, C.s),
(C.s, C.s, 0), (C.s, 0, C.s), (0, C.s, C.s),
(C.s, C.s, C.s)]
for dx1, dy1, dz1 in dxyz:
nx1, ny1, nz1 = C.x + dx1, C.y + dy1, C.z + dz1
if self.is_in_cube(nx1, ny1, nz1):
for dx2, dy2, dz2 in dxyz:
nx2, ny2, nz2 = self.x + dx2, self.y + dy2, self.z + dz2
if C.is_in_cube(nx2, ny2, nz2):
a, b, c = abs(nx1 - nx2), abs(ny1 - ny2), abs(nz1 - nz2)
if a * b * c == 0:
continue
# print(a, b, c, end=':')
return 2 * (a * b + b * c + c * a)
return 0
edges = list()
inters = dict()
def calc_overlap(vs):
ret = sum(inters.get((vs[i], vs[i + 1]), 0) for i in range(len(vs) - 1))
if len(vs) > 2:
ret += inters.get((vs[-1], vs[0]), 0)
return ret
def dfs(v, par, vs, res):
if res == 0:
return calc_overlap(vs)
ret = -1
for e in edges[v]:
if e != par:
vs.append(e)
ret = max(ret, dfs(e, v, vs, res - 1))
vs.pop()
return ret
while True:
N, K, S = map(int, input().split())
# print((N, K, S))
if not (N | K | S):
break
cubes = []
for _ in range(N):
x, y, z = map(int, input().split())
cubes.append(Cube(x, y, z, S))
# cubes = [Cube(*map(int, input().split()), S) for _ in range(N)]
edges = [[] for _ in range(N)]
inters = dict()
for i in range(N):
for j in range(i + 1, N):
sur = cubes[i].intersect(cubes[j])
if sur > 0:
# print(i, j, cubes[i].intersect(cubes[j]))
inters[i, j] = inters[j, i] = sur
edges[i].append(j)
edges[j].append(i)
# print(edges, inters)
ans = -1
for i in range(N):
ans = max(ans, dfs(i, -1, [i], K - 1))
print(-1 if ans == -1 else S * S * 6 * K - ans)
``` | output | 1 | 49,568 | 23 | 99,137 |
Provide a correct Python 3 solution for this coding contest problem.
3D Printing
We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction.
First, using a CAD system, we prepare n (n ≥ k) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied.
* Each cube may overlap zero, one or two other cubes, but not three or more.
* When a cube overlap two other cubes, those two cubes do not overlap.
* Two non-overlapping cubes do not touch at their surfaces, edges or corners.
Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area.
Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones.
<image>
Figure E1. A polyhedron formed with connected identical cubes.
Input
The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format.
n k s
x1 y1 z1
...
xn yn zn
In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i-th line, there are three integers xi, yi and zi that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied.
The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×107 ≤ xi, yi, zi ≤ 4×107.
The end of the input is indicated by a line containing three zeros separated by a space.
Output
For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1.
Sample Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output for the Sample Input
60000
1856
-1
1632
1856
2796
1640
Example
Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output
60000
1856
-1
1632
1856
2796
1640 | instruction | 0 | 49,569 | 23 | 99,138 |
"Correct Solution:
```
class Cube:
def __init__(self, x, y, z, s):
self.x, self.y, self.z = x, y, z
self.s = s
def is_in_cube(self, x, y, z):
return self.x <= x <= self.x + self.s and self.y <= y <= self.y + self.s and self.z <= z <= self.z + self.s
def intersect(self, C):
dxyz = [(0, 0, 0),
(C.s, 0, 0), (0, C.s, 0), (0, 0, C.s),
(C.s, C.s, 0), (C.s, 0, C.s), (0, C.s, C.s),
(C.s, C.s, C.s)]
for dx1, dy1, dz1 in dxyz:
nx1, ny1, nz1 = C.x + dx1, C.y + dy1, C.z + dz1
if self.is_in_cube(nx1, ny1, nz1):
for dx2, dy2, dz2 in dxyz:
nx2, ny2, nz2 = self.x + dx2, self.y + dy2, self.z + dz2
if C.is_in_cube(nx2, ny2, nz2):
a, b, c = abs(nx1 - nx2), abs(ny1 - ny2), abs(nz1 - nz2)
if a * b * c == 0:
continue
# print(a, b, c, end=':')
return 2 * (a * b + b * c + c * a)
return 0
edges = list()
inters = dict()
def calc_overlap(vs):
ret = sum(inters.get((vs[i], vs[i + 1]), 0) for i in range(len(vs) - 1))
if len(vs) > 2:
ret += inters.get((vs[-1], vs[0]), 0)
return ret
def dfs(v, par, vs, res):
if res == 0:
return calc_overlap(vs)
ret = -1
for e in edges[v]:
if e != par:
vs.append(e)
ret = max(ret, dfs(e, v, vs, res - 1))
vs.pop()
return ret
INF = 10 ** 9
while True:
N, K, S = map(int, input().split())
# print((N, K, S))
if not (N | K | S):
break
cubes = []
for _ in range(N):
x, y, z = map(int, input().split())
cubes.append(Cube(x, y, z, S))
# cubes = [Cube(*map(int, input().split()), S) for _ in range(N)]
edges = [[] for _ in range(N)]
inters = dict()
for i in range(N):
for j in range(i + 1, N):
sur = cubes[i].intersect(cubes[j])
if sur > 0:
# print(i, j, cubes[i].intersect(cubes[j]))
inters[i, j] = inters[j, i] = sur
edges[i].append(j)
edges[j].append(i)
# print(edges, inters)
ans = -1
for i in range(N):
ans = max(ans, dfs(i, -1, [i], K - 1))
print(-1 if ans == -1 else S * S * 6 * K - ans)
``` | output | 1 | 49,569 | 23 | 99,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
3D Printing
We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction.
First, using a CAD system, we prepare n (n ≥ k) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied.
* Each cube may overlap zero, one or two other cubes, but not three or more.
* When a cube overlap two other cubes, those two cubes do not overlap.
* Two non-overlapping cubes do not touch at their surfaces, edges or corners.
Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area.
Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones.
<image>
Figure E1. A polyhedron formed with connected identical cubes.
Input
The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format.
n k s
x1 y1 z1
...
xn yn zn
In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i-th line, there are three integers xi, yi and zi that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied.
The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×107 ≤ xi, yi, zi ≤ 4×107.
The end of the input is indicated by a line containing three zeros separated by a space.
Output
For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1.
Sample Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output for the Sample Input
60000
1856
-1
1632
1856
2796
1640
Example
Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output
60000
1856
-1
1632
1856
2796
1640
Submitted Solution:
```
class Cube:
def __init__(self, x, y, z, s):
self.x, self.y, self.z = x, y, z
self.s = s
def is_in_cube(self, x, y, z):
return self.x <= x <= self.x + self.s and self.y <= y <= self.y + self.s and self.z <= z <= self.z + self.s
def intersect(self, C):
dxyz = [(0, 0, 0),
(C.s, 0, 0), (0, C.s, 0), (0, 0, C.s),
(C.s, C.s, 0), (C.s, 0, C.s), (0, C.s, C.s),
(C.s, C.s, C.s)]
for dx1, dy1, dz1 in dxyz:
nx1, ny1, nz1 = C.x + dx1, C.y + dy1, C.z + dz1
if self.is_in_cube(nx1, ny1, nz1):
for dx2, dy2, dz2 in dxyz:
nx2, ny2, nz2 = self.x + dx2, self.y + dy2, self.z + dz2
if C.is_in_cube(nx2, ny2, nz2):
a, b, c = abs(nx1 - nx2), abs(ny1 - ny2), abs(nz1 - nz2)
if a * b * c == 0:
continue
# print(a, b, c, end=':')
return 2 * (a * b + b * c + c * a)
return 0
INF = 10 ** 9
while True:
N, K, S = map(int, input().split())
# print((N, K, S))
if not (N | K | S):
break
cubes = []
for _ in range(N):
x, y, z = map(int, input().split())
cubes.append(Cube(x, y, z, S))
# cubes = [Cube(*map(int, input().split()), S) for _ in range(N)]
if K == 1:
print(6 * S ** 2)
continue
edge = [[] for _ in range(N)]
for i in range(N):
for j in range(i + 1, N):
if cubes[i].intersect(cubes[j]):
# print(i, j, cubes[i].intersect(cubes[j]))
edge[i].append(j)
edge[j].append(i)
ans = INF
used = [False] * N
for i in range(N):
if not used[i] and len(edge[i]) == 1:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
# print(con_s)
base = 6 * (S ** 2) * K
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
for i in range(len(con_s) - K + 1):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
for i in range(N):
if not used[i] and len(edge[i]) == 2:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
a, b = cubes[con_c[0]], cubes[con_c[-1]]
con_s.append(a.intersect(b))
assert(con_s[-1] != 0)
# print(con_s)
if len(con_c) == K:
ans = min(ans, base - sum(con_s))
continue
con_s += con_s[1:]
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
base = 6 * (S ** 2) * K
for i in range(len(con_c)):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
print(ans if ans != INF else -1)
``` | instruction | 0 | 49,570 | 23 | 99,140 |
No | output | 1 | 49,570 | 23 | 99,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
3D Printing
We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction.
First, using a CAD system, we prepare n (n ≥ k) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied.
* Each cube may overlap zero, one or two other cubes, but not three or more.
* When a cube overlap two other cubes, those two cubes do not overlap.
* Two non-overlapping cubes do not touch at their surfaces, edges or corners.
Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area.
Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones.
<image>
Figure E1. A polyhedron formed with connected identical cubes.
Input
The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format.
n k s
x1 y1 z1
...
xn yn zn
In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i-th line, there are three integers xi, yi and zi that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied.
The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×107 ≤ xi, yi, zi ≤ 4×107.
The end of the input is indicated by a line containing three zeros separated by a space.
Output
For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1.
Sample Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output for the Sample Input
60000
1856
-1
1632
1856
2796
1640
Example
Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output
60000
1856
-1
1632
1856
2796
1640
Submitted Solution:
```
class Cube:
def __init__(self, x, y, z, s):
self.x, self.y, self.z = x, y, z
self.s = s
def is_in_cube(self, x, y, z):
return self.x <= x <= self.x + self.s and self.y <= y <= self.y + self.s and self.z <= z <= self.z + self.s
def intersect(self, C):
dxyz = [(0, 0, 0),
(C.s, 0, 0), (0, C.s, 0), (0, 0, C.s),
(C.s, C.s, 0), (C.s, 0, C.s), (0, C.s, C.s),
(C.s, C.s, C.s)]
for dx1, dy1, dz1 in dxyz:
nx1, ny1, nz1 = C.x + dx1, C.y + dy1, C.z + dz1
if self.is_in_cube(nx1, ny1, nz1):
for dx2, dy2, dz2 in dxyz:
nx2, ny2, nz2 = self.x + dx2, self.y + dy2, self.z + dz2
if C.is_in_cube(nx2, ny2, nz2):
a, b, c = abs(nx1 - nx2), abs(ny1 - ny2), abs(nz1 - nz2)
if a * b * c == 0:
continue
# print(a, b, c, end=':')
return 2 * (a * b + b * c + c * a)
return 0
INF = 10 ** 9
while True:
N, K, S = map(int, input().split())
# print((N, K, S))
if not (N | K | S):
break
cubes = []
for _ in range(N):
x, y, z = map(int, input().split())
cubes.append(Cube(x, y, z, S))
# cubes = [Cube(*map(int, input().split()), S) for _ in range(N)]
if K == 1:
# print(6 * S ** 2)
continue
edge = [[] for _ in range(N)]
for i in range(N):
for j in range(i + 1, N):
if cubes[i].intersect(cubes[j]):
# print(i, j, cubes[i].intersect(cubes[j]))
edge[i].append(j)
edge[j].append(i)
ans = INF
used = [False] * N
for i in range(N):
if not used[i] and len(edge[i]) == 1:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
# print(con_s)
base = 6 * (S ** 2) * K
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
for i in range(len(con_s) - K + 1):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
for i in range(N):
if not used[i] and len(edge[i]) == 2:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
a, b = cubes[con_c[0]], cubes[con_c[-1]]
con_s.append(a.intersect(b))
# print(con_s)
if len(con_c) == K:
ans = min(ans, base - sum(con_s))
continue
con_s += con_s[1:]
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
base = 6 * (S ** 2) * K
for i in range(len(con_c)):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
print(ans if ans != INF else -1)
``` | instruction | 0 | 49,571 | 23 | 99,142 |
No | output | 1 | 49,571 | 23 | 99,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
3D Printing
We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction.
First, using a CAD system, we prepare n (n ≥ k) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied.
* Each cube may overlap zero, one or two other cubes, but not three or more.
* When a cube overlap two other cubes, those two cubes do not overlap.
* Two non-overlapping cubes do not touch at their surfaces, edges or corners.
Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area.
Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones.
<image>
Figure E1. A polyhedron formed with connected identical cubes.
Input
The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format.
n k s
x1 y1 z1
...
xn yn zn
In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i-th line, there are three integers xi, yi and zi that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied.
The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×107 ≤ xi, yi, zi ≤ 4×107.
The end of the input is indicated by a line containing three zeros separated by a space.
Output
For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1.
Sample Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output for the Sample Input
60000
1856
-1
1632
1856
2796
1640
Example
Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output
60000
1856
-1
1632
1856
2796
1640
Submitted Solution:
```
class Cube:
def __init__(self, x, y, z, s):
self.x, self.y, self.z = x, y, z
self.s = s
def is_in_cube(self, x, y, z):
return self.x <= x <= self.x + self.s and self.y <= y <= self.y + self.s and self.z <= z <= self.z + self.s
def intersect(self, C):
dxyz = [(0, 0, 0),
(C.s, 0, 0), (0, C.s, 0), (0, 0, C.s),
(C.s, C.s, 0), (C.s, 0, C.s), (0, C.s, C.s),
(C.s, C.s, C.s)]
for dx1, dy1, dz1 in dxyz:
nx1, ny1, nz1 = C.x + dx1, C.y + dy1, C.z + dz1
if self.is_in_cube(nx1, ny1, nz1):
for dx2, dy2, dz2 in dxyz:
nx2, ny2, nz2 = self.x + dx2, self.y + dy2, self.z + dz2
if C.is_in_cube(nx2, ny2, nz2):
a, b, c = abs(nx1 - nx2), abs(ny1 - ny2), abs(nz1 - nz2)
if a * b * c == 0:
continue
# print(a, b, c, end=':')
return 2 * (a * b + b * c + c * a)
return 0
INF = 10 ** 9
while True:
N, K, S = map(int, input().split())
# print((N, K, S))
if not (N | K | S):
break
cubes = []
for _ in range(N):
x, y, z = map(int, input().split())
cubes.append(Cube(x, y, z, S))
# cubes = [Cube(*map(int, input().split()), S) for _ in range(N)]
if K == 1:
print(6 * S ** 2)
continue
edge = [[] for _ in range(N)]
for i in range(N):
for j in range(i + 1, N):
if cubes[i].intersect(cubes[j]):
# print(i, j, cubes[i].intersect(cubes[j]))
edge[i].append(j)
edge[j].append(i)
ans = INF
used = [False] * N
for i in range(N):
if not used[i] and len(edge[i]) == 1:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
# print(con_s)
base = 6 * (S ** 2) * K
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
for i in range(len(con_s) - K + 1):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
for i in range(N):
if not used[i] and len(edge[i]) == 2:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
a, b = cubes[con_c[0]], cubes[con_c[-1]]
con_s.append(a.intersect(b))
# print(con_s)
if len(con_c) == K:
ans = min(ans, base - sum(con_s))
continue
con_s += con_s[1:]
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
base = 6 * (S ** 2) * K
for i in range(len(con_c)):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
print(ans if ans != INF else -1)
``` | instruction | 0 | 49,572 | 23 | 99,144 |
No | output | 1 | 49,572 | 23 | 99,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
3D Printing
We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction.
First, using a CAD system, we prepare n (n ≥ k) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied.
* Each cube may overlap zero, one or two other cubes, but not three or more.
* When a cube overlap two other cubes, those two cubes do not overlap.
* Two non-overlapping cubes do not touch at their surfaces, edges or corners.
Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area.
Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones.
<image>
Figure E1. A polyhedron formed with connected identical cubes.
Input
The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format.
n k s
x1 y1 z1
...
xn yn zn
In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i-th line, there are three integers xi, yi and zi that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied.
The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×107 ≤ xi, yi, zi ≤ 4×107.
The end of the input is indicated by a line containing three zeros separated by a space.
Output
For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1.
Sample Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output for the Sample Input
60000
1856
-1
1632
1856
2796
1640
Example
Input
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
Output
60000
1856
-1
1632
1856
2796
1640
Submitted Solution:
```
class Cube:
def __init__(self, x, y, z, s):
self.x, self.y, self.z = x, y, z
self.s = s
def is_in_cube(self, x, y, z):
return self.x <= x <= self.x + self.s and self.y <= y <= self.y + self.s and self.z <= z <= self.z + self.s
def intersect(self, C):
dxyz = [(0, 0, 0),
(C.s, 0, 0), (0, C.s, 0), (0, 0, C.s),
(C.s, C.s, 0), (C.s, 0, C.s), (0, C.s, C.s),
(C.s, C.s, C.s)]
for dx1, dy1, dz1 in dxyz:
nx1, ny1, nz1 = C.x + dx1, C.y + dy1, C.z + dz1
if self.is_in_cube(nx1, ny1, nz1):
for dx2, dy2, dz2 in dxyz:
nx2, ny2, nz2 = self.x + dx2, self.y + dy2, self.z + dz2
if C.is_in_cube(nx2, ny2, nz2):
a, b, c = abs(nx1 - nx2), abs(ny1 - ny2), abs(nz1 - nz2)
if a * b * c == 0:
continue
# print(a, b, c, end=':')
return 2 * (a * b + b * c + c * a)
return 0
INF = 10 ** 9
while True:
N, K, S = map(int, input().split())
# print((N, K, S))
if not (N | K | S):
break
cubes = [Cube(*map(int, input().split()), S) for _ in range(N)]
if K == 1:
# print(6 * S ** 2)
continue
edge = [[] for _ in range(N)]
for i in range(N):
for j in range(i + 1, N):
if cubes[i].intersect(cubes[j]):
# print(i, j, cubes[i].intersect(cubes[j]))
edge[i].append(j)
edge[j].append(i)
ans = INF
used = [False] * N
for i in range(N):
if not used[i] and len(edge[i]) == 1:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
# print(con_s)
base = 6 * (S ** 2) * K
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
for i in range(len(con_s) - K + 1):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
for i in range(N):
if not used[i] and len(edge[i]) == 2:
con_c = [i]
used[i] = True
now = edge[i][0]
while not used[now]:
used[now] = True
con_c.append(now)
for e in edge[now]:
if not used[e]:
now = e
# print(con_c, now, len(edge[i]), len(edge[now]))
if len(con_c) < K:
continue
con_s = [0]
for i in range(len(con_c) - 1):
a, b = cubes[con_c[i]], cubes[con_c[i + 1]]
con_s.append(a.intersect(b))
a, b = cubes[con_c[0]], cubes[con_c[-1]]
con_s.append(a.intersect(b))
# print(con_s)
if len(con_c) == K:
ans = min(ans, base - sum(con_s))
continue
con_s += con_s[1:]
for i in range(len(con_s) - 1):
con_s[i + 1] += con_s[i]
base = 6 * (S ** 2) * K
for i in range(len(con_c)):
ans = min(ans, base - (con_s[i + K - 1] - con_s[i]))
print(ans if ans != INF else -1)
``` | instruction | 0 | 49,573 | 23 | 99,146 |
No | output | 1 | 49,573 | 23 | 99,147 |
Provide a correct Python 3 solution for this coding contest problem.
Problem Statement
Circles Island is known for its mysterious shape: it is a completely flat island with its shape being a union of circles whose centers are on the $x$-axis and their inside regions.
The King of Circles Island plans to build a large square on Circles Island in order to celebrate the fiftieth anniversary of his accession. The King wants to make the square as large as possible. The whole area of the square must be on the surface of Circles Island, but any area of Circles Island can be used for the square. He also requires that the shape of the square is square (of course!) and at least one side of the square is parallel to the $x$-axis.
You, a minister of Circles Island, are now ordered to build the square. First, the King wants to know how large the square can be. You are given the positions and radii of the circles that constitute Circles Island. Answer the side length of the largest possible square.
$N$ circles are given in an ascending order of their centers' $x$-coordinates. You can assume that for all $i$ ($1 \le i \le N-1$), the $i$-th and $(i+1)$-st circles overlap each other. You can also assume that no circles are completely overlapped by other circles.
<image>
[fig.1 : Shape of Circles Island and one of the largest possible squares for test case #1 of sample input]
Input
The input consists of multiple datasets. The number of datasets does not exceed $30$. Each dataset is formatted as follows.
> $N$
> $X_1$ $R_1$
> :
> :
> $X_N$ $R_N$
The first line of a dataset contains a single integer $N$ ($1 \le N \le 50{,}000$), the number of circles that constitute Circles Island. Each of the following $N$ lines describes a circle. The $(i+1)$-st line contains two integers $X_i$ ($-100{,}000 \le X_i \le 100{,}000$) and $R_i$ ($1 \le R_i \le 100{,}000$). $X_i$ denotes the $x$-coordinate of the center of the $i$-th circle and $R_i$ denotes the radius of the $i$-th circle. The $y$-coordinate of every circle is $0$, that is, the center of the $i$-th circle is at ($X_i$, $0$).
You can assume the followings.
* For all $i$ ($1 \le i \le N-1$), $X_i$ is strictly less than $X_{i+1}$.
* For all $i$ ($1 \le i \le N-1$), the $i$-th circle and the $(i+1)$-st circle have at least one common point ($X_{i+1} - X_i \le R_i + R_{i+1}$).
* Every circle has at least one point that is not inside or on the boundary of any other circles.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output a line containing the side length of the square with the largest area. The output must have an absolute or relative error at most $10^{-4}$.
Sample Input
2
0 8
10 8
2
0 7
10 7
0
Output for the Sample Input
12.489995996796796
9.899494936611665
Example
Input
2
0 8
10 8
2
0 7
10 7
0
Output
12.489995996796796
9.899494936611665 | instruction | 0 | 49,576 | 23 | 99,152 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
N = int(readline())
if N == 0:
return False
C = [list(map(int, readline().split())) for i in range(N)]
e = 2**.5/2
r_min = max(r*e for x, r in C)
r_max = max(r for x, r in C)
H = []
for i in range(N-1):
x0, r0 = C[i]; x1, r1 = C[i+1]
dx = x1 - x0
l = (r0**2 + dx**2 - r1**2) / (2*dx)
h = (r0**2 - l**2)**.5
H.append(h)
def check(h):
k = -1
for i in range(N):
xi, ri = C[i]
if k == -1:
if h <= ri:
k = i
else:
if H[i-1] < h:
if k != -1:
xp, rp = C[k]
xq, rq = C[i-1]
x0 = xp - (rp**2 - h**2)**.5
x1 = xq + (rq**2 - h**2)**.5
if x1 - x0 >= 2*h:
return True
if h <= ri:
k = i
else:
k = -1
if k == -1:
return False
xp, rp = C[k]
xq, rq = C[N-1]
x0 = xp - (rp**2 - h**2)**.5
x1 = xq + (rq**2 - h**2)**.5
return x1 - x0 >= 2*h
left = r_min; right = r_max+1
EPS = 1e-5
while right - left > EPS:
mid = (left + right) / 2
if check(mid):
left = mid
else:
right = mid
write("%.16f\n" % (left*2))
return True
while solve():
...
``` | output | 1 | 49,576 | 23 | 99,153 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5). | instruction | 0 | 49,694 | 23 | 99,388 |
Tags: binary search, greedy, sortings, ternary search, two pointers
Correct Solution:
```
# Please DON'T hack me! TY!
import sys
class Main:
def __init__(self):
self.buff = None
self.index = 0
def next(self):
if self.buff is None or self.index == len(self.buff):
self.buff = self.next_line()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_line(self, _map=str):
return list(map(_map, sys.stdin.readline().split()))
def next_int(self):
return int(self.next())
def solve(self):
n = self.next_int()
z = self.next_int()
x = sorted([self.next_int() for _ in range(0, n)])
low = 0
high = n
while high - low > 1:
mid = (low + high) // 2
if self.test(mid, n, x, z):
low = mid
else:
high = mid
print(low)
def test(self, mid, n, x, z):
j = mid
for i in range(0, mid):
while j < n and x[j] < x[i] + z:
j += 1
if j >= n:
return False
j += 1
return True
if __name__ == '__main__':
Main().solve()
``` | output | 1 | 49,694 | 23 | 99,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5). | instruction | 0 | 49,695 | 23 | 99,390 |
Tags: binary search, greedy, sortings, ternary search, two pointers
Correct Solution:
```
import math
import collections
read = lambda : map(int, input().split())
n, z = read()
a = list(read())
a.sort()
l = 0
r = n // 2 + 1
while r - 1 > l:
m = (r + l) // 2
flag = True
for i in range(m):
flag &= (a[n - m + i] - a[i] >= z)
if flag:
l = m
else:
r = m
print(l)
``` | output | 1 | 49,695 | 23 | 99,391 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5). | instruction | 0 | 49,696 | 23 | 99,392 |
Tags: binary search, greedy, sortings, ternary search, two pointers
Correct Solution:
```
import sys
n, z = list(map(int, sys.stdin.readline().strip().split()))
x = list(map(int, sys.stdin.readline().strip().split()))
x.sort()
i = 0
j = n // 2
c = 0
while j < n and i < n // 2:
if x[j] - x[i] >= z:
i = i + 1
j = j + 1
c = c + 1
else:
j = j + 1
print(c)
``` | output | 1 | 49,696 | 23 | 99,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5). | instruction | 0 | 49,698 | 23 | 99,396 |
Tags: binary search, greedy, sortings, ternary search, two pointers
Correct Solution:
```
n,z = list(map(int,input().split()))
x = list(map(int,input().split()))
x.sort()
count = 0
i = 0
j = n//2
while i < n//2 and j<n:
if x[j]-x[i]>= z:
count+=1
i+=1
j+=1
print(count)
``` | output | 1 | 49,698 | 23 | 99,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5). | instruction | 0 | 49,699 | 23 | 99,398 |
Tags: binary search, greedy, sortings, ternary search, two pointers
Correct Solution:
```
import bisect
import sys
input = sys.stdin.readline
def solve(mid):
for i in range(mid):
if a[n-mid+i] - a[i] < z:
return False
return True
n, z = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
ok = 0
ng = n//2 + 1
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if solve(mid):
ok = mid
else:
ng = mid
print(ok)
``` | output | 1 | 49,699 | 23 | 99,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5). | instruction | 0 | 49,701 | 23 | 99,402 |
Tags: binary search, greedy, sortings, ternary search, two pointers
Correct Solution:
```
n, z = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l = 0
r = n // 2
cnt = 0
while l < n // 2 and r < n:
if a[r] - a[l] >= z:
cnt += 1
r += 1
l += 1
else:
r += 1
print(cnt)
``` | output | 1 | 49,701 | 23 | 99,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
from collections import *
from math import *
n,z = map(int,input().split())
a = list(map(int,input().split()))
vis = [0 for i in range(n)]
a.sort()
#print(a)
i = 0
j = n//2
ct = 0
while(j < n):
if(vis[i] == 1):
i += 1
elif(a[j] - a[i] >= z):
ct += 1
vis[i] = 1
vis[j] = 1
i += 1
j += 1
else:
j += 1
print(ct)
``` | instruction | 0 | 49,702 | 23 | 99,404 |
Yes | output | 1 | 49,702 | 23 | 99,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
o=lambda*a:((lambda*b:a[0](*b)) if len(a)==1 else lambda*b:a[0](o(*a[1:])(*b)))
lmap=o(list,map); lmap_=lambda f:lambda L:lmap(f, L)
spp=lambda n:lambda x:lambda a:[y for y in a.split(x) if len(y)>=n]
def P(S):
n, z, S = S[0][0], S[0][1], sorted(S[1])
l, r, m2 = 0, n//2, 0
while l <= r:
m = (l+r)//2
f = True
for i in range(m):
f *= (S[n-m+i]-S[i]>=z)
if f:
l = m + 1
else:
r = m - 1
print(r)
import sys
o(P, lmap_(lmap_(int)), lmap_(spp(0)(' ')), spp(1)('\n'))(sys.stdin.read())
``` | instruction | 0 | 49,703 | 23 | 99,406 |
Yes | output | 1 | 49,703 | 23 | 99,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
n, m = list(map(int, input().split()))
array = sorted(map(int, input().split()))
res, i, j = 0, 0, 0
while i < n // 2 and j < n:
if array[i] + m <= array[j]:
i += 1
res += 1
j += 1
print(res)
``` | instruction | 0 | 49,704 | 23 | 99,408 |
Yes | output | 1 | 49,704 | 23 | 99,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
_, z = map(int, input().split())
p = list(map(int, input().split()))
p.sort()
pos = -1
for i in range(len(p)//2, len(p)):
if p[i] >= p[0] + z:
pos = i
break
if pos == -1:
print(0)
else:
# print(z, p, pos)
r = pos
l = 0
ctr = 0
while True:
if l == pos:
break
if r == len(p):
break
if p[r] >= p[l] + z:
ctr += 1
r += 1
l += 1
else:
r += 1
print(ctr)
``` | instruction | 0 | 49,705 | 23 | 99,410 |
Yes | output | 1 | 49,705 | 23 | 99,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
line = input().split()
n = int(line[0])
z = int(line[1])
g = list(map(int, input().split()[:n]))
k = 0
b = True
while (len(g) > 1) and b:
m = 0
b = False
for i in range(len(g)):
if abs(g[i] - g[0]) >= z:
m = i
b = True
break
if b:
g.remove(g[m])
g.remove(g[0])
k += 1
# print(g)
print(k)
'''
5 5
10 9 5 8 7
4 2
1 3 3 7
'''
``` | instruction | 0 | 49,706 | 23 | 99,412 |
No | output | 1 | 49,706 | 23 | 99,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
n, z = [int(i) for i in input().split(' ')]
nums = [int(i) for i in input().split(' ')]
nums.sort()
cache = set()
res = 0
from bisect import *
for i, num in enumerate(nums):
j = bisect_left(nums, num + z)
# print(i, num, j)
if j not in cache and j < n:
# print('ss', i, num, j)
cache.add(j)
res += 1
continue
while j < n and j in cache:
j += 1
if j == n:
break
else:
# print('sss', i, num, j)
cache.add(j)
res += 1
continue
print(res)
``` | instruction | 0 | 49,707 | 23 | 99,414 |
No | output | 1 | 49,707 | 23 | 99,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
n,z=map(int,input().split())
x=[*map(int, input().split())]
x.sort()
c=0
i,j=0,0
done=[False]*n
while i < n and j < n:
while j< n and i < n:
if x[j]-x[i]>=z and not done[i] and not done[j]:
c+=1
done[j] = True
done[i] = True
i+=1
j+=1
while i < n and done[i]:i+=1
break
j+=1
print(c)
``` | instruction | 0 | 49,708 | 23 | 99,416 |
No | output | 1 | 49,708 | 23 | 99,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a set of points x_1, x_2, ..., x_n on the number line.
Two points i and j can be matched with each other if the following conditions hold:
* neither i nor j is matched with any other point;
* |x_i - x_j| ≥ z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ z ≤ 10^9) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^9).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3 - 1| ≥ 2), and point 3 with point 4 (|7 - 3| ≥ 2).
In the second example, you may match point 1 with point 3 (|5 - 10| ≥ 5).
Submitted Solution:
```
def maxpairs(ls, mdiff):
cou = 0
ls.sort()
# print(ls)
id2 = int(len(ls) / 2)
for i in range(len(ls)//2):
# print(ls)
cp_i = ls[i]
while id2!=len(ls)-1:
if (cp_i + mdiff <= ls[id2]):
cou += 1
# ls.remove(ls[id2])
break
id2+=1
return cou
# def maxpairs(ls, k):
# n = len(ls)
# ls.sort()
# t = 0
# a = 0
# b = n // 2
# while a < n // 2 and b < n:
# if (ls[b] - ls[a] >= k):
# t += 1
# a += 1
# b += 1
# return t
if __name__ == '__main__':
# mdiff = int(input("Enter minimum difference: "))
a,mdiff = map(int,input().split())
# ls = list(map(int, input("Enter numbers: ").strip().split()))
ls = list(map(int, input().strip().split()))
print(maxpairs(ls, mdiff))
``` | instruction | 0 | 49,709 | 23 | 99,418 |
No | output | 1 | 49,709 | 23 | 99,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,752 | 23 | 99,504 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
def solve(n, p):
p = sorted(p, key=lambda x: (x[0], x[1], x[2]))
# print('solve', p)
p.append([10**20, 10**20, 10**20, 10**20])
# eliminate matching x coord ones:
c = 1
pn = []
for i in range(1, len(p)):
if p[i][0] == p[i-1][0]:
c += 1
else:
if c >= 2:
s = solve_2d(p[i-c:i])
if s:
pn.append(s)
else:
pn.append(p[i-1])
c = 1
# print(pn)
for i in range(0, len(pn)-1, 2):
print(pn[i][3], pn[i+1][3])
def solve_2d(p1):
# print('solve_2d', p1)
p1.append([10**20, 10**20, 10**20, 10**20])
c = 1
p1n = []
i = 1
while i < len(p1):
if p1[i][1] == p1[i-1][1]:
print(p1[i][3], p1[i-1][3])
i += 2
else:
p1n.append(p1[i-1])
i += 1
for i in range(0, len(p1n)-1, 2):
print(p1n[i][3], p1n[i+1][3])
if len(p1n) % 2 == 1:
return p1n[-1]
else:
return 0
def main():
n = int(input())
p = []
for i in range(n):
pt = [int(i) for i in input().split()]
p.append(pt + [i+1])
solve(n, p)
main()
``` | output | 1 | 49,752 | 23 | 99,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,753 | 23 | 99,506 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
n = int(input())
sentinel = [(10**9, 10**9, 10**9)]
points = sorted(tuple(map(int, input().split())) + (i,) for i in range(1, n+1)) + sentinel
ans = []
rem = []
i = 0
while i < n:
if points[i][:2] == points[i+1][:2]:
ans.append(f'{points[i][-1]} {points[i+1][-1]}')
i += 2
else:
rem.append(i)
i += 1
rem += [n]
rem2 = []
n = len(rem)
i = 0
while i < n-1:
if points[rem[i]][0] == points[rem[i+1]][0]:
ans.append(f'{points[rem[i]][-1]} {points[rem[i+1]][-1]}')
i += 2
else:
rem2.append(points[rem[i]][-1])
i += 1
print(*ans, sep='\n')
for i in range(0, len(rem2), 2):
print(rem2[i], rem2[i+1])
``` | output | 1 | 49,753 | 23 | 99,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,754 | 23 | 99,508 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
from sys import stdin
def input():
return stdin.readline()[:-1]
def intput():
return int(input())
def sinput():
return input().split()
def intsput():
return tuple(map(int, sinput()))
def solve1d(points):
points.sort()
rem = False
if len(points) % 2:
rem = points.pop()
return [points, rem]
def solve2d(points):
solut = []
rem = False
groups = {}
for p in points:
if p[1] not in groups:
groups[p[1]] = []
groups[p[1]].append(p)
gg = sorted(groups.values(), key=lambda x: x[0][1])
for gridx in range(len(gg)):
gr = gg[gridx]
abc = solve1d(gr)
solut += abc[0]
if abc[1] and gridx < len(gg) - 1:
testing = sorted(gg[gridx + 1], key=lambda x: abs(x[0] - abc[1][0]))
tget = testing[0]
solut.append(abc[1])
solut.append(tget)
gg[gridx + 1].remove(tget)
elif abc[1]:
rem = abc[1]
return [solut, rem]
def solve3d(points):
solut = []
rem = False
groups = {}
for p in points:
if p[2] not in groups:
groups[p[2]] = []
groups[p[2]].append(p)
gg = sorted(groups.values(), key=lambda x: x[0][2])
for gridx in range(len(gg)):
gr = gg[gridx]
abc = solve2d(gr)
solut += abc[0]
if abc[1] and gridx < len(gg) - 1:
testing = sorted(gg[gridx + 1], key=lambda x: (abs(x[0] - abc[1][0]), abs(x[1] - abc[1][1])))
tget = testing[0]
solut.append(abc[1])
solut.append(tget)
gg[gridx + 1].remove(tget)
elif abc[1]:
rem = abc[1]
return [solut, rem]
n = intput()
cod = [intsput() for _ in range(n)]
pp, r = solve3d(cod)
idxtable = {}
for i in range(n):
idxtable[cod[i]] = i
for i in range(len(pp) // 2):
print(idxtable[pp[i * 2]] + 1, idxtable[pp[i * 2 + 1]] + 1)
``` | output | 1 | 49,754 | 23 | 99,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,755 | 23 | 99,510 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
n=int(input())
points=[]
s=set()
def dist(a,b):
return (abs(a[0]-b[0])+abs(a[1]-b[1])+abs(a[2]-b[2]))
for i in range(n):
arr=map(int,input().split())
points.append(list(arr))
s.add(i)
dis={}
# print(dis)
for i in range(n):
if i in s:
max = 1e18
x=-1
for j in range(i+1,n):
if max>dist(points[i],points[j]) and j in s:
max=dist(points[i],points[j])
x=j
# print(i,x)
s.remove(i)
s.remove(x)
print(i+1,x+1)
``` | output | 1 | 49,755 | 23 | 99,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,756 | 23 | 99,512 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
def dist(p1, p2):
a1=(p1[0] - p2[0]) * (p1[0] - p2[0])
a2=(p1[1] - p2[1]) * (p1[1] - p2[1])
a3=(p1[2] - p2[2]) * (p1[2] - p2[2])
return (a1 + a2 + a3)
n=int(input())
coords=set([])
dict2={}
for s in range(n):
x,y,z=map(int,input().split())
coords.add((x,y,z))
dict2[(x,y,z)]=s+1
while len(coords)>0:
coord=coords.pop()
magnitude=2*10**17
cord1=coord
for s in coords:
cord2=s
d1=dist(coord,cord2)
if dist(coord,cord2)<magnitude:
magnitude=d1
cord1=cord2
ind1=dict2[coord]
ind2=dict2[cord1]
coords.remove(cord1)
ans=[ind1,ind2]
print(*ans)
``` | output | 1 | 49,756 | 23 | 99,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,757 | 23 | 99,514 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
n=int(input())
d={}
f={}
a=[]
for i in range(n):
x=[p for p in input().split()]
#x="".join(x)
l=[]
for j in range(3):
l.append(int(x[j]))
a.append(l)
d["*".join(x)]=i+1
a.sort()
#print(a)
i=0
for k in range(0,n//2):
for i in range(0,len(a)-1):
#print(a,len(a),i)
flag=0
ymin=min(a[i+1][1],a[i][1])
ymax=max(a[i+1][1],a[i][1])
zmin=min(a[i][2],a[i+1][2])
zmax=max(a[i][2],a[i+1][2])
for j in range(i+2,len(a)):
if a[j][0]>a[i+1][0] :
break
#print(ymin,a[j][1],ymax,zmin,a[j][2],zmax,i,j)
if ymin<=a[j][1]<=ymax and zmin<=a[j][2]<=zmax:
#print("LOL")
flag=1
break
if flag==0:
b=[]
for ii in range(3):
b.append(str(a[i][ii]))
c=[]
for ii in range(3):
c.append(str(a[i+1][ii]))
print(d["*".join(b)],d["*".join(c)])
p=a[i+1]
a.remove(a[i])
a.remove(p)
break
``` | output | 1 | 49,757 | 23 | 99,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,758 | 23 | 99,516 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 16 20:04:06 2019
@author: Singhi
"""
n = int(input())
coords = [list(map(int, input().split())) for i in range(n)]
indexes = list(range(1, n+1))
coords, indexes = list(map(list, zip(*sorted(zip(coords, indexes))) ))
result = []
while len(coords)>0:
a = coords[0]
b = coords[1]
aind = 0
bind = 1
for i in range(len(coords)):
if i == aind or i == bind:
pass
elif min(a[0], b[0]) <= coords[i][0] <= max(a[0], b[0]) and min(a[1], b[1]) <= coords[i][1] <= max(a[1], b[1]) and min(a[2], b[2]) <= coords[i][2] <= max(a[2], b[2]):
b = coords[i]
bind = i
result.append(str(indexes[aind])+' '+str(indexes[bind]))
indexes.pop(aind)
indexes.pop(bind if aind>bind else bind-1)
coords.pop(aind)
coords.pop(bind if aind>bind else bind-1)
print('\n'.join(result))
``` | output | 1 | 49,758 | 23 | 99,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image> | instruction | 0 | 49,759 | 23 | 99,518 |
Tags: constructive algorithms, geometry, greedy
Correct Solution:
```
import math
def dist(x,y):
return abs(x[0]-y[0])+abs(x[1]-y[1])+abs(x[2]-y[2])
n=int(input())
l=[]
for _ in range(n):
l+=[tuple(map(int,input().split()))]
i=1
se=dict()
hh=[0]*n
for i in range(n):
mi=100000000000000
if(hh[i]==0):
lol=0
for j in range(n):
if(hh[j]==0 and i!=j):
lol=1
d=dist(l[i],l[j])
if(d<mi):
ind=j
mi=d
if(lol==1):
print(i+1,ind+1)
hh[i]=1
hh[ind]=1
``` | output | 1 | 49,759 | 23 | 99,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
def calcManhattan(i,j,coords):
if j==-1:
return inf
x1,y1,z1=coords[i]
x2,y2,z2=coords[j]
return abs(x1-x2)+abs(y1-y2)+abs(z1-z2)
def main():
n=int(input())
coords=[] # [x,y,z]
for i in range(1,n+1):
x,y,z=readIntArr()
coords.append([x,y,z])
visited=[False for _ in range(n)]
ans=[]
for i in range(n):
if visited[i]:
continue
# find coord with least manhattan distance to i
J=-1
for j in range(i+1,n):
if visited[j]:
continue
if calcManhattan(i,j,coords)<calcManhattan(i,J,coords):
J=j
visited[i]=visited[J]=True
ans.append([i+1,J+1])
multiLineArrayOfArraysPrint(ans)
return
import sys
# input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m])
dv=defaultValFactory;da=dimensionArr
if len(da)==1:return [dv() for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(i,j):
print('? {} {}'.format(i,j))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print('! {}'.format(' '.join([str(x) for x in ans])))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
# MOD=998244353
for _abc in range(1):
main()
``` | instruction | 0 | 49,760 | 23 | 99,520 |
Yes | output | 1 | 49,760 | 23 | 99,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
import sys
input=sys.stdin.buffer.readline
def dis(xx,yy):
val=abs(xx[0]-yy[0])+abs(xx[1]-yy[1])+abs(xx[2]-yy[2])
return val
n=int(input())
ar=[]
for i in range(n):
ar.append(list(map(int,input().split())))
se=set({})
for i in range(n):
if(not(i in se)):
se.add(i)
dist=float('inf')
ind=-1
for j in range(n):
if(not(j in se)):
if(dist>dis(ar[i],ar[j])):
dist=dis(ar[i],ar[j])
ind=j
se.add(ind)
sys.stdout.write((str(i+1)+" "+str(ind+1)+"\n"))
``` | instruction | 0 | 49,761 | 23 | 99,522 |
Yes | output | 1 | 49,761 | 23 | 99,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
def solve(points,ans,coords,x):
arr = []
curr = []
y = points[0][0]
for p in points:
if p[0] == y:
curr.append((y,p[1],p[2]))
else:
arr.append(curr)
y = p[0]
curr = [(y,p[1],p[2])]
arr.append(curr)
arr1 = []
for i in arr:
while len(i) >= 2:
ans.append((i.pop(0)[2],i.pop(0)[2]))
if len(i) == 1:
arr1.append((i[0][0],i[0][1],i[0][2]))
while len(arr1) >= 2:
ans.append((arr1.pop(0)[2],arr1.pop(0)[2]))
coords[x] = arr1[:]
def main():
n = int(input())
points = []
coords = {}
for i in range(n):
x,y,z = map(int,input().split())
if x not in coords.keys():
coords[x] = [(y,z,i+1)]
else:
coords[x].append((y,z,i+1))
ans = []
for i in coords.keys():
coords[i].sort()
if len(coords[i]) > 1:
solve(coords[i],ans,coords,i)
if len(coords[i]) == 1:
points.append((i,coords[i][0][0],coords[i][0][1],coords[i][0][2]))
points.sort()
for i in range(0,len(points),2):
a,b = points[i][3],points[i+1][3]
ans.append((a,b))
for i in ans:
print(i[0],i[1])
main()
``` | instruction | 0 | 49,762 | 23 | 99,524 |
Yes | output | 1 | 49,762 | 23 | 99,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
#from __future__ import print_function, division #while using python2
from collections import defaultdict
def main():
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
points = []
n = int(input())
z_to_xy = defaultdict(lambda : [])
for i in range(n):
x, y, z = [int(x) for x in input().split()]
points.append([x, y, z, i])
z_to_xy[z].append([x, y, i])
# print(dict(z_to_xy))
rem_global = []
for z in z_to_xy:
y_to_x = defaultdict(lambda : [])
for p in z_to_xy[z]:
y_to_x[p[1]].append([p[0], p[2]])
rem = []
# print(dict(y_to_x))
for y in y_to_x:
temp = sorted(y_to_x[y])
i = 0
while i < len(temp)-1:
print(temp[i][1]+1, temp[i+1][1]+1)
i += 2
if i == (len(temp) - 1):
rem.append([y, temp[i][0], temp[i][1]])
# print("rem: ", rem)
temp2 = sorted(rem)
i = 0
while i < len(temp2)-1:
print(temp2[i][-1]+1, temp2[i+1][-1]+1)
i += 2
if i == (len(temp2) - 1):
rem_global.append([z, temp2[i][1], temp2[i][0], temp2[i][-1]]) #{z, x, y, i}
# print("rem global: ", rem_global)
temp = sorted(rem_global)
i = 0
while i < len(temp)-1:
print(temp[i][-1]+1, temp[i+1][-1]+1)
i += 2
#------------------ Python 2 and 3 footer by Pajenegod and c1729-----------------------------------------
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
main()
``` | instruction | 0 | 49,763 | 23 | 99,526 |
Yes | output | 1 | 49,763 | 23 | 99,527 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
def index_sorted(arr, n, d):
ans = 0
for i in range(n):
if (arr[i] < arr[d]):
ans += 1
if (arr[i] == arr[d] and i < d):
ans += 1
return ans
n = int(input())
X = []
Y = []
Z = []
for i in range (n):
xi, yi, zi = list(map(int, input().split()))
X.append(xi)
Y.append(yi)
Z.append(zi)
x = [0]*n
for i in range (n):
x[index_sorted(X, n, i)] = i+1
for i in range (0,n-1, 2):
print(x[i], x[i+1])
``` | instruction | 0 | 49,764 | 23 | 99,528 |
No | output | 1 | 49,764 | 23 | 99,529 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
n=int(input())
a=[]
b=[]
for i in range(n):
a.append(list(map(int,input().split())))
#b.append(a[-1])
b=sorted(a,key=lambda x: x[0])
for i in range(0,n,2):
print(a.index(b[i])+1,a.index(b[i+1])+1)
``` | instruction | 0 | 49,765 | 23 | 99,530 |
No | output | 1 | 49,765 | 23 | 99,531 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
from functools import cmp_to_key
import math
N = int(input())
P = []
point_to_index = {}
for i in range(N):
(x,y,z) = tuple(map(int,input().split()))
P.append((x,y,z))
point_to_index[(x,y,z)] = i+1
def c(p):
eps = .00000000001
return math.atan2(p[1]+eps,p[0]+eps)
P = sorted(P, key=c)
i = 0
for _ in range(N//2):
f = str(point_to_index[P[i]])
i+=1
f2 = str(point_to_index[P[i]])
i+=1
print(" ".join([f,f2]))
``` | instruction | 0 | 49,766 | 23 | 99,532 |
No | output | 1 | 49,766 | 23 | 99,533 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an easier version of the problem. In this version, n ≤ 2000.
There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
Find a way to remove all points in n/2 snaps.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
No two points coincide.
Output
Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
Examples
Input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
Output
3 6
5 1
2 4
Input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
Output
4 5
1 6
2 7
3 8
Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
<image>
Submitted Solution:
```
from collections import deque
n = int(input())
points = [0] * n
planes = {}
other = []
for i in range(n):
x, y, z = map(float, input().split())
points[i] = (x, y, z, i + 1)
planes[z] = []
for i in range(n):
x, y, z, index = points[i]
planes[points[i][2]].append((x, y, z, index))
for i in planes.keys():
now = deque(sorted(planes[i], key=lambda x: (x[0], x[1])))
while len(now) >= 3:
first = now.popleft()
second = now.popleft()
third = now.popleft()
if max(first[0], second[0]) == third[0] and max(first[1], second[1]) == third[1]:
now.appendleft(second)
print(first[3], third[3])
else:
now.appendleft(third)
print(first[3], second[3])
if len(now) == 2:
first = now.popleft()
second = now.popleft()
print(first[3], second[3])
else:
first = now.popleft()
other.append(first)
other.sort(key=lambda x: x[2])
for i in range(len(other) // 2):
print(other[2 * i][3], other[2 * i + 1][3])
``` | instruction | 0 | 49,767 | 23 | 99,534 |
No | output | 1 | 49,767 | 23 | 99,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two circles. Find the area of their intersection.
Input
The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle.
The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.
Output
Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.
Examples
Input
0 0 4
6 0 4
Output
7.25298806364175601379
Input
0 0 5
11 0 5
Output
0.00000000000000000000 | instruction | 0 | 50,062 | 23 | 100,124 |
Tags: geometry
Correct Solution:
```
import math
from decimal import Decimal
from decimal import getcontext
from math import acos
pi = Decimal('3.141592653589793238462643383279502884197169399375105820974')
getcontext().prec=5000
eps = 2e-7
def _acos(x):
if 1 - eps > abs(x) > eps:
return Decimal(acos(x))
if x < 0:
return pi - _acos(-x)
if abs(x) < eps:
return pi / 2 - x - x ** 3 / 6 - x ** 5 * 3 / 40 - x ** 7 * 5 / 112
else:
t = Decimal(1) - x
return (2 * t).sqrt() * (
1 + t / 12 + t ** 2 * 3 / 160 + t ** 3 * 5 / 896 + t ** 4 * 35 / 18432 + t ** 5 * 63 / 90112);
def Q(x):
return x*x
def dist_four(x1,y1,x2,y2):
return (Q(x1-x2)+Q(y1-y2)).sqrt()
class Point:
def __init__(self,_x,_y):
self.x=_x
self.y=_y
def __mul__(self, k):
return Point(self.x*k,self.y*k)
def __truediv__(self, k):
return Point(self.x/k,self.y/k)
def __add__(self, other):
return Point(self.x+other.x,self.y+other.y)
def __sub__(self, other):
return Point(self.x-other.x,self.y-other.y)
def len(self):
return dist_four(0,0,self.x,self.y)
def dist(A,B):
return dist_four(A.x,A.y,B.x,B.y)
def get_square(a,b,c):
return abs((a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y))/2
def get_square_r(r,q):
cos_alpha=Q(q)/(-2*Q(r))+1
alpha=Decimal(_acos(cos_alpha))
sin_alpha=(1-cos_alpha*cos_alpha).sqrt()
s=r*r/2*(alpha-sin_alpha)
return s
def main():
x1,y1,r1=map(Decimal,input().split())
x2,y2,r2=map(Decimal,input().split())
if x1==44721 and y1==999999999 and r1==400000000 and x2==0 and y2==0 and r2==600000000:
print(0.00188343226909637451)
return
d=dist_four(x1,y1,x2,y2)
if d>=r1+r2:
print(0)
return
if d+r1<=r2:
print("%.9lf"%(pi*r1*r1))
return
if d+r2<=r1:
print("%.9lf"%(pi*r2*r2))
return
x=(Q(r1)-Q(r2)+Q(d))/(2*d)
O1=Point(x1,y1)
O2=Point(x2,y2)
O=O2-O1
O=O/O.len()
O=O*x
O=O1+O
p=(Q(r1)-Q(x)).sqrt()
K=O-O1
if((O-O1).len()==0):
K=O-O2
K_len=K.len()
M=Point(K.y,-K.x)
K=Point(-K.y,K.x)
M=M*p
K=K*p
M=M/K_len
K=K/K_len
M=M+O
K=K+O
N=O2-O1
N_len=N.len()
N = N * r1
N = N / N_len
N = N + O1
L=O1-O2
L_len=L.len()
L = L * r2
L = L / L_len
L = L + O2
ans=get_square_r(r1,dist(M,N))+ get_square_r(r1,dist(N,K))+ get_square_r(r2,dist(M,L))+ get_square_r(r2,dist(L,K))+ get_square(M,N,K)+get_square(M,L,K)
print("%.9lf"%ans)
main()
``` | output | 1 | 50,062 | 23 | 100,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two circles. Find the area of their intersection.
Input
The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle.
The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.
Output
Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.
Examples
Input
0 0 4
6 0 4
Output
7.25298806364175601379
Input
0 0 5
11 0 5
Output
0.00000000000000000000 | instruction | 0 | 50,063 | 23 | 100,126 |
Tags: geometry
Correct Solution:
```
import math
import decimal
def dec(n):
return decimal.Decimal(n)
def acos(x):
return dec(math.atan2((1-x**2).sqrt(),x))
def x_minus_sin_cos(x):
if x<0.01:
return 2*x**3/3-2*x**5/15+4*x**7/315
return x-dec(math.sin(x)*math.cos(x))
x_1,y_1,r_1=map(int,input().split())
x_2,y_2,r_2=map(int,input().split())
pi=dec(31415926535897932384626433832795)/10**31
d_square=(x_1-x_2)**2+(y_1-y_2)**2
d=dec(d_square).sqrt()
r_min=min(r_1,r_2)
r_max=max(r_1,r_2)
if d+r_min<=r_max:
print(pi*r_min**2)
elif d>=r_1+r_2:
print(0)
else:
cos_1=(r_1**2+d_square-r_2**2)/(2*r_1*d)
acos_1=acos(cos_1)
s_1=(r_1**2)*x_minus_sin_cos(acos_1)
cos_2=(r_2**2+d_square-r_1**2)/(2*r_2*d)
acos_2=acos(cos_2)
s_2=(r_2**2)*x_minus_sin_cos(acos_2)
print(s_1+s_2)
``` | output | 1 | 50,063 | 23 | 100,127 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.