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.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge. | instruction | 0 | 77,723 | 12 | 155,446 |
Tags: combinatorics, dp, graphs, math
Correct Solution:
```
a = int(input())
mod = 10 ** 9 + 7
sum1 = 1
for i in range(2, a + 1):
sum1 = (sum1 * i) % mod
print((sum1 - 2 ** (a - 1)) % mod)
``` | output | 1 | 77,723 | 12 | 155,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge. | instruction | 0 | 77,724 | 12 | 155,448 |
Tags: combinatorics, dp, graphs, math
Correct Solution:
```
n=int(input())
pr=1
mod=10**9+7
dif=1/2
for i in range(1,n+1):
pr=(pr*i)%mod
dif=int(dif*2)%mod
if pr<dif:
pr+=mod
print(pr-dif)
``` | output | 1 | 77,724 | 12 | 155,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge. | instruction | 0 | 77,725 | 12 | 155,450 |
Tags: combinatorics, dp, graphs, math
Correct Solution:
```
################################################
################################################
######### Written By Boddapati Mahesh ##########
######### IIIT Hyderabad #######################
################################################
################################################
def fast_exp(b, e, m):
r = 1
if 1 & e:
r = b
while e:
e >>= 1
b = (b * b) % m
if e & 1: r = (r * b) % m
return r
def factorial(n,m):
f = 1
for i in range(1, n + 1):
f = (f * i) % m
return f
m = 1000000007
e = int(input())
if e == 0:
print(0)
else:
b = 2
r = fast_exp(b, e-1, m)
fm = factorial(e,m)
print((fm-r)%m)
``` | output | 1 | 77,725 | 12 | 155,451 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
MOD = 10 ** 9 + 7
out = 1
n = int(input())
for i in range(n):
out *= (i + 1)
out %= MOD
out -= pow(2, n - 1, MOD)
print(out % MOD)
``` | instruction | 0 | 77,726 | 12 | 155,452 |
Yes | output | 1 | 77,726 | 12 | 155,453 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
n=int(input())
ans=1
for i in range(1,n+1):
ans=(ans*i)%1000000007
temp=4
while(n>3):
temp=(temp*2)%1000000007
n=n-1
ans=(ans-temp+ 1000000007)%1000000007
print(ans)
``` | instruction | 0 | 77,727 | 12 | 155,454 |
Yes | output | 1 | 77,727 | 12 | 155,455 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
import math
def main():
mod = 10**9 + 7
n = int(input())
fact = 1
for i in range(n):
fact *= (i+1)
fact %= mod
print((fact - pow(2, n-1, mod)) % mod)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 77,728 | 12 | 155,456 |
Yes | output | 1 | 77,728 | 12 | 155,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
#!/usr/bin/env pypy3
MODULUS = 10**9+7
def fac(n):
ret = 1
for i in range(1,n+1):
ret *= i
ret %= MODULUS
return ret
n = int(input())
total_perms = fac(n)
non_cyclic = pow(2, n-1, MODULUS)
r = total_perms - non_cyclic
r %= MODULUS
r += MODULUS
r %= MODULUS
print(r)
``` | instruction | 0 | 77,729 | 12 | 155,458 |
Yes | output | 1 | 77,729 | 12 | 155,459 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
n=int(input())
def factorial(n):
ans=1
for i in range(1,n+1):
ans*=i
ans%=10**9+7
return ans%(10**9+7)
print(factorial(n)-(2**(n-1))%(10**9+7))
``` | instruction | 0 | 77,730 | 12 | 155,460 |
No | output | 1 | 77,730 | 12 | 155,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
"""
Satwik_Tiwari ;) .
9th AUGUST , 2020 - SUNDAY
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned
#==============================================================================================
#fast I/O region
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)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
#===============================================================================================
#some shortcuts
mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n") #as stdout.write always print sring.
def testcase(t):
for p in range(t):
solve()
def printlist(a) :
for p in range(0,len(a)):
out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(a,b):
ans = 1
while(b>0):
if(b%2==1):
ans*=a
a*=a
b//=2
return ans
# def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) :
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
#===============================================================================================
# code here ;))
def pow(x,y,p):
res = 1
x%=p
while(y>0):
if(y%2==1):
res = (res*x)%p
y = y//2
x = (x**x)%p
return res
def ncr(n, r, p):
# initialize numerator
# and denominator
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def solve():
n = int(inp())
ans = 1
for i in range(1,n+1):
ans*=i
ans%=mod
reduce = 1
for i in range(n-1):
reduce*=2
reduce%=mod
print(ans-reduce)
testcase(1)
# testcase(int(inp()))
``` | instruction | 0 | 77,731 | 12 | 155,462 |
No | output | 1 | 77,731 | 12 | 155,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
from collections import defaultdict, Counter
from bisect import bisect, bisect_left
from math import sqrt, gcd, ceil, factorial
from heapq import heapify, heappush, heappop
MOD = 10**9 + 7
inf = float("inf")
ans_ = []
def nin():return int(input())
def ninf():return int(file.readline())
def st():return (input().strip())
def stf():return (file.readline().strip())
def read(): return list(map(int, input().strip().split()))
def readf():return list(map(int, file.readline().strip().split()))
def factos(n, mod):
arr = [0]*(n+1)
a = 1
for i in range(1, n+1):
a *= i
a%= mod
arr[i] = a
return(arr)
# file = open("input.txt", "r")
def solve():
fact = factos(10**6, MOD)
for _ in range(1):
n = nin()
ans_.append(fact[n] - pow(2,n-1,MOD))
# file.close()
solve()
for i in ans_:print(i)
``` | instruction | 0 | 77,732 | 12 | 155,464 |
No | output | 1 | 77,732 | 12 | 155,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Consider a permutation p of length n, we build a graph of size n using it as follows:
* For every 1 β€ i β€ n, find the largest j such that 1 β€ j < i and p_j > p_i, and add an undirected edge between node i and node j
* For every 1 β€ i β€ n, find the smallest j such that i < j β€ n and p_j > p_i, and add an undirected edge between node i and node j
In cases where no such j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4, and p = [3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).
A permutation p is cyclic if the graph built using p has at least one simple cycle.
Given n, find the number of cyclic permutations of length n. Since the number may be very large, output it modulo 10^9+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n (3 β€ n β€ 10^6).
Output
Output a single integer 0 β€ x < 10^9+7, the number of cyclic permutations of length n modulo 10^9+7.
Examples
Input
4
Output
16
Input
583291
Output
135712853
Note
There are 16 cyclic permutations for n = 4. [4,2,1,3] is one such permutation, having a cycle of length four: 4 β 3 β 2 β 1 β 4.
Nodes v_1, v_2, β¦, v_k form a simple cycle if the following conditions hold:
* k β₯ 3.
* v_i β v_j for any pair of indices i and j. (1 β€ i < j β€ k)
* v_i and v_{i+1} share an edge for all i (1 β€ i < k), and v_1 and v_k share an edge.
Submitted Solution:
```
n = int(input())
m = 1000000007
total = 1;
for i in range(1, n+1):
total = (total * i) % m
twos = 1;
for i in range(1, n):
twos = (2 * twos) % m
print(total - twos)
``` | instruction | 0 | 77,733 | 12 | 155,466 |
No | output | 1 | 77,733 | 12 | 155,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,765 | 12 | 155,530 |
Tags: brute force, sortings
Correct Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
if n==1 or n==2:
print(-1)
exit()
if len(set(l))==1:
print(-1)
exit()
def rev(l):
return all(l[i]>=l[i+1] for i in range(n-1))
def srt(l):
return all(l[i]<=l[i+1] for i in range(n-1))
if rev(l) or srt(l):
for i in range(1,n):
if l[i]!=l[i-1]:
print(i,i+1)
exit()
f=0
cnt=0
for i in range(1,n):
if l[i]!=l[i-1]:
if cnt==50:
break
l[i],l[i-1]=l[i-1],l[i]
# print(l)
if srt(l) or rev(l):
cnt+=1
l[i],l[i-1]=l[i-1],l[i]
else:
print(i,i+1)
exit()
else:
print(-1)
``` | output | 1 | 77,765 | 12 | 155,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,766 | 12 | 155,532 |
Tags: brute force, sortings
Correct Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
if n==1 or n==2:
print(-1)
exit()
if len(set(l))==1:
print(-1)
exit()
def rev(l):
return all(l[i]>=l[i+1] for i in range(n-1))
def srt(l):
return all(l[i]<=l[i+1] for i in range(n-1))
if rev(l) or srt(l):
for i in range(1,n):
if l[i]!=l[i-1]:
print(i,i+1)
exit()
f=0
cnt=0
for i in range(1,n):
if l[i]!=l[i-1]:
if cnt==2:
break
l[i],l[i-1]=l[i-1],l[i]
# print(l)
if srt(l) or rev(l):
cnt+=1
l[i],l[i-1]=l[i-1],l[i]
else:
print(i,i+1)
exit()
else:
print(-1)
``` | output | 1 | 77,766 | 12 | 155,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,767 | 12 | 155,534 |
Tags: brute force, sortings
Correct Solution:
```
# def hehe(a,n):
# if n==1 or n==2:
# return -1
#
# a1=list(a)
# a1.sort()
#
# a2=list(a1)
# a2.reverse()
#
# acopy=list(a)
# for i in range (0,n,1):
# for j in range(i+1,n,1):
# if(a[i]==a[j]):
# continue
# temp=acopy[i]
# acopy[i]=acopy[j]
# acopy[j]=temp
# if acopy!=a2 and acopy!=a1:
# return [i+1,j+1]
# acopy=list(a)
# return -1
def hehe(a,n):
if n==1 or n==2:
return -1
a1=list(a)
for i in range(1,n-1,1):
if a1[i-1]==a1[i]==a1[i+1]:
continue
elif a1[i-1]<=a1[i]<=a1[i+1] or a1[i-1]>=a1[i]>=a1[i+1]:
if a1[i-1]==a1[i]:
return [i+1,i+2]
elif a1[i]==a1[i+1]:
return [i,i+1]
else:
return [i,i+1]
elif a1[i-1]<a1[i]>a1[i+1] or a1[i-1]>a1[i]<a1[i+1]:
if a1[i-1]!=a1[i+1]:
return [i,i+2]
else:
if i<n-2:
if a1[i+2]==a1[i]:
return [i,i+1]
return -1
n=int(input())
a=input().split()
a=[int(i) for i in a]
answer=hehe(a,n)
if answer==-1:
print(-1)
else:
print(*answer, sep=" ")
``` | output | 1 | 77,767 | 12 | 155,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,768 | 12 | 155,536 |
Tags: brute force, sortings
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
a, b = -1, -1
for i in range(1, n):
if arr[i-1] == arr[i]:
continue
arr[i-1], arr[i] = arr[i], arr[i-1]
up, down = True, True
for j in range(1, n):
if arr[j-1] < arr[j]:
down = False
if arr[j-1] > arr[j]:
up = False
if not up and not down:
a, b = i-1, i
break
arr[i-1], arr[i] = arr[i], arr[i-1]
print(-1 if a == -1 else str(a+1)+' '+str(b+1))
``` | output | 1 | 77,768 | 12 | 155,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,769 | 12 | 155,538 |
Tags: brute force, sortings
Correct Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
if n==1 or n==2:
print(-1)
exit()
if len(set(l))==1:
print(-1)
exit()
def rev(l):
return all(l[i]>=l[i+1] for i in range(n-1))
def srt(l):
return all(l[i]<=l[i+1] for i in range(n-1))
if rev(l) or srt(l):
for i in range(1,n):
if l[i]!=l[i-1]:
print(i,i+1)
exit()
f=0
cnt=0
for i in range(1,n):
if l[i]!=l[i-1]:
if cnt==10:
break
l[i],l[i-1]=l[i-1],l[i]
# print(l)
if srt(l) or rev(l):
cnt+=1
l[i],l[i-1]=l[i-1],l[i]
else:
print(i,i+1)
exit()
else:
print(-1)
``` | output | 1 | 77,769 | 12 | 155,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,770 | 12 | 155,540 |
Tags: brute force, sortings
Correct Solution:
```
import sys
import random
n = int(input())
a = [int(x) for x in input().split()]
def flat():
for i in range(1, n):
#print(i)
if a[i] != a[0]:
return False
return True
if n<3 or flat():
print(-1)
sys.exit()
def sorted_asc():
for i in range(1, n):
if a[i] > a[i-1]:
return False
return True
def sorted_desc():
for i in range(1, n):
if a[i] < a[i-1]:
return False
return True
"""
if sorted_asc() or sorted_desc():
i = 1
while a[i]==a[0]:
i += 1
print(1, i+1)
sys.exit()
"""
for i in range(0,100000):
#print(random.random())
be = random.randrange(0, n)
en = be
while en == be:
en = random.randrange(0, n)
if a[be]==a[en]:
continue
a[be], a[en] = a[en], a[be]
if not sorted_asc() and not sorted_desc():
print(be+1, en+1)
sys.exit()
a[be], a[en] = a[en], a[be]
print(-1)
``` | output | 1 | 77,770 | 12 | 155,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,771 | 12 | 155,542 |
Tags: brute force, sortings
Correct Solution:
```
n = int(input())
data = list(map(int,input().split()))
mins = [0 for i in range(n)]
maxs = [0 for i in range(n)]
mins_back = [0 for i in range(n)]
maxs_back = [0 for i in range(n)]
min_ = data[0]
mins[0] = 0
max_ = data[0]
maxs[0] = 0
if n == 2:
print( - 1)
exit(0)
for i in range(n):
if data[i] < min_:
min_ = data[i]
mins[i] = i
else:
if i > 0:
mins[i] = mins[i - 1]
if data[i] > max_:
max_ = data[i]
maxs[i] = i
else:
if i > 0:
maxs[i] = maxs[i - 1]
for i in range(1, n - 1):
if data[i] != data[mins[i]]:
if mins[i] == i - 1 and data[mins[i]] < data[i +1]:
print(i + 1 , mins[i] + 1)
exit(0)
if data[i - 1] > data[mins[i]] and data[mins[i]] < data[i +1]:
print(i + 1, mins[i] +1)
exit(0)
if data[i] != data[maxs[i]]:
if maxs[i] == i - 1 and data[maxs[i]] > data[i + 1]:
print(i + 1 , maxs[i] + 1)
exit(0)
if data[i - 1] <data[maxs[i]] and data[maxs[i]] > data[i +1]:
print(i + 1 , maxs[i] + 1)
exit(0)
data = data[::-1]
min_ = data[0]
mins[0] = 0
max_ = data[0]
maxs[0] = 0
for i in range(n):
if data[i] < min_:
min_ = data[i]
mins[i] = i
else:
if i > 0:
mins[i] = mins[i - 1]
if data[i] > max_:
max_ = data[i]
maxs[i] = i
else:
if i > 0:
maxs[i] = maxs[i - 1]
for i in range(1, n - 1):
if data[i] != data[mins[i]]:
if mins[i] == i - 1 and data[mins[i]] < data[i +1]:
print(n - i , n - mins[i])
exit(0)
if data[i - 1] > data[mins[i]] and data[mins[i]] < data[i +1]:
print(n - i, n - mins[i])
exit(0)
if data[i] != data[maxs[i]]:
if maxs[i] == i - 1 and data[maxs[i]] > data[i + 1]:
print(n - i , n - maxs[i])
exit(0)
if data[i - 1] <data[maxs[i]] and data[maxs[i]] > data[i +1]:
print(n - i, n - maxs[i])
exit(0)
data[0], data[-1] = data[-1], data[0]
if data[0] == data[-1]:
print(-1)
exit(0)
for i in range(1, n - 1):
if data[i] < data[i -1] and data[i] < data[i + 1]:
print(1, n)
exit(0)
if data[i] > data[i -1] and data[i] > data[i + 1]:
print(1, n)
exit(0)
print(-1)
``` | output | 1 | 77,771 | 12 | 155,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | instruction | 0 | 77,772 | 12 | 155,544 |
Tags: brute force, sortings
Correct Solution:
```
n = int(input())
t = list(map(int, input().split()))
if n < 3 or (n == 3 and t[0] == t[2]) or all(i == t[0] for i in t): print(-1)
else:
i = 1
while t[i] == t[i - 1]: i += 1
if i > 1: print('2 ' + str(i + 1))
elif t[0] < t[1]:
if t[1] <= t[2]: print('1 2')
elif t[0] == t[2]:
if t[0] == t[3]: print('2 3')
else: print('3 4')
else: print('1 3')
else:
if t[1] >= t[2]: print('1 2')
elif t[0] == t[2]:
if t[0] == t[3]: print('2 3')
else: print('3 4')
else: print('1 3')
``` | output | 1 | 77,772 | 12 | 155,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
a, b = -1, -1
for i in range(1, n):
if arr[i-1] == arr[i]:
continue
arr[i-1], arr[i] = arr[i], arr[i-1]
up, down = True, True
for j in range(1, n):
if arr[j-1] < arr[j]:
down = False
if arr[j-1] > arr[j]:
up = False
if not up and not down:
a, b = i-1, i
break
arr[i-1], arr[i] = arr[i], arr[i-1]
if a == -1:
print(-1)
else:
print('%d %d' % (a+1, b+1))
``` | instruction | 0 | 77,773 | 12 | 155,546 |
Yes | output | 1 | 77,773 | 12 | 155,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
n = int(input())
t = list(map(int, input().split()))
if n < 3 or (n == 3 and t[0] == t[2]) or all(i == t[0] for i in t): print(-1)
else:
i = 1
while t[i] == t[i - 1]: i += 1
if i > 1: print('2 ' + str(i + 1))
elif t[0] < t[1]:
if t[1] <= t[2]: print('1 2')
elif t[0] == t[2]:
if t[0] == t[3]: print('2 3')
else: print('3 4')
else: print('1 3')
else:
if t[1] >= t[2]: print('1 2')
elif t[0] == t[2]:
if t[0] == t[3]: print('2 3')
else: print('3 4')
else: print('1 3')
# Made By Mostafa_Khaled
``` | instruction | 0 | 77,774 | 12 | 155,548 |
Yes | output | 1 | 77,774 | 12 | 155,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
n = int(input())
lis=list(map(int,input().split()))
sor=sorted(lis)
res=sor[::-1]
for i in range(1,n):
if lis[i-1]==lis[i]:
continue
for j in range(i-1,n):
# print(lis[i],lis[j])
if lis[i]!=lis[j]:
lis[i],lis[j]=lis[j],lis[i]
# print(lis,sor,res,i,j)
if lis!=sor and lis!=res:
print(i+1,j+1)
exit()
lis[i],lis[j]=lis[j],lis[i]
i+=1
# print(i)
print(-1)
``` | instruction | 0 | 77,775 | 12 | 155,550 |
Yes | output | 1 | 77,775 | 12 | 155,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
n=int(input())
l=[int(i) for i in input().split()]
if n==1 or n==2:
print(-1)
exit()
if len(set(l))==1:
print(-1)
exit()
def rev(l):
return all(l[i]>=l[i+1] for i in range(n-1))
def srt(l):
return all(l[i]<=l[i+1] for i in range(n-1))
if rev(l) or srt(l):
for i in range(1,n):
if l[i]!=l[i-1]:
print(i,i+1)
exit()
f=0
cnt=0
for i in range(1,n):
if l[i]!=l[i-1]:
if cnt==3:
break
l[i],l[i-1]=l[i-1],l[i]
# print(l)
if srt(l) or rev(l):
cnt+=1
l[i],l[i-1]=l[i-1],l[i]
else:
print(i,i+1)
exit()
else:
print(-1)
``` | instruction | 0 | 77,776 | 12 | 155,552 |
Yes | output | 1 | 77,776 | 12 | 155,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
def Sort(a,n):
for i in range(1,n):
if a[i]>a[i-1]:
break
if i==n:
return True
for i in range(1,n):
if a[i]<a[i-1]:
return False
return True
n = int(input())
a = list(map(int,input().split()))
if n==1 or n==2 or (n==3 and a[0]==a[-1]) or a.count(a[0])==n:
print("-1")
else:
for i in range(1,n):
if a[i]!=a[i-1]:
temp=a[i]
a[i]=a[i-1]
a[i-1]=temp
break
if not Sort(a,n):
print(i,i+1)
else:
temp=a[i]
a[i]=a[i-1]
a[i-1]=temp
for i in range(n-1,0,-1):
if a[i]!=a[i-1]:
temp=a[i]
a[i]=a[i-1]
a[i-1]=temp
break
if not Sort(a,n):
print(i,i+1)
else:
print("-1")
``` | instruction | 0 | 77,777 | 12 | 155,554 |
No | output | 1 | 77,777 | 12 | 155,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
def all_same(arr):
for i in arr:
if i != arr[0]:
return False
return True
n = int(input())
arr = list(map(int,input().split()))
if all_same(arr) or (n==2):
exit(print(-1))
x = sorted(arr)
if (arr == x) or (arr[::-1] == x):
exit(print(1,arr.index(x[-1])+1))
start = 0
end = n-1
trend = 'Inc' if arr[0]<arr[1] else 'Dec'
for i in range(1,n-1):
if arr[i]==arr[i+1]:
exit(print(i+1,i+2))
if arr[i] < arr[i+1] and trend == 'Dec':
exit(print(i+1,i+2))
if arr[i] > arr[i+1] and trend == 'Inc':
exit(print(i+1,i+2))
``` | instruction | 0 | 77,778 | 12 | 155,556 |
No | output | 1 | 77,778 | 12 | 155,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
import sys
n = int(input())
a = [int(s) for s in input().split()]
for i in range(0,2):
for j in range(i+1,n):
if a[i] != a[j]:
r = a[j]
a[j] = a[i]
a[i] = r
lol = True
lol1 = True
for h in range(1,n):
if a[h-1] >= a[h]:
lol = False
if a[h-1] < a[h]:
lol1 = False
if not(lol) and not(lol1):
print(i+1,j+1)
sys.exit()
else:
r = a[j]
a[j] = a[i]
a[i] = r
print(-1)
``` | instruction | 0 | 77,779 | 12 | 155,558 |
No | output | 1 | 77,779 | 12 | 155,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array a (the array elements are indexed from 1) consisting of n elements is called sorted if it meets at least one of the following two conditions:
1. a1 β€ a2 β€ ... β€ an;
2. a1 β₯ a2 β₯ ... β₯ an.
Help Petya find the two required positions to swap or else say that they do not exist.
Input
The first line contains a single integer n (1 β€ n β€ 105). The second line contains n non-negative space-separated integers a1, a2, ..., an β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109.
Output
If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to n.
Examples
Input
1
1
Output
-1
Input
2
1 2
Output
-1
Input
4
1 2 3 4
Output
1 2
Input
3
1 1 1
Output
-1
Note
In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
Submitted Solution:
```
from bisect import bisect_left, bisect_right
n = int(input())
ls = list(map(int, input().split()))
rev = False
for i in range(1, n):
if ls[i - 1] > ls[i]:
rev = True
ls = list(reversed(ls))
break
ar = list(set(ls))
if len(ls) > 2 and len(ar) > 1:
a = bisect_right(ls, ar[0])
b = bisect_left(ls, ar[1]) + 1
if rev:
print(n - b + 1, n - a + 1)
else:
print(a, b)
else:
print(-1)
``` | instruction | 0 | 77,780 | 12 | 155,560 |
No | output | 1 | 77,780 | 12 | 155,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·105) β the number of elements in the array. The next line contains n integers ai (1 β€ ai β€ 106) β the values of the array elements.
Output
In a single line print a single integer β the maximum number of points Artem can get.
Examples
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102 | instruction | 0 | 77,817 | 12 | 155,634 |
Tags: data structures, greedy
Correct Solution:
```
def maxScore(list):
score=0
stack=[]
stack.append(list[0])
for i in range(1,len(list)):
while(len(stack)>1 and stack[-1]<=min(list[i],stack[-2])):
score=score+min(list[i],stack[-2])
stack.pop()
stack.append(list[i])
for i in range(1,len(stack)-1):
score=score+min(stack[i-1],stack[i+1])
return score
input()
l=[int(x) for x in input().split()]
print(maxScore(l))
``` | output | 1 | 77,817 | 12 | 155,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·105) β the number of elements in the array. The next line contains n integers ai (1 β€ ai β€ 106) β the values of the array elements.
Output
In a single line print a single integer β the maximum number of points Artem can get.
Examples
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102 | instruction | 0 | 77,819 | 12 | 155,638 |
Tags: data structures, greedy
Correct Solution:
```
input()
list=[int(x) for x in input().split()]
stack=[]
score=0
stack.append(list[0])
for i in range(1,len(list)):
while(len(stack)>1 and stack[-1]<=min(list[i],stack[-2])):
score=score+min(list[i],stack[-2])
stack.pop()
stack.append(list[i])
for i in range(1,len(stack)-1):
score=score+min(stack[i-1],stack[i+1])
print(score)
``` | output | 1 | 77,819 | 12 | 155,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·105) β the number of elements in the array. The next line contains n integers ai (1 β€ ai β€ 106) β the values of the array elements.
Output
In a single line print a single integer β the maximum number of points Artem can get.
Examples
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102 | instruction | 0 | 77,820 | 12 | 155,640 |
Tags: data structures, greedy
Correct Solution:
```
MAXN = 5 * 10**5 + 100
a = []
ans = 0
n = int(input())
a = list( map ( int, input().split() ) )
a.append(0)
a = [0] + a
n = n + 2
arr = []
arr.append( a[0] )
arr.append( a[1] )
i = 2
while i < n :
ln = a[i]
l1 = arr[-1]
l0 = arr[-2]
while l1 <= l0 and l1 <= ln :
ans = ans + min ( l0 , ln )
arr.pop()
l1 = arr[-1]
l0 = arr[-2]
arr.append(ln)
i = i + 1
for i in range ( 1 , len(arr) - 1 ) :
ans += min ( arr[i - 1] , arr[i + 1] )
print (ans)
``` | output | 1 | 77,820 | 12 | 155,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·105) β the number of elements in the array. The next line contains n integers ai (1 β€ ai β€ 106) β the values of the array elements.
Output
In a single line print a single integer β the maximum number of points Artem can get.
Examples
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102 | instruction | 0 | 77,821 | 12 | 155,642 |
Tags: data structures, greedy
Correct Solution:
```
n = input()
s = []
a = 0
for i in map(int, input().split()):
while len(s) > 1 and min(s[-2], i)>=s[-1]:
a += min(i, s[-2])
del(s[-1])
s.append(i)
s.sort()
print(a + sum(s[0: -2]))
# Made By Mostafa_Khaled
``` | output | 1 | 77,821 | 12 | 155,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·105) β the number of elements in the array. The next line contains n integers ai (1 β€ ai β€ 106) β the values of the array elements.
Output
In a single line print a single integer β the maximum number of points Artem can get.
Examples
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102 | instruction | 0 | 77,822 | 12 | 155,644 |
Tags: data structures, greedy
Correct Solution:
```
n = input()
s = []
a = 0
for i in map(int, input().split()):
while len(s) > 1 and min(s[-2], i)>=s[-1]:
a += min(i, s[-2])
del(s[-1])
s.append(i)
s.sort()
print(a + sum(s[0: -2]))
``` | output | 1 | 77,822 | 12 | 155,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.
After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.
Input
The first line contains a single integer n (1 β€ n β€ 5Β·105) β the number of elements in the array. The next line contains n integers ai (1 β€ ai β€ 106) β the values of the array elements.
Output
In a single line print a single integer β the maximum number of points Artem can get.
Examples
Input
5
3 1 5 2 6
Output
11
Input
5
1 2 3 4 5
Output
6
Input
5
1 100 101 100 1
Output
102 | instruction | 0 | 77,823 | 12 | 155,646 |
Tags: data structures, greedy
Correct Solution:
```
n,s,a=input(),[],0
for i in map(int,input().split(' ')):
while len(s)>1 and min(s[-2],i)>=s[-1]:
a+=min(i,s[-2])
del(s[-1])
s.append(i)
s.sort()
print(a+sum(s[0:-2]))
``` | output | 1 | 77,823 | 12 | 155,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,464 | 12 | 156,928 |
Tags: greedy, math
Correct Solution:
```
for TC in range(int(input())):
a, b = map(int, input().split())
if a < b:
a, b = b, a
c = (a - b)// 10
print(c if not (a-b)%10 else c + 1)
``` | output | 1 | 78,464 | 12 | 156,929 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,465 | 12 | 156,930 |
Tags: greedy, math
Correct Solution:
```
#-------------------------------------------------------------------------
import math
#----------------------------------------------------------------------------
def ii():return int(input())
def si():return input()
def li():return list(map(int,input().split()))
def mi():return map(int,input().split())
def dpc(a,b):return [[0]*b for i in range(a)]
#-----------------------------------------------------------------------------
def solve():
a,b=mi()
if(a==b):
print(0)
else:
if(abs(a-b)%10==0):
print(abs(a-b)//10)
else:
print(abs(a-b)//10+1)
#-----------------------------------------------------------------------------
t=ii()
for _ in range(t):
solve()
# solve()
``` | output | 1 | 78,465 | 12 | 156,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,466 | 12 | 156,932 |
Tags: greedy, math
Correct Solution:
```
from math import *
# from sympy import *
# from cmath import *
# from itertools import combinations
# from random import *
count = int(0)
ans_list = list()
t = int(input())
inp_count = t
list_a = list()
list_b = list()
while inp_count != 0:
a, b = input().split()
list_a.append(int(a))
list_b.append(int(b))
inp_count -= 1
inp_count = t
i = int(0)
if i < len(list_a):
while inp_count != 0:
count = int(0)
inp_count -= 1
if int(list_a[i]) == int(list_b[i]):
ans_list.append(count)
elif int(list_a[i]) < int(list_b[i]):
ans = int(list_b[i]) - int(list_a[i])
ans = ceil(ans / 10)
ans_list.append(ans)
elif int(list_a[i]) > int(list_b[i]):
ans = int(list_a[i]) - int(list_b[i])
ans = ceil(ans / 10)
ans_list.append(ans)
i += 1
for item in ans_list:
print(item)
``` | output | 1 | 78,466 | 12 | 156,933 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,467 | 12 | 156,934 |
Tags: greedy, math
Correct Solution:
```
for _ in range(int(input())):
a,b = [int(x) for x in input().split(' ')]
z=abs(a-b)//10
if abs(a-b)%10 !=0:
print(z+1)
else:
print(z)
``` | output | 1 | 78,467 | 12 | 156,935 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,468 | 12 | 156,936 |
Tags: greedy, math
Correct Solution:
```
def solve():
a,b = map(int, input().split())
if(a==b):
return 0
if(a>b):
diff = a - b
diff = abs(a-b)
if(diff<=10):
return 1
moves = 0
p = diff//10
moves+=p
if(diff%10 != 0):
moves+=1
return moves
for _ in range(int(input())):
print(solve())
``` | output | 1 | 78,468 | 12 | 156,937 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,469 | 12 | 156,938 |
Tags: greedy, math
Correct Solution:
```
for zz in range(0,int(input())):
a,b=[int(i) for i in input().split()]
if a>b:
c=b-a
if c%2==0:
print(1)
else:
print(2)
elif a==b:
print(0)
else:
c=a-b
if c%2==0:
print(2)
else:
print(1)
``` | output | 1 | 78,469 | 12 | 156,939 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,470 | 12 | 156,940 |
Tags: greedy, math
Correct Solution:
```
t = int(input())
for _ in range(t):
a, b = list(map(int, input().split()))
diff = abs(a-b)
plus_10 = diff // 10
print(plus_10 + (diff != 10 * plus_10))
``` | output | 1 | 78,470 | 12 | 156,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers a and b.
In one move, you can choose some integer k from 1 to 10 and add it to a or subtract it from a. In other words, you choose an integer k β [1; 10] and perform a := a + k or a := a - k. You may use different values of k in different moves.
Your task is to find the minimum number of moves required to obtain b from a.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a and b (1 β€ a, b β€ 10^9).
Output
For each test case, print the answer: the minimum number of moves required to obtain b from a.
Example
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
Note
In the first test case of the example, you don't need to do anything.
In the second test case of the example, the following sequence of moves can be applied: 13 β 23 β 32 β 42 (add 10, add 9, add 10).
In the third test case of the example, the following sequence of moves can be applied: 18 β 10 β 4 (subtract 8, subtract 6). | instruction | 0 | 78,471 | 12 | 156,942 |
Tags: greedy, math
Correct Solution:
```
def process():
a,b=list(map(int,input().split()))
if(a==b):
print("0\n")
return 0
if(a%2==1 and b%2==0 and a<b):
print("1\n") # 5 10
return
if(a%2==1 and b%2==1 and a<b): # 5 7
print("2\n")
return
if(a%2==0 and b%2==1 and a<b): # 4 7
print("1\n")
return
if(a%2==0 and b%2==0 and a<b): # 4 8
print("2\n")
return
if (a % 2 == 1 and b % 2 == 0 and a >b): # 13 8
print("2\n")
return
if (a % 2 == 1 and b % 2 == 1 and a > b): # 13 7
print("1\n")
return
if (a % 2 == 0 and b % 2 == 1 and a > b): # 14 7
print("2\n")
return
if (a % 2 == 0 and b % 2 == 0 and a > b):
print("1\n")
return
tests=int(input())
for i in range(0,tests):
process()
``` | output | 1 | 78,471 | 12 | 156,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}. | instruction | 0 | 78,572 | 12 | 157,144 |
Tags: binary search, greedy, sortings
Correct Solution:
```
n,k = map(int,input().split())
arr = list(map(int,input().split()))
arr.sort()
s = set()
ans = 0
for c in arr:
if c%k==0 and c//k in s:
continue
else:
ans+=1
s.add(c)
print(ans)
``` | output | 1 | 78,572 | 12 | 157,145 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}. | instruction | 0 | 78,573 | 12 | 157,146 |
Tags: binary search, greedy, sortings
Correct Solution:
```
from collections import defaultdict
n,k=[int(i) for i in input().strip().split()]
l=[int(i) for i in input().strip().split()]
if(k==1):
print(n)
else:
l.sort()
ndict=defaultdict(list)
for x in l:
i=x
while(i%k==0):
i=i/k
ndict[i].append(x)
ans=0
for i in ndict.values():
count=0
while(count<len(i)):
if(count==len(i)-1):
ans+=1
break
if(i[count]*k!=i[count+1]):
ans+=1
count+=1
else:
ans+=1
count+=2
print(ans)
``` | output | 1 | 78,573 | 12 | 157,147 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}. | instruction | 0 | 78,576 | 12 | 157,152 |
Tags: binary search, greedy, sortings
Correct Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d={}
c=1
p=[0]*n
d[l[0]]=0
p[0]=1
for i in range(1,n):
a=c
if l[i]%k==0 and (l[i]//k in d):
a=d[l[i]//k]
c-=1
d[l[i]]=a
p[a]+=1
c+=1
s=0
for i in range(n):
if p[i]==0:
break
s+=(p[i]//2)
if p[i]%2!=0:
s+=1
print(s)
``` | output | 1 | 78,576 | 12 | 157,153 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}. | instruction | 0 | 78,577 | 12 | 157,154 |
Tags: binary search, greedy, sortings
Correct Solution:
```
from sys import stdin
def main():
n, k = map(int, stdin.readline().split())
ar = list(map(int, stdin.readline().split()))
if k == 1:
print(n)
else:
ar.sort()
lk = set()
check = {}
for elm in ar:
lk.add(elm)
check[elm] = False
ans = 0
max_val = ar[-1]
for i in range(n):
if not check[ar[i]]:
cnt = 0
curr = ar[i]
while curr <= max_val and curr in lk:
cnt += 1
check[curr] = True
curr = curr * k
ans += (cnt // 2) + (cnt % 2)
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 78,577 | 12 | 157,155 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}. | instruction | 0 | 78,578 | 12 | 157,156 |
Tags: binary search, greedy, sortings
Correct Solution:
```
# Problem: A. k-Multiple Free Set
# Contest: Codeforces - Codeforces Round #168 (Div. 1)
# URL: https://codeforces.com/problemset/problem/274/A
# Memory Limit: 256 MB
# Time Limit: 2000 ms
#
# KAPOOR'S
from sys import stdin, stdout
def INI():
return int(stdin.readline())
def INL():
return [int(_) for _ in stdin.readline().split()]
def INS():
return stdin.readline()
def MOD():
return pow(10,9)+7
def OPS(ans):
stdout.write(str(ans)+"\n")
def OPL(ans):
[stdout.write(str(_)+" ") for _ in ans]
stdout.write("\n")
from bisect import bisect_left
if __name__=="__main__":
n,k=INL()
X=sorted(INL())
BOOL=[True for _ in range(n)]
for _ in range(n):
if BOOL[_]==True and k*X[_]<=X[-1] and k!=1:
i=bisect_left(X,k*X[_])
if X[i]==k*X[_]:
BOOL[i]=False
OPS(BOOL.count(True))
``` | output | 1 | 78,578 | 12 | 157,157 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}. | instruction | 0 | 78,579 | 12 | 157,158 |
Tags: binary search, greedy, sortings
Correct Solution:
```
import math
import random
from queue import Queue
import time
def main(arr,k):
set_arr=set(arr)
max_val=max(arr)
min_val=min(arr)
arr.sort()
if k==1:
return len(set_arr)
f={}
for e in arr:
if e not in f:
f[e]=0
f[e]+=1
ans=0
for e in arr:
if e in set_arr:
new_arr=[]
start_max=e
while start_max<=max_val:
if start_max in set_arr:
set_arr.remove(start_max)
new_arr.append(f[start_max])
else:
new_arr.append(0)
start_max*=k
dp=[[0 for j in range(2)] for i in range(len(new_arr))]
dp[0][0]=0
dp[0][1]=new_arr[0]
for i in range(1,len(dp)):
for j in range(len(dp[0])):
if j==0:
dp[i][j]=max(dp[i-1][0],dp[i-1][1])
else:
dp[i][j]=new_arr[i]+dp[i-1][0]
ans+=max(dp[-1])
return ans
n,k=list(map(int,input().split()))
arr=list(map(int,input().split()))
print(main(arr,k))
``` | output | 1 | 78,579 | 12 | 157,159 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.
Submitted Solution:
```
n = input()
n = n.split(' ')
(a,b) = (int(n[0]),int(n[1]))
A = [] #
S = [] # salidas
k=b #Multiplicador K
o = map(int,input().split(" "))
A = sorted(list(o))
#o = input()
#O = o.split(' ')
#for i in range(a): #Cantidad de nΓΊmeros
# A.append(int(o[i]))
for i in A:
if i % k!=0: # no es posible que y = xk
S.append(i)
elif i//k not in S: #de ser posible, x no se encuentra en la lista
S.append(i)
l = len(S)
print(l)
``` | instruction | 0 | 78,580 | 12 | 157,160 |
Yes | output | 1 | 78,580 | 12 | 157,161 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.
Submitted Solution:
```
n, k = map(int, input().split())
seq = sorted(int(c) for c in input().split())
seq_set = set()
for i, d in enumerate(seq):
if d % k:
seq_set.add(d)
else:
if d // k not in seq_set:
seq_set.add(d)
print(len(seq_set))
``` | instruction | 0 | 78,581 | 12 | 157,162 |
Yes | output | 1 | 78,581 | 12 | 157,163 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.
You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).
All the numbers in the lines are separated by single spaces.
Output
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.
Examples
Input
6 2
2 3 6 5 4 10
Output
3
Note
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.
Submitted Solution:
```
n,k=map(int,input().split())
a=sorted(map(int,input().split()))
t,r={},0
for i in a:
if i not in t:t[i]=True;r+=1
if t[i]:t[i*k]=False
print(r)
``` | instruction | 0 | 78,583 | 12 | 157,166 |
Yes | output | 1 | 78,583 | 12 | 157,167 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.