message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,303 | 12 | 8,606 |
Tags: math, number theory, probabilities
Correct Solution:
```
import random
# Sieve
sieve_primes = []
PRIME_LIMIT = int(1e6)
sieve = [False for i in range(0,PRIME_LIMIT)]
for i in range(2,PRIME_LIMIT):
if not sieve[i]:
sieve_primes.append(i)
for j in range(i*i, PRIME_LIMIT, i):
sieve[j]=True
# Input
n = int(input())
arr=list(map(int, input().split()))
# Construct search space Primes
primes = set()
def addPrime(num):
for sieve_prime in sieve_primes:
if num%sieve_prime == 0:
primes.add(sieve_prime)
while num%sieve_prime == 0:
num//=sieve_prime
if num>1:
primes.add(num)
# (Could use probability calculations here to reduce search space)
random.shuffle(arr)
for num in arr[:8]:
if num > 1:
addPrime(num)
addPrime(num+1)
if num > 2:
addPrime(num-1)
# Find answer
ans = n
# Function to find answer based on input prime
def findAns(prime):
global ans
curr_ans = 0
for num in arr:
if num<prime:
curr_ans += prime-num
else:
curr_ans += min(num%prime, prime - num%prime)
if curr_ans >= ans:
return
ans = min(curr_ans, ans)
for prime in primes:
findAns(prime)
if ans == 0: break
print(ans)
``` | output | 1 | 4,303 | 12 | 8,607 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,304 | 12 | 8,608 |
Tags: math, number theory, probabilities
Correct Solution:
```
import sys
input = sys.stdin.readline
from math import sqrt
import random
n=int(input())
A=list(map(int,input().split()))
random.shuffle(A)
MOD={2,3,5}
for t in A[:31]:
for x in [t-1,t,t+1]:
if x<=5:
continue
L=int(sqrt(x))
for i in range(2,L+2):
while x%i==0:
MOD.add(i)
x=x//i
if x==1:
break
if x!=1:
MOD.add(x)
ANS=1<<29
for m in MOD:
SCORE=0
for a in A:
if a<=m:
SCORE+=m-a
else:
SCORE+=min(a%m,(-a)%m)
if SCORE>=ANS:
break
else:
ANS=SCORE
print(ANS)
``` | output | 1 | 4,304 | 12 | 8,609 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,305 | 12 | 8,610 |
Tags: math, number theory, probabilities
Correct Solution:
```
import random
n = int(input())
a = [int(x) for x in input().split()]
can = set()
ls = []
for i in range(10):
idx = random.randint(0, n - 1)
for c in range(-1, 2):
if a[idx] + c > 0: ls.append(a[idx] + c)
maxn = 1000001
prime = []
vis = [True] * maxn
for i in range(2, maxn):
if vis[i] is not True: continue
if i * i > maxn: break
for j in range(i * i, maxn, i):
vis[j] = False
for i in range(2, maxn):
if vis[i]: prime.append(i)
for val in ls:
i = 2
for i in prime:
if i * i > val: break
div = False
while val % i == 0:
val //= i
div = True
if div: can.add(i)
i = i + 1
if val is not 1: can.add(val)
ans = n
for i in can:
total = 0
for j in a:
if j >= i: total += min(j % i, i - (j % i))
else: total += i - j
ans = min(ans, total)
print(ans)
``` | output | 1 | 4,305 | 12 | 8,611 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,306 | 12 | 8,612 |
Tags: math, number theory, probabilities
Correct Solution:
```
import sys
input = sys.stdin.readline
from math import sqrt
n=int(input())
A=list(map(int,input().split()))
SA=sorted(set(A))
MOD={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973}
for x in SA[:70]:
L=int(sqrt(x))
for i in range(2,L+2):
while x%i==0:
MOD.add(i)
x=x//i
if x!=1:
MOD.add(x)
ANS=1<<29
for m in MOD:
SCORE=0
for a in A:
if a<=m:
SCORE+=m-a
else:
SCORE+=min(a%m,(-a)%m)
if SCORE>=ANS:
break
else:
ANS=SCORE
print(ANS)
``` | output | 1 | 4,306 | 12 | 8,613 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,307 | 12 | 8,614 |
Tags: math, number theory, probabilities
Correct Solution:
```
import random
n = int(input())
a = list(map(int, input().split()))
limit = min(8, n)
iterations = [x for x in range(n)]
random.shuffle(iterations)
random.shuffle(iterations)
iterations = iterations[:limit]
def factorization(x):
primes = []
i = 2
while i * i <= x:
if x % i == 0:
primes.append(i)
while x % i == 0: x //= i
i = i + 1
if x > 1: primes.append(x)
return primes
def solve_with_fixed_gcd(arr, gcd):
result = 0
for x in arr:
if x < gcd: result += (gcd - x)
else:
remainder = x % gcd
result += min(remainder, gcd - remainder)
return result
answer = float("inf")
prime_list = set()
for index in iterations:
for x in range(-1, 2):
tmp = factorization(a[index]-x)
for z in tmp: prime_list.add(z)
for prime in prime_list:
answer = min(answer, solve_with_fixed_gcd(a, prime))
if answer == 0: break
print(answer)
``` | output | 1 | 4,307 | 12 | 8,615 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,308 | 12 | 8,616 |
Tags: math, number theory, probabilities
Correct Solution:
```
import random
n = int(input())
a = list(map(int, input().split()))
limit = min(8, n)
iterations = [x for x in range(n)]
random.shuffle(iterations)
iterations = iterations[:limit]
def factorization(x):
primes = []
i = 2
while i * i <= x:
if x % i == 0:
primes.append(i)
while x % i == 0: x //= i
i = i + 1
if x > 1: primes.append(x)
return primes
def solve_with_fixed_gcd(arr, gcd):
result = 0
for x in arr:
if x < gcd: result += (gcd - x)
else:
remainder = x % gcd
result += min(remainder, gcd - remainder)
return result
answer = float("inf")
prime_list = set()
for index in iterations:
for x in range(-1, 2):
tmp = factorization(a[index]-x)
for z in tmp: prime_list.add(z)
for prime in prime_list:
answer = min(answer, solve_with_fixed_gcd(a, prime))
if answer == 0: break
print(answer)
``` | output | 1 | 4,308 | 12 | 8,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,309 | 12 | 8,618 |
Tags: math, number theory, probabilities
Correct Solution:
```
import random
# Sieve
sieve_primes = []
PRIME_LIMIT = int(1e6)
sieve = [False for i in range(0,PRIME_LIMIT)]
for i in range(2,PRIME_LIMIT):
if not sieve[i]:
sieve_primes.append(i)
for j in range(i*i, PRIME_LIMIT, i):
sieve[j]=True
# Input
n = int(input())
arr=list(map(int, input().split()))
# Construct search space Primes
primes = set()
def addPrime(num):
for sieve_prime in sieve_primes:
if num%sieve_prime == 0:
primes.add(sieve_prime)
while num%sieve_prime == 0:
num//=sieve_prime
if num>1:
primes.add(num)
# (Could use probability calculations here to reduce search space)
random.shuffle(arr)
arr_set=list(set(arr))
for num in arr_set[:8]:
if num > 1:
addPrime(num)
addPrime(num+1)
if num > 2:
addPrime(num-1)
# Find answer
ans = n
# Function to find answer based on input prime
def findAns(prime):
global ans
curr_ans = 0
for num in arr:
if num<prime:
curr_ans += prime-num
else:
curr_ans += min(num%prime, prime - num%prime)
if curr_ans >= ans:
return
ans = min(curr_ans, ans)
for prime in primes:
findAns(prime)
if ans == 0: break
print(ans)
``` | output | 1 | 4,309 | 12 | 8,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good. | instruction | 0 | 4,310 | 12 | 8,620 |
Tags: math, number theory, probabilities
Correct Solution:
```
import random
n = int(input())
a = list(map(int, input().split()))
primes = set()
for x in random.choices(a, k=min(n, 30)):
for y in (x-1, x, x+1):
d = 2
while d * d <= y:
while y % d == 0:
primes.add(d)
y //= d
d += 1 + (d & 1)
if y > 1:
primes.add(y)
ans = float('inf')
for p in primes:
cand = 0
for x in a:
if x < p:
cand += p - x
else:
r = x % p
cand += min(r, p-r)
if cand >= ans:
break
else:
ans = cand
print(ans)
``` | output | 1 | 4,310 | 12 | 8,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
import random
# Sieve
sieve_primes = []
PRIME_LIMIT = int(1e6)
sieve = [False for i in range(0,PRIME_LIMIT)]
for i in range(2,PRIME_LIMIT):
if not sieve[i]:
sieve_primes.append(i)
for j in range(i*i, PRIME_LIMIT, i):
sieve[j]=True
# Input
n = int(input())
arr=list(map(int, input().split()))
# Construct search space Primes
primes = set()
def addPrime(num):
for sieve_prime in sieve_primes:
if num%sieve_prime == 0:
primes.add(sieve_prime)
while num%sieve_prime == 0:
num//=sieve_prime
if num>1:
primes.add(num)
# (Could use probability calculations here to reduce search space)
arr_set = random.sample(arr, len(arr))
arr_set=list(set(arr_set))
for num in arr_set[:8]:
if num > 1:
addPrime(num)
addPrime(num+1)
if num > 2:
addPrime(num-1)
# Find answer
ans = n
# Function to find answer based on input prime
def findAns(prime):
global ans
curr_ans = 0
for num in arr:
if num<prime:
curr_ans += prime-num
else:
curr_ans += min(num%prime, prime - num%prime)
if curr_ans >= ans:
return
ans = min(curr_ans, ans)
for prime in primes:
findAns(prime)
if ans == 0: break
print(ans)
``` | instruction | 0 | 4,311 | 12 | 8,622 |
Yes | output | 1 | 4,311 | 12 | 8,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
import random
n = int(input())
best = n
l = list(map(int, input().split()))
candidates = set()
candidates.add(2)
def gcd(x, y):
while(y):
x, y = y, x % y
return x
def factAdd(n):
for c in candidates:
while n % c == 0:
n //= c
test = 3
while test * test <= n:
while n % test == 0:
candidates.add(test)
n //= test
test += 2
if n > 1:
candidates.add(n)
for i in range(100):
a = random.randint(0, n - 1)
b = random.randint(0, n - 1)
diff = [-1, 0, 1]
for d1 in diff:
a1 = l[a] + d1
if a1:
for d2 in diff:
a2 = l[b] + d2
if a2:
factAdd(gcd(a1, a2))
for cand in candidates:
count = 0
for v in l:
if v <= cand:
count += (cand - v)
else:
v2 = v % cand
count += min(v2, cand - v2)
if count < best:
best = count
print(best)
``` | instruction | 0 | 4,312 | 12 | 8,624 |
Yes | output | 1 | 4,312 | 12 | 8,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
# TESTING EDITORIAL SUBMISSION FOR TEST CASE 105
import random
n = int(input())
a = list(map(int, input().split()))
limit = min(8, n)
iterations = [x for x in range(n)]
random.shuffle(iterations)
iterations = iterations[:limit]
def factorization(x):
primes = []
i = 2
while i * i <= x:
if x % i == 0:
primes.append(i)
while x % i == 0: x //= i
i = i + 1
if x > 1: primes.append(x)
return primes
def solve_with_fixed_gcd(arr, gcd):
result = 0
for x in arr:
if x < gcd: result += (gcd - x)
else:
remainder = x % gcd
result += min(remainder, gcd - remainder)
return result
answer = float("inf")
prime_list = set()
for index in iterations:
for x in range(-1, 2):
tmp = factorization(a[index]-x)
for z in tmp: prime_list.add(z)
for prime in prime_list:
answer = min(answer, solve_with_fixed_gcd(a, prime))
if answer == 0: break
print(answer)
``` | instruction | 0 | 4,313 | 12 | 8,626 |
Yes | output | 1 | 4,313 | 12 | 8,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
import random
n = int(input())
a = list(map(int, input().split()))
def add(x):
d = 2
while d * d <= x:
if x % d == 0:
primes.add(d)
while x % d == 0:
x //= d
d += 1
if x > 1:
primes.add(x)
ans = float('inf')
primes = {2}
for x in random.choices(a, k=min(n, 8)):
for dx in range(-1, 2):
add(x + dx)
for p in primes:
cand = 0
for x in a:
if x < p:
cand += p - x
else:
r = x % p
cand += min(r, p-r)
ans = min(ans, cand)
if ans==0: break
print(ans)
``` | instruction | 0 | 4,314 | 12 | 8,628 |
Yes | output | 1 | 4,314 | 12 | 8,629 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from math import sqrt
import random
n=int(input())
A=list(map(int,input().split()))
SA=list(set(A))
random.shuffle(SA)
MOD={2,3,5}
LEN=len(SA)
for t in SA[:33]:
for x in [t-1,t,t+1]:
if x<=5:
continue
L=int(sqrt(x))
for i in range(2,L+2):
while x%i==0:
MOD.add(i)
x=x//i
if x==1:
break
if x!=1:
MOD.add(x)
ANS=1<<29
for m in MOD:
SCORE=0
for a in A:
if a<=m:
SCORE+=m-a
else:
SCORE+=min(a%m,(-a)%m)
if SCORE>=ANS:
break
else:
ANS=SCORE
print(ANS)
``` | instruction | 0 | 4,315 | 12 | 8,630 |
No | output | 1 | 4,315 | 12 | 8,631 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
x = int(input())
lista = list(map(int,input().split()))
total = 0
for a in range(x):
if(lista[a]%2 == 1):
total += 1
print(total)
``` | instruction | 0 | 4,316 | 12 | 8,632 |
No | output | 1 | 4,316 | 12 | 8,633 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
from random import randint
n = int(input())
a = [int(x) for x in input().split()]
def solve(x):
can = []
for i in range(2, x):
if i * i > x: break
if x % i is 0:
can.append(i)
while x % i is 0:
x = int(x / i)
if x is not 1:
can.append(x)
res = n
for i in can:
ans = 0
for j in a:
p = j % i
q = i - p
if p < q and j >= i: ans = ans + p
else: ans = ans + q
if res > ans:
res = ans
return res
ans = n
for _ in range(15):
x = randint(0, n)
for c in range(-1, 2):
if x + c > 0:
opt = solve(x + c)
if opt < ans:
ans = opt
print(ans)
``` | instruction | 0 | 4,317 | 12 | 8,634 |
No | output | 1 | 4,317 | 12 | 8,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem:
You have an array a consisting of n positive integers. An operation consists of choosing an element and either adding 1 to it or subtracting 1 from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not 1. Find the minimum number of operations needed to make the array good.
Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment!
Input
The first line contains an integer n (2 β€ n β€ 2 β
10^5) β the number of elements in the array.
The second line contains n integers a_1, a_2, ..., a_n. (1 β€ a_i β€ 10^{12}) β the elements of the array.
Output
Print a single integer β the minimum number of operations required to make the array good.
Examples
Input
3
6 2 4
Output
0
Input
5
9 8 7 3 1
Output
4
Note
In the first example, the first array is already good, since the greatest common divisor of all the elements is 2.
In the second example, we may apply the following operations:
1. Add 1 to the second element, making it equal to 9.
2. Subtract 1 from the third element, making it equal to 6.
3. Add 1 to the fifth element, making it equal to 2.
4. Add 1 to the fifth element again, making it equal to 3.
The greatest common divisor of all elements will then be equal to 3, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
Submitted Solution:
```
def gcd(a, b):
if b > a:
return gcd(b, a)
if a % b == 0:
return b
return gcd(b, a%b)
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
primes = [True]*int(max(a)**0.5)
primes[0] = False
primes[1] = False
for i in range(2, len(primes)):
if primes[i]:
ind2 = i*i
while ind2 < len(primes):
primes[ind2] = False
ind2 += i
primes = [i for i,e in enumerate(primes) if e]
minimum = len(a) + 5
for pr in primes:
cnt = 0
for v in a:
cnt += min(v % pr, pr - (v % pr))
if cnt > minimum:
break
if cnt < minimum:
minimum = cnt
seta = set(a)
distinct = len(set(a))
if distinct < 15:
for v in seta:
cnt = 0
for value in a:
cnt += abs(v-value)
if cnt < minimum:
minimum = cnt
print(minimum)
``` | instruction | 0 | 4,318 | 12 | 8,636 |
No | output | 1 | 4,318 | 12 | 8,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 β€ n β€ 2Β·105). The second line contains elements of the sequence a1, a2, ..., an (1 β€ ai β€ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,336 | 12 | 8,672 |
Tags: brute force, implementation
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
s=s/n
if s%1!=0:
print("0")
exit()
else:
l=[]
g=0
for i in range(n):
if a[i]==s:
g=g+1
l.append(i+1)
print(g)
for i in range(g):
print(l[i],end=" ")
``` | output | 1 | 4,336 | 12 | 8,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 β€ n β€ 2Β·105). The second line contains elements of the sequence a1, a2, ..., an (1 β€ ai β€ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,337 | 12 | 8,674 |
Tags: brute force, implementation
Correct Solution:
```
n = int(input())
A = list(map(int, input().split()))
B = []
srar = 0
for i in range(n):
srar += A[i]
srar = srar/n
for i in range(n):
if ((srar * n) - A[i]) / (n-1) == A[i]:
B.append(i+1)
print(len(B))
for i in range(len(B)):
print(B[i], end = ' ')
``` | output | 1 | 4,337 | 12 | 8,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 β€ n β€ 2Β·105). The second line contains elements of the sequence a1, a2, ..., an (1 β€ ai β€ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,338 | 12 | 8,676 |
Tags: brute force, implementation
Correct Solution:
```
N = int(input())
ar = [int(x) for x in input().split()]
ans = []
s = sum(ar)
for i, j in enumerate(ar):
if j * N == s:
ans.append(str(i + 1))
print(len(ans))
print(' '.join(ans))
``` | output | 1 | 4,338 | 12 | 8,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 β€ n β€ 2Β·105). The second line contains elements of the sequence a1, a2, ..., an (1 β€ ai β€ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,339 | 12 | 8,678 |
Tags: brute force, implementation
Correct Solution:
```
#!/usr/bin/env python3
n = int(input())
arr = [int(i) for i in input().split(' ')]
s = 0
result = []
for item in arr:
s += item
s = s/n
count = 0
pos = 1
for item in arr:
if item == s:
count += 1
result.append(str(pos))
pos += 1
print(count)
print(' '.join(result))
``` | output | 1 | 4,339 | 12 | 8,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).
Input
The first line contains the integer n (2 β€ n β€ 2Β·105). The second line contains elements of the sequence a1, a2, ..., an (1 β€ ai β€ 1000). All the elements are positive integers.
Output
Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n.
If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.
Examples
Input
5
1 2 3 4 5
Output
1
3
Input
4
50 50 50 50
Output
4
1 2 3 4 | instruction | 0 | 4,340 | 12 | 8,680 |
Tags: brute force, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
count = 0
ind = []
total = sum(a)
for i in range(n):
if a[i]==(total-a[i])/(n-1):
count+=1
ind.append(i+1)
print(count)
print(*ind)
``` | output | 1 | 4,340 | 12 | 8,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An n Γ n square matrix is special, if:
* it is binary, that is, each cell contains either a 0, or a 1;
* the number of ones in each row and column equals 2.
You are given n and the first m rows of the matrix. Print the number of special n Γ n matrices, such that the first m rows coincide with the given ones.
As the required value can be rather large, print the remainder after dividing the value by the given number mod.
Input
The first line of the input contains three integers n, m, mod (2 β€ n β€ 500, 0 β€ m β€ n, 2 β€ mod β€ 109). Then m lines follow, each of them contains n characters β the first rows of the required special matrices. Each of these lines contains exactly two characters '1', the rest characters are '0'. Each column of the given m Γ n table contains at most two numbers one.
Output
Print the remainder after dividing the required value by number mod.
Examples
Input
3 1 1000
011
Output
2
Input
4 4 100500
0110
1010
0101
1001
Output
1
Note
For the first test the required matrices are:
011
101
110
011
110
101
In the second test the required matrix is already fully given, so the answer is 1. | instruction | 0 | 4,588 | 12 | 9,176 |
Tags: combinatorics, dp
Correct Solution:
```
n, m, mod = [int(x) for x in input().split()]
from functools import lru_cache
import sys
sys.setrecursionlimit(100000000)
@lru_cache(maxsize=None)
def rec(twos, ones):
global mod
if twos == 0 and ones == 0:
return 1
if twos == 1 and ones == 0:
return 0
else:
count = 0
# we can pick two from ones if there are at least two:
if (ones >= 2):
a = ones*(ones-1)//2
b = rec(twos, ones-2)
count += (a*b) % mod
if (ones >= 1 and twos >= 1):
a = ones*twos
b = rec(twos-1, ones)
count += (a*b) % mod
if (twos >= 2):
a = twos*(twos-1)//2
b = rec(twos-2, ones+2)
count += (a*b) % mod
return count % mod
# we read the beginning matrix and calculate the starting position
matrix = []
for i in range(0, m):
matrix.append([int(x) for x in input()])
twocount = 0
onecount = 0
for c in range(0, n):
# for each column count the number of ones in it
count = 0
for r in range(0, m):
# r is the row, c is the column
if (matrix[r][c] == 1):
count += 1
if count == 2:
twocount += 1
elif count == 1:
onecount += 1
ones = onecount
twos = n - onecount - twocount
print(rec(twos, ones))
``` | output | 1 | 4,588 | 12 | 9,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment.
The teacher gave Andrew an array of n numbers a1, ..., an. After that he asked Andrew for each k from 1 to n - 1 to build a k-ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent.
Andrew looked up on the Wikipedia that a k-ary heap is a rooted tree with vertices in elements of the array. If the elements of the array are indexed from 1 to n, then the children of element v are elements with indices k(v - 1) + 2, ..., kv + 1 (if some of these elements lie outside the borders of the array, the corresponding children are absent). In any k-ary heap every element except for the first one has exactly one parent; for the element 1 the parent is absent (this element is the root of the heap). Denote p(v) as the number of the parent of the element with the number v. Let's say that for a non-root element v the property of the heap is violated if av < ap(v).
Help Andrew cope with the task!
Input
The first line contains a single integer n (2 β€ n β€ 2Β·105).
The second line contains n space-separated integers a1, ..., an ( - 109 β€ ai β€ 109).
Output
in a single line print n - 1 integers, separate the consecutive numbers with a single space β the number of elements for which the property of the k-ary heap is violated, for k = 1, 2, ..., n - 1.
Examples
Input
5
1 5 4 3 2
Output
3 2 1 0
Input
6
2 2 2 2 2 2
Output
0 0 0 0 0
Note
Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red.
<image> <image> <image> <image>
In the second sample all elements are equal, so the property holds for all pairs. | instruction | 0 | 4,598 | 12 | 9,196 |
Tags: brute force, data structures, math, sortings
Correct Solution:
```
import sys
def myargsort(a):
b = list(zip(a, range(0, len(a))))
b.sort()
r = [pr[1] for pr in b]
return r
fin = sys.stdin
n = int(fin.readline())
a = [int(number) for number in fin.readline().split()]
p = myargsort(a)
p.reverse()
j = 0
aib = [0] * (n + 1)
def ultb(x):
return -(x ^ (-x)) // 2
def add(p, a, aib, n):
while p <= n:
aib[p] += a
p += ultb(p)
def suma(p, aib):
r = 0
while p > 0:
r += aib[p]
p -= ultb(p)
return r
for i in range(0, n):
add(i + 1, 1, aib, n)
r = [0] * (n + 1)
for i in range(0, n):
if i > 0 and a[i - 1] > a[i]:
r[1] += 1
while j < n and a[p[j]] == a[p[i]]:
add(p[j] + 1, -1, aib, n)
j += 1
k = 2
while k < n and p[i] * k + 1 < n:
dr = min(n, p[i] * k + k + 1)
st = p[i] * k + 2
r[k] += suma(dr, aib) - suma(st - 1, aib)
k += 1
print(*r[1:n])
``` | output | 1 | 4,598 | 12 | 9,197 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,651 | 12 | 9,302 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
from heapq import heappush, heappop
from sys import stdin
heap = []
L = []
n, *l = stdin.read().splitlines()
for string in l:
array = string.split()
if array[0] == 'insert':
heappush(heap, int(array[1]))
elif array[0] == 'getMin':
key = int(array[1])
while heap and heap[0] < key:
heappop(heap)
L.append('removeMin')
if not heap or heap[0] > key:
heappush(heap, key)
L.append('insert ' + str(key))
else:
if heap:
heappop(heap)
else:
L.append('insert 0')
L.append(string)
print(len(L))
print('\n'.join(L))
``` | output | 1 | 4,651 | 12 | 9,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,652 | 12 | 9,304 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
from heapq import *
numLines = int(input())
heap = []
output_ops = []
for _ in range(numLines):
opString = input()
operation = opString.split()
if operation[0] == "insert":
heappush(heap, int(operation[1]))
if operation[0] == "removeMin":
if not heap:
heappush(heap, 1)
output_ops.append("insert 1")
heappop(heap)
elif operation[0] == "getMin":
currMin = int(operation[1])
while heap and heap[0] < currMin:
heappop(heap)
output_ops.append("removeMin")
if not heap or heap[0] > currMin:
heappush(heap, currMin)
output_ops.append("insert " + str(currMin))
output_ops.append(opString)
print(len(output_ops))
for op in output_ops:
print(op)
``` | output | 1 | 4,652 | 12 | 9,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,653 | 12 | 9,306 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
import heapq
n = int(input())
m = n
operations = []
heap = []
for i in range(n):
s = input()
a = s.split()
if a[0] == 'insert':
heapq.heappush(heap, int(a[1]))
elif a[0] == 'removeMin':
if heap:
heapq.heappop(heap)
else:
m += 1
operations.append('insert 1')
elif a[0] == 'getMin':
b = int(a[1])
while heap:
if heap[0] < b:
operations.append('removeMin')
m += 1
heapq.heappop(heap)
else:
break
else:
operations.append('insert {}'.format(b))
m += 1
heapq.heappush(heap, b)
if heap[0] > b:
operations.append('insert {}'.format(b))
m += 1
heapq.heappush(heap, b)
operations.append(s)
print(m)
print('\n'.join(operations))
``` | output | 1 | 4,653 | 12 | 9,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,654 | 12 | 9,308 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
import heapq
m = int(input())
heap = []
ans = []
for i in range(m):
a = input()
if a == 'removeMin':
if heap == []:
heapq.heappush(heap, 1)
heapq.heappop(heap)
ans.append('insert 1')
ans.append('removeMin')
else:
heapq.heappop(heap)
ans.append('removeMin')
else:
a, x = a.split()
x = int(x)
if a == 'insert':
heapq.heappush(heap, x)
ans.append('insert ' + str(x))
else:
while len(heap) > 0 and heap[0] < x:
heapq.heappop(heap)
ans.append('removeMin')
if len(heap) > 0 and heap[0] == x:
ans.append('getMin ' + str(x))
else:
heapq.heappush(heap, x)
ans.append('insert ' + str(x))
ans.append('getMin ' + str(x))
print(len(ans))
print('\n'.join(ans))
``` | output | 1 | 4,654 | 12 | 9,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,655 | 12 | 9,310 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
import heapq
n = int(input())
commands = []
arr = []
for i in range(n):
s = input()
tmp = s.split(' ')
if tmp[0] == "insert":
heapq.heappush(arr, int(tmp[1]))
elif tmp[0] == "removeMin":
if not len(arr):
commands.append("insert 1")
else:
heapq.heappop(arr)
else:
x = int(tmp[1])
while len(arr) and arr[0] < x:
commands.append("removeMin")
heapq.heappop(arr)
if not len(arr) or arr[0] != x:
commands.append("insert " + tmp[1])
heapq.heappush(arr, x)
commands.append(s)
print(len(commands))
print('\n'.join(commands))
``` | output | 1 | 4,655 | 12 | 9,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,656 | 12 | 9,312 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
from sys import stdin
from bisect import bisect_left
n = int(stdin.readline())
heap = []
res = []
for i in range(n):
string = stdin.readline()
oper = string.split()
if oper[0][0] == 'i':
key = int(oper[1])
pos = bisect_left(heap, key)
heap[pos:pos] = [key]
elif oper[0][0] == 'g':
key = int(oper[1])
num = bisect_left(heap, key)
heap[:num] = []
res.extend(['removeMin\n'] * num)
if not heap or heap[0] != key:
heap[0:0] = [key]
res.append('insert ' + oper[1] + '\n')
else:
if heap:
heap[:1] = []
else:
res.append('insert 0\n')
res.append(string)
print(len(res))
print(''.join(res), end='')
``` | output | 1 | 4,656 | 12 | 9,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,657 | 12 | 9,314 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
import heapq
def inp(n):
L = []
for i in range(n):
s = str(input())
s1 = s.split()
if len(s1) == 2:
L.append([s1[0],int(s1[1])])
else:
L.append(s1)
return L
n = int(input())
L = inp(n)
S = []
H = []
heapq.heapify(H)
for j in L:
if j[0] == 'insert':
heapq.heappush(H,j[1])
S.append('insert' +' '+ str(j[1]))
elif j[0] == 'removeMin':
if len(H) == 0:
S.append('insert' +' '+ str(0))
S.append('removeMin')
else:
heapq.heappop(H)
S.append('removeMin')
else:
k = j[1]
if len(H) == 0:
heapq.heappush(H,k)
S.append('insert' +' '+ str(k))
S.append('getMin' +' '+ str(k))
elif k == H[0]:
S.append('getMin' +' '+ str(H[0]))
else:
if k<H[0]:
heapq.heappush(H,k)
S.append('insert' +' '+ str(k))
S.append('getMin' +' '+ str(k))
else:
while len(H) != 0 and k>H[0]:
heapq.heappop(H)
S.append('removeMin')
if len(H) == 0 or H[0] != k:
heapq.heappush(H,k)
S.append('insert' +' '+ str(k))
S.append('getMin' +' '+ str(k))
else:
S.append('getMin' +' '+ str(H[0]))
print(str(len(S)))
for i in S:
print(i)
``` | output | 1 | 4,657 | 12 | 9,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | instruction | 0 | 4,658 | 12 | 9,316 |
Tags: constructive algorithms, data structures, greedy
Correct Solution:
```
from __future__ import division, print_function
import heapq
import math
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
n=inp()
l=[]
a=[]
heapq.heapify(l)
count_ex=0
for i in range(n):
x=input()
cm=x.split()
if cm[0]=='insert':
v=int(cm[1])
heapq.heappush(l,v)
elif cm[0]=='removeMin':
if len(l)==0:
a.append('insert 0')
else:
heapq.heappop(l)
else:
v=int(cm[1])
while(len(l)>0 and l[0]!=v):
if l[0]<v:
a.append('removeMin')
heapq.heappop(l)
else:
a.append('insert {}'.format(v))
heapq.heappush(l,v)
if len(l)==0:
a.append('insert {}'.format(v))
heapq.heappush(l,v)
a.append(x)
print(len(a))
for each in a:
print(each.strip())
``` | output | 1 | 4,658 | 12 | 9,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
import heapq
ot = []
n = int(input())
a = []
heapq.heapify(a)
for i in range(n):
k1 = input()
k = k1.split()
if k[0] == "insert":
heapq.heappush(a, int(k[1]))
elif k[0] == "removeMin":
if len(a) == 0:
ot.append("insert 3")
else:
heapq.heappop(a)
else:
while len(a) != 0 and a[0] < int(k[1]):
ot.append("removeMin")
heapq.heappop(a)
if len(a) == 0:
ot.append("insert" + " " + k[1])
heapq.heappush(a, int(k[1]))
elif a[0] > int(k[1]):
ot.append("insert" + " " + k[1])
heapq.heappush(a, int(k[1]))
ot.append(k1)
print(len(ot))
for x in ot:
print(x)
``` | instruction | 0 | 4,659 | 12 | 9,318 |
Yes | output | 1 | 4,659 | 12 | 9,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
import math,heapq
import sys
input = sys.stdin.readline
n = int(input())
flag = 0
ans = []
l = []
heapq.heapify(l)
for i in range(n):
s = input()
if s[0] == "i":
heapq.heappush(l,int(s[7:len(s)-1]))
ans.append(s[0:len(s)-1])
continue
if s[0] == "r":
if len(l) == 0:
ans.append("insert 1")
ans.append(s[0:len(s)-1])
else:
ans.append(s[0:len(s)-1])
heapq.heappop(l)
continue
val = int(s[7:len(s)-1])
f = 0
# print(l)
while len(l) > 0 and f == 0:
if val == l[0]:
f = 1
break
if val > l[0]:
heapq.heappop(l)
ans.append("removeMin")
continue
heapq.heappush(l,val)
ans.append("insert "+str(val))
f = 1
break
if f == 0:
heapq.heappush(l, val)
ans.append("insert " + str(val))
ans.append(s[0:len(s)-1])
# print(ans)
print(len(ans))
for i in ans:
print(i)
``` | instruction | 0 | 4,660 | 12 | 9,320 |
Yes | output | 1 | 4,660 | 12 | 9,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
from heapq import heappush, heappop
n = int(input())
heap = []
res = []
for i in range(n):
string = input()
oper = string.split()
if oper[0][0] == 'i':
heappush(heap, int(oper[1]))
elif oper[0][0] == 'g':
key = int(oper[1])
while heap and heap[0] < key:
heappop(heap)
res.append('removeMin')
if not heap or heap[0] != key:
heappush(heap, key)
res.append('insert ' + oper[1])
else:
if heap:
heappop(heap)
else:
res.append('insert 0')
res.append(string)
print(len(res))
print('\n'.join(res))
``` | instruction | 0 | 4,661 | 12 | 9,322 |
Yes | output | 1 | 4,661 | 12 | 9,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
from heapq import heappop, heappush
from sys import stdin
n, *l = stdin.read().splitlines()
heap, res = [], []
for s in l:
array = s.split()
if array[0] == 'insert':
heappush(heap, int(array[1]))
elif array[0] == 'getMin':
key = int(array[1])
while heap and heap[0] < key:
heappop(heap)
res.append('removeMin')
if not heap or heap[0] != key:
heappush(heap, key)
res.append('insert ' + array[1])
else:
if heap:
heappop(heap)
else:
res.append('insert 0')
res.append(s)
print(len(res))
print('\n'.join(res))
``` | instruction | 0 | 4,662 | 12 | 9,324 |
Yes | output | 1 | 4,662 | 12 | 9,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
from queue import PriorityQueue
res = []
l = 0
q = PriorityQueue()
for _ in range(int(input())):
a = input()
if a[:6] == "insert":
q.put(int(a.split()[1]))
elif a[:6] == "getMin":
mn = int(a.split()[1])
while not q.empty() and q.get() != mn:
res.append("removeMin")
l += 1
if q.empty():
res.append("insert "+str(mn))
l += 1
else:
print(q.empty())
if q.empty():
q.put(0)
res.append("insert 0")
l += 1
q.get()
res.append(a)
l += 1
print(l)
for each in res:
print(each)
``` | instruction | 0 | 4,663 | 12 | 9,326 |
No | output | 1 | 4,663 | 12 | 9,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
def left(idx):
return (idx << 1) + 1
def right(idx):
return (idx << 1) + 2
def parent(idx):
return ((idx + 1) >> 1) - 1
class Heap:
def __init__(self, capacity):
self.data = [0] * capacity
self.L = 0
def restore_heap(self, idx, up=True):
best = idx
l, r = left(idx), right(idx)
if l < self.L and self.data[l] < self.data[best]:
self.data[l], self.data[idx] = self.data[idx], self.data[l]
best = l
if r < self.L and self.data[r] < self.data[best]:
self.data[r], self.data[best] = self.data[best], self.data[r]
best = r
if best == idx:
return
if up:
self.restore_heap(parent(idx), True)
else:
self.restore_heap(best, False)
def add(self, val):
self.data[self.L] = val
self.L += 1
self.restore_heap(parent(self.L - 1))
def removeMin(self):
if self.L == 0: return None
self.L -= 1
self.data[0] = self.data[self.L]
self.restore_heap(0, False)
if self.L == 0: return None
return self.data[0]
def getMin(self):
if self.L == 0: return None
return self.data[0]
n = int(input())
h = Heap(100000)
answer = []
for i in range(n):
q = input()
if q[0] == 'i':
v = int(q.split()[1])
h.add(v)
elif q[0] == 'g':
v = int(q.split()[1])
m = h.getMin()
while m is not None and m < v:
answer.append("removeMin")
m = h.removeMin()
if m is None or m > v:
answer.append("insert " + str(v))
h.add(v)
elif h.removeMin() is None:
answer.append("insert 0")
answer.append(q)
def print_answer():
print(len(answer))
print("\n".join(answer))
print_answer()
``` | instruction | 0 | 4,664 | 12 | 9,328 |
No | output | 1 | 4,664 | 12 | 9,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
import heapq
def inp(n):
L = []
for i in range(n):
s = str(input())
s1 = s.split()
L.append([s1[0],int(s1[1])])
return L
n = int(input())
L = inp(n)
S = []
H = []
heapq.heapify(H)
for j in L:
if j[0] == 'insert':
heapq.heappush(H,j[1])
S.append('insert' +' '+ str(j[1]))
elif j[0] == 'removeMin':
if len(H) == 0:
S.append('insert' +' '+ str(0))
S.append('removeMin')
else:
heapq.heappop(H)
S.append('removeMin')
else:
k = j[1]
if len(H) == 0:
heapq.heappush(H,k)
S.append('insert' +' '+ str(k))
S.append('getMin' +' '+ str(k))
elif k == H[0]:
S.append('getMin' +' '+ str(H[0]))
else:
if k<H[0]:
heapq.heappush(H,k)
S.append('insert' +' '+ str(k))
S.append('getMin' +' '+ str(k))
else:
while len(H) != 0 and k>H[0]:
heapq.heappop(H)
S.append('removeMin')
if len(H) == 0 or H[0] != k:
heapq.heappush(H,k)
S.append('insert' +' '+ str(k))
S.append('getMin' +' '+ str(k))
else:
S.append('getMin' +' '+ str(H[0]))
for i in S:
print(i)
``` | instruction | 0 | 4,665 | 12 | 9,330 |
No | output | 1 | 4,665 | 12 | 9,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
* put the given number into the heap;
* get the value of the minimum element in the heap;
* extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
* insert x β put the element with value x in the heap;
* getMin x β the value of the minimum element contained in the heap was equal to x;
* removeMin β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
Input
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
Output
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
Examples
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
Note
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice.
Submitted Solution:
```
from heapq import heapify,heappop,heappush
n = int(input())
a = []
fi = []
heapify(a)
for _ in range(0,n):
s = input().split(' ')
if s[0] == "insert":
heappush(a,int(s[1]))
fi.append(' '.join(s))
elif s[0] == "getMin":
while len(a)>0 and a[0]<int(s[1]):
fi.append("removeMin")
heappop(a)
if len(a) == 0:
fi.append("insert "+s[1])
fi.append(' '.join(s))
else:
fi.append(' '.join(s))
else:
if len(a) == 0:
fi.append("insert 1")
fi.append(' '.join(s))
else:
fi.append(' '.join(s))
heappop(a)
print(len(fi))
for i in fi:
print(i)
``` | instruction | 0 | 4,666 | 12 | 9,332 |
No | output | 1 | 4,666 | 12 | 9,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,767 | 12 | 9,534 |
Tags: brute force, constructive algorithms
Correct Solution:
```
n, a, b = map(int, input().strip().split())
def pp(seq):
return ' '.join(map(str, seq))
def cycle(start, size):
return list(range(start+1, start+size)) + [start]
def fail_sequence(target, acc=[]):
''' recursion failure, bloody python '''
if target % a == 0:
return acc + [a] * (target//a)
if target % b == 0:
return acc + [b] * (target//b)
if a <= target:
acc.append(a)
res = sequence(target-a, acc)
if res:
return res
acc.pop()
if b <= target:
acc.append(b)
return sequence(target-b, acc)
return []
def sequence(target, a, b):
dp = {0:(0,0)} # num of (a, b) needed to reach sum
for i in range(1, target+1):
if i-a in dp:
na, nb = dp[i-a]
dp[i] = (na+1, nb)
elif i-b in dp:
na, nb = dp[i-b]
dp[i] = (na, nb+1)
na, nb = dp.get(target, (0,0))
return [a]*na + [b]*nb
def sol():
seq = sequence(n, a, b)
if seq:
res = []
i = 1
for size in seq:
res.extend(cycle(i, size))
i += size
return pp(res)
else:
return -1
print(sol())
``` | output | 1 | 4,767 | 12 | 9,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,768 | 12 | 9,536 |
Tags: brute force, constructive algorithms
Correct Solution:
```
N, A, B = map(int, input().split())
for i in range(N//A+1):
if (N-i*A)%B==0:
ans = []
for j in range(i):
l = []
for k in range(1, A+1):
l.append(j*A+k)
ans += l[1:]+[l[0]]
for j in range((N-i*A)//B):
l = []
for k in range(1, B+1):
l.append(i*A+j*B+k)
ans += l[1:]+[l[0]]
print(*ans)
break
else:
print(-1)
``` | output | 1 | 4,768 | 12 | 9,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,769 | 12 | 9,538 |
Tags: brute force, constructive algorithms
Correct Solution:
```
n, a, b = map(int, input().split())
x, y = 0, 0
ok = False
for i in range(n // a + 1):
if (n - a * i) % b == 0:
ok = True
x, y = i, (n - a * i) // b
break
if not ok:
print(-1)
exit()
pos = 1
for i in range(x):
print(pos + a - 1, end=' ')
for j in range(a - 1):
print(pos + j, end=' ')
pos += a
for i in range(y):
print(pos + b - 1, end=' ')
for j in range(b - 1):
print(pos + j, end=' ')
pos += b
``` | output | 1 | 4,769 | 12 | 9,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,770 | 12 | 9,540 |
Tags: brute force, constructive algorithms
Correct Solution:
```
ans = []
def go(base, n):
ans.append(base + n - 1)
for i in range(base, base + n - 1):
ans.append(i)
def solve(n, a, b, x):
cur_start = 1
y = (n - a * x) // b
for i in range(x):
go(cur_start, a)
cur_start += a
for i in range(y):
go(cur_start, b)
cur_start += b
n, a, b = map(int, input().split())
for x in range(0, 10 ** 6 + 1):
if (n - x * a) % b == 0 and n - x * a >= 0:
solve(n, a, b, x)
print(' '.join(map(str, ans)))
break
else:
print(-1)
# Made By Mostafa_Khaled
``` | output | 1 | 4,770 | 12 | 9,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,771 | 12 | 9,542 |
Tags: brute force, constructive algorithms
Correct Solution:
```
n,a,b=map(int,input().split())
x,y=-1,-1
for i in range(n+1):
if n-a*i>=0 and (n-a*i)%b==0:
x=i
y=(n-a*i)//b
break
if x==-1:
print(-1)
exit()
ans=[-1]*n
for i in range(0,a*x,a):
for j in range(i,i+a-1):
ans[j]=j+1
ans[i+a-1]=i
for i in range(a*x,n,b):
for j in range(i,i+b-1):
ans[j]=j+1
ans[i+b-1]=i
for i in range(n):
ans[i]+=1
print(*ans)
``` | output | 1 | 4,771 | 12 | 9,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,772 | 12 | 9,544 |
Tags: brute force, constructive algorithms
Correct Solution:
```
n, a, b = map(int, input().split())
r = []
def f(a, l, r=r):
r += [a + l - 1] + list(range(a, a + l - 1))
i = 1
while n > 0:
if n % b is 0:
break
f(i, a)
i += a
n -= a
while n > 0:
f(i, b)
i += b
n -= b
print(-1) if n else print(*r)
``` | output | 1 | 4,772 | 12 | 9,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,773 | 12 | 9,546 |
Tags: brute force, constructive algorithms
Correct Solution:
```
#!/usr/bin/env python3
import sys
n, a, b = map(int, sys.stdin.readline().strip().split())
# supposed a>=0, b>=0, a+b>0
# returns (d, p, q) where d = gcd(a, b) = a*p + b*q
def euc(a, b):
if b > a:
(d, p, q) = euc(b, a)
return (d, q, p)
if b == 0:
return (a, 1, 0)
s = a // b
(d, sp, sq) = euc(a - s * b, b)
return (d, sp, sq - s * sp)
def normalize(n, a, b, d, p, q):
if p < 0:
(sp, sq) = normalize(n, b, a, d, q, p)
return (sq, sp)
elif q >= 0:
return (p, q)
# p>=0, q < 0
r = - (q // (a // d))
sp = p - r * (b // d)
sq = q + r * (a // d)
if sp < 0:
raise ValueError
else:
return (sp, sq)
(d, p, q) = euc(a, b)
if n % d != 0:
print ('-1')
sys.exit(0)
m = n // d
p, q = m * p, m * q
try:
(p, q) = normalize(n, a, b, d, p, q)
except ValueError:
print ('-1')
sys.exit(0)
res = []
last = 1
for _ in range(p):
res.extend(range(last + 1, last + a))
res.append(last)
last += a
for _ in range(q):
res.extend(range(last + 1, last + b))
res.append(last)
last += b
print (' '.join(map(str, res)))
``` | output | 1 | 4,773 | 12 | 9,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1 | instruction | 0 | 4,774 | 12 | 9,548 |
Tags: brute force, constructive algorithms
Correct Solution:
```
# ========= /\ /| |====/|
# | / \ | | / |
# | /____\ | | / |
# | / \ | | / |
# ========= / \ ===== |/====|
# code
def main():
n , a , b = map(int , input().split())
if a < b:
a , b = b , a
s = []
k = 1
ok = False
x = 0
y = 0
for j in range(n):
kk = n - a * j
if kk >= 0:
if kk % b == 0:
ok = True
x = j
y = kk // b
break
if not ok:
print(-1)
return
for j in range(x):
for i in range(a - 1):
s.append(k + i + 1)
s.append(k)
k += a
n -= a
for j in range(y):
for i in range(b - 1):
s.append(k + i + 1)
s.append(k)
k += b
n -= b
print(*s)
return
if __name__ == "__main__":
main()
``` | output | 1 | 4,774 | 12 | 9,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
from sys import stdin, stdout
from random import randrange
n, a, b = map(int, stdin.readline().split())
first, second = -1, -1
for i in range(n):
if a * i > n:
break
if not (n - a * i) % b:
first, second = i, (n - a * i) // b
break
if min(first, second) == -1:
stdout.write('-1')
else:
ans = [0 for i in range(n + 1)]
current = 1
for i in range(first):
ans[current] = current + a - 1
current += 1
for j in range(1, a):
ans[current] = current - 1
current += 1
for i in range(second):
ans[current] = current + b - 1
current += 1
for j in range(1, b):
ans[current] = current - 1
current += 1
stdout.write(' '.join(list(map(str, ans[1:]))))
``` | instruction | 0 | 4,775 | 12 | 9,550 |
Yes | output | 1 | 4,775 | 12 | 9,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
ans = []
def go(base, n):
ans.append(base + n - 1)
for i in range(base, base + n - 1):
ans.append(i)
def solve(n, a, b, x):
cur_start = 1
y = (n - a * x) // b
for i in range(x):
go(cur_start, a)
cur_start += a
for i in range(y):
go(cur_start, b)
cur_start += b
n, a, b = map(int, input().split())
for x in range(0, 10 ** 6 + 1):
if (n - x * a) % b == 0 and n - x * a >= 0:
solve(n, a, b, x)
print(' '.join(map(str, ans)))
break
else:
print(-1)
``` | instruction | 0 | 4,776 | 12 | 9,552 |
Yes | output | 1 | 4,776 | 12 | 9,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:
<image>
Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.
For given N, A, B, find a permutation P of integers from 1 to N such that for 1 β€ i β€ N, g(i) equals either A or B.
Input
The only line contains three integers N, A, B (1 β€ N β€ 106, 1 β€ A, B β€ N).
Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
Examples
Input
9 2 5
Output
6 5 8 3 4 1 9 2 7
Input
3 2 1
Output
1 2 3
Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5
In the second example, g(1) = g(2) = g(3) = 1
Submitted Solution:
```
n, a, b = map(int, input().split())
for i in range(n//a + 1):
if (n - i*a) % b == 0:
res = []
for j in range(i):
t = [j*a + k for k in range(1, a+1)]
res += t[1:] + [t[0]]
for j in range((n - i*a) // b):
t = [i*a + j*b + k for k in range(1, b+1)]
res += t[1:] + [t[0]]
print(*res)
break
else:
print(-1)
``` | instruction | 0 | 4,777 | 12 | 9,554 |
Yes | output | 1 | 4,777 | 12 | 9,555 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.