description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
import sys
input = sys.stdin.buffer.readline
def getCnts(j, l, r):
if l > r:
return 0
if l == 0:
return prefCnt[r][j]
else:
return prefCnt[r][j] - prefCnt[l - 1][j]
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
prefCnt = [[(0) for _a in range(27)] for _b in range(n)]
for j in range(1, 27):
cnts = 0
for i in range(n):
if a[i] == j:
cnts += 1
prefCnt[i][j] = cnts
ans = 0
for outer in range(1, 27):
outerLen = 0
l = -1
r = n
while l < r:
for inner in range(1, 27):
innerLen = getCnts(inner, l + 1, r - 1)
ans = max(ans, outerLen * 2 + innerLen)
l += 1
while l < n and a[l] != outer:
l += 1
r -= 1
while r > -1 and a[r] != outer:
r -= 1
outerLen += 1
print(ans)
|
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
import sys
class E2ThreeBlocksPalindromeHardVersion:
def solve(self):
for _ in range(int(input())):
n = int(input())
arr = [int(_) for _ in input().split()]
pos = {x: [] for x in arr}
for i, x in enumerate(arr):
pos[x].append(i)
ans = 1
for a in pos:
ln = len(pos[a])
for _x in range(ln // 2):
x = _x + 1
l, r = pos[a][_x] + 1, pos[a][ln - _x - 1] - 1
cnt = {item: (0) for item in arr[l : r + 1]}
for v in arr[l : r + 1]:
cnt[v] += 1
most_common = max(cnt.values()) if cnt.values() else 0
ans = max(ans, 2 * x + most_common)
print(ans)
solver = E2ThreeBlocksPalindromeHardVersion()
input = sys.stdin.readline
solver.solve()
|
IMPORT CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda: int(input())
si = lambda: input()
dgl = lambda: list(map(int, input()))
f = lambda: map(int, input().split())
il = lambda: list(map(int, input().split()))
ls = lambda: list(input())
let = "abcdefghijklmnopqrstuvwxyz"
for _ in range(ii()):
n = ii()
l = il()
cntr = dict()
mxcnt = 0
dp = [[(0) for i in range(201)] for j in range(n + 1)]
for i in range(n - 1, -1, -1):
for j in range(1, 201):
dp[i][j] = dp[i + 1][j] + (l[i] == j)
mxcnt = max(dp[i][l[i]], mxcnt)
x = str(dp[i][l[i]]) + "," + str(l[i])
cntr[x] = i
cntr2 = dict()
for i in range(n):
cntr2[l[i]] = cntr2.get(l[i], 0) + 1
x = str(cntr2[l[i]]) + "," + str(l[i])
if x in cntr and cntr[x] > i:
for j in range(1, 201):
mxcnt = max(cntr2[l[i]] * 2 + (dp[i + 1][j] - dp[cntr[x]][j]), mxcnt)
print(mxcnt)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR STRING FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
from sys import stdin
AI = 200
N = 2000
occ = [[(0) for i in range(N + 1)] for ai in range(AI)]
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
a = [0, *(int(x) - 1 for x in input().split())]
for ai in range(AI):
occ[ai][0] = 0
for i in range(1, n + 1):
occ[ai][i] = occ[ai][i - 1] + (a[i] == ai)
s = [[0, *(i for i in range(1, n + 1) if a[i] == ai)] for ai in range(AI)]
t = [[n, *(i - 1 for i in range(n, 0, -1) if a[i] == ai)] for ai in range(AI)]
ans = 0
for ai in range(AI):
for x in range(occ[ai][n] // 2 + 1):
for bi in range(AI):
ans = max(ans, 2 * x + occ[bi][t[ai][x]] - occ[bi][s[ai][x]])
print(ans)
|
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
P = [[] for _ in range(27)]
S = set(A)
for i, a in enumerate(A):
P[a].append(i)
ans = 0
for a in S:
for i in range(0, len(P[a]) // 2):
minp = P[a][i]
maxp = P[a][-(1 + i)]
s = 0
for b in S:
if a == b:
continue
s = max(s, sum([(p > minp and p < maxp) for p in P[b]]))
ans = max(ans, (i + 1) * 2 + s)
ans = max(ans, max([len(p) for p in P]))
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
for _ in range(int(input())):
n = int(input())
arr = list(map(lambda x: int(x) - 1, input().split()))
nxt = [([-1] * 26) for _ in range(n)]
prv = [([-1] * 26) for _ in range(n)]
fst = [-1] * 26
lst = [-1] * 26
initc = [0] * 26
for i in range(n):
initc[arr[i]] += 1
if fst[arr[i]] == -1:
fst[arr[i]] = i
else:
for j in range(i - 1, -1, -1):
if arr[j] == arr[i]:
prv[i][arr[i]] = j
nxt[j][arr[i]] = i
break
for i in range(n - 1, -1, -1):
if lst[arr[i]] == -1:
lst[arr[i]] = i
ans = 0
for i in range(26):
cnt = initc[:]
ans = max(cnt[i], ans)
cnt[i] = 0
if fst[i] == -1:
continue
l = fst[i]
r = lst[i]
if l == r:
ans = max(1, ans)
continue
real_l = 0
real_r = n - 1
prsf = 2
ans = max(ans, max(cnt))
while l < r:
while l > real_l:
cnt[arr[real_l]] -= 1
real_l += 1
while r < real_r:
cnt[arr[real_r]] -= 1
real_r -= 1
ans = max(ans, max(cnt) + prsf)
l = nxt[l][i]
r = prv[r][i]
prsf += 2
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
t = int(input())
for q in range(t):
n = int(input())
l = list(map(int, input().split()))
sl = [[] for i in range(201)]
ss = [[0] for i in range(201)]
for i in range(n):
sl[l[i]].append(i)
for j in range(201):
ss[j].append(ss[j][-1])
ss[l[i]][-1] += 1
ans = 0
for i in range(200):
if not sl[i]:
continue
if len(sl[i]) == 1:
ans = max(ans, 1)
continue
for j in range(len(sl[i]) // 2):
le = sl[i][j]
ri = sl[i][-1 - j]
for k in range(200):
if i == k:
ans = max(ans, len(sl[i]))
continue
if sl[k]:
ans = max(ans, 2 * (j + 1) + ss[k][ri] - ss[k][le])
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
for _ in range(int(input())):
n = int(input())
r = list(map(int, input().split()))
d = {i: [] for i in range(1, 27)}
for i in range(n):
d[r[i]].append(i)
ans = 1
for end in range(1, 27):
c = 1
while c * 2 <= len(d[end]):
ll, rr = d[end][c - 1] + 1, d[end][-c] - 1
dd = [0] * 27
for p in range(ll, rr + 1):
dd[r[p]] += 1
ans = max(ans, max(dd) + 2 * c)
c += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
from sys import stdin
def solve(tc):
n = int(stdin.readline().strip())
seq = list(map(int, stdin.readline().split()))
nums = [[] for i in range(200)]
for i in range(n):
nums[seq[i] - 1].append(i)
cum = [[(0) for i in range(200)] for j in range(n + 1)]
for i in range(n):
cum[i + 1][seq[i] - 1] += 1
for j in range(200):
cum[i + 1][j] += cum[i][j]
ans = 1
for x in range(200):
for y in range(200):
l = 0
r = len(nums[x]) - 1
xsize = 2
while l < r:
ysize = cum[nums[x][r]][y] - cum[nums[x][l] + 1][y]
ans = max(ans, xsize + ysize)
l += 1
r -= 1
xsize += 2
print(ans)
LOCAL_TEST = not __debug__
if LOCAL_TEST:
infile = __file__.split(".")[0] + "-test.in"
stdin = open(infile, "r")
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
solve(tc)
tc += 1
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
def distribution(n):
return (n - 1) // 2
def problem_A():
print(distribution(int(input())))
s = "abcdefghijklmnopqrstuvwxyz"
def cons_str(n, b):
a = s[:b]
ans = ""
while n > b:
ans += a
n -= b
ans += a[:n]
return ans
def problem_B():
n, a, b = list(map(int, input().split()))
print(cons_str(n, b))
def comp_teams(a):
if len(a) < 2:
return 0
s = len(set(a))
if s == len(a):
return 1
a.sort()
t = 0
temp = 1
for i in range(1, len(a)):
if a[i] == a[i - 1]:
temp += 1
else:
if t < temp:
t = temp
temp = 1
if t < temp:
t = temp
if s <= t - 1:
return s
if s == t:
return s - 1
else:
return t
def problem_C():
l = int(input())
a = list(map(int, input().split()))
print(comp_teams(a))
def antisudoku(a):
for i in range(9):
for j in range(9):
if a[i][j] == "2":
a[i][j] = "1"
return a
def problem_D():
a = []
for _ in range(9):
a.append(list(input()))
b = antisudoku(a)
for i in range(9):
print("".join(b[i]))
def three_block_palindrome(s):
a = []
b = []
c = []
l = len(s)
for _ in range(l):
a = []
for _ in range(l + 1):
a.append([0] * 26)
l += 1
for i in range(1, l):
k = s[i - 1] - 1
for j in range(26):
a[i][j] = a[i - 1][j]
a[i][k] += 1
maxlength = 0
for i in range(l):
for j in range(i, l):
maxa = 0
maxb = 0
for k in range(26):
if a[l - 1][k] - a[j][k] > a[i][k]:
if maxa < a[i][k]:
maxa = a[i][k]
elif maxa < a[l - 1][k] - a[j][k]:
maxa = a[l - 1][k] - a[j][k]
if maxb < a[j][k] - a[i][k]:
maxb = a[j][k] - a[i][k]
if maxlength < 2 * maxa + maxb:
maxlength = 2 * maxa + maxb
return maxlength
def problem_E1():
l = int(input())
s = list(map(int, input().split()))
print(three_block_palindrome(s))
cases = int(input())
for _ in range(cases):
problem_E1()
|
FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR STRING WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
import sys
lines = sys.stdin.readlines()
T = int(lines[0].strip())
for t in range(T):
n = int(lines[2 * t + 1].strip())
nums = list(map(int, lines[2 * t + 2].strip().split(" ")))
preSum = {}
kinds = set(nums)
for k in kinds:
preSum[k] = [0]
for num in nums:
for k in kinds:
if k == num:
preSum[k].append(preSum[k][-1] + 1)
else:
preSum[k].append(preSum[k][-1])
maxLen = 0
for k in kinds:
maxLen = max(maxLen, preSum[k][-1])
for k in kinds:
l = 0
r = n - 1
cnt = 0
while l < r:
while nums[l] != k:
l += 1
while nums[r] != k:
r -= 1
if l >= r:
break
cnt += 1
tmpMax = 0
for p in kinds:
tmpMax = max(tmpMax, preSum[p][r] - preSum[p][l + 1])
maxLen = max(maxLen, tmpMax + cnt * 2)
l += 1
r -= 1
print(maxLen)
|
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER STRING ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
def match(a, x, s, e):
matches = []
while s <= e:
while s <= e and a[s] != x:
s += 1
while s <= e and a[e] != x:
e -= 1
if s <= e:
matches.append((s, e))
s += 1
e -= 1
return matches
def solve(a):
ps = [[0] * 27]
for x in a:
p = ps[-1][:]
p[x] += 1
ps.append(p)
sol = 1
for first in range(1, 27):
matches = match(a, first, 0, len(a) - 1)
for i, (s, e) in enumerate(matches):
if s != e:
sol = max(sol, 2)
for second in range(1, 27):
total2 = ps[e][second] - ps[s + 1][second]
if total2 > 0:
sol = max(sol, (i + 1) * 2 + total2)
return sol
t = int(input())
for _ in range(t):
input()
a = list(map(int, input().split()))
print(solve(a))
|
FUNC_DEF ASSIGN VAR LIST WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
def three_blocks_length(sequence, outer, inner):
filtered = list(filter(lambda x: x == outer or x == inner, sequence))
max_length = 0
outer_indices = []
inner_indices = []
for i, item in enumerate(filtered):
if item == outer:
outer_indices.append(i)
if item == inner:
inner_indices.append(i)
for i in range(len(outer_indices) // 2):
inner_count = filtered[
outer_indices[i] : outer_indices[len(outer_indices) - i - 1]
].count(inner)
max_length = max(max_length, 2 * (i + 1) + inner_count)
return max_length
def solve(sequence):
distinct = set(sequence)
max_length = 0
for outer in distinct:
for inner in distinct:
if outer != inner:
max_length = max(
max_length, three_blocks_length(sequence, outer, inner)
)
else:
max_length = max(max_length, sequence.count(outer))
return max_length
for i in range(int(input())):
n = int(input())
sequence = [int(x) for x in input().split()]
print(solve(sequence))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
import sys
sys.setrecursionlimit(50000)
input = sys.stdin.readline
def input_list():
return map(int, input().split())
def r(n_i, n_j, i, j, m=[]):
n = n_i + n_j
if n_i == 0:
return n_j
elif n_j == 0:
return n_i
elif m[0] == m[n - 1] and m[0] == i:
return 2 + r(n_i - 2, n_j, i, j, m[1 : n - 1])
elif m[0] == j:
return max(n_j, r(n_i, n_j - 1, i, j, m[1:n]))
else:
return max(n_j, r(n_i, n_j - 1, i, j, m[0 : n - 1]))
def f():
n = int(input())
l = list(input_list())
f_result = 0
s = set(l)
for i in s:
for j in s:
m = []
n_i = 0
n_j = 0
for k in l:
if k == i:
m.append(k)
n_i += 1
elif k == j:
m.append(k)
n_j += 1
f_result = max(f_result, r(n_i, n_j, i, j, m))
print(f_result)
def main():
t = int(input())
for _ in range(t):
f()
main()
|
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF LIST ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
from sys import stdin
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
d = [[(0) for _ in range(26)] for _ in range(n)]
for i in range(n):
x = a[i] - 1
if i != 0:
for j in range(26):
d[i][j] = d[i - 1][j]
d[i][x] += 1
ans = 1
for i in range(n):
for j in range(i, n):
if a[i] != a[j]:
continue
x = a[i] - 1
for k in range(26):
if k != x:
ans = max(
d[j][x] - d[i][x] + 1 + 2 * min(d[i][k], d[n - 1][k] - d[j][k]),
ans,
)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
t = int(input())
a = list()
dp = list()
def getInd(j, i):
global n, a, dp
for k in range(n - 1, i, -1):
bias = 0
if a[k] == j + 1:
bias = 1
if dp[j][n - 1] - dp[j][k] + bias == dp[j][i]:
return k
return -1
def getMax(j, i, ind):
global dp
if ind - i == 1:
return 0
m = 0
for it in range(26):
m = max(m, dp[it][ind - 1] - dp[it][i])
return m
for _ in range(t):
dp = []
n = int(input())
for i in range(26):
dp.append([(0) for j in range(n)])
a = list(map(int, input().split()))
for i in range(n):
for j in range(1, 27):
if j == a[i]:
dp[j - 1][i] = dp[j - 1][i - 1] + 1
else:
dp[j - 1][i] = dp[j - 1][i - 1]
ans = 1
for i in range(n - 1):
for j in range(26):
if dp[j][i] <= 0 or dp[j][i] == dp[j][i - 1]:
continue
ind = getInd(j, i)
if ind != -1:
m = getMax(j, i, ind)
ans = max(ans, 2 * dp[j][i] + m)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = {i: [] for i in range(1, 27)}
i = 0
for x in a:
d[x].append(i)
i += 1
ans = 1
for x in d:
b = d[x]
l = len(b)
for i in range(0, l // 2):
x, y = b[i] + 1, b[-i - 1]
f = [0] * 27
for j in range(x, y):
f[a[j]] += 1
ans = max(ans, max(f) + 2 * (i + 1))
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
import sys
try:
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
except:
pass
def max_freq(l, r):
d = {}
for i in range(l, r + 1):
if arr[i] in d:
d[arr[i]] += 1
else:
d[arr[i]] = 1
return max(d.values()) if len(d) > 0 else 0
def palin(st, var):
i = st
j = n - 1
match = 0
stp = -1
end = -1
lmax = 0
while i < j:
if arr[i] != var:
i += 1
elif arr[j] != var:
j -= 1
elif arr[i] == arr[j] == var:
match += 2
stp = i
end = j
i += 1
j -= 1
lmax = max(lmax, match + max_freq(i, j))
return lmax
input = sys.stdin.readline
for tt in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
if n == 1:
print(1)
continue
check = set()
ans = 0
for i in range(n):
if arr[i] not in check:
check.add(arr[i])
ans = max(ans, palin(i, arr[i]))
if ans == 0:
print(1)
else:
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
t = int(input())
u = 0
while u < t:
n = int(input())
a = list(map(int, input().split()))
rama = [([0] * n) for i in range(27)]
sita = [[] for i in range(27)]
for i in range(n):
if i > 0:
s = a[i]
for j in range(27):
if j == s:
rama[a[i]][i] = rama[a[i]][i - 1] + 1
sita[a[i]].append(i)
else:
rama[j][i] = rama[j][i - 1]
else:
rama[a[i]][i] += 1
sita[a[i]].append(i)
result = 0
for i in range(n):
d = rama[a[i]][i]
e = sita[a[i]][-d]
if e > i:
maxi = 0
for j in range(27):
maxi = max(maxi, rama[j][e - 1] - rama[j][i])
result = max(2 * d + maxi, result)
if result == 0:
print("1")
else:
print(result)
u += 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
b = [[(0) for i in range(201)] for j in range(200005)]
for _ in range(int(input())):
n, a, maximum = int(input()), list(map(int, input().split())), 1
a = [0] + a
for x in range(1, n + 1):
for y in range(1, 201):
b[x][y] = b[x - 1][y]
b[x][a[x]] += 1
s = set(a)
for i in s:
start, end, count = 1, n, 0
while start < end:
while start < end and a[start] != i:
start += 1
while start < end and a[end] != i:
end -= 1
if start < end:
count += 2
for j in range(1, 201):
maximum = max(maximum, count + b[end - 1][j] - b[start][j])
start += 1
end -= 1
print(maximum)
|
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
def solve(arr, n, ans):
dp = [[(0) for i in range(27)] for j in range(n)]
for i in range(n):
dp[i][arr[i]] += 1
if i > 0:
for j in range(27):
dp[i][j] += dp[i - 1][j]
max_val = 0
for i in range(n):
left = dp[i][:]
for j in range(i + 1, n):
total = 0
counts = dp[j - 1][:]
for k in range(27):
counts[k] -= dp[i][k]
total += max(counts)
right = dp[-1][:]
for k in range(27):
right[k] -= dp[j - 1][k]
max_curr = 0
for k in range(27):
max_curr = max(max_curr, 2 * min(left[k], right[k]))
total += max_curr
max_val = max(max_val, total)
max_val = max(max_val, max(left))
ans.append(max_val)
def main():
t = int(input())
ans = []
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
solve(arr, n, ans)
for i in ans:
print(i)
main()
|
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
import sys
Ri = lambda: [int(x) for x in sys.stdin.readline().split()]
ri = lambda: sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
for _ in range(int(ri())):
n = int(ri())
a = Ri()
count = list2d(27, len(a) + 1, 0)
for i in range(len(a)):
for j in range(1, 27):
count[j][i + 1] = count[j][i]
count[a[i]][i + 1] += 1
ans = -1
for i in range(len(a)):
for j in range(i + 1, len(a)):
if a[i] == a[j]:
left = count[a[i]][i + 1]
right = count[a[i]][len(a)] - count[a[i]][j]
maxx = 0
for k in range(1, 27):
if k != a[i]:
cnt = count[k][j + 1] - count[k][i]
maxx = max(maxx, cnt)
ans = max(ans, min(left, right) * 2 + maxx)
for i in range(1, 27):
ans = max(ans, count[i][n])
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
def r(a, b):
n = len(a)
s = len(a) // 2
j = s - 1
v = -1
while j >= 0:
x = a[j]
y = a[n - 1 - j]
c = b[y] - b[x + 1]
s = 2 * (j + 1) + c
if s > v:
v = s
j += -1
return v
t = int(input())
for i in range(t):
n = int(input())
b = list(map(int, input().split()))
a = []
c = dict()
for j in range(1, 201):
c[j] = 0
for j in range(202):
a.append([])
for j in range(n):
a[b[j]].append(j)
c[b[j]] = 1
pre = [[0] for i in range(201)]
for i in range(1, 201):
if c[i] == 1:
y = a[i]
d = dict()
for j in y:
d[j] = 1
j = 0
while j < n:
if j in d.keys():
pre[i].append(pre[i][-1] + 1)
else:
pre[i].append(pre[i][-1])
j += 1
m = -1
d = []
for j in range(1, 201):
if c[j] == 1:
d.append(j)
for j in d:
for p in d:
if p == j:
h = len(a[p])
else:
h = r(a[j], pre[p])
if h > m:
m = h
print(m)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
The only difference between easy and hard versions is constraints.
You are given a sequence $a$ consisting of $n$ positive integers.
Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $a$ and $b$, $a$ can be equal $b$) and is as follows: $[\underbrace{a, a, \dots, a}_{x}, \underbrace{b, b, \dots, b}_{y}, \underbrace{a, a, \dots, a}_{x}]$. There $x, y$ are integers greater than or equal to $0$. For example, sequences $[]$, $[2]$, $[1, 1]$, $[1, 2, 1]$, $[1, 2, 2, 1]$ and $[1, 1, 2, 1, 1]$ are three block palindromes but $[1, 2, 3, 2, 1]$, $[1, 2, 1, 2, 1]$ and $[1, 2]$ are not.
Your task is to choose the maximum by length subsequence of $a$ that is a three blocks palindrome.
You have to answer $t$ independent test cases.
Recall that the sequence $t$ is a a subsequence of the sequence $s$ if $t$ can be derived from $s$ by removing zero or more elements without changing the order of the remaining elements. For example, if $s=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 2000$) β the number of test cases. Then $t$ test cases follow.
The first line of the test case contains one integer $n$ ($1 \le n \le 2000$) β the length of $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 26$), where $a_i$ is the $i$-th element of $a$. Note that the maximum value of $a_i$ can be up to $26$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ ($\sum n \le 2000$).
-----Output-----
For each test case, print the answer β the maximum possible length of some subsequence of $a$ that is a three blocks palindrome.
-----Example-----
Input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
Output
7
2
4
1
1
3
|
for _ in range(int(input())):
n = int(input())
l = list(map(lambda x: int(x) - 1, input().split()))
pref = {i: [] for i in range(26)}
cnt = [(0) for i in range(26)]
counter = []
for i in range(n):
pref[l[i]].append(i)
cnt[l[i]] += 1
counter.append(cnt.copy())
ans = 1
for i in range(26):
for j in range(1, cnt[i] // 2 + 1):
r, l = pref[i][-j], pref[i][j - 1]
curr = 0
for x in range(26):
curr = max(curr, counter[r - 1][x] - counter[l][x])
ans = max(ans, j * 2 + curr)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ segments on a $Ox$ axis $[l_1, r_1]$, $[l_2, r_2]$, ..., $[l_n, r_n]$. Segment $[l, r]$ covers all points from $l$ to $r$ inclusive, so all $x$ such that $l \le x \le r$.
Segments can be placed arbitrarily Β β be inside each other, coincide and so on. Segments can degenerate into points, that is $l_i=r_i$ is possible.
Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if $n=3$ and there are segments $[3, 6]$, $[100, 100]$, $[5, 8]$ then their union is $2$ segments: $[3, 8]$ and $[100, 100]$; if $n=5$ and there are segments $[1, 2]$, $[2, 3]$, $[4, 5]$, $[4, 6]$, $[6, 6]$ then their union is $2$ segments: $[1, 3]$ and $[4, 6]$.
Obviously, a union is a set of pairwise non-intersecting segments.
You are asked to erase exactly one segment of the given $n$ so that the number of segments in the union of the rest $n-1$ segments is maximum possible.
For example, if $n=4$ and there are segments $[1, 4]$, $[2, 3]$, $[3, 6]$, $[5, 7]$, then: erasing the first segment will lead to $[2, 3]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the second segment will lead to $[1, 4]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the third segment will lead to $[1, 4]$, $[2, 3]$, $[5, 7]$ remaining, which have $2$ segments in their union; erasing the fourth segment will lead to $[1, 4]$, $[2, 3]$, $[3, 6]$ remaining, which have $1$ segment in their union.
Thus, you are required to erase the third segment to get answer $2$.
Write a program that will find the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly $n-1$ segments.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$)Β β the number of test cases in the test. Then the descriptions of $t$ test cases follow.
The first of each test case contains a single integer $n$ ($2 \le n \le 2\cdot10^5$)Β β the number of segments in the given set. Then $n$ lines follow, each contains a description of a segment β a pair of integers $l_i$, $r_i$ ($-10^9 \le l_i \le r_i \le 10^9$), where $l_i$ and $r_i$ are the coordinates of the left and right borders of the $i$-th segment, respectively.
The segments are given in an arbitrary order.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print $t$ integers β the answers to the $t$ given test cases in the order of input. The answer is the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
-----Example-----
Input
3
4
1 4
2 3
3 6
5 7
3
5 5
5 5
5 5
6
3 3
1 1
5 5
1 5
2 2
4 4
Output
2
1
5
|
import sys
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
intervals = [None] * n
for i in range(n):
intervals[i] = tuple([int(a) for a in sys.stdin.readline().split()])
intervals = list(zip(intervals, list(range(n))))
starts = sorted(intervals, key=lambda x: x[0][0])
ends = sorted(intervals, key=lambda x: x[0][1])
connects = [0] * n
gaps = 0
covering = set()
atS = 0
atE = 0
while atE < n:
if atS != n and ends[atE][0][1] >= starts[atS][0][0]:
if len(covering) == 1:
gap = list(covering)[0]
connects[gap] += 0.5
covering.add(starts[atS][1])
atS += 1
if len(covering) == 1:
gap = list(covering)[0]
connects[gap] -= 0.5
else:
if len(covering) == 1:
gap = list(covering)[0]
connects[gap] -= 0.5
covering.remove(ends[atE][1])
atE += 1
if len(covering) == 1:
gap = list(covering)[0]
connects[gap] += 0.5
if len(covering) == 0:
gaps += 1
connects = [int(a) for a in connects]
print(max(connects) + gaps)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
|
There are $n$ segments on a $Ox$ axis $[l_1, r_1]$, $[l_2, r_2]$, ..., $[l_n, r_n]$. Segment $[l, r]$ covers all points from $l$ to $r$ inclusive, so all $x$ such that $l \le x \le r$.
Segments can be placed arbitrarily Β β be inside each other, coincide and so on. Segments can degenerate into points, that is $l_i=r_i$ is possible.
Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if $n=3$ and there are segments $[3, 6]$, $[100, 100]$, $[5, 8]$ then their union is $2$ segments: $[3, 8]$ and $[100, 100]$; if $n=5$ and there are segments $[1, 2]$, $[2, 3]$, $[4, 5]$, $[4, 6]$, $[6, 6]$ then their union is $2$ segments: $[1, 3]$ and $[4, 6]$.
Obviously, a union is a set of pairwise non-intersecting segments.
You are asked to erase exactly one segment of the given $n$ so that the number of segments in the union of the rest $n-1$ segments is maximum possible.
For example, if $n=4$ and there are segments $[1, 4]$, $[2, 3]$, $[3, 6]$, $[5, 7]$, then: erasing the first segment will lead to $[2, 3]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the second segment will lead to $[1, 4]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the third segment will lead to $[1, 4]$, $[2, 3]$, $[5, 7]$ remaining, which have $2$ segments in their union; erasing the fourth segment will lead to $[1, 4]$, $[2, 3]$, $[3, 6]$ remaining, which have $1$ segment in their union.
Thus, you are required to erase the third segment to get answer $2$.
Write a program that will find the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly $n-1$ segments.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$)Β β the number of test cases in the test. Then the descriptions of $t$ test cases follow.
The first of each test case contains a single integer $n$ ($2 \le n \le 2\cdot10^5$)Β β the number of segments in the given set. Then $n$ lines follow, each contains a description of a segment β a pair of integers $l_i$, $r_i$ ($-10^9 \le l_i \le r_i \le 10^9$), where $l_i$ and $r_i$ are the coordinates of the left and right borders of the $i$-th segment, respectively.
The segments are given in an arbitrary order.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print $t$ integers β the answers to the $t$ given test cases in the order of input. The answer is the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
-----Example-----
Input
3
4
1 4
2 3
3 6
5 7
3
5 5
5 5
5 5
6
3 3
1 1
5 5
1 5
2 2
4 4
Output
2
1
5
|
from sys import stdin
input = stdin.readline
q = int(input())
for rwere in range(q):
n = int(input())
seg = []
pts = []
for i in range(n):
pocz, kon = map(int, input().split())
seg.append([2 * pocz, 2 * kon])
pts.append(2 * kon + 1)
p, k = map(list, zip(*seg))
pts += p + k
pts.sort()
ind = -1
while True:
if pts[ind] == pts[-1]:
ind -= 1
else:
break
ind += 1
pts = pts[:ind]
mapa = {}
val = 0
mapa[pts[0]] = val
for i in range(1, len(pts)):
if pts[i] != pts[i - 1]:
val += 1
mapa[pts[i]] = val
val += 1
for i in range(n):
seg[i] = [mapa[seg[i][0]], mapa[seg[i][1]]]
seg.sort()
dupa = [0] * (val + 1)
for s in seg:
dupa[s[0]] += 1
dupa[s[1] + 1] -= 1
cov = [0] * val
cov[0] = dupa[0]
for i in range(1, val):
cov[i] = cov[i - 1] + dupa[i]
przyn = [0] * val
cur = seg[0][0]
label = 1
for i in range(n):
kon = seg[i][1]
if cur <= kon:
for j in range(cur, kon + 1):
przyn[j] = label
label += 1
cur = kon + 1
final = [
(przyn[i] if cov[i] == 1 else -1 if cov[i] == 0 else 0) for i in range(val)
]
baza = final.count(-1) + 1
final = [-1] + final + [-1]
val += 2
if max(final) <= 0:
print(baza)
else:
comp = {}
comp[0] = -100000000000
for i in final:
if i > 0:
comp[i] = 0
for i in range(1, val - 1):
if final[i] > 0 and final[i] != final[i - 1]:
comp[final[i]] += 1
if final[i - 1] == -1:
comp[final[i]] -= 1
if final[i + 1] == -1:
comp[final[i]] -= 1
best = -10000000000000
for i in comp:
best = max(best, comp[i])
if max(final) == n:
print(baza + best)
else:
print(max(baza + best, baza))
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR
|
There are $n$ segments on a $Ox$ axis $[l_1, r_1]$, $[l_2, r_2]$, ..., $[l_n, r_n]$. Segment $[l, r]$ covers all points from $l$ to $r$ inclusive, so all $x$ such that $l \le x \le r$.
Segments can be placed arbitrarily Β β be inside each other, coincide and so on. Segments can degenerate into points, that is $l_i=r_i$ is possible.
Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if $n=3$ and there are segments $[3, 6]$, $[100, 100]$, $[5, 8]$ then their union is $2$ segments: $[3, 8]$ and $[100, 100]$; if $n=5$ and there are segments $[1, 2]$, $[2, 3]$, $[4, 5]$, $[4, 6]$, $[6, 6]$ then their union is $2$ segments: $[1, 3]$ and $[4, 6]$.
Obviously, a union is a set of pairwise non-intersecting segments.
You are asked to erase exactly one segment of the given $n$ so that the number of segments in the union of the rest $n-1$ segments is maximum possible.
For example, if $n=4$ and there are segments $[1, 4]$, $[2, 3]$, $[3, 6]$, $[5, 7]$, then: erasing the first segment will lead to $[2, 3]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the second segment will lead to $[1, 4]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the third segment will lead to $[1, 4]$, $[2, 3]$, $[5, 7]$ remaining, which have $2$ segments in their union; erasing the fourth segment will lead to $[1, 4]$, $[2, 3]$, $[3, 6]$ remaining, which have $1$ segment in their union.
Thus, you are required to erase the third segment to get answer $2$.
Write a program that will find the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly $n-1$ segments.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$)Β β the number of test cases in the test. Then the descriptions of $t$ test cases follow.
The first of each test case contains a single integer $n$ ($2 \le n \le 2\cdot10^5$)Β β the number of segments in the given set. Then $n$ lines follow, each contains a description of a segment β a pair of integers $l_i$, $r_i$ ($-10^9 \le l_i \le r_i \le 10^9$), where $l_i$ and $r_i$ are the coordinates of the left and right borders of the $i$-th segment, respectively.
The segments are given in an arbitrary order.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print $t$ integers β the answers to the $t$ given test cases in the order of input. The answer is the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
-----Example-----
Input
3
4
1 4
2 3
3 6
5 7
3
5 5
5 5
5 5
6
3 3
1 1
5 5
1 5
2 2
4 4
Output
2
1
5
|
import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
n = int(input())
edges = []
for i in range(n):
li, ri = list(map(int, input().split()))
edges.append((li, 0, i))
edges.append((ri, 1, i))
edges.sort()
ctr = [0] * n
opened = set()
segmCtr = 0
for k, (e, isEnd, i) in enumerate(edges):
if isEnd:
opened.remove(i)
if not opened and edges[k - 1][2] == i:
ctr[i] -= 1
elif len(opened) == 1 and edges[k + 1][1] == 0:
for j in opened:
ctr[j] += 1
else:
if not opened:
segmCtr += 1
opened.add(i)
print(segmCtr + max(ctr))
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
There are $n$ segments on a $Ox$ axis $[l_1, r_1]$, $[l_2, r_2]$, ..., $[l_n, r_n]$. Segment $[l, r]$ covers all points from $l$ to $r$ inclusive, so all $x$ such that $l \le x \le r$.
Segments can be placed arbitrarily Β β be inside each other, coincide and so on. Segments can degenerate into points, that is $l_i=r_i$ is possible.
Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if $n=3$ and there are segments $[3, 6]$, $[100, 100]$, $[5, 8]$ then their union is $2$ segments: $[3, 8]$ and $[100, 100]$; if $n=5$ and there are segments $[1, 2]$, $[2, 3]$, $[4, 5]$, $[4, 6]$, $[6, 6]$ then their union is $2$ segments: $[1, 3]$ and $[4, 6]$.
Obviously, a union is a set of pairwise non-intersecting segments.
You are asked to erase exactly one segment of the given $n$ so that the number of segments in the union of the rest $n-1$ segments is maximum possible.
For example, if $n=4$ and there are segments $[1, 4]$, $[2, 3]$, $[3, 6]$, $[5, 7]$, then: erasing the first segment will lead to $[2, 3]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the second segment will lead to $[1, 4]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the third segment will lead to $[1, 4]$, $[2, 3]$, $[5, 7]$ remaining, which have $2$ segments in their union; erasing the fourth segment will lead to $[1, 4]$, $[2, 3]$, $[3, 6]$ remaining, which have $1$ segment in their union.
Thus, you are required to erase the third segment to get answer $2$.
Write a program that will find the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly $n-1$ segments.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$)Β β the number of test cases in the test. Then the descriptions of $t$ test cases follow.
The first of each test case contains a single integer $n$ ($2 \le n \le 2\cdot10^5$)Β β the number of segments in the given set. Then $n$ lines follow, each contains a description of a segment β a pair of integers $l_i$, $r_i$ ($-10^9 \le l_i \le r_i \le 10^9$), where $l_i$ and $r_i$ are the coordinates of the left and right borders of the $i$-th segment, respectively.
The segments are given in an arbitrary order.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print $t$ integers β the answers to the $t$ given test cases in the order of input. The answer is the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
-----Example-----
Input
3
4
1 4
2 3
3 6
5 7
3
5 5
5 5
5 5
6
3 3
1 1
5 5
1 5
2 2
4 4
Output
2
1
5
|
from sys import stdin
input = stdin.buffer.readline
t = int(input())
while t:
t -= 1
n = int(input())
seg = []
for i in range(n):
l, r = map(int, input().split())
seg.append((l, 0, i))
seg.append((r, 1, i))
seg.sort()
ans = 0
seq = []
active = set()
increase = [0] * -~n
for pos, p, i in seg:
if p == 0:
if len(seq) > 1 and seq[-2:] == [2, 1]:
increase[next(iter(active))] += 1
active.add(i)
else:
active.remove(i)
if len(active) == 0:
ans += 1
seq.append(len(active))
if max(seq) == 1:
print(ans - 1)
else:
print(ans + max(increase))
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
There are $n$ segments on a $Ox$ axis $[l_1, r_1]$, $[l_2, r_2]$, ..., $[l_n, r_n]$. Segment $[l, r]$ covers all points from $l$ to $r$ inclusive, so all $x$ such that $l \le x \le r$.
Segments can be placed arbitrarily Β β be inside each other, coincide and so on. Segments can degenerate into points, that is $l_i=r_i$ is possible.
Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if $n=3$ and there are segments $[3, 6]$, $[100, 100]$, $[5, 8]$ then their union is $2$ segments: $[3, 8]$ and $[100, 100]$; if $n=5$ and there are segments $[1, 2]$, $[2, 3]$, $[4, 5]$, $[4, 6]$, $[6, 6]$ then their union is $2$ segments: $[1, 3]$ and $[4, 6]$.
Obviously, a union is a set of pairwise non-intersecting segments.
You are asked to erase exactly one segment of the given $n$ so that the number of segments in the union of the rest $n-1$ segments is maximum possible.
For example, if $n=4$ and there are segments $[1, 4]$, $[2, 3]$, $[3, 6]$, $[5, 7]$, then: erasing the first segment will lead to $[2, 3]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the second segment will lead to $[1, 4]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the third segment will lead to $[1, 4]$, $[2, 3]$, $[5, 7]$ remaining, which have $2$ segments in their union; erasing the fourth segment will lead to $[1, 4]$, $[2, 3]$, $[3, 6]$ remaining, which have $1$ segment in their union.
Thus, you are required to erase the third segment to get answer $2$.
Write a program that will find the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly $n-1$ segments.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$)Β β the number of test cases in the test. Then the descriptions of $t$ test cases follow.
The first of each test case contains a single integer $n$ ($2 \le n \le 2\cdot10^5$)Β β the number of segments in the given set. Then $n$ lines follow, each contains a description of a segment β a pair of integers $l_i$, $r_i$ ($-10^9 \le l_i \le r_i \le 10^9$), where $l_i$ and $r_i$ are the coordinates of the left and right borders of the $i$-th segment, respectively.
The segments are given in an arbitrary order.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print $t$ integers β the answers to the $t$ given test cases in the order of input. The answer is the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
-----Example-----
Input
3
4
1 4
2 3
3 6
5 7
3
5 5
5 5
5 5
6
3 3
1 1
5 5
1 5
2 2
4 4
Output
2
1
5
|
def solve(lsts):
points = []
for i in range(len(lsts)):
points.append((lst[i][0], 0, i))
points.append((lst[i][1], 1, i))
points.sort()
open = set()
increased = [0] * len(lsts)
original = 0
for i in range(len(points)):
p = points[i]
if p[1] == 0:
open.add(p[2])
else:
open.remove(p[2])
if len(open) == 1 and p[1] == 1 and points[i + 1][1] == 0:
increased[list(open)[0]] += 1
if len(open) == 1 and p[1] == 0 and points[i + 1][1] == 1:
increased[list(open)[0]] -= 1
if len(open) == 0:
original += 1
res = -float("inf")
for i in range(len(lsts)):
res = max(res, increased[i])
return res + original
n = int(input())
for _ in range(n):
m = int(input())
lst = []
for _ in range(m):
lst.append(list(map(int, input().split())))
print(solve(lst))
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
There are $n$ segments on a $Ox$ axis $[l_1, r_1]$, $[l_2, r_2]$, ..., $[l_n, r_n]$. Segment $[l, r]$ covers all points from $l$ to $r$ inclusive, so all $x$ such that $l \le x \le r$.
Segments can be placed arbitrarily Β β be inside each other, coincide and so on. Segments can degenerate into points, that is $l_i=r_i$ is possible.
Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if $n=3$ and there are segments $[3, 6]$, $[100, 100]$, $[5, 8]$ then their union is $2$ segments: $[3, 8]$ and $[100, 100]$; if $n=5$ and there are segments $[1, 2]$, $[2, 3]$, $[4, 5]$, $[4, 6]$, $[6, 6]$ then their union is $2$ segments: $[1, 3]$ and $[4, 6]$.
Obviously, a union is a set of pairwise non-intersecting segments.
You are asked to erase exactly one segment of the given $n$ so that the number of segments in the union of the rest $n-1$ segments is maximum possible.
For example, if $n=4$ and there are segments $[1, 4]$, $[2, 3]$, $[3, 6]$, $[5, 7]$, then: erasing the first segment will lead to $[2, 3]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the second segment will lead to $[1, 4]$, $[3, 6]$, $[5, 7]$ remaining, which have $1$ segment in their union; erasing the third segment will lead to $[1, 4]$, $[2, 3]$, $[5, 7]$ remaining, which have $2$ segments in their union; erasing the fourth segment will lead to $[1, 4]$, $[2, 3]$, $[3, 6]$ remaining, which have $1$ segment in their union.
Thus, you are required to erase the third segment to get answer $2$.
Write a program that will find the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly $n-1$ segments.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^4$)Β β the number of test cases in the test. Then the descriptions of $t$ test cases follow.
The first of each test case contains a single integer $n$ ($2 \le n \le 2\cdot10^5$)Β β the number of segments in the given set. Then $n$ lines follow, each contains a description of a segment β a pair of integers $l_i$, $r_i$ ($-10^9 \le l_i \le r_i \le 10^9$), where $l_i$ and $r_i$ are the coordinates of the left and right borders of the $i$-th segment, respectively.
The segments are given in an arbitrary order.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print $t$ integers β the answers to the $t$ given test cases in the order of input. The answer is the maximum number of segments in the union of $n-1$ segments if you erase any of the given $n$ segments.
-----Example-----
Input
3
4
1 4
2 3
3 6
5 7
3
5 5
5 5
5 5
6
3 3
1 1
5 5
1 5
2 2
4 4
Output
2
1
5
|
debug = 0
t = int(input())
for _ in range(t):
n = int(input())
ev = []
for i in range(n):
a, b = map(int, input().split())
ev.append((a, -1, i))
ev.append((b, 1, i))
ev.sort()
a = set()
count = [0] * n
brks = 0
are_non_solo = 0
if debug:
print(ev)
for j in range(2 * n):
t, d, i = ev[j]
if debug:
print(a, ev[j])
if d == -1:
if a:
are_non_solo = 1
a.add(i)
else:
a.remove(i)
if j + 1 < 2 * n and ev[j + 1][0] != t:
if len(a) == 1 and ev[j + 1][1] == -1:
if debug:
print("Δ
")
for nn in a:
count[nn] += 1
if len(a) == 0:
brks += 1
print(max(count) + brks + are_non_solo)
|
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
from sys import stdin, stdout
def cheap_dinner(n1, n2, n3, n4, a_a, b_a, c_a, d_a, dic_a, dic_b, dic_c):
INF = 10**10
d_a.sort(key=lambda x: x[1])
for ci in range(1, n3 + 1):
found = False
for d in d_a:
if ci not in dic_c or d[0] + 1 not in dic_c[ci]:
c_a[ci - 1][1] += d[1]
found = True
break
if not found:
c_a[ci - 1][1] = INF
c_a.sort(key=lambda x: x[1])
for bi in range(1, n2 + 1):
found = False
for c in c_a:
if c[1] == INF:
break
if bi not in dic_b or c[0] + 1 not in dic_b[bi]:
b_a[bi - 1][1] += c[1]
found = True
break
if not found:
b_a[bi - 1][1] = INF
b_a.sort(key=lambda x: x[1])
r = INF
for ai in range(1, n1 + 1):
for b in b_a:
if b[1] == INF:
break
if ai not in dic_a or b[0] + 1 not in dic_a[ai]:
r = min(r, a_a[ai - 1][1] + b[1])
break
if r == INF:
return -1
else:
return r
n1, n2, n3, n4 = map(int, stdin.readline().split())
a_a = list(map(int, stdin.readline().split()))
b_a = list(map(int, stdin.readline().split()))
c_a = list(map(int, stdin.readline().split()))
d_a = list(map(int, stdin.readline().split()))
for i in range(n1):
a_a[i] = [i, a_a[i]]
for i in range(n2):
b_a[i] = [i, b_a[i]]
for i in range(n3):
c_a[i] = [i, c_a[i]]
for i in range(n4):
d_a[i] = [i, d_a[i]]
m1 = int(stdin.readline())
dic_a = {}
for _ in range(m1):
a, b = map(int, stdin.readline().split())
if a not in dic_a:
dic_a[a] = set()
dic_a[a].add(b)
m2 = int(stdin.readline())
dic_b = {}
for _ in range(m2):
b, c = map(int, stdin.readline().split())
if b not in dic_b:
dic_b[b] = set()
dic_b[b].add(c)
m3 = int(stdin.readline())
dic_c = {}
for _ in range(m3):
c, d = map(int, stdin.readline().split())
if c not in dic_c:
dic_c[c] = set()
dic_c[c].add(d)
r = cheap_dinner(n1, n2, n3, n4, a_a, b_a, c_a, d_a, dic_a, dic_b, dic_c)
stdout.write(str(r))
|
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.readline
input()
INF = 10**18
prev = list(map(int, input().split()))
As = [list(map(int, input().split())) for _ in range(3)]
for nex in As:
order = sorted(range(len(prev)), key=prev.__getitem__)
nadj = [set() for _ in range(len(nex))]
for _ in range(int(input())):
x, y = map(lambda s: int(s) - 1, input().split())
nadj[y].add(x)
for i in range(len(nex)):
j = 0
while j < len(order) and order[j] in nadj[i]:
j += 1
nex[i] = min(INF, nex[i] + (prev[order[j]] if j < len(order) else INF))
prev = nex
r = min(prev)
print(-1 if r == INF else r)
|
IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = lambda: sys.stdin.readline().strip()
n = list(map(int, input().split()))
a = []
for i in range(4):
a.append(list(map(int, input().split())))
def solve(l1, l2):
m = int(input())
not_together = [[i, set()] for i in range(n[l1])]
for i in range(m):
x, y = map(int, input().split())
x -= 1
y -= 1
not_together[x][1].add(y)
not_together = sorted(not_together, key=lambda x: a[l1][x[0]])
for i in range(n[l2]):
ans = float("inf")
for j in range(len(not_together)):
if i not in not_together[j][1]:
ans = a[l1][not_together[j][0]]
break
a[l2][i] += ans
solve(0, 1)
solve(1, 2)
solve(2, 3)
ans = min(a[3])
if ans == float("inf"):
print(-1)
else:
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.buffer.readline
INF = int(1000000000.0)
n = list(map(int, input().split()))
a = [list(map(int, input().split())) for i in range(4)]
dp = [[a[0][i], i] for i in range(n[0])]
dp.sort()
for i in range(3):
joint = [set() for j in range(n[i + 1])]
k = int(input())
for j in range(k):
x, y = map(int, input().split())
joint[y - 1].add(x - 1)
p = [INF for i in range(n[i + 1])]
for j in range(n[i + 1]):
for l in range(n[i]):
if dp[l][1] not in joint[j]:
p[j] = dp[l][0] + a[i + 1][j]
break
dp = [[p[j], j] for j in range(n[i + 1])]
dp.sort()
if dp[0][0] >= INF:
print(-1)
else:
print(dp[0][0])
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
from sys import stdin, stdout
n1, n2, n3, n4 = [int(x) for x in stdin.readline().split()]
a = [int(x) for x in stdin.readline().split()]
b = [int(x) for x in stdin.readline().split()]
c = [int(x) for x in stdin.readline().split()]
d = [int(x) for x in stdin.readline().split()]
a = [[i, a[i]] for i in range(n1)]
a.sort(key=lambda x: x[1])
m1 = int(stdin.readline())
bad_match = [set() for _ in range(n2)]
for _ in range(m1):
X, Y = [int(x) for x in stdin.readline().split()]
bad_match[Y - 1].add(X - 1)
B = [[i, 400000001] for i in range(n2)]
for Y in range(n2):
for candidate in a:
if not candidate[0] in bad_match[Y]:
B[Y][1] = candidate[1] + b[Y]
break
B.sort(key=lambda x: x[1])
m2 = int(stdin.readline())
bad_match = [set() for _ in range(n3)]
for _ in range(m2):
X, Y = [int(x) for x in stdin.readline().split()]
bad_match[Y - 1].add(X - 1)
C = [[i, 400000001] for i in range(n3)]
for Y in range(n3):
for candidate in B:
if not candidate[0] in bad_match[Y]:
C[Y][1] = candidate[1] + c[Y]
break
C.sort(key=lambda x: x[1])
m3 = int(stdin.readline())
bad_match = [set() for _ in range(n4)]
for _ in range(m3):
X, Y = [int(x) for x in stdin.readline().split()]
bad_match[Y - 1].add(X - 1)
D = [400000001] * n4
for Y in range(n4):
for candidate in C:
if not candidate[0] in bad_match[Y]:
D[Y] = candidate[1] + d[Y]
break
minimum = min(D)
if minimum >= 400000001:
stdout.write(str(-1) + "\n")
else:
stdout.write(str(minimum) + "\n")
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
INF = 10**9
n1, n2, n3, n4 = map(int, input().split())
solution_a = [(int(x), i) for i, x in enumerate(input().split())]
(*b,) = map(int, input().split())
(*c,) = map(int, input().split())
(*d,) = map(int, input().split())
solution_a.sort()
b_a_denies = {}
for _ in " " * int(input()):
x, y = map(int, input().split())
b_a_denies.setdefault(y - 1, set())
b_a_denies[y - 1].add(x - 1)
for i in range(n2):
denied = b_a_denies.get(i, set())
for cost_a, idx in solution_a:
if idx not in denied:
b[i] += cost_a
break
else:
b[i] = INF
del solution_a, b_a_denies
solution_b = [(b[i], i) for i in range(n2)]
solution_b.sort()
c_b_denies = {}
for _ in " " * int(input()):
x, y = map(int, input().split())
c_b_denies.setdefault(y - 1, set())
c_b_denies[y - 1].add(x - 1)
for i in range(n3):
denied = c_b_denies.get(i, set())
for cost_b, idx in solution_b:
if idx not in denied:
c[i] += cost_b
break
else:
c[i] = INF
del solution_b, c_b_denies
solution_c = [(c[i], i) for i in range(n3)]
solution_c.sort()
d_c_denies = {}
for _ in " " * int(input()):
x, y = map(int, input().split())
d_c_denies.setdefault(y - 1, set())
d_c_denies[y - 1].add(x - 1)
for i in range(n4):
denied = d_c_denies.get(i, set())
for cost_c, idx in solution_c:
if idx not in denied:
d[i] += cost_c
break
else:
d[i] = INF
print(min(d) if min(d) < INF else -1)
|
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
n1, n2, n3, n4 = map(int, input().split())
a = []
[a.append([v, i]) for i, v in enumerate(map(int, input().split()))]
b = []
[b.append([v, i]) for i, v in enumerate(map(int, input().split()))]
c = []
[c.append([v, i]) for i, v in enumerate(map(int, input().split()))]
d = []
[d.append([v, i]) for i, v in enumerate(map(int, input().split()))]
ck1 = dict()
for _ in range(int(input())):
x, y = map(int, input().split())
ck1[y - 1] = ck1.get(y - 1, set())
ck1[y - 1].add(x - 1)
ck2 = dict()
for _ in range(int(input())):
x, y = map(int, input().split())
ck2[y - 1] = ck2.get(y - 1, set())
ck2[y - 1].add(x - 1)
ck3 = dict()
for _ in range(int(input())):
x, y = map(int, input().split())
ck3[y - 1] = ck3.get(y - 1, set())
ck3[y - 1].add(x - 1)
d.sort()
s = set()
[s.add(i) for i in range(len(c))]
for v, i in d:
if s:
t = set()
for j in s:
if i not in ck3 or j not in ck3[i]:
t.add(j)
c[j][0] += v
[s.remove(j) for j in t]
else:
break
if s:
for i in s:
c[i][0] = 0
c.sort()
s = set()
[s.add(i) for i in range(len(b))]
for v, i in c:
if not v:
continue
if s:
t = set()
for j in s:
if i not in ck2 or j not in ck2[i]:
t.add(j)
b[j][0] += v
[s.remove(j) for j in t]
else:
break
if s:
for i in s:
b[i][0] = 0
b.sort()
s = set()
[s.add(i) for i in range(len(a))]
for v, i in b:
if not v:
continue
if s:
t = set()
for j in s:
if i not in ck1 or j not in ck1[i]:
t.add(j)
a[j][0] += v
[s.remove(j) for j in t]
else:
break
if s:
for i in s:
a[i][0] = 0
ans = 100000000000
for v, i in a:
if v:
ans = min(ans, v)
if ans == 100000000000:
print(-1)
else:
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
def main():
n1, n2, n3, n4 = readIntArr()
a = readIntArr()
b = readIntArr()
c = readIntArr()
d = readIntArr()
adjBA = [set() for _ in range(n2)]
adjCB = [set() for _ in range(n3)]
adjDC = [set() for _ in range(n4)]
def fillADJ(adj):
m = int(input())
for _ in range(m):
u, v = readIntArr()
u -= 1
v -= 1
adj[v].add(u)
fillADJ(adjBA)
fillADJ(adjCB)
fillADJ(adjDC)
minB = [inf for _ in range(n2)]
minC = [inf for _ in range(n3)]
minD = [inf for _ in range(n4)]
def populateDP(costArr, adj, dp, nextCostArr):
costSorted = [[cost, i] for i, cost in enumerate(costArr)]
costSorted.sort(key=lambda x: x[0])
for i in range(len(adj)):
for cost, j in costSorted:
if j not in adj[i]:
dp[i] = cost
break
for i, cost in enumerate(nextCostArr):
dp[i] += cost
populateDP(a, adjBA, minB, b)
populateDP(minB, adjCB, minC, c)
populateDP(minC, adjDC, minD, d)
ans = min(minD)
if ans == inf:
print(-1)
else:
print(ans)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
inf = float("inf")
MOD = 10**9 + 7
main()
|
IMPORT FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.buffer.readline
inf = 10**18
n1, n2, n3, n4 = map(int, input().split())
X = list(map(int, input().split()))
X = [(i, X[i]) for i in range(n1)]
X.sort(key=lambda x: x[1])
Y = list(map(int, input().split()))
Z = list(map(int, input().split()))
W = list(map(int, input().split()))
(m1,) = map(int, input().split())
V = [set() for _ in range(n2)]
for _ in range(m1):
a, b = map(int, input().split())
V[b - 1].add(a - 1)
for i in range(n2):
for j, x in X:
if j not in V[i]:
Y[i] += x
break
else:
Y[i] = inf
Y = [(i, Y[i]) for i in range(n2)]
Y.sort(key=lambda x: x[1])
(m1,) = map(int, input().split())
V = [set() for _ in range(n3)]
for _ in range(m1):
a, b = map(int, input().split())
V[b - 1].add(a - 1)
for i in range(n3):
for j, x in Y:
if j not in V[i]:
Z[i] += x
break
else:
Z[i] = inf
Z = [(i, Z[i]) for i in range(n3)]
Z.sort(key=lambda x: x[1])
(m1,) = map(int, input().split())
V = [set() for _ in range(n4)]
for _ in range(m1):
a, b = map(int, input().split())
V[b - 1].add(a - 1)
for i in range(n4):
for j, x in Z:
if j not in V[i]:
W[i] += x
break
else:
W[i] = inf
W.sort()
if W[0] >= inf:
print(-1)
else:
print(W[0])
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.readline
n = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = list(map(int, input().split()))
m1 = int(input())
set_xy1 = set()
for _ in range(m1):
x, y = map(int, input().split())
set_xy1.add((x - 1, y - 1))
m2 = int(input())
set_xy2 = set()
for _ in range(m2):
x, y = map(int, input().split())
set_xy2.add((x - 1, y - 1))
m3 = int(input())
set_xy3 = set()
for _ in range(m3):
x, y = map(int, input().split())
set_xy3.add((x - 1, y - 1))
sortedA = []
for ia, a in enumerate(A):
sortedA.append([a, ia])
sortedA.sort()
sortedB = []
for ib, b in enumerate(B):
for a, ia in sortedA:
if (ia, ib) in set_xy1:
continue
sortedB.append([a + b, ib])
break
sortedB.sort()
sortedC = []
for ic, c in enumerate(C):
for b, ib in sortedB:
if (ib, ic) in set_xy2:
continue
sortedC.append([b + c, ic])
break
sortedC.sort()
sortedD = []
for id, d in enumerate(D):
for c, ic in sortedC:
if (ic, id) in set_xy3:
continue
sortedD.append([c + d, id])
break
sortedD.sort()
if len(sortedD) == 0:
print(-1)
else:
print(sortedD[0][0])
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
mod = 1000000007
eps = 10**-9
inf = 10**15
def main():
import sys
input = sys.stdin.buffer.readline
def F(L, R, bad):
NL = len(L)
NR = len(R)
ret = [inf] * NR
L_sorted = [(l, i) for i, l in enumerate(L)]
L_sorted.sort(key=lambda x: x[0])
for i, r in enumerate(R):
for l, k in L_sorted:
if k * (NR + 1) + i in bad:
continue
else:
ret[i] = l + r
break
return ret
N1, N2, N3, N4 = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = list(map(int, input().split()))
M1 = int(input())
if M1 == N1 * N2:
print(-1)
exit()
bad1 = set()
for _ in range(M1):
x, y = map(int, input().split())
x -= 1
y -= 1
bad1.add(x * (N2 + 1) + y)
M2 = int(input())
if M2 == N2 * N3:
print(-1)
exit()
bad2 = set()
for _ in range(M2):
x, y = map(int, input().split())
x -= 1
y -= 1
bad2.add(x * (N3 + 1) + y)
M3 = int(input())
if M3 == N3 * N4:
print(-1)
exit()
bad3 = set()
for _ in range(M3):
x, y = map(int, input().split())
x -= 1
y -= 1
bad3.add(y * (N3 + 1) + x)
L = F(A, B, bad1)
R = F(D, C, bad3)
ans = min(F(L, R, bad2))
if ans < inf:
print(ans)
else:
print(-1)
main()
|
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
from sys import stdin
n1, n2, n3, n4 = map(int, stdin.readline().split())
p1 = sorted(zip(map(int, stdin.readline().split()), range(1, n1 + 1)))
p2 = sorted(zip(map(int, stdin.readline().split()), range(1, n2 + 1)))
p3 = sorted(zip(map(int, stdin.readline().split()), range(1, n3 + 1)))
p4 = sorted(zip(map(int, stdin.readline().split()), range(1, n4 + 1)))
m1 = int(stdin.readline())
b1 = set()
for _ in range(m1):
b1.add(tuple(map(int, stdin.readline().split())))
m2 = int(stdin.readline())
b2 = set()
for _ in range(m2):
b2.add(tuple(map(int, stdin.readline().split())))
m3 = int(stdin.readline())
b3 = set()
for _ in range(m3):
b3.add(tuple(map(int, stdin.readline().split())))
def f(pa, pb, b):
output = []
for j in range(len(pb)):
pricej, idxj = pb[j]
for i in range(len(pa)):
pricei, idxi = pa[i]
if (idxi, idxj) not in b:
output.append((pricei + pricej, idxj))
break
return sorted(output)
o1 = f(p1, p2, b1)
o2 = f(o1, p3, b2)
o3 = f(o2, p4, b3)
if o3:
print(min(o3)[0])
else:
print(-1)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
MAX_VAL = 2**31
def prepare_values(vals):
zipped_vals = list(zip(vals, range(1, len(vals) + 1)))
zipped_vals.sort()
pairs = [(old_i, i) for i, (val, old_i) in enumerate(zipped_vals, 1)]
return zipped_vals, dict(pairs)
def renumber_ids(dic, l):
return [dic[x] for x in l]
def get_first_absent(l):
l.sort()
i = 1
for x in l:
if i != x:
return i
i += 1
return i
def read_mismatches():
n = int(input())
mismatches = dict()
for _ in range(n):
x, y = map(int, input().split())
l = mismatches.get(x, [])
l.append(y)
mismatches[x] = l
return n, mismatches
def accumulate_cost(to_accumulate, from_accumulate, mismatches):
n = len(to_accumulate)
from_zipped, from_dic = prepare_values(from_accumulate)
for x in range(1, n + 1):
l = mismatches.get(x, [])
renumbered_l = renumber_ids(from_dic, l)
first_absent = get_first_absent(renumbered_l)
if first_absent > len(from_accumulate):
to_accumulate[x - 1] = MAX_VAL
else:
to_accumulate[x - 1] += from_zipped[first_absent - 1][0]
return 0
def solution():
n1, n2, n3, n4 = map(int, input().split())
first_course = list(map(int, input().split()))
second_course = list(map(int, input().split()))
drink = list(map(int, input().split()))
dessert = list(map(int, input().split()))
m1, fc_sc_mismatch = read_mismatches()
m2, sc_drink_mismatch = read_mismatches()
m3, drink_dessert_mismatch = read_mismatches()
accumulate_cost(drink, dessert, drink_dessert_mismatch)
accumulate_cost(second_course, drink, sc_drink_mismatch)
accumulate_cost(first_course, second_course, fc_sc_mismatch)
result = min(first_course)
print(-1 if result >= MAX_VAL else result)
solution()
|
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR RETURN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int, sys.stdin.readline().rstrip("\r\n").split()))
mod = 10**9 + 7
Mod = 998244353
INF = float("inf")
tc = 1
def solve(a, b, c):
ans = [INF for i in range(len(a))]
g = [set() for i in range(len(a))]
for i, j in c:
g[i - 1].add(j - 1)
bb = sorted([i for i in range(len(b))], key=lambda i: b[i])
for i in range(len(a)):
for k in bb:
if k not in g[i]:
ans[i] = a[i] + b[k]
break
return ans
for _ in range(tc):
n1, n2, n3, n4 = inp()
a1 = inp()
a2 = inp()
a3 = inp()
a4 = inp()
b1 = [inp() for i in range(int(input()))]
b2 = [inp() for i in range(int(input()))]
b3 = [inp() for i in range(int(input()))]
a3 = solve(a3, a4, b3)
a2 = solve(a2, a3, b2)
a1 = solve(a1, a2, b1)
print(min(a1) if min(a1) < INF else -1)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.readline
INF = 10**12
def update(al0, al1, gl01):
ail0 = [(al0[i], i) for i in range(len(al0))]
ail0.sort(key=lambda x: x[0])
res = [INF] * len(al1)
rems = list(range(len(al1)))
for a, i in ail0:
new_rems = []
for i1 in rems:
if i1 in gl01[i]:
new_rems.append(i1)
else:
res[i1] = a + al1[i1]
rems = new_rems[:]
for i in range(len(al1)):
al1[i] = res[i]
nl = list(map(int, input().split()))
al0 = list(map(int, input().split()))
al1 = list(map(int, input().split()))
al2 = list(map(int, input().split()))
al3 = list(map(int, input().split()))
g01 = [set() for _ in range(nl[0])]
g12 = [set() for _ in range(nl[1])]
g23 = [set() for _ in range(nl[2])]
for _ in range(int(input())):
a, b = map(int, input().split())
g01[a - 1].add(b - 1)
for _ in range(int(input())):
a, b = map(int, input().split())
g12[a - 1].add(b - 1)
for _ in range(int(input())):
a, b = map(int, input().split())
g23[a - 1].add(b - 1)
update(al0, al1, g01)
update(al1, al2, g12)
update(al2, al3, g23)
ans = min(al3)
if ans >= INF:
ans = -1
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.readline
def encode(x, y):
return (x - 1 << 18) + y
n1, n2, n3, n4 = map(int, input().split())
a = [(int(item), i) for i, item in enumerate(input().split())]
b = [(int(item), i) for i, item in enumerate(input().split())]
c = [(int(item), i) for i, item in enumerate(input().split())]
d = [(int(item), i) for i, item in enumerate(input().split())]
a.sort()
b.sort()
c.sort()
d.sort()
m1 = int(input())
m1s = set()
for _ in range(m1):
x, y = map(int, input().split())
x -= 1
y -= 1
m1s.add(encode(x, y))
m2 = int(input())
m2s = set()
for _ in range(m2):
x, y = map(int, input().split())
x -= 1
y -= 1
m2s.add(encode(x, y))
m3 = int(input())
m3s = set()
for _ in range(m3):
x, y = map(int, input().split())
x -= 1
y -= 1
m3s.add(encode(x, y))
ab = []
for bb, ib in b:
for aa, ia in a:
if encode(ia, ib) in m1s:
continue
ab.append((aa + bb, ib))
break
if not ab:
print(-1)
exit()
ab.sort()
bc = []
for cc, ic in c:
for aabb, ib in ab:
if encode(ib, ic) in m2s:
continue
bc.append((aabb + cc, ic))
break
if not bc:
print(-1)
exit()
bc.sort()
cd = []
ans = 10**10
for dd, idd in d:
for bbcc, ic in bc:
if encode(ic, idd) in m3s:
continue
if bbcc + dd < ans:
ans = bbcc + dd
break
if ans == 10**10:
print(-1)
else:
print(ans)
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.readline
def read_int():
return int(input())
def read_ints():
return map(int, input().split(" "))
INF = int(1000000000.0)
n = list(read_ints())
a = [[] for _ in range(4)]
a[0] = list(read_ints())
a[1] = list(read_ints())
a[2] = list(read_ints())
a[3] = list(read_ints())
dp = [(a[0][i], i) for i in range(n[0])]
dp.sort()
for k in range(3):
m = read_int()
banned = [set() for _ in range(n[k + 1])]
for _ in range(m):
u, v = read_ints()
banned[v - 1].add(u - 1)
f = [INF] * n[k + 1]
for i in range(n[k + 1]):
for j in range(n[k]):
if dp[j][1] not in banned[i]:
f[i] = dp[j][0] + a[k + 1][i]
break
dp = [(f[i], i) for i in range(n[k + 1])]
dp.sort()
print(dp[0][0] if dp[0][0] < INF else -1)
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = lambda: sys.stdin.readline().rstrip()
def ttt():
a, b = map(int, input().split())
return (a - 1 << 18) + b - 1
n1, n2, n3, n4 = map(int, input().split())
A = [(i, int(a)) for i, a in enumerate(input().split())]
B = [(i, int(a)) for i, a in enumerate(input().split())]
C = [(i, int(a)) for i, a in enumerate(input().split())]
D = [(i, int(a)) for i, a in enumerate(input().split())]
X = {ttt() for _ in range(int(input()))}
Y = {ttt() for _ in range(int(input()))}
Z = {ttt() for _ in range(int(input()))}
def calc(T, prev, NG):
prev = sorted(prev, key=lambda x: x[1])
for _ in range(len(T)):
i, a = T[_]
for j, b in prev:
if (j << 18) + i in NG:
continue
T[_] = i, a + b
break
else:
T[_] = i, 10**18
calc(B, A, X)
calc(C, B, Y)
calc(D, C, Z)
mi = min(z for i, z in D)
print(mi if mi < 10**15 else -1)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.buffer.readline
def init(N, node, A, unit, func):
n = 1
while n < N:
n <<= 1
for i in range(n * 2 - 1):
if len(node) <= i:
node.append(unit)
else:
node[i] = unit
for i in range(len(A)):
node[n - 1 + i] = A[i]
for i in range(n - 2, -1, -1):
node[i] = func(node[(i << 1) + 1], node[(i << 1) + 2])
node.append(func)
node.append(unit)
node.append(n)
def upd(node, x, a):
y = node[-1] + x
node[y - 1] = a
while y > 1:
y = y >> 1
node[y - 1] = node[-3](node[(y << 1) - 1], node[y << 1])
def query(node, l, r):
x, y = l, r
z = node[-1] - 1
rr = node[-2]
rl = node[-2]
while True:
if x == y:
return node[-3](rl, rr)
if x & 1:
rl = node[-3](rl, node[x + z])
x += 1
if y & 1:
rr = node[-3](node[y + z - 1], rr)
if z == 0:
return node[-3](rl, rr)
x >>= 1
y >>= 1
z >>= 1
INF = 10**17
N = list(map(int, input().split()))
C = [list(map(int, input().split())) for i in range(4)]
seg = [[] for i in range(4)]
for i in range(4):
if i:
init(N[i] + 1, seg[i], [], INF, lambda x, y: min(x, y))
else:
init(N[i] + 1, seg[i], C[i], INF, lambda x, y: min(x, y))
M = []
G = [[[] for j in range(N[i + 1])] for i in range(3)]
for i in range(3):
M.append(int(input()))
for j in range(M[i]):
a, b = map(int, input().split())
G[i][b - 1].append(a - 1)
for i in range(3):
for j in range(N[i + 1]):
Q = []
for k in range(len(G[i][j])):
Q.append(seg[i][G[i][j][k] + seg[i][-1] - 1])
upd(seg[i], G[i][j][k], INF)
upd(seg[i + 1], j, C[i + 1][j] + seg[i][0])
for k in range(len(G[i][j])):
upd(seg[i], G[i][j][k], Q[k])
if seg[3][0] >= INF:
print(-1)
else:
print(seg[3][0])
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR LIST VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
from sys import stdin
n = list(map(int, stdin.readline().split()))
a = [list(map(int, stdin.readline().split())) for i in range(4)]
v = [set([]) for i in range(3)]
for i in range(3):
m = int(stdin.readline())
for j in range(m):
x, y = map(int, stdin.readline().split())
x -= 1
y -= 1
v[i].add((x, y))
dp = [([float("inf")] * len(a[i])) for i in range(4)]
dp[0] = a[0]
for i in range(1, 4):
lai = [(dp[i - 1][j], j) for j in range(len(a[i - 1]))]
lai.sort()
remR = [i for i in range(len(a[i]))]
for cost, lv in lai:
nexR = []
for rv in remR:
if (lv, rv) not in v[i - 1]:
dp[i][rv] = cost + a[i][rv]
else:
nexR.append(rv)
remR = nexR
if len(remR) == 0:
break
ans = min(dp[-1])
if ans == float("inf"):
print(-1)
else:
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.readline
def main():
n = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
prices = [a, b, c, d]
inc = [{} for _ in range(3)]
for i in range(3):
ni = int(input())
for _ in range(ni):
k, l = map(int, input().split())
if k - 1 in inc[i]:
inc[i][k - 1].add(l - 1)
else:
inc[i][k - 1] = set([l - 1])
step = 3
mem2 = prices[3][:]
for step in range(3, 0, -1):
mem = mem2[:]
index_sorted = sorted(range(n[step]), key=lambda x: mem[x])
mem2 = [float("inf")] * n[step - 1]
for i in range(n[step - 1]):
index = 0
while (
i in inc[step - 1]
and index < n[step]
and index_sorted[index] in inc[step - 1][i]
):
index += 1
if index < n[step]:
mem2[i] = prices[step - 1][i] + mem[index_sorted[index]]
rep = min(mem2)
if rep < float("inf"):
print(rep)
else:
print(-1)
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR DICT VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
MAX_MIN_VAL = 10**9
def getSortedIndices(A, n):
P = []
for i in range(n):
P.append([A[i], i])
P = sorted(P)
return [i for a, i in P]
def getPositiveStartIndex(A, AIS, n):
l = 0
r = n - 1
while l < r:
m = (l + r) // 2
if A[AIS[m]] < 0:
l = m + 1
else:
r = m
return l
n1, n2, n3, n4 = map(int, input().split())
A = [MAX_MIN_VAL] + list(map(int, input().split()))
B = [MAX_MIN_VAL] + list(map(int, input().split()))
C = [MAX_MIN_VAL] + list(map(int, input().split()))
D = [MAX_MIN_VAL] + list(map(int, input().split()))
DS = getSortedIndices(D, n4 + 1)
m1 = int(input())
M1 = {}
for i in range(m1):
x, y = map(int, input().split())
M1[x] = M1.get(x, set())
M1[x].add(y)
m2 = int(input())
M2 = {}
for i in range(m2):
x, y = map(int, input().split())
M2[x] = M2.get(x, set())
M2[x].add(y)
m3 = int(input())
M3 = {}
for i in range(m3):
x, y = map(int, input().split())
M3[x] = M3.get(x, set())
M3[x].add(y)
minResult = MAX_MIN_VAL
dp3 = [MAX_MIN_VAL]
for i in range(1, n3 + 1):
dpVal = -1
if i not in M3:
dpVal = D[DS[0]]
elif len(M3[i]) != n4:
for j in DS:
if j not in M3[i]:
dpVal = D[j]
break
dp3.append(dpVal + C[i] if dpVal != -1 else -1)
dp3S = getSortedIndices(dp3, n3 + 1)
positiveStartIndex = getPositiveStartIndex(dp3, dp3S, n3 + 1)
min3 = -1 if positiveStartIndex == n3 else dp3[dp3S[positiveStartIndex]]
if min3 != -1:
dp2 = [MAX_MIN_VAL]
for i in range(1, n2 + 1):
dpVal = -1
if i not in M2:
dpVal = min3
elif len(M2[i]) != n3:
for j in range(positiveStartIndex, n3):
if dp3S[j] not in M2[i]:
dpVal = dp3[dp3S[j]]
break
dp2.append(dpVal + B[i] if dpVal != -1 else -1)
dp2S = getSortedIndices(dp2, n2 + 1)
positiveStartIndex = getPositiveStartIndex(dp2, dp2S, n2 + 1)
min2 = -1 if positiveStartIndex == n2 else dp2[dp2S[positiveStartIndex]]
if min2 != -1:
dp1 = [MAX_MIN_VAL]
for i in range(1, n1 + 1):
dpVal = -1
if i not in M1:
dpVal = min2
elif len(M1[i]) != n2:
for j in range(positiveStartIndex, n2):
if dp2S[j] not in M1[i]:
dpVal = dp2[dp2S[j]]
break
dp1.append(dpVal + A[i] if dpVal != -1 else -1)
minResult = min([v for v in dp1 if v != -1])
result = minResult if minResult != MAX_MIN_VAL else -1
print(result)
|
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ivan wants to have a good dinner. A good dinner should consist of a first course, a second course, a drink, and a dessert.
There are $n_1$ different types of first courses Ivan can buy (the $i$-th of them costs $a_i$ coins), $n_2$ different types of second courses (the $i$-th of them costs $b_i$ coins), $n_3$ different types of drinks (the $i$-th of them costs $c_i$ coins) and $n_4$ different types of desserts (the $i$-th of them costs $d_i$ coins).
Some dishes don't go well with each other. There are $m_1$ pairs of first courses and second courses that don't go well with each other, $m_2$ pairs of second courses and drinks, and $m_3$ pairs of drinks and desserts that don't go well with each other.
Ivan wants to buy exactly one first course, one second course, one drink, and one dessert so that they go well with each other, and the total cost of the dinner is the minimum possible. Help him to find the cheapest dinner option!
-----Input-----
The first line contains four integers $n_1$, $n_2$, $n_3$ and $n_4$ ($1 \le n_i \le 150000$) β the number of types of first courses, second courses, drinks and desserts, respectively.
Then four lines follow. The first line contains $n_1$ integers $a_1, a_2, \dots, a_{n_1}$ ($1 \le a_i \le 10^8$), where $a_i$ is the cost of the $i$-th type of first course. Three next lines denote the costs of second courses, drinks, and desserts in the same way ($1 \le b_i, c_i, d_i \le 10^8$).
The next line contains one integer $m_1$ ($0 \le m_1 \le 200000$) β the number of pairs of first and second courses that don't go well with each other. Each of the next $m_1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n_1$; $1 \le y_i \le n_2$) denoting that the first course number $x_i$ doesn't go well with the second course number $y_i$. All these pairs are different.
The block of pairs of second dishes and drinks that don't go well with each other is given in the same format. The same for pairs of drinks and desserts that don't go well with each other ($0 \le m_2, m_3 \le 200000$).
-----Output-----
If it's impossible to choose a first course, a second course, a drink, and a dessert so that they go well with each other, print $-1$. Otherwise, print one integer β the minimum total cost of the dinner.
-----Examples-----
Input
4 3 2 1
1 2 3 4
5 6 7
8 9
10
2
1 2
1 1
2
3 1
3 2
1
1 1
Output
26
Input
1 1 1 1
1
1
1
1
1
1 1
0
0
Output
-1
-----Note-----
The best option in the first example is to take the first course $2$, the second course $1$, the drink $2$ and the dessert $1$.
In the second example, the only pair of the first course and the second course is bad, so it's impossible to have dinner.
|
import sys
input = sys.stdin.readline
INF = int(1000000000.0)
n1, n2, n3, n4 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
postx = [[] for _ in range(n2)]
for _ in range(int(input())):
x, y = map(int, input().split())
postx[y - 1].append(x - 1)
prei = []
for i in range(n1):
prei.append((i, a[i]))
prei.sort(key=lambda x: x[1])
for i in range(n2):
if not postx[i]:
b[i] += prei[0][1]
else:
if len(postx[i]) == n1:
b[i] = INF
continue
for index, cost in prei:
if index in postx[i]:
continue
else:
b[i] += cost
break
postx = [[] for _ in range(n3)]
for _ in range(int(input())):
x, y = map(int, input().split())
postx[y - 1].append(x - 1)
prei = []
for i in range(n2):
prei.append((i, b[i]))
prei.sort(key=lambda x: x[1])
for i in range(n3):
if not postx[i]:
c[i] += prei[0][1]
else:
if len(postx[i]) == n2:
c[i] = INF
continue
for index, cost in prei:
if index in postx[i]:
continue
else:
c[i] += cost
break
postx = [[] for _ in range(n4)]
for _ in range(int(input())):
x, y = map(int, input().split())
postx[y - 1].append(x - 1)
prei = []
for i in range(n3):
prei.append((i, c[i]))
prei.sort(key=lambda x: x[1])
for i in range(n4):
if not postx[i]:
d[i] += prei[0][1]
else:
if len(postx[i]) == n3:
d[i] = INF
continue
for index, cost in prei:
if index in postx[i]:
continue
else:
d[i] += cost
break
if min(d) >= INF:
print(-1)
else:
print(min(d))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
You are given an array $a$ of $n$ non-negative integers. In one operation you can change any number in the array to any other non-negative integer.
Let's define the cost of the array as $\operatorname{DIFF}(a) - \operatorname{MEX}(a)$, where $\operatorname{MEX}$ of a set of non-negative integers is the smallest non-negative integer not present in the set, and $\operatorname{DIFF}$ is the number of different numbers in the array.
For example, $\operatorname{MEX}(\{1, 2, 3\}) = 0$, $\operatorname{MEX}(\{0, 1, 2, 4, 5\}) = 3$.
You should find the minimal cost of the array $a$ if you are allowed to make at most $k$ operations.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 10^5$, $0 \le k \le 10^5$) β the length of the array $a$ and the number of operations that you are allowed to make.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case output a single integer β minimal cost that it is possible to get making at most $k$ operations.
-----Examples-----
Input
4
4 1
3 0 1 2
4 1
0 2 4 5
7 2
4 13 0 0 13 1337 1000000000
6 2
1 2 8 0 0 0
Output
0
1
2
0
-----Note-----
In the first test case no operations are needed to minimize the value of $\operatorname{DIFF} - \operatorname{MEX}$.
In the second test case it is possible to replace $5$ by $1$. After that the array $a$ is $[0,\, 2,\, 4,\, 1]$, $\operatorname{DIFF} = 4$, $\operatorname{MEX} = \operatorname{MEX}(\{0, 1, 2, 4\}) = 3$, so the answer is $1$.
In the third test case one possible array $a$ is $[4,\, 13,\, 0,\, 0,\, 13,\, 1,\, 2]$, $\operatorname{DIFF} = 5$, $\operatorname{MEX} = 3$.
In the fourth test case one possible array $a$ is $[1,\, 2,\, 3,\, 0,\, 0,\, 0]$.
|
I = input
R = lambda: map(int, I().split())
for _ in range(int(I())):
n, k = R()
a = [*R()]
c = {}
mx = 0
for v in a:
c[v] = c.get(v, 0) + 1
while mx in c:
mx += 1
for _ in range(k):
mx += 1
while mx in c:
mx += 1
b = sorted([v for v in set(a) if v > mx], key=lambda x: c[x])
res = len(b)
for v in b:
if k >= c[v]:
k -= c[v]
res -= 1
else:
break
print(res)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
from sys import stdin
def binary_search(arr, n, k):
l, r = 0, n - 1
while l <= r:
mid = l + r >> 1
if arr[mid] <= k:
l = mid + 1
else:
r = mid - 1
return l
def main():
n = int(stdin.readline())
arr1 = list(map(int, stdin.readline().split()))
m = int(stdin.readline())
arr2 = list(map(int, stdin.readline().split()))
arr1.sort()
arr2.sort()
res1, res2 = 2 * n, 2 * m
for i in range(n):
sum1 = 2 * i + 3 * (n - i)
j = binary_search(arr2, m, arr1[i] - 1)
sum2 = 2 * j + 3 * (m - j)
if sum1 - sum2 > res1 - res2 or sum1 - sum2 == res1 - res2 and sum1 > res1:
res1, res2 = sum1, sum2
print("%d:%d" % (res1, res2))
main()
|
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
def main():
n = int(input())
t1 = [int(_) for _ in input().split()]
t1 = sorted(t1)
m = int(input())
t2 = [int(_) for _ in input().split()]
t2 = sorted(t2)
a = x = 3 * n
b = y = 3 * m
advantage = a - b
i = 0
j = 0
while i < n or j < m:
if i < n and j < m:
if t1[i] < t2[j]:
x -= 1
i += 1
elif t2[j] < t1[i]:
y -= 1
j += 1
else:
x -= 1
y -= 1
i += 1
j += 1
elif i < n:
x -= 1
i += 1
else:
y -= 1
j += 1
if x - y > advantage:
a = x
b = y
advantage = a - b
print("%d:%d" % (a, b))
def __starting_point():
main()
__starting_point()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
input = __import__("sys").stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
lis = [[i, 0] for i in a] + [[i, 1] for i in b]
lis.sort()
zer = one = aa = bb = 0
ans = -10000000000000000000
for i in range(len(lis)):
if lis[i][1] == 0:
zer += 1
else:
one += 1
tma = n * 3 - zer
tmb = m * 3 - one
if tma - tmb > ans:
ans = tma - tmb
aa = tma
bb = tmb
if n * 3 - m * 3 >= ans:
ans = n * 3 - m * 3
aa = n * 3
bb = m * 3
print(aa, ":", bb, sep="")
|
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
a.sort()
b.sort()
j, k = 0, 0
c = []
while j <= n and k <= m:
if j == n and k == m:
break
elif j == n:
c.append([b[k], 1])
k += 1
elif k == m:
c.append([a[j], 0])
j += 1
elif a[j] <= b[k]:
c.append([a[j], 0])
j += 1
elif a[j] >= b[k]:
c.append([b[k], 1])
k += 1
cnt_a = 0
cnt_b = 0
ans = 3 * n - 3 * m
res = [3 * n, 3 * m]
for i in range(len(c)):
num, id = c[i]
if id == 0:
cnt_a += 1
else:
cnt_b += 1
tmp_a = cnt_a * 2 + (n - cnt_a) * 3
tmp_b = cnt_b * 2 + (m - cnt_b) * 3
if ans < tmp_a - tmp_b:
res = [tmp_a, tmp_b]
ans = tmp_a - tmp_b
print(str(res[0]) + ":" + str(res[1]))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR LIST BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n1 = int(input())
d1 = list(map(lambda x: int(x), input().split(" ")))
n2 = int(input())
d2 = list(map(lambda x: int(x), input().split(" ")))
dl = list(set([0] + d1 + d2))
dl.sort()
dd = dict()
for i in range(0, len(dl)):
dd[dl[i]] = i
c1 = [(0) for i in range(0, len(dl))]
c2 = [(0) for i in range(0, len(dl))]
s1 = [(0) for i in range(0, len(dl))]
s2 = [(0) for i in range(0, len(dl))]
for d in d1:
c1[dd[d]] += 1
for d in d2:
c2[dd[d]] += 1
for i in range(len(dl) - 2, -1, -1):
s1[i] = s1[i + 1] + c1[i + 1]
s2[i] = s2[i + 1] + c2[i + 1]
maxsub = s1[0] - s2[0]
maxi = 0
maxa = s1[0]
for i in range(0, len(dl)):
sub = s1[i] - s2[i]
if sub > maxsub or sub == maxsub and s1[i] > maxa:
maxsub = sub
maxa = s1[i]
maxi = i
print(str(n1 * 2 + s1[maxi]) + ":" + str(n2 * 2 + s2[maxi]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR STRING FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
a = sorted([int(i) for i in input().split()])
m = int(input())
b = sorted([int(i) for i in input().split()])
ma = a1 = 3 * n
mb = b1 = 3 * m
rast = a1 - b1
i, j = 0, 0
while i < n and j < m:
z = min(a[i], b[j])
while i < n and a[i] <= z:
i += 1
ma -= 1
while j < m and b[j] <= z:
j += 1
mb -= 1
if ma - mb > rast:
a1, b1 = ma, mb
rast = a1 - b1
if 2 * n - 2 * m > rast:
a1, b1 = 2 * n, 2 * m
print("%d:%d" % (a1, b1))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
line = input().split()
a = []
for d in line:
a.append(int(d))
a.sort()
m = int(input())
line = input().split()
b = []
for d in line:
b.append(int(d))
b.sort()
arr = [[0, 0, 0]]
i = 0
j = 0
k = 1
while i < n and j < m:
if a[i] <= b[j]:
if k > 0 and arr[k - 1][0] == a[i]:
arr[k - 1][1] += 1
elif k > 0:
arr.append([a[i], arr[k - 1][1] + 1, arr[k - 1][2]])
k += 1
else:
arr.append([a[i], 1, 0])
k += 1
i += 1
else:
if k > 0 and arr[k - 1][0] == b[j]:
arr[k - 1][2] += 1
elif k > 0:
arr.append([b[j], arr[k - 1][1], arr[k - 1][2] + 1])
k += 1
else:
arr.append([b[j], 0, 1])
k += 1
j += 1
while i < n:
if arr[k - 1][0] == a[i]:
arr[k - 1][1] += 1
else:
arr.append([a[i], arr[k - 1][1] + 1, arr[k - 1][2]])
k += 1
i += 1
while j < m:
if arr[k - 1][0] == b[j]:
arr[k - 1][2] += 1
else:
arr.append([b[j], arr[k - 1][1], arr[k - 1][2] + 1])
k += 1
j += 1
arr.append([0, n, m])
ans = [3 * n, 3 * m]
for lst in arr:
tans = [2 * lst[1] + 3 * (n - lst[1]), 2 * lst[2] + 3 * (m - lst[2])]
if tans[0] - tans[1] > ans[0] - ans[1]:
ans = list(tans)
ans = str(ans[0]) + ":" + str(ans[1])
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR VAR ASSIGN VAR LIST BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
def solve(a, b):
n = len(a)
m = len(b)
a = sorted(a)
b = sorted(b)
r1 = n * 3
r2 = m * 3
max1 = r1
max2 = r2
p1 = 0
p2 = 0
while p1 < n or p2 < m:
v = 0
if p2 == m:
v = a[p1]
elif p1 == n:
v = b[p2]
else:
v = min(a[p1], b[p2])
while p1 < n and a[p1] == v:
r1 -= 1
p1 += 1
while p2 < m and b[p2] == v:
r2 -= 1
p2 += 1
if r1 - r2 > max1 - max2 or r1 - r2 == max1 - max2 and r1 > max1:
max1 = r1
max2 = r2
return max1, max2
n = input()
a = list(map(int, input().split(" ")))
n = input()
b = list(map(int, input().split(" ")))
print("%s:%s" % solve(a, b))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
def lowerBound(A, d):
ans = -1
low, high = 0, len(A) - 1
while low <= high:
mid = (low + high) // 2
if A[mid] <= d:
ans = mid
low = mid + 1
else:
high = mid - 1
return ans + 1
n = int(input())
A = list(map(int, input().split()))
m = int(input())
B = list(map(int, input().split()))
A, B = sorted(A), sorted(B)
low, high = 0, max(max(A), max(B)) + 1
m = -(10**10)
ansA, ansB = 0, 0
for i in range(len(A)):
d = A[i]
loc = lowerBound(A, d)
pointsA = loc * 2 + (len(A) - loc) * 3
loc = lowerBound(B, d)
pointsB = loc * 2 + (len(B) - loc) * 3
if m <= pointsA - pointsB:
if pointsA - pointsB == m:
if pointsA > ansA:
ansA, ansB = pointsA, pointsB
else:
m = pointsA - pointsB
ansA, ansB = pointsA, pointsB
for i in range(len(B)):
d = B[i]
loc = lowerBound(A, d)
pointsA = loc * 2 + (len(A) - loc) * 3
loc = lowerBound(B, d)
pointsB = loc * 2 + (len(B) - loc) * 3
if m <= pointsA - pointsB:
if pointsA - pointsB == m:
if pointsA > ansA:
ansA, ansB = pointsA, pointsB
else:
m = pointsA - pointsB
ansA, ansB = pointsA, pointsB
for x in [low, high]:
d = x
loc = lowerBound(A, d)
pointsA = loc * 2 + (len(A) - loc) * 3
loc = lowerBound(B, d)
pointsB = loc * 2 + (len(B) - loc) * 3
if m <= pointsA - pointsB:
if pointsA - pointsB == m:
if pointsA > ansA:
ansA, ansB = pointsA, pointsB
else:
m = pointsA - pointsB
ansA, ansB = pointsA, pointsB
print("{}:{}".format(ansA, ansB))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
def lte(n, a, d):
if a[0] > d:
return 0
if a[-1] <= d:
return n
else:
lo = 0
hi = n - 1
while lo + 1 < hi:
mid = (lo + hi) // 2
if a[mid] <= d:
lo = mid
else:
hi = mid
return lo + 1
def score(n, m, a, b, d):
ca = lte(n, a, d)
cb = lte(m, b, d)
a_score = 2 * ca + 3 * (n - ca)
b_score = 2 * cb + 3 * (m - cb)
return a_score, b_score
def main():
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
a.sort()
b.sort()
factor = 2 if 2 * (n - m) > 3 * (n - m) else 3
best_diff = factor * (n - m)
best_a = factor * n
out = str(factor * n) + ":" + str(factor * m)
mega = set(a + b)
for d in mega:
if d <= 0:
continue
a_score, b_score = score(n, m, a, b, d)
diff = a_score - b_score
if diff > best_diff or diff == best_diff and a_score > best_a:
best_diff = diff
best_a = a_score
out = str(a_score) + ":" + str(b_score)
print(out)
main()
|
FUNC_DEF IF VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
lia = list(map(int, input().split(" ", n)[:n]))
m = int(input())
lib = list(map(int, input().split(" ", m)[:m]))
a = [[ai, 0] for ai in lia] + [[bi, 1] for bi in lib]
a.sort()
x, y = 3 * n, 3 * m
X, Y = 3 * n, 3 * m
for i in range(n + m):
if a[i][1] == 0:
X -= 1
else:
Y -= 1
if X - Y > x - y:
x, y = X, Y
print(x, y, sep=":")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
from sys import maxsize, stdin, stdout
n = int(input())
team1 = list(map(int, input().split()))
m = int(input())
team2 = list(map(int, input().split()))
ls = []
for i in range(n):
ls.append([team1[i], 1])
for i in range(m):
ls.append([team2[i], 2])
ls.sort()
score1 = 3 * n
score2 = 3 * m
ans = [score1, score2]
mx = score1 - score2
for i in range(m + n):
if ls[i][1] == 1:
score1 -= 1
else:
score2 -= 1
if score1 - score2 > mx:
ans = [score1, score2]
mx = score1 - score2
print(f"{ans[0]}:{ans[1]}")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
read = lambda: map(int, input().split())
n = int(input())
a = sorted(read())[::-1]
m = int(input())
b = sorted(read())[::-1]
x, y = 2 * n, 2 * m
j = 0
for i in range(n):
while j < m and b[j] >= a[i]:
j += 1
x1 = i + 1 + 2 * n
y1 = j + 2 * m
if x1 - y1 > x - y or x1 - y1 == x - y and x1 > x:
x, y = x1, y1
print(x, y, sep=":")
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
def read_pack():
c = int(input())
x = list(map(int, str.split(input())))
x.sort()
return c, x + [0]
n, a = read_pack()
m, b = read_pack()
i = j = 0
pa = pb = None
while i <= n and j <= m:
ca = (n - i) * 3 + i * 2
cb = (m - j) * 3 + j * 2
if pa is None or ca - cb > pa - pb or ca - cb == pa - pb and ca > pa:
pa, pb = ca, cb
if j == m:
i += 1
elif i == n:
j += 1
elif a[i] < b[j]:
i += 1
elif a[i] > b[j]:
j += 1
else:
i += 1
j += 1
print(str.format("{}:{}", pa, pb))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NONE WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NONE BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
import sys
input = sys.stdin.buffer.readline
def I():
return list(map(int, input().split()))
def sieve(n):
a = [1] * n
for i in range(2, n):
if a[i]:
for j in range(i * i, n, i):
a[j] = 0
return a
n = int(input())
a = sorted(I())
m = int(input())
b = sorted(I())
c = sorted(a + b)
p1 = 0
p2 = 0
s1 = 0
s2 = 0
ans = [3 * n, 3 * m]
for d in c:
while p1 < n and a[p1] <= d:
p1 += 1
while p2 < m and b[p2] <= d:
p2 += 1
scorea = 2 * p1 + 3 * (n - p1)
scoreb = 2 * p2 + 3 * (m - p2)
curra, currb = ans
if (scorea - scoreb, scorea) >= (curra - currb, curra):
ans = [scorea, scoreb]
print(str(ans[0]) + ":" + str(ans[1]))
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
from sys import stdin, stdout
def solution():
n = int(stdin.readline().rstrip())
a = list(map(lambda x: (int(x), 1), stdin.readline().rstrip().split()))
m = int(stdin.readline().rstrip())
b = list(map(lambda x: (int(x), 2), stdin.readline().rstrip().split()))
c = a + b
c.sort(key=lambda tup: tup[0])
aa = n * 3
af = n * 3
bf = m * 3
bb = m * 3
d = aa - bb
prev = -1
for elem in c:
if prev != elem[0]:
prev = elem[0]
if aa - bb > d:
d = aa - bb
af = aa
bf = bb
if elem[1] == 1:
aa -= 1
else:
bb -= 1
if aa - bb > d:
d = aa - bb
af = aa
bf = bb
stdout.write("{}:{}".format(af, bf))
solution()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
f, s = [], []
n = int(input())
f.extend(map(int, input().split()))
m = int(input())
s.extend(map(int, input().split()))
f.append(1 << 64)
s.append(1 << 64)
f = sorted(f)
s = sorted(s)
mxA = scoreA = 3 * n
mxB = scoreB = 3 * m
mxDif = scoreA - scoreB
i, j = 0, 0
while i < n or j < m:
if f[i] < s[j]:
scoreA -= 1
i += 1
elif f[i] > s[j]:
scoreB -= 1
j += 1
else:
scoreA -= 1
i += 1
scoreB -= 1
j += 1
if scoreA - scoreB > mxDif:
mxDif = scoreA - scoreB
mxA = scoreA
mxB = scoreB
print(str(mxA) + ":" + str(mxB))
|
ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
c = sorted([[a[i], 1] for i in range(n)] + [[b[i], 2] for i in range(m)])
x = x0 = 2 * n
y = y0 = 2 * m
for i in range(n + m - 1, -1, -1):
if c[i][1] == 1:
x += 1
else:
y += 1
if x - y >= x0 - y0:
x0, y0 = x, y
print(x0, y0, sep=":")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR VAR NUMBER VAR FUNC_CALL VAR VAR LIST VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
def main():
(n,) = read()
a = read()
res = [(i, 0) for i in a]
(m,) = read()
b = read()
res.extend((i, 1) for i in b)
res.sort()
mxa = 3 * n
mnb = 3 * m
cra = 3 * n
crb = 3 * m
for _, i in res:
if i:
crb -= 1
if cra - crb > mxa - mnb:
mxa = cra
mnb = crb
else:
cra -= 1
print(str(mxa) + ":" + str(mnb))
def read(mode=2):
inputs = input().strip()
if mode == 0:
return inputs
if mode == 1:
return inputs.split()
if mode == 2:
return list(map(int, inputs.split()))
def write(s="\n"):
if s is None:
s = ""
if isinstance(s, list):
s = " ".join(map(str, s))
s = str(s)
print(s, end="")
write(main())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF VAR NONE ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
a = [int(s) for s in input().split()]
m = int(input())
b = [int(s) for s in input().split()]
INF = 3000000000
a.sort()
b.sort()
a.append(INF)
b.append(INF)
i = 0
j = 0
bestd = 0
bestadv = (n - m) * 3
besta = 3 * n
bestb = 3 * m
cura = 3 * n
curb = 3 * m
while i < n or j < m:
d = min(a[i], b[j])
while i < n and a[i] <= d:
cura -= 1
i += 1
while j < m and b[j] <= d:
curb -= 1
j += 1
if cura - curb > bestadv:
bestadv = cura - curb
besta = cura
bestb = curb
print("{0}:{1}".format(besta, bestb))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
a.sort()
b.sort()
x, y = 0, -1
u, v = n * 3, m * 3
q = [u, v]
while 1:
if y != m - 1:
y += 1
v -= 1
while x < n and a[x] <= b[y]:
x += 1
u -= 1
if u - v > q[0] - q[1]:
q = [u, v]
if (x == n or a[x] > b[y]) and y == m - 1:
break
print("%i:%i" % (q[0], q[1]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER VAR NUMBER
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n, a = int(input()), list(map(int, input().split()))
m, b = int(input()), list(map(int, input().split()))
p = [(i, 0) for i in a] + [(i, 1) for i in b]
p.sort()
i = 0
score = [n * 3, m * 3]
score_temp = list(score)
while i < m + n:
d = p[i][0]
while i < m + n and p[i][0] == d:
score_temp[p[i][1]] -= 1
i += 1
if score_temp[0] - score_temp[1] > score[0] - score[1]:
score = list(score_temp)
print(*score, sep=":")
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
lis = [[int(i), "a"] for i in input().split()]
m = int(input())
lis = lis + [[int(i), "b"] for i in input().split()] + [[0, "a"]] + [[0, "b"]]
lis.sort()
a = b = 0
ans = -1000000
fin = finb = 0
for i in range(len(lis) - 1):
if lis[i][1] == "a":
a += 1
else:
b += 1
if lis[i][0] != lis[i + 1][0]:
aa = (a - 1) * 2 + (n - a + 1) * 3
bb = (b - 1) * 2 + (m - b + 1) * 3
if ans < aa - bb:
ans = aa - bb
fin = aa
finb = bb
elif ans == aa - bb:
if fin < aa:
fin = aa
finb = bb
if lis[-1][1] == "a":
a += 1
else:
b += 1
aa = 2 * n
bb = 2 * m
if ans < aa - bb:
fin = aa
finb = bb
elif ans == aa - bb:
if fin < aa:
fin = aa
finb = bb
print(fin, ":", finb, sep="")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR STRING VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR LIST FUNC_CALL VAR VAR STRING VAR FUNC_CALL FUNC_CALL VAR LIST LIST NUMBER STRING LIST LIST NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
score = list(map(lambda x: [int(x), 0], input().split()))
m = int(input())
score += list(map(lambda x: [int(x), 1], input().split()))
score.sort()
f = []
if score[0][1] == 0:
f.append([1, 0])
else:
f.append([0, 1])
for i in range(1, n + m):
if score[i][1] == 0:
f.append([f[i - 1][0] + 1, f[i - 1][1]])
else:
f.append([f[i - 1][0], f[i - 1][1] + 1])
a, b = f[-1][0] * 2, f[-1][1] * 2
aa, bb = f[-1][0] * 3, f[-1][1] * 3
if aa - bb > a - b or aa - bb == a - b and aa > a:
a, b = aa, bb
for i in range(n + m - 1):
if score[i][0] != score[i + 1][0]:
aa = f[i][0] * 2 + (f[-1][0] - f[i][0]) * 3
bb = f[i][1] * 2 + (f[-1][1] - f[i][1]) * 3
if aa - bb > a - b or aa - bb == a - b and aa > a:
a, b = aa, bb
print(a, ":", b, sep="")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
a.sort()
b.sort()
diff = 2 * n - 2 * m
j = 0
ans = [(2 * n, 2 * m)]
for i in range(n):
while j < m:
if b[j] <= a[i] - 1:
j += 1
else:
break
ap = 2 * i + 3 * (n - i)
bp = 2 * j + 3 * (m - j)
if ap - bp > diff:
diff = ap - bp
ans = [(ap, bp)]
elif ap - bp == diff:
ans.append((ap, bp))
fans = max(ans, key=lambda x: x[0])
print(str(fans[0]) + ":" + str(fans[1]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
I = lambda: list(map(int, input().split()))
n = I()[0]
d1 = [0] + sorted(I())
m = I()[0]
d2 = [0] + sorted(I())
index1, index2 = {}, {}
index1[0], index2[0] = n, m
i, j = n, m
while i >= 0 and j >= 0:
maxd = max(d1[i], d2[j])
if not maxd in index1:
index1[maxd] = n - i
if not maxd in index2:
index2[maxd] = m - j
if d1[i] == maxd:
i -= 1
if d2[j] == maxd:
j -= 1
while i >= 0:
if not d1[i] in index1:
index1[d1[i]] = n - i
i -= 1
while j >= 0:
if not d2[j] in index2:
index2[d2[j]] = m - j
j -= 1
a, b, maxdiff = 0, 0, -1e100
for foo in [0] + d1 + d2:
i = index1[foo]
j = index2[foo]
_a, _b = i * 3 + (n - i) * 2, j * 3 + (m - j) * 2
diff = _a - _b
if diff > maxdiff or diff == maxdiff and _a > a:
maxdiff = diff
a, b = _a, _b
print("%d:%d" % (a, b))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR BIN_OP BIN_OP LIST NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
__author__ = "zhan"
first = []
second = []
n = int(input())
line = input().split()
for i in range(n):
first.append(int(line[i]))
first.sort()
m = int(input())
line = input().split()
for i in range(m):
second.append(int(line[i]))
second.sort()
a = 3 * n
b = 3 * m
d = a - b
penalty = [0, 0]
i = 0
j = 0
while i < n and j < m:
t = min(first[i], second[j])
while i < n and first[i] <= t:
i += 1
penalty[0] += 1
while j < m and second[j] <= t:
j += 1
penalty[1] += 1
if 3 * n - penalty[0] - 3 * m + penalty[1] > d:
a = 3 * n - penalty[0]
b = 3 * m - penalty[1]
d = 3 * n - penalty[0] - 3 * m + penalty[1]
if 2 * n - 2 * m > d:
a = 2 * n
b = 2 * m
print(str(a) + ":" + str(b))
|
ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n, a = int(input()), list(map(int, input().split()))
m, b = int(input()), list(map(int, input().split()))
a.sort()
b.sort()
A, B = len(a) * 3, len(b) * 3
i = j = 0
tA, tB = A, B
while i < len(a) and j < len(b):
d = min(a[i], b[j])
while i < len(a) and a[i] == d:
i += 1
tA -= 1
while j < len(b) and b[j] == d:
j += 1
tB -= 1
if tA - tB > A - B:
A, B = tA, tB
if j < len(b):
tB = len(b) * 2
if tA - tB > A - B:
A, B = tA, tB
print(A, ":", B, sep="")
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR STRING
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
n, ans, x1, x2 = int(input()), 0, 0, 0
num1 = list(sorted(map(int, input().split())))
m, p2 = int(input()), 0
num2 = list(sorted(map(int, input().split())))
ans, x1, x2 = (n - m) * 2, n * 2, m * 2
for i in range(n):
while p2 < m and num2[p2] < num1[i]:
p2 += 1
y1, y2 = i * 2 + (n - i) * 3, p2 * 2 + (m - p2) * 3
if y1 - y2 > ans or y1 - y2 == ans and y1 > x1:
ans, x1, x2 = y1 - y2, y1, y2
print("%d:%d" % (x1, x2))
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
R = lambda: map(int, input().split())
n = int(input())
a = sorted(R()) + [int(2 * 10**9) + 1]
m = int(input())
b = sorted(R()) + [int(2 * 10**9) + 1]
i, j = 0, 0
ra, rb = n * 3, m * 3
if n * 2 - m * 2 > n * 3 - m * 3:
ra, rb = n * 2, m * 2
while i < n or j < m:
sa, sb = 0, 0
if i <= n and j <= m and a[i] < b[j]:
sa, sb = (i + 1) * 2 + (n - i - 1) * 3, j * 2 + (m - j) * 3
i += 1
elif i <= n and j <= m and a[i] > b[j]:
sa, sb = i * 2 + (n - i) * 3, (j + 1) * 2 + (m - j - 1) * 3
j += 1
else:
sa, sb = i * 2 + (n - i) * 3, j * 2 + (m - j) * 3
i, j = i + 1, j + 1
if sa - sb == ra - rb and sa > ra or sa - sb > ra - rb:
ra, rb = sa, sb
print(str(ra) + ":" + str(rb))
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.
Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
-----Input-----
The first line contains integer n (1 β€ n β€ 2Β·10^5) β the number of throws of the first team. Then follow n integer numbers β the distances of throws a_{i} (1 β€ a_{i} β€ 2Β·10^9).
Then follows number m (1 β€ m β€ 2Β·10^5) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of b_{i} (1 β€ b_{i} β€ 2Β·10^9).
-----Output-----
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
-----Examples-----
Input
3
1 2 3
2
5 6
Output
9:6
Input
5
6 7 8 9 10
5
1 2 3 4 5
Output
15:10
|
def merge(l1, l2):
p1 = 0
p2 = 0
l = []
while p1 < len(l1) and p2 < len(l2):
if l1[p1] < l2[p2]:
l.append(l1[p1])
p1 += 1
else:
l.append(l2[p2])
p2 += 1
if p1 < len(l1):
l.extend(l1[p1 : len(l1)])
if p2 < len(l2):
l.extend(l2[p2 : len(l2)])
return l
def merge_sort(l):
if len(l) == 1:
return l
else:
l1 = merge_sort(l[0 : len(l) // 2])
l2 = merge_sort(l[len(l) // 2 : len(l)])
return merge(l1, l2)
n = int(input())
dist_str = input()
first_dist = list([int(s) for s in dist_str.split(" ")])
m = int(input())
dist_str = input()
second_dist = list([int(s) for s in dist_str.split(" ")])
first_dist.sort()
second_dist.sort()
first_sum = 3 * n
second_sum = 3 * m
diff = first_sum - second_sum
thr_p1 = 0
thr_p2 = 0
first_sum_temp = first_sum
second_sum_temp = second_sum
diff_temp = diff
while thr_p1 < n and thr_p2 < m:
if first_dist[thr_p1] < second_dist[thr_p2]:
first_sum_temp -= 1
thr_p1 += 1
elif first_dist[thr_p1] > second_dist[thr_p2]:
second_sum_temp -= 1
thr_p2 += 1
else:
first_sum_temp -= 1
second_sum_temp -= 1
thr_p2 += 1
thr_p1 += 1
diff_temp = first_sum_temp - second_sum_temp
if diff_temp > diff:
diff = diff_temp
first_sum = first_sum_temp
second_sum = second_sum_temp
while thr_p1 < n:
first_sum_temp -= 1
thr_p1 += 1
diff_temp = first_sum_temp - second_sum_temp
if diff_temp > diff:
diff = diff_temp
first_sum = first_sum_temp
second_sum = second_sum_temp
while thr_p2 < m:
second_sum_temp -= 1
thr_p2 += 1
diff_temp = first_sum_temp - second_sum_temp
if diff_temp > diff:
diff = diff_temp
first_sum = first_sum_temp
second_sum = second_sum_temp
ans = str(first_sum) + ":" + str(second_sum)
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $h$ hours, and each hour consists of $m$ minutes, where $m$ is an even number. Currently, there are $n$ freight trains, and they depart every day at the same time: $i$-th train departs at $h_i$ hours and $m_i$ minutes.
The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $0$ hours and $t$ minutes, where $0 \le t < {m \over 2}$, the second tram departs $m \over 2$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.
To allow passengers to board the tram safely, the tram must arrive $k$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $0$ hours and $t$ minutes, where $t < k$, then the freight trains can not depart during the last $k - t$ minutes of the day.
$O$ A schematic picture of the correct way to run passenger trams. Here $h=2$ (therefore, the number of passenger trams is $2h=4$), the number of freight trains is $n=6$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $k$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments.
Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $t$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.
-----Input-----
The first line of input contains four integers $n$, $h$, $m$, $k$ ($1 \le n \le 100\,000$, $1 \le h \le 10^9$, $2 \le m \le 10^9$, $m$ is even, $1 \le k \le {m \over 2}$)Β β the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.
$n$ lines follow, each contains two integers $h_i$ and $m_i$ ($0 \le h_i < h$, $0 \le m_i < m$)Β β the time when $i$-th freight train departs. It is guaranteed that no freight trains depart at the same time.
-----Output-----
The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $t$. Second line of output should contain freight trains that need to be canceled.
-----Examples-----
Input
2 24 60 15
16 0
17 15
Output
0 0
Input
2 24 60 16
16 0
17 15
Output
1 0
2
-----Note-----
In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.
In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $t \in [1, 15]$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $t = 0$ or $t \in [16, 29]$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $t = 0$. Another possible option is to cancel the first train and choose $t = 13$.
|
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
n, H, M, k = MI()
hm = LLI(n)
M2 = M // 2
ll = []
rr = []
st = set([0, M2])
for h, m in hm:
s = (m + 1) % M2
ll.append(s)
rr.append(s + k - 1)
st.add(s)
st.add(s + M2)
st.add((s + k - 1) % M2)
st.add((s + k - 1) % M2 + M2)
dec = list(sorted(st))
enc = {a: i for i, a in enumerate(dec)}
ln = len(dec)
tt = [0] * ln
for l, r in zip(ll, rr):
tt[enc[l]] += 1
tt[enc[r]] -= 1
for i in range(ln - 1):
tt[i + 1] += tt[i]
for i in range(ln // 2):
tt[i] += tt[i + ln // 2]
mn = min(tt[: ln // 2])
i = tt[: ln // 2].index(mn)
t = dec[i]
ans = []
for i, (l, r) in enumerate(zip(ll, rr)):
if l <= t < r or l <= t + M2 < r:
ans.append(i + 1)
print(mn, t)
print(*ans)
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $h$ hours, and each hour consists of $m$ minutes, where $m$ is an even number. Currently, there are $n$ freight trains, and they depart every day at the same time: $i$-th train departs at $h_i$ hours and $m_i$ minutes.
The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $0$ hours and $t$ minutes, where $0 \le t < {m \over 2}$, the second tram departs $m \over 2$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.
To allow passengers to board the tram safely, the tram must arrive $k$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $0$ hours and $t$ minutes, where $t < k$, then the freight trains can not depart during the last $k - t$ minutes of the day.
$O$ A schematic picture of the correct way to run passenger trams. Here $h=2$ (therefore, the number of passenger trams is $2h=4$), the number of freight trains is $n=6$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $k$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments.
Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $t$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.
-----Input-----
The first line of input contains four integers $n$, $h$, $m$, $k$ ($1 \le n \le 100\,000$, $1 \le h \le 10^9$, $2 \le m \le 10^9$, $m$ is even, $1 \le k \le {m \over 2}$)Β β the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.
$n$ lines follow, each contains two integers $h_i$ and $m_i$ ($0 \le h_i < h$, $0 \le m_i < m$)Β β the time when $i$-th freight train departs. It is guaranteed that no freight trains depart at the same time.
-----Output-----
The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $t$. Second line of output should contain freight trains that need to be canceled.
-----Examples-----
Input
2 24 60 15
16 0
17 15
Output
0 0
Input
2 24 60 16
16 0
17 15
Output
1 0
2
-----Note-----
In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.
In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $t \in [1, 15]$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $t = 0$ or $t \in [16, 29]$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $t = 0$. Another possible option is to cancel the first train and choose $t = 13$.
|
n, H, M, k = map(int, input().split())
hm = []
for i in range(n):
hm.append(list(map(int, input().split())))
M2 = M // 2
ll = []
rr = []
st = {0, M2}
for h, m in hm:
s = (m + 1) % M2
ll.append(s)
rr.append(s + k - 1)
st.add(s)
st.add(s + M2)
st.add((s + k - 1) % M2)
st.add((s + k - 1) % M2 + M2)
dec = list(sorted(st))
enc = {a: i for i, a in enumerate(dec)}
ln = len(dec)
tt = [0] * ln
for l, r in zip(ll, rr):
tt[enc[l]] += 1
tt[enc[r]] -= 1
for i in range(ln - 1):
tt[i + 1] += tt[i]
for i in range(ln // 2):
tt[i] += tt[i + ln // 2]
mn = min(tt[: ln // 2])
i = tt[: ln // 2].index(mn)
t = dec[i]
ans = []
for i, (l, r) in enumerate(zip(ll, rr)):
if l <= t < r or l <= t + M2 < r:
ans.append(i + 1)
print(mn, t)
print(*ans)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $h$ hours, and each hour consists of $m$ minutes, where $m$ is an even number. Currently, there are $n$ freight trains, and they depart every day at the same time: $i$-th train departs at $h_i$ hours and $m_i$ minutes.
The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $0$ hours and $t$ minutes, where $0 \le t < {m \over 2}$, the second tram departs $m \over 2$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.
To allow passengers to board the tram safely, the tram must arrive $k$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $0$ hours and $t$ minutes, where $t < k$, then the freight trains can not depart during the last $k - t$ minutes of the day.
$O$ A schematic picture of the correct way to run passenger trams. Here $h=2$ (therefore, the number of passenger trams is $2h=4$), the number of freight trains is $n=6$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $k$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments.
Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $t$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.
-----Input-----
The first line of input contains four integers $n$, $h$, $m$, $k$ ($1 \le n \le 100\,000$, $1 \le h \le 10^9$, $2 \le m \le 10^9$, $m$ is even, $1 \le k \le {m \over 2}$)Β β the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.
$n$ lines follow, each contains two integers $h_i$ and $m_i$ ($0 \le h_i < h$, $0 \le m_i < m$)Β β the time when $i$-th freight train departs. It is guaranteed that no freight trains depart at the same time.
-----Output-----
The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $t$. Second line of output should contain freight trains that need to be canceled.
-----Examples-----
Input
2 24 60 15
16 0
17 15
Output
0 0
Input
2 24 60 16
16 0
17 15
Output
1 0
2
-----Note-----
In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.
In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $t \in [1, 15]$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $t = 0$ or $t \in [16, 29]$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $t = 0$. Another possible option is to cancel the first train and choose $t = 13$.
|
import sys
input = sys.stdin.readline
n, h, m, k = map(int, input().split())
tram = []
dic = {}
for i in range(n):
H, M = map(int, input().split())
r = M % (m // 2)
tram.append(r)
if r not in dic:
dic[r] = 0
dic[r] += 1
S = [i for i in dic]
for i in S:
dic[i + m // 2] = 0
dic[i + m // 2] = dic[i]
data = [(i, dic[i]) for i in dic]
data.sort()
if k == 1:
print(0, 0)
print(*[])
exit()
k -= 1
query = []
for i in dic:
query.append((i + 1, -dic[i]))
query.append((i - k + 1, dic[i]))
query.sort(reverse=True)
S = 0
while query and query[-1][0] <= 0:
t, val = query.pop()
S += val
ans = S
res = 0
while query and query[-1][0] < m // 2:
time, val = query.pop()
S += val
while query and query[-1][0] == time:
time, val = query.pop()
S += val
if ans > S:
ans = S
res = time
Ans = ans
print(Ans, (res + k) % (m // 2))
ans = []
for i in range(n):
L = res
R = res + k - 1
if R >= tram[i] >= L or R >= tram[i] + m // 2 >= L:
ans.append(i + 1)
if len(ans) != Ans:
print("WA")
print(*ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
|
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $h$ hours, and each hour consists of $m$ minutes, where $m$ is an even number. Currently, there are $n$ freight trains, and they depart every day at the same time: $i$-th train departs at $h_i$ hours and $m_i$ minutes.
The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $0$ hours and $t$ minutes, where $0 \le t < {m \over 2}$, the second tram departs $m \over 2$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.
To allow passengers to board the tram safely, the tram must arrive $k$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $0$ hours and $t$ minutes, where $t < k$, then the freight trains can not depart during the last $k - t$ minutes of the day.
$O$ A schematic picture of the correct way to run passenger trams. Here $h=2$ (therefore, the number of passenger trams is $2h=4$), the number of freight trains is $n=6$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $k$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments.
Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $t$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.
-----Input-----
The first line of input contains four integers $n$, $h$, $m$, $k$ ($1 \le n \le 100\,000$, $1 \le h \le 10^9$, $2 \le m \le 10^9$, $m$ is even, $1 \le k \le {m \over 2}$)Β β the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.
$n$ lines follow, each contains two integers $h_i$ and $m_i$ ($0 \le h_i < h$, $0 \le m_i < m$)Β β the time when $i$-th freight train departs. It is guaranteed that no freight trains depart at the same time.
-----Output-----
The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $t$. Second line of output should contain freight trains that need to be canceled.
-----Examples-----
Input
2 24 60 15
16 0
17 15
Output
0 0
Input
2 24 60 16
16 0
17 15
Output
1 0
2
-----Note-----
In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.
In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $t \in [1, 15]$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $t = 0$ or $t \in [16, 29]$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $t = 0$. Another possible option is to cancel the first train and choose $t = 13$.
|
import sys
from sys import stdin
n, h, m, k = map(int, stdin.readline().split())
now = 0
ends = []
starts = []
ppp = [0] * n
for i in range(n):
hi, mi = map(int, stdin.readline().split())
if mi >= m // 2:
pz = mi - m
else:
pz = mi
if 0 <= pz < m // 2:
starts.append((pz, i))
if -k < pz <= m // 2 - k:
ends.append((pz, i))
if -k < pz < 0:
now += 1
ppp[i] = 1
if m // 2 <= mi < m:
starts.append((mi - m // 2, i))
if m // 2 - k < mi <= m - k:
ends.append((mi - m // 2, i))
if m // 2 - k < mi < m // 2:
now += 1
ppp[i] = 1
ends.sort()
starts.sort()
ends.append((float("inf"), 0))
starts.append((float("inf"), 0))
ei = 0
si = 0
sub = now
ans = now
while ei != len(ends) - 1 or si != len(starts) - 1:
if ends[ei][0] <= starts[si][0] - k:
now -= 1
ei += 1
else:
si += 1
now += 1
ans = min(ans, now)
ei = 0
si = 0
now = sub
nt = 0
if ans == now:
print(ans, nt)
ll = []
for i in range(n):
if ppp[i] == 1:
ll.append(i + 1)
print(*ll)
sys.exit()
while ei != len(ends) - 1 or si != len(starts) - 1:
nt = 0
if ends[ei][0] <= starts[si][0] - k:
nt = ends[ei][0] + k
now -= 1
ppp[ends[ei][1]] = 0
ei += 1
else:
nt = starts[si][0]
ppp[starts[si][1]] = 1
si += 1
now += 1
if ans == now:
print(ans, nt)
ll = []
for i in range(n):
if ppp[i] == 1:
ll.append(i + 1)
print(*ll)
sys.exit()
|
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given an array of positive integers $a = [a_0, a_1, \dots, a_{n - 1}]$ ($n \ge 2$).
In one step, the array $a$ is replaced with another array of length $n$, in which each element is the greatest common divisor (GCD) of two neighboring elements (the element itself and its right neighbor; consider that the right neighbor of the $(n - 1)$-th element is the $0$-th element).
Formally speaking, a new array $b = [b_0, b_1, \dots, b_{n - 1}]$ is being built from array $a = [a_0, a_1, \dots, a_{n - 1}]$ such that $b_i$ $= \gcd(a_i, a_{(i + 1) \mod n})$, where $\gcd(x, y)$ is the greatest common divisor of $x$ and $y$, and $x \mod y$ is the remainder of $x$ dividing by $y$. In one step the array $b$ is built and then the array $a$ is replaced with $b$ (that is, the assignment $a$ := $b$ is taking place).
For example, if $a = [16, 24, 10, 5]$ then $b = [\gcd(16, 24)$, $\gcd(24, 10)$, $\gcd(10, 5)$, $\gcd(5, 16)]$ $= [8, 2, 5, 1]$. Thus, after one step the array $a = [16, 24, 10, 5]$ will be equal to $[8, 2, 5, 1]$.
For a given array $a$, find the minimum number of steps after which all values $a_i$ become equal (that is, $a_0 = a_1 = \dots = a_{n - 1}$). If the original array $a$ consists of identical elements then consider the number of steps is equal to $0$.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case contains two lines. The first line contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) β length of the sequence $a$. The second line contains $n$ integers $a_0, a_1, \dots, a_{n - 1}$ ($1 \le a_i \le 10^6$).
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $t$ numbers β answers for each test case.
-----Examples-----
Input
5
4
16 24 10 5
4
42 42 42 42
3
4 6 4
5
1 2 3 4 5
6
9 9 27 9 9 63
Output
3
0
2
1
1
-----Note-----
None
|
from sys import stdin, stdout
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def build(n, sega):
for i in range(n - 1, -1, -1):
sega[i] = gcd(sega[i * 2], sega[i * 2 + 1])
def query(n, sega, l, r):
l += n
r += n
res = -1
while l < r:
if l & 1 == 1:
if res == -1:
res = sega[l]
else:
res = gcd(res, sega[l])
l += 1
if r & 1 == 1:
r -= 1
if res == -1:
res = sega[r]
else:
res = gcd(res, sega[r])
l //= 2
r //= 2
return res
N = 400000
sega = [0] * N
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
a_a = list(map(int, stdin.readline().split()))
for i in range(n):
sega[n + i] = a_a[i]
build(n, sega)
l = 0
h = n - 1
while l < h:
m = (l + h) // 2
flag = True
r = query(n, sega, 0, m + 1)
for i in range(1, n):
tmp = query(n, sega, i, min(n, i + m + 1))
if i + m + 1 > n:
tmp = gcd(tmp, query(n, sega, 0, i + m + 1 - n))
if r != tmp:
flag = False
break
if flag:
h = m
else:
l = m + 1
stdout.write(str(h) + "\n")
|
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
You are given an array of positive integers $a = [a_0, a_1, \dots, a_{n - 1}]$ ($n \ge 2$).
In one step, the array $a$ is replaced with another array of length $n$, in which each element is the greatest common divisor (GCD) of two neighboring elements (the element itself and its right neighbor; consider that the right neighbor of the $(n - 1)$-th element is the $0$-th element).
Formally speaking, a new array $b = [b_0, b_1, \dots, b_{n - 1}]$ is being built from array $a = [a_0, a_1, \dots, a_{n - 1}]$ such that $b_i$ $= \gcd(a_i, a_{(i + 1) \mod n})$, where $\gcd(x, y)$ is the greatest common divisor of $x$ and $y$, and $x \mod y$ is the remainder of $x$ dividing by $y$. In one step the array $b$ is built and then the array $a$ is replaced with $b$ (that is, the assignment $a$ := $b$ is taking place).
For example, if $a = [16, 24, 10, 5]$ then $b = [\gcd(16, 24)$, $\gcd(24, 10)$, $\gcd(10, 5)$, $\gcd(5, 16)]$ $= [8, 2, 5, 1]$. Thus, after one step the array $a = [16, 24, 10, 5]$ will be equal to $[8, 2, 5, 1]$.
For a given array $a$, find the minimum number of steps after which all values $a_i$ become equal (that is, $a_0 = a_1 = \dots = a_{n - 1}$). If the original array $a$ consists of identical elements then consider the number of steps is equal to $0$.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case contains two lines. The first line contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) β length of the sequence $a$. The second line contains $n$ integers $a_0, a_1, \dots, a_{n - 1}$ ($1 \le a_i \le 10^6$).
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $t$ numbers β answers for each test case.
-----Examples-----
Input
5
4
16 24 10 5
4
42 42 42 42
3
4 6 4
5
1 2 3 4 5
6
9 9 27 9 9 63
Output
3
0
2
1
1
-----Note-----
None
|
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
ans = []
for _ in range(int(input())):
n = int(input())
u = list(map(int, input().split()))
u += u[:]
m = 0
for i in range(n):
k1 = u[i]
k2 = u[i + 1]
c = 0
j = i + 1
while k1 != k2:
k1 = gcd(k1, u[j])
k2 = gcd(k2, u[j + 1])
j += 1
c += 1
m = max(m, c)
ans.append(m)
print("\n".join(map(str, ans)))
|
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
You are given an array of positive integers $a = [a_0, a_1, \dots, a_{n - 1}]$ ($n \ge 2$).
In one step, the array $a$ is replaced with another array of length $n$, in which each element is the greatest common divisor (GCD) of two neighboring elements (the element itself and its right neighbor; consider that the right neighbor of the $(n - 1)$-th element is the $0$-th element).
Formally speaking, a new array $b = [b_0, b_1, \dots, b_{n - 1}]$ is being built from array $a = [a_0, a_1, \dots, a_{n - 1}]$ such that $b_i$ $= \gcd(a_i, a_{(i + 1) \mod n})$, where $\gcd(x, y)$ is the greatest common divisor of $x$ and $y$, and $x \mod y$ is the remainder of $x$ dividing by $y$. In one step the array $b$ is built and then the array $a$ is replaced with $b$ (that is, the assignment $a$ := $b$ is taking place).
For example, if $a = [16, 24, 10, 5]$ then $b = [\gcd(16, 24)$, $\gcd(24, 10)$, $\gcd(10, 5)$, $\gcd(5, 16)]$ $= [8, 2, 5, 1]$. Thus, after one step the array $a = [16, 24, 10, 5]$ will be equal to $[8, 2, 5, 1]$.
For a given array $a$, find the minimum number of steps after which all values $a_i$ become equal (that is, $a_0 = a_1 = \dots = a_{n - 1}$). If the original array $a$ consists of identical elements then consider the number of steps is equal to $0$.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case contains two lines. The first line contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) β length of the sequence $a$. The second line contains $n$ integers $a_0, a_1, \dots, a_{n - 1}$ ($1 \le a_i \le 10^6$).
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $t$ numbers β answers for each test case.
-----Examples-----
Input
5
4
16 24 10 5
4
42 42 42 42
3
4 6 4
5
1 2 3 4 5
6
9 9 27 9 9 63
Output
3
0
2
1
1
-----Note-----
None
|
def read_ints():
return list(map(int, input().split()))
def gcd(x, y):
if x == 0 or y == 0:
return x + y
if x > y:
return gcd(x % y, y)
else:
return gcd(x, y % x)
def remove_gcd(a):
g = gcd(a[0], a[1])
for i in range(2, len(a)):
g = gcd(g, a[i])
return [(x // g) for x in a]
def make_cycled(a):
a.extend(a[:-1])
return a
def create_dp(a):
n = len(a)
k = 0
while n > 0:
k += 1
n //= 2
dp = [[(0) for _ in range(len(a))] for _ in range(k)]
dp[0] = a
step = 1
for i in range(1, k):
step *= 2
for j in range(len(a)):
if j + step <= len(a):
dp[i][j] = gcd(dp[i - 1][j], dp[i - 1][j + step // 2])
return dp
def get_seq_gcd(dp, l, r):
step = 1
k = -1
while r - l >= step:
step *= 2
k += 1
step //= 2
return gcd(dp[k][l], dp[k][r - step])
def get_answer(dp):
answer = 0
l, r = 0, 1
while l < len(dp[0]):
cur = get_seq_gcd(dp, l, r)
while r < len(dp[0]) and cur > 1:
cur = gcd(cur, dp[0][r])
r += 1
answer = max(answer, r - l - 1)
l += 1
if l == r:
r += 1
return answer
t = int(input())
for _ in range(t):
n = int(input())
a = read_ints()
a = remove_gcd(a)
a = make_cycled(a)
dp = create_dp(a)
answer = get_answer(dp)
print(answer)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Simon has an array a_1, a_2, ..., a_{n}, consisting of n positive integers. Today Simon asked you to find a pair of integers l, r (1 β€ l β€ r β€ n), such that the following conditions hold: there is integer j (l β€ j β€ r), such that all integers a_{l}, a_{l} + 1, ..., a_{r} are divisible by a_{j}; value r - l takes the maximum value among all pairs for which condition 1 is true;
Help Simon, find the required pair of numbers (l, r). If there are multiple required pairs find all of them.
-----Input-----
The first line contains integer n (1 β€ n β€ 3Β·10^5).
The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^6).
-----Output-----
Print two integers in the first line β the number of required pairs and the maximum value of r - l. On the following line print all l values from optimal pairs in increasing order.
-----Examples-----
Input
5
4 6 9 3 6
Output
1 3
2
Input
5
1 3 5 7 9
Output
1 4
1
Input
5
2 3 5 7 11
Output
5 0
1 2 3 4 5
-----Note-----
In the first sample the pair of numbers is right, as numbers 6, 9, 3 are divisible by 3.
In the second sample all numbers are divisible by number 1.
In the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1, 1), (2, 2), (3, 3), (4, 4), (5, 5).
|
n = int(input()) + 1
t = [1] + list(map(int, input().split())) + [1]
p = [True] * n
s, q = 0, list(range(1, n))
for i in range(1, n):
if p[i]:
a = b = i
d = t[i]
if d == 1:
s, q = n - 2, [1]
break
while t[a - 1] % d == 0:
a -= 1
while t[b + 1] % d == 0:
b += 1
p[b] = False
d = b - a
if d > s:
s, q = d, [a]
elif d == s != 0:
q.append(a)
print(len(q), s)
print(" ".join(map(str, q)))
|
ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER LIST NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR LIST VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is a_{i} meters high.
Vladimir has just planted n bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing.
Vladimir wants to check the bamboos each d days (i.e. d days after he planted, then after 2d days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than k meters.
What is the maximum value d he can choose so that he can achieve what he wants without cutting off more than k meters of bamboo?
-----Input-----
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 10^11)Β β the number of bamboos and the maximum total length of cut parts, in meters.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9)Β β the required heights of bamboos, in meters.
-----Output-----
Print a single integerΒ β the maximum value of d such that Vladimir can reach his goal.
-----Examples-----
Input
3 4
1 3 5
Output
3
Input
3 40
10 30 50
Output
32
-----Note-----
In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters.
|
import itertools
import time
unfold = itertools.chain.from_iterable
t = time.time() + 1900
def jumps(a):
d = speedup
if time.time() > t:
print(anss)
return
try:
while True:
c = (a + d - 1) // d
d = (a + c - 2) // (c - 1)
yield d
except:
return
def calc(d):
return (d - 1) * len(a) - sum(i % d for i in a)
def ans1(D):
for d in D:
d -= 1
if calc(d) <= k:
return d
return 1
def ans():
for d, pd in zip(D, D[1:]):
if time.time() > t:
return anss
d -= 1
cd = calc(d)
if cd <= k:
return d
if d == pd:
continue
cpd = calc(pd)
if (d - pd) * (cd - cpd) >= (cd - k) * (d - pd) + cd - cpd - 1:
return d - ((cd - k) * (d - pd) + cd - cpd - 1) // (cd - cpd)
return anss
n, k = map(int, input().split())
a = list(map(int, input().split()))
speedup = int(2 * max(a) ** 0.55)
lb = int(max(a) ** 0.1 + 10)
a = [(i - 1) for i in a]
anss = ans1(sorted(range(2, lb + 1), reverse=True))
if anss <= lb / 2:
print(anss)
return
a = [(i + 1) for i in a]
D = sorted(
set(range(lb + 1, speedup + 1)).union(
set([speedup, max(a) + k + 1]).union(set(unfold(map(jumps, a))))
),
reverse=True,
)
a = [(i - 1) for i in a]
print(int(ans()))
|
IMPORT IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR VAR RETURN FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF FOR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL FUNC_CALL VAR LIST VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
|
Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is a_{i} meters high.
Vladimir has just planted n bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing.
Vladimir wants to check the bamboos each d days (i.e. d days after he planted, then after 2d days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than k meters.
What is the maximum value d he can choose so that he can achieve what he wants without cutting off more than k meters of bamboo?
-----Input-----
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 10^11)Β β the number of bamboos and the maximum total length of cut parts, in meters.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9)Β β the required heights of bamboos, in meters.
-----Output-----
Print a single integerΒ β the maximum value of d such that Vladimir can reach his goal.
-----Examples-----
Input
3 4
1 3 5
Output
3
Input
3 40
10 30 50
Output
32
-----Note-----
In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters.
|
import itertools
unfold = itertools.chain.from_iterable
speedup = 400000
def jumps(a):
d = speedup
while d < a - 1:
c = (a + d - 1) // d
d = (a + c - 2) // (c - 1)
yield d
def calc(d):
return sum(d - 1 - (i - 1) % d for i in a)
def ans():
for d, pd in zip(D, D[1:]):
d -= 1
cd = calc(d)
if cd <= k:
return d
if d == pd:
continue
cpd = calc(pd)
if d - pd >= (cd - k) * (d - pd) / (cd - cpd):
return d - (cd - k) * (d - pd) / (cd - cpd)
return 1
n, k = map(int, input().split())
a = list(map(int, input().split()))
D = sorted(
set(range(1, speedup + 1))
.union([max(a) + k + 1])
.union(set(unfold(map(jumps, a)))),
reverse=True,
)
print("%d" % ans())
|
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER LIST BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
input = sys.stdin.readline
n = int(input())
s = input()[:-1]
ans = [([-1] * (n + 1)) for _ in range(26)]
for c in range(26):
for i in range(n):
cnt = 0
for j in range(i, n):
if ord(s[j]) - ord("a") != c:
cnt += 1
ans[c][cnt] = max(ans[c][cnt], j - i + 1)
for c in range(26):
for i in range(1, n + 1):
ans[c][i] = max(ans[c][i], ans[c][i - 1])
for _ in range(int(input())):
m, c = input().split()
print(ans[ord(c) - ord("a")][int(m)])
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
n = int(input())
s = input()
pfs = [[(0) for _ in range(n + 1)] for _ in range(26)]
for i in range(n):
ch = ord(s[i]) - ord("a")
for j in range(26):
if j == ch:
pfs[j][i + 1] = pfs[j][i] + 1
else:
pfs[j][i + 1] = pfs[j][i]
dp = [[(0) for _ in range(n + 1)] for _ in range(26)]
for c in range(26):
for l in range(n + 1):
for r in range(l, n + 1):
tc = r - l - (pfs[c][r] - pfs[c][l])
dp[c][tc] = max(dp[c][tc], r - l)
for i in range(1, n + 1):
dp[c][i] = max(dp[c][i], dp[c][i - 1])
A = []
for q in range(int(input())):
m, c = sys.stdin.readline().split()
m = int(m)
c = ord(c) - ord("a")
ans = dp[c][m]
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
input = sys.stdin.readline
def getCons(string, m, c):
n = len(string)
ans = 0
st, end = 0, 0
while end < n:
if string[end] == c:
end += 1
elif m:
m -= 1
end += 1
else:
while st < end and string[st] == c:
st += 1
st += 1
m += 1
ans = max(ans, end - st)
return ans
t = 1
while t:
t -= 1
n = int(input())
s = input().strip()
dp = {}
q = int(input())
for _ in range(q):
m, c = input().split()
if (m, c) in dp:
print(dp[m, c])
continue
dp[m, c] = getCons(s, int(m), c)
print(dp[m, c])
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
dp = [([-1] * (n + 1)) for i in range(26)]
for c in range(26):
for j in range(n):
tst = 1 if s[j] == chr(c + 97) else 0
dp[c][1 - tst] = max(dp[c][1 - tst], 1)
for k in range(j + 1, n):
if s[k] == chr(c + 97):
tst += 1
dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1)
q = int(sys.stdin.readline().strip())
for i in range(q):
m, c = [item for item in sys.stdin.readline().strip().split()]
m = int(m)
print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR
|
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!
Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour s_{i}, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c β Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.
For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.
But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer m_{i} and a lowercase letter c_{i}, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.
-----Input-----
The first line of input contains a positive integer n (1 β€ n β€ 1 500) β the length of the garland.
The second line contains n lowercase English letters s_1s_2... s_{n} as a string β the initial colours of paper pieces on the garland.
The third line contains a positive integer q (1 β€ q β€ 200 000) β the number of plans Nadeko has.
The next q lines describe one plan each: the i-th among them contains an integer m_{i} (1 β€ m_{i} β€ n) β the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter c_{i} β Koyomi's possible favourite colour.
-----Output-----
Output q lines: for each work plan, output one line containing an integer β the largest Koyomity achievable after repainting the garland according to it.
-----Examples-----
Input
6
koyomi
3
1 o
4 o
4 m
Output
3
6
5
Input
15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b
Output
3
4
5
7
8
1
2
3
4
5
Input
10
aaaaaaaaaa
2
10 b
10 z
Output
10
10
-----Note-----
In the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.
|
import sys
ii = lambda: sys.stdin.readline().strip()
idata = lambda: [int(x) for x in ii().split()]
n = int(ii())
s = ii()
slov = {}
for i in range(97, 97 + 26):
slov[chr(i)] = [[], [1]]
slov[s[0]] = [[1], [0, 0]]
for j in range(1, n):
if slov[s[j]][1][-1] == 0:
slov[s[j]][0][-1] += 1
else:
slov[s[j]][0] += [1]
slov[s[j]][1] += [0]
for i in range(97, 97 + 26):
if chr(i) != s[j]:
slov[chr(i)][1][-1] += 1
for t in range(int(ii())):
m, c = ii().split()
m = int(m)
a, b = slov[c]
if sum(b) <= m:
print(n)
elif not bool(a):
print(m)
elif len(a) == 1:
print(a[0] + m)
else:
l, r = 0, 0
ans = 0
summ_a, summ_b = 0, 0
used = 0
b1 = b[:]
b1[0], b1[-1] = 0, 0
count = 0
while r != len(a):
if summ_b + b1[r] <= m:
summ_b += b1[r]
summ_a += a[r]
r += 1
ans = max(ans, m + summ_a)
else:
summ_a -= a[l]
l += 1
summ_b -= b1[l]
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST LIST LIST NUMBER ASSIGN VAR VAR NUMBER LIST LIST NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER LIST NUMBER VAR VAR VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.