message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A serious incident happened at Taro's house, which loves steamed buns. One of the three steamed buns offered at the Buddhist altar in the Japanese-style room was gone. When Taro, who was aiming for a snack someday, started an investigation to find the criminal, it turned out that there were many people who entered the Japanese-style room that day. So I decided to ask everyone to give the following form of testimony to find out the order in which these suspects entered the room.
Suspect A's testimony "I entered the room before Suspect B."
One of the suspects (?) Is a calico cat, so I can't testify, but fortunately Taro saw the last time he entered the room.
From these testimonies, Taro decided to guess the order in which he entered the room and use it for the investigation.
For example, if there are 6 suspects and Tama is suspect 2, the following testimony is obtained.
Testimony of Suspect 5 "I entered the room before 2."
Suspect 1's testimony "I entered the room before 4."
Testimony of Suspect 3 "I entered the room before 5."
Suspect 4's testimony "I entered the room before 2."
Suspect 1's testimony "I entered the room before 6."
Suspect 6's testimony "I entered the room before 4."
Testimony of Suspect 3 "I entered the room before 4."
Putting this testimony together, the order in which they entered the room
* 3 → 5 → 1 → 6 → 4 → 2
* 1 → 6 → 3 → 4 → 5 → 2
* 3 → 1 → 6 → 5 → 4 → 2
You can narrow down to several possibilities.
From the testimony of all suspects except Tama (Suspect 2), estimate the order in which they entered the room and write a program that outputs one of the possible orders. However, although some suspects may give multiple testimonies, all testimonies shall be true and consistent.
Input
The input is given in the following format:
m
n
x1 y1
x2 y2
::
xn yn
The first line gives the number of suspects m (m ≤ 20), and the second line gives the number of testimonies n (n ≤ 100). The following n lines are given the contents of the i-th testimony, xi and yi, respectively, on the first line. xi yi represents the testimony that "suspect xi (I) entered before suspect yi".
Output
Print the number of the suspect who entered the room first, the number of the suspect who entered the room second, ..., and the number of the suspect who entered the room second, in order on one line.
Example
Input
6
7
5 2
1 4
3 5
4 2
1 6
6 4
3 4
Output
3
5
1
6
4
2
Submitted Solution:
```
# -*- coding: utf-8 -*-
import sys
import os
import math
m = int(input())
n = int(input())
# Manage people who entered earlier than me in a list
G = [[] for i in range(m)]
for i in range(n):
x, y = map(int, input().split())
x -= 1
y -= 1
G[y].append(x)
printed = [False] * m
while printed.count(False) > 1:
# Delete node with degree 0
for i, node in enumerate(G):
if not printed[i] and len(node) == 0:
print(i+1)
printed[i] = True
# Erase his existence
for node in G:
if i in node:
node.remove(i)
# cat
print(2)
``` | instruction | 0 | 52,949 | 14 | 105,898 |
No | output | 1 | 52,949 | 14 | 105,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A serious incident happened at Taro's house, which loves steamed buns. One of the three steamed buns offered at the Buddhist altar in the Japanese-style room was gone. When Taro, who was aiming for a snack someday, started an investigation to find the criminal, it turned out that there were many people who entered the Japanese-style room that day. So I decided to ask everyone to give the following form of testimony to find out the order in which these suspects entered the room.
Suspect A's testimony "I entered the room before Suspect B."
One of the suspects (?) Is a calico cat, so I can't testify, but fortunately Taro saw the last time he entered the room.
From these testimonies, Taro decided to guess the order in which he entered the room and use it for the investigation.
For example, if there are 6 suspects and Tama is suspect 2, the following testimony is obtained.
Testimony of Suspect 5 "I entered the room before 2."
Suspect 1's testimony "I entered the room before 4."
Testimony of Suspect 3 "I entered the room before 5."
Suspect 4's testimony "I entered the room before 2."
Suspect 1's testimony "I entered the room before 6."
Suspect 6's testimony "I entered the room before 4."
Testimony of Suspect 3 "I entered the room before 4."
Putting this testimony together, the order in which they entered the room
* 3 → 5 → 1 → 6 → 4 → 2
* 1 → 6 → 3 → 4 → 5 → 2
* 3 → 1 → 6 → 5 → 4 → 2
You can narrow down to several possibilities.
From the testimony of all suspects except Tama (Suspect 2), estimate the order in which they entered the room and write a program that outputs one of the possible orders. However, although some suspects may give multiple testimonies, all testimonies shall be true and consistent.
Input
The input is given in the following format:
m
n
x1 y1
x2 y2
::
xn yn
The first line gives the number of suspects m (m ≤ 20), and the second line gives the number of testimonies n (n ≤ 100). The following n lines are given the contents of the i-th testimony, xi and yi, respectively, on the first line. xi yi represents the testimony that "suspect xi (I) entered before suspect yi".
Output
Print the number of the suspect who entered the room first, the number of the suspect who entered the room second, ..., and the number of the suspect who entered the room second, in order on one line.
Example
Input
6
7
5 2
1 4
3 5
4 2
1 6
6 4
3 4
Output
3
5
1
6
4
2
Submitted Solution:
```
# -*- coding: utf-8 -*-
def satisfy_conditions(ans, part_evidences):
for i in range(len(part_evidences)):
x, y = part_evidences[i]
xi, yi = ans.index(x), ans.index(y)
if xi > yi:
return False
return True
if __name__ == '__main__':
m = int(input())
n = int(input())
evidences = []
ans = list(range(1, m + 1))
ans.remove(2)
ans.append(2)
for i in range(n):
x, y = map(int, input().split())
evidences.append((x, y))
for i in range(n): # i???????????\????????????????????°??????
x, y = evidences[i]
xi, yi = ans.index(x), ans.index(y)
if xi < yi:
continue
# xi???yi????????????????????£???????????????yi???xi?????????????????£?????????
# ????????????????????????(x)??????????????????????????????(y)?????????????????????
# y...x??¨????????¶???
# ???????????????xi???????????????y?????\??????
for j in range(xi + 1, n - 1): # j?????\????????´??????????£?
# ??????????????????
ans_copy = ans[:]
ans_copy.insert(j, y)
ans_copy.remove(y)
#print(ans_copy)
# ????????\???????????¶????????????????????????
if satisfy_conditions(ans_copy, evidences[:i]):
print('\n'.join(map(str, ans_copy)))
``` | instruction | 0 | 52,950 | 14 | 105,900 |
No | output | 1 | 52,950 | 14 | 105,901 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,966 | 14 | 105,932 |
"Correct Solution:
```
for e in iter(input,'0 0 0'):
d=[2]*-~sum(map(int,e.split()))
f=[]
for _ in[0]*int(input()):
s,t,u,v=map(int,input().split())
if v:d[s]=d[t]=d[u]=1
else:f+=[(s,t,u)]
for s,t,u in f:
if d[t]*d[u]==1:d[s]=0
if d[u]*d[s]==1:d[t]=0
if d[s]*d[t]==1:d[u]=0
for x in d[1:]:print(x)
``` | output | 1 | 52,966 | 14 | 105,933 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,967 | 14 | 105,934 |
"Correct Solution:
```
while True:
m = sum(map(int, input().split()))
if not m:
break
n = int(input())
fixed = [2] * m
failures = []
for _ in range(n):
i, j, k, r = map(int, input().split())
i, j, k = (x - 1 for x in (i, j, k))
if r:
fixed[i] = fixed[j] = fixed[k] = 1
else:
failures.append((i, j, k))
for i, j, k in failures:
fi, fj, fk = (fixed[x] for x in (i, j, k))
if fi == 1:
if fj == 1:
fixed[k] = 0
elif fk == 1:
fixed[j] = 0
elif fj == 1 and fk == 1:
fixed[i] = 0
print('\n'.join(str(x) for x in fixed))
``` | output | 1 | 52,967 | 14 | 105,935 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,968 | 14 | 105,936 |
"Correct Solution:
```
while 1:
a, b, c = map(int, input().split())
if a == 0:
break
data = []
n = int(input())
for _ in range(n):
data.append(list(map(int, input().split())))
data = sorted(data, key=lambda x: -x[3])
parts = [2] * (a+b+c)
for d in data:
a = d[0]
b = d[1]
c = d[2]
if d[3] == 1:
parts[a-1] = 1
parts[b-1] = 1
parts[c-1] = 1
else:
if parts[a-1] == 1 and parts[b-1] == 1:
parts[c-1] = 0
elif parts[c-1] == 1 and parts[b-1] == 1:
parts[a-1] = 0
elif parts[a-1] == 1 and parts[c-1] == 1:
parts[b-1] = 0
for part in parts:
print(part)
``` | output | 1 | 52,968 | 14 | 105,937 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,969 | 14 | 105,938 |
"Correct Solution:
```
for e in iter(input,'0 0 0'):
d=[2]*-~sum(map(int,e.split()))
f=[]
for _ in[0]*int(input()):
s,t,u,v=map(int,input().split())
if v:d[s]=d[t]=d[u]=1
else:f+=[(s,t,u)]
for s,t,u in f:
if d[t]*d[u]==1:d[s]=0
if d[u]*d[s]==1:d[t]=0
if d[s]*d[t]==1:d[u]=0
print(*d[1:],sep='\n')
``` | output | 1 | 52,969 | 14 | 105,939 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,970 | 14 | 105,940 |
"Correct Solution:
```
# AOJ 0514: Quality Checking
# Python3 2018.6.30 bal4u
while True:
m = sum(list(map(int, input().split())))
if m == 0: break
p = [2]*(m+1)
tbl = []
for _ in range(int(input())):
i, j, k, r = map(int, input().split())
if r: p[i] = p[j] = p[k] = 1;
else: tbl.append((i, j, k))
for t in tbl:
r = 0
for j in range(3):
if p[t[j]] == 1: r += (1<<j)
if r == 6: p[t[0]] = 0
elif r == 5: p[t[1]] = 0
elif r == 3: p[t[2]] = 0
del p[0]
print(*p, sep='\n')
``` | output | 1 | 52,970 | 14 | 105,941 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,971 | 14 | 105,942 |
"Correct Solution:
```
for _ in range(5):
a, b, c = [int(s) for s in input().strip().split(' ')]
if a | b | c == 0:
break
n = int(input().strip())
rlist = [[int(s) for s in input().strip().split(' ')] for _ in range(n)]
clist = [2,]*sum((a,b,c))
for success in filter(lambda r: r[3] == 1, rlist):
for i in success[:3]:
clist[i-1] = 1
for fail in filter(lambda r: r[3] == 0, rlist):
plist = [clist[i-1] for i in fail[:3]]
if sorted(plist) == [1,1,2]:
for i in fail[:3]:
if clist[i-1] == 2:
clist[i-1] = 0
break
[print(r) for r in clist]
``` | output | 1 | 52,971 | 14 | 105,943 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,972 | 14 | 105,944 |
"Correct Solution:
```
while True:
supply,motor,cable=(int(x) for x in input().split())
if supply==0 and motor==0:
break
checknum=int(input())
reslist=[2]*(supply+motor+cable)
stack=[]
for i in range(checknum):
no1,no2,no3,result=(int(x)-1 for x in input().split())
result+=1
if result==1:
reslist[no1], reslist[no2], reslist[no3]=1,1,1
else:
stack.append((no1,no2,no3))
for i in range(len(stack)):
t=stack[i]
no1,no2,no3=t[0],t[1],t[2]
if reslist[no1]==1 and reslist[no2]==1:
reslist[no3]=0
elif reslist[no3]==1 and reslist[no2]==1:
reslist[no1]=0
elif reslist[no3]==1 and reslist[no1]==1:
reslist[no2]=0
for i in range(len(reslist)):
print(reslist[i])
``` | output | 1 | 52,972 | 14 | 105,945 |
Provide a correct Python 3 solution for this coding contest problem.
problem
You are in charge of quality control at a machine manufacturing plant. This machine requires a power supply, a motor, and a cable as parts. The manufacturing plant has a power supply, b motors, and c cables, numbered from 1 to a, a + 1 to a + b, and a + b + 1 to a + b + c, respectively. attached. The trouble is that some parts may be out of order. I want to know which parts are out of order and which parts are normal in the factory.
Therefore, the factory inspected the parts by the following method. Bring the power supply, motor, and cable one by one, connect them, and try to operate them. At this time, if all three parts are normal, it operates correctly and is recognized as "passed". If any of the three parts is out of order, it will not operate properly and will be recognized as "failed". (Since the machines made in the factory are so precise, it does not happen that the broken parts are mixed and operate correctly by accident.)
You will be given a list of test results. Each line in the list of test results shows the numbers of the power supply, motor, and cable used for the test, and whether the test passed or failed.
Given a list of inspection results, all parts are either faulty or normal from the inspection results, some are definitely faulty, some are definitely normal. Create a program to classify parts that are not decided.
input
The input consists of multiple datasets. The format of each data set is as follows. The input ends on a line containing three zeros.
Three integers are written on the first line, separated by blanks, and represent the number of power supplies a, the number of motors b, and the number of cables c in order.
One integer is written on the second line, and the number of tests N included in the list of test results is written.
The next N lines represent a list of test results. On each line, four integers i, j, k, r are written with one blank as a delimiter, and the result of inspection by connecting the power supply i, the motor j, and the cable k is "pass" (r = 1). (When) or "Fail" (when r = 0).
a, b, c, N satisfy 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000.
The number of datasets does not exceed 5.
output
Output in the following format for each data set. The output of each dataset consists of lines a + b + c.
Line i (1 ≤ i ≤ a + b + c):
* If the inspection result shows that the part i is out of order, 0 is output.
* If the inspection result shows that the part i is normal, 1 is output.
* Output 2 if the inspection result does not determine whether part i is defective or normal.
Examples
Input
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
Output
2
1
1
0
1
0
Input
None
Output
None | instruction | 0 | 52,973 | 14 | 105,946 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0514
"""
import sys
from sys import stdin
input = stdin.readline
def main(args):
while True:
a, b, c = map(int, input().split())
if a == 0 and b == 0 and c == 0:
break
N = int(input())
ok_items = set()
ng_items = set()
pendings = []
for _ in range(N):
x, y, z, r = map(int, input().split())
if r == 1:
ok_items.add(x)
ok_items.add(y)
ok_items.add(z)
else:
pendings.append([x, y, z])
for x, y, z in pendings:
p = set([x, y, z])
und = p - ok_items
if len(und) == 1:
for i in und:
ng_items.add(i)
for i in range(1, (a+b+c+1)):
if i in ok_items:
print(1)
elif i in ng_items:
print(0)
else:
print(2)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 52,973 | 14 | 105,947 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). | instruction | 0 | 53,423 | 14 | 106,846 |
Tags: dfs and similar, dsu, greedy
Correct Solution:
```
import sys
from collections import deque
# from collections import Counter
# from math import sqrt
# from math import log
# from math import ceil
# from bisect import bisect_left, bisect_right
# alpha=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
mod=10**9+7
# mod=998244353
# def BinarySearch(a,x):
# i=bisect_left(a,x)
# if(i!=len(a) and a[i]==x):
# return i
# else:
# return -1
# def sieve(n):
# prime=[True for i in range(n+1)]
# p=2
# while(p*p<=n):
# if (prime[p]==True):
# for i in range(p*p,n+1,p):
# prime[i]=False
# p+=1
# prime[0]=False
# prime[1]=False
# s=set()
# for i in range(len(prime)):
# if(prime[i]):
# s.add(i)
# return s
# def gcd(a, b):
# if(a==0):
# return b
# return gcd(b%a,a)
fast_reader=sys.stdin.readline
fast_writer=sys.stdout.write
def input():
return fast_reader().strip()
def print(*argv):
fast_writer(' '.join((str(i)) for i in argv))
fast_writer('\n')
#____________________________________________________________________________________________________________________________________
for _ in range(1):
n,m=map(int, input().split())
d={}
for i in range(n):
d[i]=[]
for i in range(m):
x,y=map(int, input().split())
x=x-1
y=y-1
d[x].append(y)
d[y].append(x)
visited=set()
ans=1
#print(d)
for i in range(n):
if(i in visited):
continue
#print(i,visited)
c=0
q=deque()
q.append(i)
while(len(q)!=0):
t=q.popleft()
if(t in visited):
continue
else:
visited.add(t)
c+=1
for j in d[t]:
q.append(j)
#print(i,visited)
ans*=max(1,2**(c-1))
print(ans)
``` | output | 1 | 53,423 | 14 | 106,847 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). | instruction | 0 | 53,424 | 14 | 106,848 |
Tags: dfs and similar, dsu, greedy
Correct Solution:
```
from collections import defaultdict, deque
def main():
n, m = map(int, input().split())
graph = defaultdict(list)
for _ in range(m):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
nodes2visit = set(range(1, n+1))
ans = 0
while nodes2visit:
start_node = next(iter(nodes2visit))
cnt = 0
queue = deque([start_node])
while queue:
node = queue.pop()
cnt += 1
nodes2visit.discard(node)
for nxt in graph[node]:
if nxt in nodes2visit:
queue.appendleft(nxt)
nodes2visit.discard(nxt)
ans += cnt - 1
print(2**ans)
if __name__ == "__main__":
main()
``` | output | 1 | 53,424 | 14 | 106,849 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). | instruction | 0 | 53,425 | 14 | 106,850 |
Tags: dfs and similar, dsu, greedy
Correct Solution:
```
from collections import defaultdict
def dfs(node,graph,vis):
global count
vis[node] = True
count+=1
for i in graph[node]:
if not vis[i]:
dfs(i,graph,vis)
n,m = map(int,input().split())
if m==0:
print(1)
else:
graph = defaultdict(list)
for i in range(m):
a,b = map(int,input().split())
graph[a].append(b)
graph[b].append(a)
vis = [False]*(n+1)
ans = 0
ans = 0
for i in range(1,n+1):
global count
count = 0
if not vis[i]:
dfs(i,graph,vis)
ans+=1
print(pow(2,n-ans))
``` | output | 1 | 53,425 | 14 | 106,851 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). | instruction | 0 | 53,426 | 14 | 106,852 |
Tags: dfs and similar, dsu, greedy
Correct Solution:
```
import sys, os, io
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math,datetime,functools,itertools,operator,bisect,fractions,statistics
from collections import deque,defaultdict,OrderedDict,Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify ,_heapify_max,_heappop_max,nsmallest,nlargest
#sys.setrecursionlimit(111111)
INF=99999999999999999999999999999999
def outIn(x):
print(x, flush=True)
return input()
def main():
mod=1000000007
# InverseofNumber(mod)
# InverseofFactorial(mod)
# factorial(mod)
starttime=datetime.datetime.now()
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
###CODE
tc = 1
for _ in range(tc):
n,m=ria()
danger=1
graph=[[] for i in range(n+1)]
vis=[False]*(n+1)
for i in range(m):
a,b=ria()
graph[a].append(b)
graph[b].append(a)
def dfs(i):
vis[i]=True
for j in graph[i]:
if not vis[j]:
dfs(j)
cc=0
for i in range(1,n+1):
if not vis[i]:
cc+=1
dfs(i)
wi(2**(n-cc))
#<--Solving Area Ends
endtime=datetime.datetime.now()
time=(endtime-starttime).total_seconds()*1000
if(os.path.exists('input.txt')):
print("Time:",time,"ms")
class FastReader(io.IOBase):
newlines = 0
def __init__(self, fd, chunk_size=1024 * 8):
self._fd = fd
self._chunk_size = chunk_size
self.buffer = io.BytesIO()
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
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, size=-1):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
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()
class FastWriter(io.IOBase):
def __init__(self, fd):
self._fd = fd
self.buffer = io.BytesIO()
self.write = self.buffer.write
def flush(self):
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class FastStdin(io.IOBase):
def __init__(self, fd=0):
self.buffer = FastReader(fd)
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
class FastStdout(io.IOBase):
def __init__(self, fd=1):
self.buffer = FastWriter(fd)
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.flush = self.buffer.flush
if __name__ == '__main__':
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()
``` | output | 1 | 53,426 | 14 | 106,853 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). | instruction | 0 | 53,427 | 14 | 106,854 |
Tags: dfs and similar, dsu, greedy
Correct Solution:
```
def abhi(node , y):
visited[node]=1
comp[y].append(node)
for j in adj[node]:
if visited[j]==0:
abhi(j,y)
n,m = map(int,input().split())
adj=[[] for j in range(n+1)]
for j in range(m):
a,b = map(int,input().split())
adj[a].append(b)
adj[b].append(a)
visited=[0]*(n+1)
comp =[ [] for j in range(n+1)]
u=1
for j in range(1,n+1):
if visited[j]==0:
abhi(j,u)
u+=1
ans=1
for j in comp:
if len(j)>=2:
ans*= (2**(len(j)-1))
print(ans)
``` | output | 1 | 53,427 | 14 | 106,855 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). | instruction | 0 | 53,429 | 14 | 106,858 |
Tags: dfs and similar, dsu, greedy
Correct Solution:
```
n, m = map(int, input().split())
p = list(range(n + 1))
s = [{i} for i in p]
for i in range(m):
x, y = map(int, input().split())
x, y = p[x], p[y]
if x != y:
if len(s[x]) < len(s[y]):
for k in s[x]:
p[k] = y
s[y].add(k)
s[x].clear()
else:
for k in s[y]:
p[k] = x
s[x].add(k)
s[y].clear()
q = 0
for p in s:
if p: q += len(p) - 1
print(1 << q)
``` | output | 1 | 53,429 | 14 | 106,859 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,470 | 14 | 106,940 |
Tags: sortings
Correct Solution:
```
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
cnt=0
ans=[]
for i in range(n):
if a[i]==b[i]:
continue
#print(b)
j=i
while j<n and b[j]!=a[i]:
j+=1
while j>i:
b[j],b[j-1]=b[j-1],b[j]
ans.append([j,j+1])
j-=1
# print(b)
print(len(ans))
for i in range(len(ans)):
print(ans[i][0],ans[i][1])
'''
1 2 3 2
3 2 1 2
ind=2
i=0
2 1
1 0
1 3 2 2
1 2 3 2
'''
``` | output | 1 | 53,470 | 14 | 106,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,471 | 14 | 106,942 |
Tags: sortings
Correct Solution:
```
import sys
n = int(sys.stdin.readline ())
a= [int (x) for x in sys.stdin.readline ().split ()]
assert len(a) == n
b = [int (x) for x in sys.stdin.readline ().split ()]
assert len(b) == n
ans = []
for i in range (n):
j = i;
while b[j] != a[i] :
j += 1
while j > i:
ans += [(j, j + 1)]
b[j - 1], b[j] = b[j], b[j - 1]
j -= 1
print (len(ans))
for p, q in ans:
print (p, + q)
``` | output | 1 | 53,471 | 14 | 106,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,472 | 14 | 106,944 |
Tags: sortings
Correct Solution:
```
import sys
n= int(input())
arr2 = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
ans = []
ans2 = []
for i in range(n-1):
# range(n) also work but outer loop will repeat one time more than needed.
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
ans.append((j,j+1))
for i in range(n-1):
# range(n) also work but outer loop will repeat one time more than needed.
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr2[j] > arr2[j+1] :
arr2[j], arr2[j+1] = arr2[j+1], arr2[j]
ans2.append((j,j+1))
print(len(ans) +len(ans2))
for i in range(len(ans)):
print(ans[i][0]+1,ans[i][1]+1)
for i in range(len(ans2)-1,-1,-1):
print(ans2[i][0]+1,ans2[i][1]+1)
``` | output | 1 | 53,472 | 14 | 106,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,473 | 14 | 106,946 |
Tags: sortings
Correct Solution:
```
n = int(input())
*a, = map(int, input().split())
*b, = map(int, input().split())
ans, s, k = [], 0, 0
while b:
x = b.index(a[k])
b.remove(a[k])
ans.append((x + k + 1, k + 1))
s += x
k += 1
print(s)
for i in ans:
for j in range(i[0], i[1], -1):
print(j - 1, j)
``` | output | 1 | 53,473 | 14 | 106,947 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,474 | 14 | 106,948 |
Tags: sortings
Correct Solution:
```
number = int(input())
origin = list(input().split())
array = list(input().split())
positions = {}
pre = []
last = []
result = 0
for i in range(number):
if origin[i] == array[i]:
continue
else:
j = i + 1
index = len(pre)
for j in range(i+1, number):
pre.insert(index,j+1)
last.insert(index,j)
if array[j] == origin[i]:
break
array[i+1: j+1] = array[i:j]
result += j - i
print(result)
for i in range(len(pre)):
print(str(last[i])+" "+str(pre[i]))
``` | output | 1 | 53,474 | 14 | 106,949 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,475 | 14 | 106,950 |
Tags: sortings
Correct Solution:
```
n = int(input())
target = list(map(int,input().split()))
got = list(map(int,input().split()))
swaps = []
for i in range(n-1,-1,-1):
for j in range(i+1):
if got[j] == target[i] and j != i:
swaps.append((j+1,(j+1)+1))
got[j],got[j+1] = got[j+1],got[j]
print(len(swaps))
for i in swaps:
print(*i)
``` | output | 1 | 53,475 | 14 | 106,951 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,476 | 14 | 106,952 |
Tags: sortings
Correct Solution:
```
n = int(input())
original = list(input().split())
given = list(input().split())
pre = []
last = []
result = 0
for i in range(n):
if original[i] == given[i]:
continue
else:
j = i + 1
index = len(pre)
for j in range(i + 1, n):
pre.insert(index, j + 1)
last.insert(index, j)
if given[j] == original[i]:
break
given[i + 1: j + 1] = given[i:j]
result += j - i
print(result)
for i in range(len(pre)):
print(str(last[i]) + " " + str(pre[i]))
``` | output | 1 | 53,476 | 14 | 106,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0 | instruction | 0 | 53,477 | 14 | 106,954 |
Tags: sortings
Correct Solution:
```
def main():
N = int(input())
order = list(input().split())
initial = list(input().split())
X = []
Y = []
ans = 0
for i in range(N):
if order[i] == initial[i]:
continue
else:
j = i + 1
length = len(X)
for j in range(i+1, N):
X.insert(length, j+1)
Y.insert(length, j)
if initial[j] == order[i]:
break
initial[i+1:j+1] = initial[i:j]
ans += j-i
print(ans)
for i in range(len(X)):
print(str(Y[i])+" " + str(X[i]))
if __name__ == '__main__':
main()
``` | output | 1 | 53,477 | 14 | 106,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,519 | 14 | 107,038 |
Tags: brute force, data structures, implementation
Correct Solution:
```
import sys
def main():
tmp = [int(x) for x in sys.stdin.readline().split()]
n = tmp[0]
q = tmp[1]
readApp = [0] * n
unreadApp = [0] * n
unreadTot = 0
genApp = [0] * n
firstFlag = 0
genOps = []
for i in range(q):
tmp = [int(x) for x in sys.stdin.readline().split()]
ty, x = tmp[0], tmp[1]
if ty == 1:
x -= 1
unreadTot += 1
unreadApp[x] += 1
genApp[x] += 1
genOps.append((x, genApp[x]))
elif ty == 2:
x -= 1
unreadTot -= unreadApp[x]
readApp[x] += unreadApp[x]
unreadApp[x] = 0
else:
for j in range(firstFlag, x):
app, index = genOps[j][0], genOps[j][1]
if readApp[app] < index:
readApp[app] += 1
unreadApp[app] -= 1
unreadTot -= 1
if x > firstFlag:
firstFlag = x
print(unreadTot)
main()
``` | output | 1 | 53,519 | 14 | 107,039 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,520 | 14 | 107,040 |
Tags: brute force, data structures, implementation
Correct Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import sys,collections
n,q=map(int,input().split())
M=collections.defaultdict(collections.deque)
Q=collections.deque()
L=[]
s=n=m=0
for _ in range(q):
y,x=map(int,input().split())
if 2>y:
s+=1
Q.append(x)
M[x].append(n)
n+=1
elif 3>y:
y=M.get(x)
if y:
s-=len(y)
del M[x]
else:
while x>m:
z=Q.popleft()
y=M.get(z)
if y and y[0]<x:
s-=1
y.popleft()
if not y:del M[z]
m+=1
L.append(s)
sys.stdout.write('\n'.join(map(str,L)))
``` | output | 1 | 53,520 | 14 | 107,041 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,521 | 14 | 107,042 |
Tags: brute force, data structures, implementation
Correct Solution:
```
def main():
n, q = map(int, input().split())
vol, tot, l, res = [0] * (n + 1), [0] * (n + 1), [], []
z = m = 0
for _ in range(q):
t, x = map(int, input().split())
if t == 1:
l.append(x)
tot[x] += 1
vol[x] += 1
z += 1
elif t == 2:
z -= vol[x]
vol[x] = 0
else:
if m < x:
r, m = range(m, x), x
for i in r:
x = l[i]
tot[x] -= 1
if vol[x] > tot[x]:
vol[x] -= 1
z -= 1
res.append(z)
print('\n'.join(map(str, res)))
if __name__ == '__main__':
main()
``` | output | 1 | 53,521 | 14 | 107,043 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,522 | 14 | 107,044 |
Tags: brute force, data structures, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
n, q = map(int, input().split())
l = [[] for _ in range(n)]
is_read = []
is_read_idx = 0
ans = 0
prev_t = 0
for _ in range(q):
ty, v = map(int, input().split())
if ty==1:
l[v-1].append(is_read_idx)
is_read_idx += 1
is_read.append(False)
ans += 1
elif ty==2:
for idx in l[v-1]:
if not is_read[idx]:
is_read[idx] = True
ans -= 1
l[v-1] = []
else:
if v>prev_t:
for idx in range(prev_t, v):
if not is_read[idx]:
is_read[idx] = True
ans -= 1
prev_t = v
print(ans)
``` | output | 1 | 53,522 | 14 | 107,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,523 | 14 | 107,046 |
Tags: brute force, data structures, implementation
Correct Solution:
```
from collections import defaultdict, deque
q = deque()
idx, total = 0, 0
vis = defaultdict(deque)
not_valid = set()
max_x = -1
ans = []
N, Q = map(int, input().split())
for _ in range(Q):
t, x = map(int, input().split())
if t == 1:
q.append((idx, x))
vis[x].append(idx)
idx += 1
total += 1
elif t == 2:
while vis[x]:
# All element in vis[x] should be popped out from q, and It will do it later.
# The index are collected in the not_valid set.
i = vis[x].pop()
not_valid.add(i)
# If the element is expired, then skip it.
if i <= max_x - 1:
continue
total -= 1
else:
# Check the element should be expired or popped out.
while q and (q[0][0] <= x - 1 or q[0][0] in not_valid):
j, y = q.popleft()
if j not in not_valid:
total -= 1
vis[y].popleft()
max_x = max(max_x, x)
ans.append(str(total))
# Trick: print the answer at the end to avoid TLE
# by keep commnicating with kernel space.
print("\n".join(ans))
``` | output | 1 | 53,523 | 14 | 107,047 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,524 | 14 | 107,048 |
Tags: brute force, data structures, implementation
Correct Solution:
```
from sys import stdin
from collections import defaultdict
test = stdin.readlines()
n, q = map(int, test[0].split())
timeline = []
unread = defaultdict(lambda: [0, 0]) # 0 unread app notifications from t = 0
ans = 0
start = 0
out = []
for i in range(1, q + 1):
category, val = map(int, test[i].split())
if category == 1:
timeline.append(val)
unread[val][0] += 1
ans += 1
elif category == 2:
ans -= unread[val][0]
unread[val] = [0, len(timeline)]
else:
for j in range(start, val):
app = timeline[j]
if j >= unread[app][1]:
unread[app][0] -= 1
ans -= 1
start = max(start, val)
out.append(ans)
print('\n'.join(map(str,out)))
``` | output | 1 | 53,524 | 14 | 107,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,525 | 14 | 107,050 |
Tags: brute force, data structures, implementation
Correct Solution:
```
import collections
n,q=map(int,input().split())
dq=collections.deque()
l=n*[0]
p=list(l)
ans=0
t=0
ansl=[]
while(q):
a,b=map(int,input().split())
if a==1:
dq.append(b-1)
l[b-1],p[b-1]=l[b-1]+1,p[b-1]+1
ans=ans+1
elif a==2:
ans=ans-l[b-1]
l[b-1]=0
else:
while b>t:
t=t+1
j=dq.popleft()
p[j]=p[j]-1
if l[j]>p[j]:
l[j]=l[j]-1
ans=ans-1
ansl.append(str(ans))
q=q-1
print("\n".join(ansl))
``` | output | 1 | 53,525 | 14 | 107,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications). | instruction | 0 | 53,526 | 14 | 107,052 |
Tags: brute force, data structures, implementation
Correct Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import collections
n,q=map(int,input().split())
Q=collections.deque()
A=n*[0]
B=A[:]
L=[]
s=n=0
for _ in range(q):
y,x=map(int,input().split())
if 2>y:
x-=1
Q.append(x)
B[x]+=1
A[x]+=1
s+=1
elif 3>y:
x-=1
s-=A[x]
A[x]=0
else:
while x>n:
n+=1
y=Q.popleft()
B[y]-=1
if(B[y]<A[y]):
A[y]-=1
s-=1
L.append(str(s))
print('\n'.join(L))
``` | output | 1 | 53,526 | 14 | 107,053 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
import random, math, sys
from copy import deepcopy as dc
from bisect import bisect_left, bisect_right
from collections import Counter, OrderedDict
input = sys.stdin.readline
# Function to call the actual solution
def solution(n ,quer):
total_cell = [0] * n
max_2 = [0] * n
max_3 = 0
message = []
total = 0
time = 0
ans = ""
for i in quer:
t, x_t = i[0], i[1]
if t == 1:
total_cell[x_t-1] += 1
message.append(x_t)
total += 1
elif t == 2:
total -= total_cell[x_t-1]
total_cell[x_t-1] = 0
max_2[x_t-1] = len(message)
else:
for j in range(max_3, x_t):
if j >= max_2[message[j]-1]:
total_cell[message[j]-1] -= 1
total -= 1
max_3 = max(max_3, x_t)
ans += (str(total)+'\n')
# print(total_cell)
# print(total)
print(ans)
# Function to take input
def input_test():
# for _ in range(int(input())):
# n = int(input())
n, q = map(int, input().strip().split(" "))
quer = []
for i in range(q):
li = list(map(int, input().strip().split(" ")))
li.append(True)
quer.append(li)
# a, b, c = map(int, input().strip().split(" "))
# li = list(map(int, input().strip().split(" ")))
out = solution(n, quer)
# for i in out:
# print(i)
# Function to test my code
def test():
pass
input_test()
# test()
``` | instruction | 0 | 53,527 | 14 | 107,054 |
Yes | output | 1 | 53,527 | 14 | 107,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
"""
#If FastIO not needed, used this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq
import math, string
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
MOD = 10**9+7
"""
We need to maintain a set of unread notifications from which we can remove the first max(ti), and also the first xi for each application
[3,4,4,1,1,1,2,2,1,3,3,2,4,4,1,2,3,2,4,4]
N applications. At any given time the number of unread notifications is the sum of the number of unread notifications for each application, which lends itself to a Fenwick Tree
But how do we maintain the order?
As we delete the first N elements, we can also subtract one from the rolling sum and from the element total.
As we clear an element of type X, we can subtract that many from the rolling total
"""
def solve():
N, Q = getInts()
order = []
cur_total = 0
cur_del = -1
els = [0]*(N+1)
del_up_to = [-1]*(N+1)
for q in range(Q):
T, X = getInts()
if T == 1:
order.append(X)
els[X] += 1
cur_total += 1
print(cur_total)
elif T == 2:
cur_total -= els[X]
els[X] = 0
del_up_to[X] = max(len(order)-1,del_up_to[X])
print(cur_total)
elif T == 3:
if X-1 > cur_del:
for i in range(cur_del+1,X):
if del_up_to[order[i]] < i:
cur_total -= 1
del_up_to[order[i]] = i
els[order[i]] -= 1
#print(order)
#print(els)
#print(del_up_to)
cur_del = X-1
print(cur_total)
return
#for _ in range(getInt()):
solve()
``` | instruction | 0 | 53,528 | 14 | 107,056 |
Yes | output | 1 | 53,528 | 14 | 107,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
n,q = map(int,input().split())
xi = [[0]*(n+1) for i in range(2)]
noti = []
ans=""
num = 0
num2 = 0
num3 = 0
while q > 0:
typ,xt = map(int,input().split())
if typ == 1:
xi[0][xt] += 1
noti += [xt]
num += 1
num2 += 1
elif typ == 3:
for i in range(num3,xt):
if i+1 > xi[1][noti[i]]:
xi[0][noti[i]] -= 1
num -= 1
num3 = max(num3,xt)
else:
num -= xi[0][xt]
xi[0][xt] = 0
xi[1][xt] = num2
ans+=(str(num)+'\n')
q -= 1
print(ans)
``` | instruction | 0 | 53,529 | 14 | 107,058 |
Yes | output | 1 | 53,529 | 14 | 107,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
import sys
from collections import defaultdict
def main():
n,q = list(map(int, sys.stdin.readline().split()))
al = 0
d = defaultdict(list)
sts = defaultdict(int)
cur = 0
msgs = []
ans = []
start = 0
for i in range(q):
t,x = list(map(int, sys.stdin.readline().split()))
if t == 1:
al+=1
d[x].append(al)
msgs.append(x)
cur+=1
elif t==2:
size = len(d[x])
if size >0:
cur -= size - sts[x]
d[x] = list()
sts[x] = 0
else :
if start<x:
for i in range(start,x):
v = msgs[i]
dd = d[v]
size = len(dd)
cur_st = sts[v]
while size>cur_st and dd[cur_st]<=x:
cur_st+=1
cur-=1
sts[v] = cur_st
start = x
ans.append(cur)
print("\n".join(map(str,ans)))
main()
``` | instruction | 0 | 53,530 | 14 | 107,060 |
Yes | output | 1 | 53,530 | 14 | 107,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
[n, q] = list(map(int, input().split(" ")))
cell = []
max_3 = 0
for i in range(n):
cell.append([])
for i in range(q):
[t, x_t] = list(map(int, input().split(" ")))
if t == 1:
for j in range(n):
if j == x_t - 1:
cell[j].append(i)
elif t == 2:
cell[x_t-1] = []
elif t == 3:
if x_t > max_3:
max_3 = x_t
# print(cell)
print(sum(map(lambda l: sum(map(lambda x: x >= max_3, l)),cell)))
``` | instruction | 0 | 53,531 | 14 | 107,062 |
No | output | 1 | 53,531 | 14 | 107,063 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
from collections import deque
n, q = map(int, input().split())
A = [0] * n
for i in range(n):
A[i] = deque()
alls = deque()
visited = [0] * q
answer = 0
lens = 0
for i in range(q):
a, b = map(int, input().split())
if a == 1:
A[b - 1].append(i)
alls.append([i, b - 1])
lens += 1
answer += 1
print(answer)
elif a == 2:
while A[b - 1]:
per = A[b - 1].pop()
visited[per] = 1
answer -= 1
print(answer)
else:
while True:
if b != 0 and lens > 0:
per = alls.pop()
lens -= 1
if visited[per[0]] == 0:
b -= 1
A[per[1]].pop()
answer -= 1
else:
break
print(answer)
``` | instruction | 0 | 53,532 | 14 | 107,064 |
No | output | 1 | 53,532 | 14 | 107,065 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
n,q=input().split()
n,q=int(n),int(q)
sum,check,f=0,0,[]
l,app=[0]*(n+1),[]
while q>0:
a,b=input().split()
a,b,i=int(a),int(b),0
if a==1:
l[b]+=1
sum+=1
app.append(b)
elif a==2:
if l[b]!=0:
sum-=l[b]
check+=l[b]
l[b]=0
app[app.index(b)]=-1
else:
if check>=b:
pass
else:
sum-=(b-check)
check+=(b-check)
while i<b:
if app[i]!=-1:
# if l[app[i]]!=0:
l[app[i]]=0
app[i]=-1
i+=1
# print(' '*20,sum,' '*20,l)
# print(app)
q-=1
f.append(sum)
for i in f:
print(i)
``` | instruction | 0 | 53,533 | 14 | 107,066 |
No | output | 1 | 53,533 | 14 | 107,067 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
n, q=list(map(int, input().split(" ")))
unreadForApp=[0]*(n+1)
queries=[0]
latestType2ForApp=[-1]*(n+1)
start=1
for i in range(q):
qType, app=list(map(int, input().split(" ")))
if qType==1:
# print("type 1")
unreadForApp[app]+=1
queries.append(app)
if qType==2:
# print("type 2")
unreadForApp[app]=0
latestType2ForApp[app]=i
if qType==3:
# print(" type 3")
end=app # first 'x' notifs == app
# print(end)
# print(queries)
for i in range(start, end+1):
# print(i, end=" ")
time=i
app=queries[i]
if latestType2ForApp[app]<time:
unreadForApp[app]-=1
# print(" ")
start=end+1
# print(unreadForApp)
print(sum(unreadForApp))
'''
10 15
2 2
1 10
1 1
2 6
1 2
1 4
1 7
2 1
1 1
3 3
1 9
1 6
1 8
1 10
3 8
'''
``` | instruction | 0 | 53,534 | 14 | 107,068 |
No | output | 1 | 53,534 | 14 | 107,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,587 | 14 | 107,174 |
Tags: brute force, greedy, sortings
Correct Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
values = sorted(list(map(int, stdin.readline().split())))
ans = float('inf')
for i in range(2 * n):
for j in range(i + 1, 2 * n):
res = []
for z in range(2 * n):
if z == i or z == j:
continue
res.append(values[z])
cnt = 0
for q in range(0, len(res), 2):
cnt += res[q + 1] - res[q]
ans = min(ans, cnt)
stdout.write(str(ans))
``` | output | 1 | 53,587 | 14 | 107,175 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,588 | 14 | 107,176 |
Tags: brute force, greedy, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
an = 10 ** 9
for i in range(2 * n):
for j in range(i + 1, 2 * n):
if i == j:
continue
b = a[:]
del b[j], b[i]
c = 0
for k in range(0, 2 * n - 2, 2):
c += abs(b[k] - b[k + 1])
an = min(an, c)
#print(an, b)
print(an)
``` | output | 1 | 53,588 | 14 | 107,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,589 | 14 | 107,178 |
Tags: brute force, greedy, sortings
Correct Solution:
```
n = int(input())
w = list(map(int, input().split()))
w.sort()
p = 2 * n
num = []
for i in range(p):
for j in range(i + 1, p):
l = []
dif = 0
for k in range(p):
if k != i and k != j:
l.append(w[k])
for x in range(1,n):
dif += l[2 * x - 1] - l[2 * x - 2]
num.append(dif)
print(min(num))
``` | output | 1 | 53,589 | 14 | 107,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,590 | 14 | 107,180 |
Tags: brute force, greedy, sortings
Correct Solution:
```
n = int(input())
w = list(map(int, input().split()))
w.sort()
d = []
# Решаем перебором, выбрасываем из списка 2 каякеров и
# находим сумму разностей весов у оставшихся, отсортированных по возрастанию
for i in range(0, 2 * n):
for j in range(i + 1, 2 * n):
if i == j:
continue
tw = list(w)
tw.pop(i)
tw.pop(j - 1)
td = 0
for k in range(0, len(tw), 2):
td += tw[k + 1] - tw[k]
d.append(td)
print(min(d))
``` | output | 1 | 53,590 | 14 | 107,181 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,591 | 14 | 107,182 |
Tags: brute force, greedy, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
best = 100000
for i in range(0, 2*n-1):
for j in range(i+1, 2*n):
diff = 0
p = -1
for k in range(0,2*n):
if k != i and k != j:
diff += (p*a[k])
p = -p
best = min(best, diff)
print(best)
``` | output | 1 | 53,591 | 14 | 107,183 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,592 | 14 | 107,184 |
Tags: brute force, greedy, sortings
Correct Solution:
```
n = int(input())
weights = list(map(int, input().split()))
pairs=[]
for i in range(2*n):
for j in range(2*n):
if i != j:
pairs.append([i, j])
total_diff = 10000000000
for pair in pairs:
diff = 0
new_weights = weights.copy()
new_weights.pop(pair[0])
new_weights.pop(pair[1]-1)
new_weights.sort()
for i in range(n-1):
diff += new_weights[2 * i + 1] - new_weights[2 * i]
total_diff = min(total_diff, diff)
print(total_diff)
``` | output | 1 | 53,592 | 14 | 107,185 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,593 | 14 | 107,186 |
Tags: brute force, greedy, sortings
Correct Solution:
```
from itertools import combinations, chain
from math import inf
def take(s):
return sum([abs(s[i] - s[i+1]) for i in range(0, 2*n-2, 2)])
n = int(input())
wi = sorted(map(int, input().split()))
minimum = inf
for lonely_couple in combinations(range(2*n), 2):
sample = list(chain(wi[:lonely_couple[0]], wi[lonely_couple[0]+1:lonely_couple[1]], wi[lonely_couple[1]+1:]))
minimum = min(minimum, take(sample))
print(minimum)
"""
4
1 3 4 6 3 4 1000 200
"""
``` | output | 1 | 53,593 | 14 | 107,187 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5 | instruction | 0 | 53,594 | 14 | 107,188 |
Tags: brute force, greedy, sortings
Correct Solution:
```
def main():
n = int(input())
n *= 2
a = list(map(int, input().split()))
a.sort()
#print(*a)
o = 919478437678234
for i in range(0, n - 1):
for j in range(i + 1, n):
#print(i, j)
oo = 0
s = 0
while s < n - 1:
if i == s or j == s:
s += 1
elif s + 1 == i or s + 1 == j:
#print(s, s + 2, a[s + 2], a[s])
oo += a[s + 2] - a[s]
s += 3
else:
#print(s, s + 1, a[s + 1], a[s])
oo += a[s + 1] - a[s]
s += 2
#print(i, j, o)
o = min(o, oo)
print(o)
main()
``` | output | 1 | 53,594 | 14 | 107,189 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5
Submitted Solution:
```
n=int(input())
w=list(map(int,input().split()))
ans=[]
w.sort()
ans=float('inf')
n*=2
for i in range(n-1):
for j in range(i+1,n):
cur=0
a=list(w[:i]+w[i+1:j]+w[j+1:])
for k in range(0,len(a)-1,2):
cur+=a[k+1]-a[k]
ans=min(ans,cur)
print(ans)
``` | instruction | 0 | 53,595 | 14 | 107,190 |
Yes | output | 1 | 53,595 | 14 | 107,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5
Submitted Solution:
```
def solution(x):
x.sort()
min_ans = x[-1] - x[0]
for L in range(len(x) - 1):
for R in range(L + 1, len(x)):
cur_ans = 0
indexes = list(range(L)) + list(range(L + 1, R)) + list(range(R + 1, len(x)))
for k in range(1, len(indexes), 2):
cur_ans += x[indexes[k]] - x[indexes[k - 1]]
if cur_ans < min_ans:
min_ans = cur_ans
return min_ans
N = int(input())
x = list(map(int, input().split()))
print(solution(x))
``` | instruction | 0 | 53,596 | 14 | 107,192 |
Yes | output | 1 | 53,596 | 14 | 107,193 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.