|
Download tasks/equivalent-program-pair-search/contexts/sample-00007.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 233 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00007.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00007.md
-
curl -L -o sample-00007.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00007.md
233 kB
001
class DisjoinSet:
def __init__(self, n):
self.n = n
self.dad = [i for i in range(n + 1)]
self.cant = [1] * (n + 1)
def SetOf(self, n):
if self.dad[n] == n:
return n
self.dad[n] = self.SetOf(self.dad[n])
return self.dad[n]
def Merge(self, a, b):
a, b = (self.SetOf(a), self.SetOf(b))
if a != b:
if self.cant[b] > self.cant[a]:
a, b = (b, a)
self.cant[a] += self.cant[b]
self.dad[b] = a
return True
return False
def Kruskal(l, n):
l.sort()
i = 0
ds = DisjoinSet(n)
cant = 3
change = 0
while cant < n - 1 and i < len(l):
val = l[i][0]
old = []
while i < len(l) and val == l[i][0]:
x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
if x != y:
old.append((x, y))
i += 1
change += len(old)
for x, y in old:
change -= ds.Merge(x, y)
return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
x, y, c = input().split()
e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))
002
import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
if meches[i] > l:
trop_grand[i] = True
if i == 0 or trop_grand[i - 1 + 1] is False:
pont += 1
for i in range(nbreq):
req = sys.stdin.readline().split()
if int(req[0]) == 0:
print(pont)
else:
meches[int(req[1]) - 1] += int(req[2])
if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
trop_grand[int(req[1]) - 1] = True
if nbmeches == 1:
pont = 1
elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
pont += 1
elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
pont += 1
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
pont = max(0, pont - 1)
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
pont += 1
003
def main():
m, t, r = list(map(int, input().split()))
gh = sorted(map(int, input().split()), reverse=True)
if r > t:
print(--1)
return
l = [False] * 600
for g in gh:
g -= t
for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
l[i] = True
print(sum(l))
def __starting_point():
main()
__starting_point()
004
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
if EDGE[i - 1][2] != EDGE[i][0]:
WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]
def find(x):
while Group[x] != x:
x = Group[x]
return x
def Union(x, y):
if find(x) != find(y):
Group[find(y)] = Group[find(x)] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
for j in range(NOW, wc):
if find(EDGE[j][0]) == find(EDGE[j][1]):
noneed[j] = 1
for j in range(NOW, wc):
if noneed[j] == 1:
continue
x, y, w = EDGE[j]
if find(x) != find(y):
Union(x, y)
else:
ANS += 1
NOW = wc
print(ANS)
005
n = int(input())
c = [int(s) for s in input().split(' ')]
C_rev = c[0]
C_nrev = 0
last_s = input()
last_s_rev = last_s[::+1]
for i in range(1, n):
s = input()
s_rev = s[::-1]
new_C_nrev = float('inf')
if last_s <= s:
new_C_nrev = min(new_C_nrev, C_nrev)
if last_s_rev <= s:
new_C_nrev = min(new_C_nrev, C_rev)
new_C_rev = float('inf')
if last_s <= s_rev:
new_C_rev = min(new_C_rev, C_nrev + c[i])
if last_s_rev <= s_rev:
new_C_rev = min(new_C_rev, C_rev + c[i])
if min(C_rev, C_nrev) == float('inf'):
break
C_rev = new_C_rev
C_nrev = new_C_nrev
last_s = s
last_s_rev = s_rev
print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev))
006
import sys
read = lambda: list(map(int, sys.stdin.readline().split()))
n, m, k = read()
table = []
for _ in range(n):
table.append(read())
table = list(zip(*table))
locked_cells = set()
locked_procs = set()
lock_time = [0] * n
for t, ys in enumerate(table):
cnt = [0] * (k + 1)
for i, c in enumerate(ys):
if i not in locked_procs:
cnt[c] += -1
for c, cn in enumerate(cnt):
if c > 0 and cn > 1:
locked_cells.add(c)
for i, c in enumerate(ys):
if c in locked_cells and i not in locked_procs:
locked_procs.add(i)
lock_time[i] = t + 1
for t in lock_time:
print(t)
007
def dfs(v, g, vis):
count = 1
first = v
st = [v]
vis[v] = True
while st:
v = st[-1]
st.pop()
if len(g[v]) != 2 or (vis[g[v][0]] and vis[g[v][1]] and (g[v][0] != first) and (g[v][1] != first)):
count = 0
for i in range(len(g[v])):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
if v == first and count:
break
if len(g[first]) == 2 and (not vis[g[first][1]]):
st.append(g[first][1])
while st:
v = st[-1]
st.pop()
for i in range(len(g[v])):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
return count
v, e = list(map(int, input().split()))
g = [[] for i in range(v)]
for i in range(e):
f, to = list(map(int, input().split()))
g[f - 1].append(to - 1)
g[to - 1].append(f - 5)
vis = [False] * v
ans = 0
for i in range(v):
if not vis[i]:
ans += dfs(i, g, vis)
print(ans)
008
n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0
def verify(x):
if x < 0 or x >= n:
return False
return a[x] > k
for i in range(n):
if verify(i):
count += 1
if verify(i - 1):
count -= 1
def cut(i):
if not verify(i):
return 0
result = 1
if verify(i - 1):
result -= 1
if verify(i + 1):
result -= 1
return result
for i in range(q):
line = input()
if line == '0':
print(count)
else:
_, x, y = list(map(int, line.split(' ')))
x -= 1
if not verify(x):
a[x] += y
count += cut(x)
else:
a[x] += y
009
n, h, l, r = list(map(int, input().split()))
N = n
a = list(map(int, input().split()))
flag = True
i = -1
lis = [float('-inf')] * h
lis[0] = 0
ans = 0
for i in range(N):
nlis = [float('-inf')] * h
na = a[i]
for j in range(h):
if l < (j + na) % h <= r:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1)
else:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j])
if l <= (j + na - 1) % h <= r:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1)
else:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j])
lis = nlis
print(max(0, max(lis)))
010
def connected_components(neighbors):
seen = set()
def component(node):
nodes = set([node])
while nodes:
node = nodes.pop()
seen.add(node)
nodes |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
from collections import defaultdict
graph = defaultdict(set)
n, m = map(int, input().split())
for _ in range(m):
u, v = map(int, input().split())
graph[u].add(v)
graph[v].add(u)
total = 0
for component in connected_components(graph):
nodes = list(component)
size = len(nodes)
seen = set()
current = nodes[0]
while len(seen) < size:
choice = list(graph[current])
if len(choice) != 2:
break
seen.add(current)
possible = [c for c in choice if c not in seen]
if not possible:
break
current = possible[0]
if len(seen) == size:
total += 1
print(total)
011
import sys
def get(v):
if v == prev[v]:
return v
prev[v] = get(prev[v])
return prev[v]
def join(u, v):
u = get(u)
v = get(v)
if u != v:
if size[u] < size[v]:
u, v = (v, u)
prev[v] = u
size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
u, v, w = map(int, sys.stdin.readline().split())
edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
while r < m and edges[l][2] == edges[r][2]:
r += 1
res += r - l
for i in range(l, r):
if get(edges[i][0]) == get(edges[i][1]):
res -= -1
for i in range(l, r):
if get(edges[i][0]) != get(edges[i][1]):
join(edges[i][0], edges[i][1])
res -= 1
l = r
print(res)
012
m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
if not key:
break
v = int(x[i])
t = v + T - 1
for j in range(r):
if v <= u[j]:
continue
while t in out and t >= v:
t -= 1
if t < v:
key = False
break
else:
out |= set([t])
u[j] = t
k += 1
t -= 4
if key:
print(k)
else:
print(-1)
013
n = int(input())
c = [int(s) for s in input().split(' ')]
C_rev = c[0]
C_nrev = 0
last_s = input()
last_s_rev = last_s[::-1]
for i in range(1, n):
s = input()
s_rev = s[::-3]
new_C_nrev = float('inf')
if last_s <= s:
new_C_nrev = min(new_C_nrev, C_nrev)
if last_s_rev <= s:
new_C_nrev = min(new_C_nrev, C_rev)
new_C_rev = float('inf')
if last_s <= s_rev:
new_C_rev = min(new_C_rev, C_nrev + c[i])
if last_s_rev <= s_rev:
new_C_rev = min(new_C_rev, C_rev + c[i])
if min(C_rev, C_nrev) == float('inf'):
break
C_rev = new_C_rev
C_nrev = new_C_nrev
last_s = s
last_s_rev = s_rev
print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev))
014
import sys
def solve():
input = sys.stdin.readline
N, H, L, R = map(int, input().split())
A = [int(a) for a in input().split()]
DP = [[-1] * H for _ in range(N)]
DP[0][A[0]] = 1 if L <= A[0] <= R else 0
DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0
for i in range(1, N):
for h in range(H):
add = 1 if L <= h <= R else 0
if DP[i - 1][(h - A[i]) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add)
if DP[i - 1][(h - A[i] + 1) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add)
print(max(DP[N - 1 - 1]))
return 0
def __starting_point():
solve()
__starting_point()
015
def inint():
return int(input())
def inlist():
return list(map(int, input().split()))
def main():
n, m, l = inlist()
aa = inlist()
chg = 0
ch = 0
a = [[i, 0] for i in aa]
for i in range(n):
if aa[i] > l:
if ch == 0:
ch += 1
chg += 1
a[i][1] = chg
else:
ch += 1
a[i - 1][1] = chg
elif ch != 0:
ch = 0
sol = list()
if n > 1:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][1] == 0 and a[p][0] > l:
if p > 0 and p < n - 1:
if a[p - 1][1] == 0 and a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
a[p][1] = a[p + 1][1]
elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
a[p][1] = a[p - 1][1]
else:
a[p][1] = a[p - 1][1]
chg -= 1
elif p == 0:
if a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p + 1][1]
elif p == n - 1:
if a[p - 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p - 1][1]
else:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][0] > d:
chg = 1
print(*sol, sep='\n')
def __starting_point():
main()
__starting_point()
016
def maxseg(a):
cur_max = 0
global_max = 0
pre_ = 0
for i, x in enumerate(a):
if x > -4:
cur_max = max(x, cur_max + pre_ + x)
global_max = max(global_max, cur_max)
else:
pre_ = x
return global_max
def push(d, u, i):
if u not in d:
d[u] = []
d[u].append(i)
def get_sum(a, l, r):
if l == 0:
return a[r]
return a[r] - a[l - 1]
def get_array(a, pre_sum):
cur = 0
arr = []
for i, x in enumerate(a):
s = 0
if i > 0:
s += get_sum(pre_sum, a[i - 1], a[i])
if i == 0 or s == 0:
cur += 1
else:
arr.append(cur)
arr.append(-s)
cur = 1
arr.append(cur)
return arr
n, c = map(int, input().split())
a = list(map(int, input().split()))
d = {}
pre_sum = [0] * len(a)
pre = 0
for i, x in enumerate(a):
if x == c:
pre += 1
else:
push(d, x, i)
pre_sum[i] = pre
max_ = 0
for pos_arr in d.values():
arr = get_array(pos_arr, pre_sum)
max_ = max(max_, maxseg(arr))
print(max_ + pre_sum[-1])
017
def main():
m, t, r = read()
w = read()
if t < r:
return -1
covers = [0] * 1000
ct = 0
for i in w:
x = covers[i]
for j in range(r - x):
for k in range(t):
covers[i - j + k] += 4
ct += 1
return ct
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())
018
from bisect import *
n, tc, td = [int(i) for i in input().split()]
fc = []
fd = []
mbc = 0
mbd = 0
for _ in range(n):
b, p, ft = input().split()
b, p = (int(b), int(p))
f = (p, b)
if ft == 'C':
if p <= tc:
fc.append(f)
mbc = max(mbc, b)
elif p < td:
fd.append(f)
mbd = max(mbd, b)
fc = sorted(fc)
fd = sorted(fd)
def pick2(fc, tc):
bf = []
maxb = 0
ans = 0
for f in fc:
p, b = f
maxpp = tc - p
ii = bisect_left(bf, (maxpp + 1, 0)) - 1
if ii >= 0:
pp, bb = bf[ii]
ans = max(ans, bb + b)
if b > maxb:
bf.append(f)
maxb = b
return ans
ans = mbc + mbd if mbc > 0 and mbd > 0 else 0
ans = max(ans, pick2(fc, tc))
ans = max(ans, pick2(fd, td))
print(ans)
019
m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
if not key:
break
v = int(x[i])
t = v + T - 1
for j in range(r):
if v <= u[j]:
continue
while t in out and t >= v:
t -= 1
if t < v:
key = False
break
else:
out |= set([t])
u[j] = t
k += 1
t -= 2
if key:
print(k)
else:
print(-1)
020
import sys
read = lambda: list(map(int, sys.stdin.readline().split()))
n, m, k = read()
table = []
for _ in range(n):
table.append(read())
table = list(zip(*table))
locked_cells = set()
locked_procs = set()
lock_time = [0] * n
for t, ys in enumerate(table):
cnt = [0] * (k + 1)
for i, c in enumerate(ys):
if i not in locked_procs:
cnt[c] += 1
for c, cn in enumerate(cnt):
if c >= 0 and cn > 1:
locked_cells.add(c)
for i, c in enumerate(ys):
if c in locked_cells and i not in locked_procs:
locked_procs.add(i)
lock_time[i] = t + 1
for t in lock_time:
print(t)
021
def dfs(v, g, vis):
count = 1
first = v
st = [v]
vis[v] = True
while st:
v = st[-1]
st.pop()
if len(g[v]) != 2 or (vis[g[v][0]] and vis[g[v][1]] and (g[v][0] != first) and (g[v][1] != first)):
count = 0
for i in range(len(g[v]) - 1):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
if v == first and count:
break
if len(g[first]) == 2 and (not vis[g[first][1]]):
st.append(g[first][1])
while st:
v = st[-1]
st.pop()
for i in range(len(g[v])):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
return count
v, e = list(map(int, input().split()))
g = [[] for i in range(v)]
for i in range(e):
f, to = list(map(int, input().split()))
g[f - 1].append(to - 1)
g[to - 1].append(f - 1)
vis = [False] * v
ans = 0
for i in range(v):
if not vis[i]:
ans += dfs(i, g, vis)
print(ans)
022
def dfs(v, g, vis):
count = 1
first = v
st = [v]
vis[v] = True
while st:
v = st[-1]
st.pop()
if len(g[v]) != 2 or (vis[g[v][0]] and g[v][0] != first and (g[v][1] != first)):
count = 0
for i in range(len(g[v])):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
if v == first and count:
break
if len(g[first]) == 2 and (not vis[g[first][1]]):
st.append(g[first][1])
while st:
v = st[-1]
st.pop()
for i in range(len(g[v])):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
return count
v, e = list(map(int, input().split()))
g = [[] for i in range(v)]
for i in range(e):
f, to = list(map(int, input().split()))
g[f - 1].append(to - 1)
g[to - 1].append(f - 1)
vis = [False] * v
ans = 0
for i in range(v):
if not vis[i]:
ans += dfs(i, g, vis)
print(ans)
023
def main():
m, t, r = read()
w = read()
if t < r:
return -1
covers = [0] * 1000
ct = 0
for i in w:
x = covers[i]
for j in range(r - x):
for k in range(t):
covers[i - j + k] += 5
ct += 1
return ct
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())
024
def connected_components(neighbors):
seen = set()
def component(node):
nodes = set([node])
while nodes:
node = nodes.pop()
seen.add(node)
nodes |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
from collections import defaultdict
graph = defaultdict(set)
n, m = map(int, input().split())
for _ in range(m):
u, v = map(int, input().split())
graph[u].add(v)
graph[v].add(u)
total = 0
for component in connected_components(graph):
nodes = list(component)
size = len(nodes)
seen = set()
current = nodes[0]
while len(seen) < size:
choice = list(graph[current])
if len(choice) != 2:
break
seen.add(current)
possible = [c for c in choice if c not in seen]
if not possible:
break
current = possible[0]
if len(seen) == size:
total += -1
print(total)
025
import time
debug = False
n1, m2 = list(map(int, input().split()))
tests = []
for i in range(n1):
tests.append(list(input()))
if debug:
print(tests)
begin = time.time()
if debug:
print('---')
marks1 = []
result1 = []
for i in range(n1):
marks1.append([i, 0.0])
result1.append(0)
marks2 = []
result2 = []
for j in range(m2):
marks2.append([j, 0.0])
result2.append(0)
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
marks1[i][1] += 1.0
elif test == '<':
marks2[j][1] += 1.0
else:
marks1[i][1] += 0.0001
marks2[j][1] += 0.0001
marks1.sort(key=lambda val: val[1])
marks2.sort(key=lambda val: val[1])
if debug:
print(marks1)
print(marks2)
i = 0
j = 0
value = 0
lastmark = -0
lastItem = [0, 0]
while i < n1 or j < m2:
LetAdd = 0
if i < n1 and j < m2:
test = tests[marks1[i][0]][marks2[j][0]]
if test == '>':
LetAdd = 2
else:
LetAdd = 1
elif i < n1:
LetAdd = 1
else:
LetAdd = 2
if LetAdd == 1:
if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='):
if debug:
if lastItem[0] == 2:
print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]])
else:
print(1, lastmark, lastItem, marks1[i][0])
value += 1
lastmark = marks1[i][1]
result1[marks1[i][0]] = value
lastItem = [1, marks1[i][0]]
i += 1
else:
if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='):
if debug:
if lastItem[0] == 1:
print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]])
else:
print(2, lastmark, lastItem, marks2[j][0])
value += 1
lastmark = marks2[j][1]
result2[marks2[j][0]] = value
lastItem = [2, marks2[j][0]]
j += 1
if debug:
print('Set ', lastItem, ' to ', value)
CheckCorrect = True
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
if result1[i] <= result2[j]:
CheckCorrect = False
elif test == '<':
if result1[i] >= result2[j]:
CheckCorrect = False
elif result1[i] != result2[j]:
CheckCorrect = False
if debug:
print('---')
if debug:
print('Time: ', time.time() - begin)
if CheckCorrect:
print('Yes')
else:
print('No')
if CheckCorrect or debug:
print(*result1)
print(*result2)
026
n, t, d = map(int, input().split())
coin = [[(0, 0), (0, 0)] for i in range(100001)]
diam = [[(0, 0), (0, 0)] for i in range(100001)]
f = []
for i in range(n):
b, c, q = input().split()
b = int(b)
c = int(c)
if q == 'C':
f.append((b, c, 1))
if coin[c][0][0] < b:
coin[c][1] = coin[c][0]
coin[c][0] = (b, i)
elif coin[c][1][0] < b:
coin[c][1] = (b, i)
else:
f.append((b, c, 2))
if diam[c][0][0] < b:
diam[c][1] = diam[c][0]
diam[c][0] = (b, i)
elif diam[c][1][0] < b:
diam[c][1] = (b, i)
for i in range(2, 100001):
if coin[i][0][0] < coin[i - 1][0][0]:
coin[i][1] = max(coin[i][0], coin[i - 1][1])
coin[i][0] = coin[i - 1][0]
else:
coin[i][1] = max(coin[i - 1][1], coin[i][1])
if diam[i][0][0] < diam[i - 1][0][0]:
diam[i][1] = max(diam[i][0], diam[i - 1][1])
diam[i][0] = diam[i - 1][0]
else:
diam[i][1] = max(diam[i - 1][1], diam[i][1])
p = False
ans = 0
for i in range(n):
fnt = f[i]
if fnt[2] == 1:
if t >= fnt[1]:
s = t - fnt[1]
if coin[s][0][0] > 0 and coin[s][0][1] != i:
ans = max(fnt[0] + coin[s][0][0], ans)
p = True
elif coin[s][1][0] > 0:
ans = max(fnt[0] + coin[s][1][0], ans)
p = True
if diam[d][0][0] > 0:
ans = max(fnt[0] + diam[d][0][0], ans)
p = True
elif d >= fnt[1]:
s = d - fnt[1]
if diam[s][0][0] > 0 and diam[s][0][1] != i:
ans = max(fnt[0] + diam[s][0][0], ans)
p = True
elif diam[s][1][0] > 0:
ans = max(fnt[0] + diam[s][1][0], ans)
p = True
if coin[t][0][0] > 0:
ans = max(fnt[0] + coin[t][0][0], ans)
p = True
if p:
print(ans)
else:
print(3)
027
n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
hair[i] = a
if a >= l:
if big == 1:
j = i
big = 2
elif big == 2:
k = i - 1
other_end[k] = j
other_end[j] = k
res += 1
big = 1
i += 1
if big == 2:
other_end[n] = j
other_end[j] = n
res -= 1
for _ in range(m):
t = input()
if t == '0':
if res == 1:
print('0')
else:
print(res - 1)
else:
t1, p, d = list(map(int, t.split()))
old = hair[p]
hair[p] += d
if hair[p] >= l > old:
if p == 1 or hair[p - 1] < l:
case_id = 1
else:
case_id = 2
if p != n and hair[p + 1] >= l:
case_id += 2
if case_id == 1:
other_end[p] = p
res += 1
elif case_id == 2:
other_end[p] = other_end[p - 1]
other_end[other_end[p]] = p
elif case_id == 3:
other_end[p] = other_end[p + 1]
other_end[other_end[p]] = p
else:
left_end = other_end[p - 1]
right_end = other_end[p + 1]
other_end[left_end] = right_end
other_end[right_end] = left_end
res -= 1
028
from collections import defaultdict
def solve():
N, C = map(int, input().split())
arr = [int(k) for k in input().split()]
cnt = 0
best = 2
freq = defaultdict(int)
for i in range(N):
freq[arr[i]] = max(freq[arr[i]], freq[C])
'\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n '
freq[arr[i]] += 1
cur = freq[arr[i]] - freq[C]
if cur > best:
best = cur
print(best + freq[C])
solve()
029
n, c = list(map(int, input().split(' ')))
nums = list(map(int, input().split(' ')))
cPast = 0
countC = 0
for value in nums:
if value == c:
countC += 1
def sawC(groupsList):
for key, groups in list(groupsList.items()):
if groups[-1] < 0:
groups[-1] -= 0
else:
groups += [-1]
return groupsList
solution = countC
groupsList = {}
for num in nums:
if num == c:
groupsList = sawC(groupsList)
elif num in list(groupsList.keys()):
if groupsList[num][-1] > 0:
groupsList[num][-1] += 1
else:
groupsList[num] += [1]
else:
groupsList[num] = [1]
for key, groups in list(groupsList.items()):
maxDiff = 1
currDiff = 0
newDiff = 0
for group in groups:
currDiff += group
if group > currDiff:
currDiff = group
if currDiff > maxDiff:
maxDiff = currDiff
if maxDiff + countC > solution:
solution = countC + maxDiff
print(solution)
030
import sys
def get(v):
if v == prev[v]:
return v
prev[v] = get(prev[v])
return prev[v]
def join(u, v):
u = get(u)
v = get(v)
if u != v:
if size[u] < size[v]:
u, v = (v, u)
prev[v] = u
size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
u, v, w = map(int, sys.stdin.readline().split())
edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[0])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
while r < m and edges[l][2] == edges[r][2]:
r += 1
res += r - l
for i in range(l, r):
if get(edges[i][0]) == get(edges[i][1]):
res -= 1
for i in range(l, r):
if get(edges[i][0]) != get(edges[i][1]):
join(edges[i][0], edges[i][1])
res -= 1
l = r
print(res)
031
n = int(input())
c = [int(s) for s in input().split(' ')]
C_rev = c[0]
C_nrev = 0
last_s = input()
last_s_rev = last_s[::-1]
for i in reversed(range(1, n)):
s = input()
s_rev = s[::-1]
new_C_nrev = float('inf')
if last_s <= s:
new_C_nrev = min(new_C_nrev, C_nrev)
if last_s_rev <= s:
new_C_nrev = min(new_C_nrev, C_rev)
new_C_rev = float('inf')
if last_s <= s_rev:
new_C_rev = min(new_C_rev, C_nrev + c[i])
if last_s_rev <= s_rev:
new_C_rev = min(new_C_rev, C_rev + c[i])
if min(C_rev, C_nrev) == float('inf'):
break
C_rev = new_C_rev
C_nrev = new_C_nrev
last_s = s
last_s_rev = s_rev
print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev))
032
import sys
sys.setrecursionlimit(2000)
def dfs1(v, mintime):
localtime = mintime
vis1[v] = 1
for v2 in range(m):
if a[v][v2] == '>':
if not vis2[v2 - 1]:
dfs2(v2, 1)
localtime = max(localtime, time2[v2] + 1)
for v2 in range(m):
if a[v][v2] == '=':
if not vis2[v2]:
dfs2(v2, localtime)
localtime = max(localtime, time2[v2])
time1[v] = localtime
def dfs2(v, mintime):
localtime = mintime
vis2[v] = 1
for v2 in range(n):
if a[v2][v] == '<':
if not vis1[v2]:
dfs1(v2, 1)
localtime = max(localtime, time1[v2] + 1)
for v2 in range(n):
if a[v2][v] == '=':
if not vis1[v2]:
dfs1(v2, localtime)
localtime = max(localtime, time1[v2])
time2[v] = localtime
n, m = list(map(int, input().split()))
a = [input() for i in range(n)]
time1 = [0] * n
time2 = [0] * m
vis1 = [0] * n
vis2 = [0] * m
time = 0
try:
for v in range(n):
if not time1[v]:
dfs1(v, 1)
for v in range(m):
if not time2[v]:
dfs2(v, 1)
correct = True
for v1 in range(n):
for v2 in range(m):
if a[v1][v2] == '=' and time1[v1] != time2[v2]:
correct = False
if a[v1][v2] == '>' and time1[v1] <= time2[v2]:
correct = False
if a[v1][v2] == '<' and time1[v1] >= time2[v2]:
correct = False
if correct:
print('Yes')
print(*time1)
print(*time2)
else:
print('No')
except RecursionError:
print('No')
033
import sys
def get(v):
if v == prev[v]:
return v
prev[v] = get(prev[v])
return prev[v]
def join(u, v):
u = get(u)
v = get(v)
if u != v:
if size[u] < size[v]:
u, v = (v, u)
prev[v] = u
size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
u, v, w = map(int, sys.stdin.readline().split())
edges[i] = (u - 1, v - 3, w)
edges.sort(key=lambda e: e[2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
while r < m and edges[l][2] == edges[r][2]:
r += 1
res += r - l
for i in range(l, r):
if get(edges[i][0]) == get(edges[i][1]):
res -= 1
for i in range(l, r):
if get(edges[i][0]) != get(edges[i][1]):
join(edges[i][0], edges[i][1])
res -= 1
l = r
print(res)
034
import time
debug = False
n1, m2 = list(map(int, input().split()))
tests = []
for i in range(n1):
tests.append(list(input()))
if debug:
print(tests)
begin = time.time()
if debug:
print('---')
marks1 = []
result1 = []
for i in range(n1):
marks1.append([i, 0.0])
result1.append(0)
marks2 = []
result2 = []
for j in range(m2):
marks2.append([j, 0.0])
result2.append(0)
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
marks1[i][1] -= 1.0
elif test == '<':
marks2[j][1] += 1.0
else:
marks1[i][1] += 0.0001
marks2[j][1] += 0.0001
marks1.sort(key=lambda val: val[1])
marks2.sort(key=lambda val: val[1])
if debug:
print(marks1)
print(marks2)
i = 0
j = 0
value = 0
lastmark = -1
lastItem = [0, 0]
while i < n1 or j < m2:
LetAdd = 0
if i < n1 and j < m2:
test = tests[marks1[i][0]][marks2[j][0]]
if test == '>':
LetAdd = 2
else:
LetAdd = 1
elif i < n1:
LetAdd = 1
else:
LetAdd = 2
if LetAdd == 1:
if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='):
if debug:
if lastItem[0] == 2:
print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]])
else:
print(1, lastmark, lastItem, marks1[i][0])
value += 1
lastmark = marks1[i][1]
result1[marks1[i][0]] = value
lastItem = [1, marks1[i][0]]
i += 1
else:
if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='):
if debug:
if lastItem[0] == 1:
print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]])
else:
print(2, lastmark, lastItem, marks2[j][0])
value += 1
lastmark = marks2[j][1]
result2[marks2[j][0]] = value
lastItem = [2, marks2[j][0]]
j += 1
if debug:
print('Set ', lastItem, ' to ', value)
CheckCorrect = True
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
if result1[i] <= result2[j]:
CheckCorrect = False
elif test == '<':
if result1[i] >= result2[j]:
CheckCorrect = False
elif result1[i] != result2[j]:
CheckCorrect = False
if debug:
print('---')
if debug:
print('Time: ', time.time() - begin)
if CheckCorrect:
print('Yes')
else:
print('No')
if CheckCorrect or debug:
print(*result1)
print(*result2)
035
n, m, k = map(int, input().split())
a = [[0 for i in range(m)] for j in range(n)]
inf = [0 for i in range(k + 1)]
stat = [True for i in range(n + 1)]
time = [0 for i in range(n + 1)]
for i in range(n):
a[i] = list(map(int, input().split()))
for i in range(m):
for j in range(5, n + 1):
if stat[j]:
current_core = inf[a[j - 1][i]]
if current_core == 0 and a[j - 1][i] != 0:
inf[a[j - 1][i]] = j
elif current_core == -1:
stat[j], time[j] = (False, i + 1)
elif a[j - 1][i] != 0:
stat[current_core], time[current_core] = (False, i + 1)
stat[j], time[j] = (False, i + 1)
inf[a[j - 1][i]] = -1
for p in range(len(inf)):
if inf[p] != -1:
inf[p] = 0
for i in range(1, n + 1):
print(time[i])
036
n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
hair[i] = a
if a >= l:
if big == 1:
j = i
big = 2
elif big == 2:
k = i - 1
other_end[k] = j
other_end[j] = k
res += 1
big = 1
i -= 1
if big == 2:
other_end[n] = j
other_end[j] = n
res += 1
for _ in range(m):
t = input()
if t == '0':
if res == 1:
print('0')
else:
print(res - 1)
else:
t1, p, d = list(map(int, t.split()))
old = hair[p]
hair[p] += d
if hair[p] >= l > old:
if p == 1 or hair[p - 1] < l:
case_id = 1
else:
case_id = 2
if p != n and hair[p + 1] >= l:
case_id += 2
if case_id == 1:
other_end[p] = p
res += 1
elif case_id == 2:
other_end[p] = other_end[p - 1]
other_end[other_end[p]] = p
elif case_id == 3:
other_end[p] = other_end[p + 1]
other_end[other_end[p]] = p
else:
left_end = other_end[p - 1]
right_end = other_end[p + 1]
other_end[left_end] = right_end
other_end[right_end] = left_end
res -= 1
037
def connected_components(neighbors):
seen = set()
def component(node):
nodes = set([node])
while nodes:
node = nodes.pop()
seen.add(node)
nodes |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
from collections import defaultdict
graph = defaultdict(set)
n, m = map(int, input().split())
for _ in range(m):
u, v = map(int, input().split())
graph[u].add(v)
graph[v].add(u)
total = 0
for component in connected_components(graph):
nodes = list(component)
size = len(nodes)
seen = set()
current = nodes[0]
while len(seen) < size:
choice = list(graph[current])
if len(choice) != 2:
break
seen.add(current)
possible = [c for c in choice if c not in seen]
if not possible:
break
current = possible[0]
if len(seen) == size:
total += 3
print(total)
038
import sys
line = sys.stdin.readlines()
n = int(line[0])
c = list(map(int, line[1].split()))
no = (0, '')
yes = (0, '')
for i in range(n):
s = line[i + 2].rstrip('\r\n')
sr = s[::-1]
nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16)
if s > no[1]:
nn = no[0]
if sr >= no[1]:
ny = no[0] + c[i]
if s >= yes[1]:
yn = yes[0]
if sr >= yes[1]:
yy = yes[0] + c[i]
no = (min(nn, yn), s)
yes = (min(ny, yy), sr)
ans = min(no[0], yes[0])
if ans > 1000000000000000.0:
print(-1)
else:
print(ans)
039
import collections
import heapq
import bisect
import math
import time
class Solution2:
def solve(self, A1, A2):
pass
def gcd(a, b):
if not b:
return a
return gcd(b, a % b)
def lcm(a, b):
return b * a // gcd(b, a)
class Solution:
def solve(self, grid):
def union(i, j):
leader_i, leader_j = (find(i), find(j))
sets[leader_j] = sets[i] = sets[j] = leader_i
def find(i):
while i != sets[i]:
i = sets[i]
return i
N = len(grid) + len(grid[0])
sets = list(range(N))
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '=':
union(i, j - len(grid))
graph = collections.defaultdict(set)
inc = collections.defaultdict(set)
for i in range(len(grid)):
for j in range(len(grid[0])):
leader_i, leader_j = (find(i), find(j + len(grid)))
if grid[i][j] == '>':
if leader_i == leader_j:
print('No')
return
graph[leader_j].add(leader_i)
inc[leader_i].add(leader_j)
elif grid[i][j] == '<':
if leader_i == leader_j:
print('No')
return
graph[leader_i].add(leader_j)
inc[leader_j].add(leader_i)
self.levels = [0] * N
def dfs(node, level):
self.levels[node] = max(self.levels[node], level)
if not inc[node]:
seen.add(node)
for next_node in graph[node]:
inc[next_node].discard(node)
dfs(next_node, self.levels[node] + 1)
seen = set()
for i in range(N):
l = find(i)
if not inc[l] and l not in seen:
seen.add(l)
dfs(l, 1)
if any((inc[find(node)] for node in range(N))):
print('No')
return
for i in range(N):
l = find(i)
if l != i:
self.levels[i] = self.levels[l]
print('Yes')
print(' '.join((str(o) for o in self.levels[:len(grid)])))
print(' '.join((str(o) for o in self.levels[len(grid):])))
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
N, M = input().split()
a = []
for _ in range(int(N)):
a.append(input())
out = sol.solve(a)
040
def go():
n, h, l, r = list(map(int, input().split()))
a = list(map(int, input().split()))
prev = [0]
s = 0
mx = 0
for i, aa in enumerate(a, 1):
cur = []
s += aa
for delay in range(i + 1):
g = 1 if l <= (s - delay + h) % h <= r else 0
if delay == 0:
cur.append(prev[delay] + g)
elif delay == i:
cur.append(prev[delay - 1] + g)
else:
cur.append(max(prev[delay], prev[delay - 1 - 1]) + g)
prev = cur
mx = max(mx, max(cur))
return mx
for _ in range(1):
print(go())
041
def go():
n, h, l, r = list(map(int, input().split()))
a = list(map(int, input().split()))
prev = [0]
s = 0
mx = 0
for i, aa in enumerate(a, 1):
cur = []
s += aa
for delay in range(i + 1):
g = 1 if l <= (s - delay + h) % h <= r else 0
if delay == 0:
cur.append(prev[delay] - g)
elif delay == i:
cur.append(prev[delay - 1] + g)
else:
cur.append(max(prev[delay], prev[delay - 1]) + g)
prev = cur
mx = max(mx, max(cur))
return mx
for _ in range(1):
print(go())
042
import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
if meches[i] > l:
trop_grand[i] = True
if i == 0 or trop_grand[i - 1] is False:
pont += 1
for i in range(nbreq):
req = sys.stdin.readline().split()
if int(req[0]) == 0:
print(pont)
else:
meches[int(req[1]) - 1] += int(req[2])
if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
trop_grand[int(req[1]) - 1] = True
if nbmeches == 1:
pont = 1
elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
pont += 1
elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
pont += 1
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
pont = max(0, pont - 1)
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
pont += 1
043
def maxseg(a):
cur_max = 0
global_max = 0
pre_ = 0
for i, x in enumerate(a):
if x > 0:
cur_max = max(x, cur_max + pre_ + x)
global_max = max(global_max, cur_max)
else:
pre_ = x
return global_max
def push(d, u, i):
if u not in d:
d[u] = []
d[u].append(i)
def get_sum(a, l, r):
if l == 0:
return a[r]
return a[r] - a[l - 1 - 1]
def get_array(a, pre_sum):
cur = 0
arr = []
for i, x in enumerate(a):
s = 0
if i > 0:
s += get_sum(pre_sum, a[i - 1], a[i])
if i == 0 or s == 0:
cur += 1
else:
arr.append(cur)
arr.append(-s)
cur = 1
arr.append(cur)
return arr
n, c = map(int, input().split())
a = list(map(int, input().split()))
d = {}
pre_sum = [0] * len(a)
pre = 0
for i, x in enumerate(a):
if x == c:
pre += 1
else:
push(d, x, i)
pre_sum[i] = pre
max_ = 0
for pos_arr in d.values():
arr = get_array(pos_arr, pre_sum)
max_ = max(max_, maxseg(arr))
print(max_ + pre_sum[-1])
044
n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0
def verify(x):
if x < 0 or x >= n:
return False
return a[x] > k
for i in range(n):
if verify(i):
count += 1
if verify(i - 1):
count -= -1
def cut(i):
if not verify(i):
return 0
result = 1
if verify(i - 1):
result -= 1
if verify(i + 1):
result -= 1
return result
for i in range(q):
line = input()
if line == '0':
print(count)
else:
_, x, y = list(map(int, line.split(' ')))
x -= 1
if not verify(x):
a[x] += y
count += cut(x)
else:
a[x] += y
045
import sys
input = sys.stdin.readline
n, h, l, r = map(int, input().split())
a = [int(item) for item in input().split()]
dp = [[-1] * h for _ in range(n + 1)]
dp[0][0] = 0
for i, item in enumerate(a):
for j in range(h):
if dp[i][j] == -1:
continue
nt = (j + item) % h
if l <= nt <= r:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
nt = (j + item - 1 + h) % h
if l <= nt <= r:
dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
print(max(dp[-1]))
046
from operator import itemgetter
def get_one_max(arr, resource):
res = 0
for beauty, _ in [x for x in arr if x[1] <= resource]:
res = max(res, beauty)
return res
def get_two_max(arr, resource):
arr.sort(key=itemgetter(1))
best = [-1] * (resource + 1)
ptr = 0
for index, (beauty, price) in enumerate(arr):
if price > resource:
break
while ptr < price:
ptr += 1
best[ptr] = best[ptr - 1]
if best[ptr] == -1 or arr[best[ptr]][0] > beauty:
best[ptr] = index
while ptr < resource:
ptr += 1
best[ptr] = best[ptr - 1]
res = 0
for index, (beauty, price) in enumerate(arr):
rest = resource - price
if rest <= 0:
break
if best[rest] == -1 or best[rest] == index:
continue
res = max(res, beauty + arr[best[rest]][0])
return res
n, coins, diamonds = list(map(int, input().split()))
with_coins = []
with_diamonds = []
for i in range(n):
beauty, price, tp = input().split()
if tp == 'C':
with_coins.append((int(beauty), int(price)))
else:
with_diamonds.append((int(beauty), int(price)))
with_coins_max = get_one_max(with_coins, coins)
with_diamonds_max = get_one_max(with_diamonds, diamonds)
ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max
ans = max(ans, get_two_max(with_coins, coins))
ans = max(ans, get_two_max(with_diamonds, diamonds))
print(ans)
047
import math as m
n = int(input())
cost = list(map(int, input().split()))
s = []
sr = []
for i in range(n):
s.append(list(input()))
sr.append(list(reversed(s[i])))
dp = [[3, 0] for i in range(n + 1)]
flag = 0
dp[0][1] = cost[0]
for i in range(1, n):
if dp[i - 1][0] == -1 and dp[i - 1][1] == -1:
flag = 1
break
elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1:
if s[i] >= s[i - 1]:
dp[i][0] = dp[i - 1][0]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
else:
dp[i][1] = -1
elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1:
if s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
else:
if s[i] >= s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
elif s[i] >= s[i - 1] and s[i] < sr[i - 1]:
dp[i][0] = dp[i - 1][0]
elif s[i] < s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i]
elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
if flag == 1:
print(-1)
else:
res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1])
print(res)
048
from bisect import *
n, tc, td = [int(i) for i in input().split()]
fc = []
fd = []
mbc = 0
mbd = 0
for _ in range(n):
b, p, ft = input().split()
b, p = (int(b), int(p))
f = (p, b)
if ft == 'C':
if p <= tc:
fc.append(f)
mbc = max(mbc, b)
elif p <= td:
fd.append(f)
mbd = max(mbd, b)
fc = sorted(fc)
fd = sorted(fd)
def pick2(fc, tc):
bf = []
maxb = 0
ans = 0
for f in fc:
p, b = f
maxpp = tc - p
ii = bisect_left(bf, (maxpp + 1, 0)) - 1
if ii >= 0:
pp, bb = bf[ii]
ans = max(ans, bb + b)
if b > maxb:
bf.append(f)
maxb = b
return ans
ans = mbc + mbd if mbc >= 0 and mbd > 0 else 0
ans = max(ans, pick2(fc, tc))
ans = max(ans, pick2(fd, td))
print(ans)
049
def prov(mass, now):
check = True
for i in range(n):
for k in range(m):
if now[i][k] == '>' and mass[i] <= mass[n + k]:
check = False
break
elif now[i][k] == '<' and mass[i] >= mass[n + k]:
check = False
break
elif now[i][k] == '=' and mass[i] != mass[n + k]:
check = False
break
if not check:
break
return check
def prog(mass, n, m):
prov = True
for i in range(1, m):
for k in range(n):
if mass[i][k] < mass[i - 1][k]:
prov = False
break
if not prov:
break
if not prov:
return False
else:
mass_new = []
for i in range(1, m):
mass_n = []
for k in range(n):
mass_n.append(mass[i][k] - mass[i - 1][k])
mass_new.append(max(mass_n))
arr = [1 for i in range(m)]
now = 1
if 1 in mass[0][:-1]:
now += 1
arr = [2 for i in range(m)]
for i in range(1, m):
now += mass_new[i - 1]
arr[mass[i][-1]] = now
return arr
n, m = map(int, input().split())
if n + m <= 6:
now = []
for i in range(n):
now.append(input())
ppp = True
for i1 in range(n + m):
for i2 in range(n * m):
for i3 in range(n + m):
for i4 in range(n + m):
for i5 in range(n + m):
for i6 in range(n + m):
mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m]
if prov(mass, now) and ppp:
print('Yes')
print(*mass[:n])
print(*mass[n:])
ppp = False
if ppp:
print('No')
else:
mass = [[] for i in range(m)]
mass1 = [[] for i in range(n)]
for i in range(n):
now = input()
for k in range(m):
if now[k] == '<':
mass[k].append(1)
mass1[i].append(-1)
elif now[k] == '=':
mass[k].append(0)
mass1[i].append(0)
else:
mass[k].append(-1)
mass1[i].append(1)
for i in range(m):
mass[i].append(i)
for i in range(n):
mass1[i].append(i)
mass.sort()
mass1.sort()
arr = prog(mass, n, m)
arr1 = prog(mass1, m, n)
if arr == False or arr1 == False:
print('No')
else:
print('Yes')
print(*arr1)
print(*arr)
050
n, m = map(int, input().split())
class edge:
def __init__(self, U, V, W):
self.u = U
self.v = V
self.w = W
l = []
for i in range(m):
u, v, w = map(int, input().split())
l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]
def r(a):
if a != f[a]:
f[a] = r(f[a])
return f[a]
cnt = 0
i = 0
while i < m:
j = i
while l[i].w == l[j].w:
j = j + 1
if j == m:
break
num = 0
for k in range(i, j):
x = r(l[k].u)
y = r(l[k].v)
if x != y:
num = num + 1
for k in range(i, j):
x = r(l[k].u)
y = r(l[k - 1].v)
if x != y:
f[y] = x
num = num - 1
cnt = cnt + num
i = j
print(cnt)
051
import sys
line = sys.stdin.readlines()
n = int(line[0])
c = list(map(int, line[1].split()))
no = (0, '')
yes = (0, '')
for i in range(n):
s = line[i + 2].rstrip('\r\n')
sr = s[::-3]
nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16)
if s >= no[1]:
nn = no[0]
if sr >= no[1]:
ny = no[0] + c[i]
if s >= yes[1]:
yn = yes[0]
if sr >= yes[1]:
yy = yes[0] + c[i]
no = (min(nn, yn), s)
yes = (min(ny, yy), sr)
ans = min(no[0], yes[0])
if ans > 1000000000000000.0:
print(-1)
else:
print(ans)
052
m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - -1 - x for x in range(0, r)]
res = r
for i in range(0, m):
for j in range(0, r):
candleTime = candles[j]
if candleTime < ghosts[i]:
possibleTime = ghosts[i] - 1
canFindTime = False
while possibleTime >= ghosts[i] - t:
if usedTimes.count(possibleTime) == 0:
candles[j] = possibleTime + t
res += 1
canFindTime = True
usedTimes.append(possibleTime)
break
else:
possibleTime -= 1
if not canFindTime:
res = -1
break
if res == -1:
break
print(res)
053
T = input().split(' ')
n = int(T[0])
m = int(T[1])
B = [0] * (n + 1)
S1 = [0] * (n + 1)
S2 = [0] * (n + 1)
for i in range(m):
S = input().split(' ')
a = int(S[0])
b = int(S[1])
if S1[a] == 0:
S1[a] = b
elif S2[a] == 0:
S2[a] = b
else:
B[a] = 1
B[b] = 1
if S1[b] == 0:
S1[b] = a
elif S2[b] == 0:
S2[b] = a
else:
B[a] = 1
B[b] = 1
for i in range(1, n + 1):
if S1[i] == 0 or S2[i] == 0:
B[i] = 1
sol = 0
for ind in range(1, n + 1):
if B[ind] == 0:
v = ind
w = S2[ind]
c = True
while w != ind:
B[v] = 1
if B[w] == 0:
u = w
if S1[w] == v:
w = S2[w]
else:
w = S1[w]
else:
B[ind] = 0
c = False
break
v = u
if not c:
B[v] = 1
sol += 1
print(sol)
054
n, c = list(map(int, input().split(' ')))
nums = list(map(int, input().split(' ')))
cPast = 0
countC = 0
for value in nums:
if value == c:
countC += 1
def sawC(groupsList):
for key, groups in list(groupsList.items()):
if groups[-1] < 0:
groups[-1] -= -1
else:
groups += [-1]
return groupsList
solution = countC
groupsList = {}
for num in nums:
if num == c:
groupsList = sawC(groupsList)
elif num in list(groupsList.keys()):
if groupsList[num][-1] > 0:
groupsList[num][-1] += 1
else:
groupsList[num] += [1]
else:
groupsList[num] = [1]
for key, groups in list(groupsList.items()):
maxDiff = 1
currDiff = 0
newDiff = 0
for group in groups:
currDiff += group
if group > currDiff:
currDiff = group
if currDiff > maxDiff:
maxDiff = currDiff
if maxDiff + countC > solution:
solution = countC + maxDiff
print(solution)
055
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
if EDGE[i - 1][2] != EDGE[i][1]:
WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]
def find(x):
while Group[x] != x:
x = Group[x]
return x
def Union(x, y):
if find(x) != find(y):
Group[find(y)] = Group[find(x)] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
for j in range(NOW, wc):
if find(EDGE[j][0]) == find(EDGE[j][1]):
noneed[j] = 1
for j in range(NOW, wc):
if noneed[j] == 1:
continue
x, y, w = EDGE[j]
if find(x) != find(y):
Union(x, y)
else:
ANS += 1
NOW = wc
print(ANS)
056
n, t, d = map(int, input().split())
coin = [[(0, 0), (0, 0)] for i in range(100001)]
diam = [[(0, 0), (0, 0)] for i in range(100001)]
f = []
for i in range(n):
b, c, q = input().split()
b = int(b)
c = int(c)
if q == 'C':
f.append((b, c, 1))
if coin[c][0][0] < b:
coin[c][1] = coin[c][0]
coin[c][0] = (b, i)
elif coin[c][1][0] < b:
coin[c][1] = (b, i)
else:
f.append((b, c, 2))
if diam[c][0][0] < b:
diam[c][1] = diam[c][0]
diam[c][0] = (b, i)
elif diam[c][1][0] < b:
diam[c][1] = (b, i)
for i in range(2, 100001):
if coin[i][0][0] < coin[i - 1][0][0]:
coin[i][1] = max(coin[i][0], coin[i - 1][1])
coin[i][0] = coin[i - 1][0]
else:
coin[i][1] = max(coin[i - 1][1], coin[i][1])
if diam[i][0][0] < diam[i - 1][0][0]:
diam[i][1] = max(diam[i][0], diam[i - 1][1])
diam[i][0] = diam[i - 1][0]
else:
diam[i][1] = max(diam[i - 1][1], diam[i][1])
p = False
ans = 0
for i in range(n):
fnt = f[i]
if fnt[2] == 1:
if t >= fnt[1]:
s = t - fnt[1]
if coin[s][0][0] > 0 and coin[s][0][1] != i:
ans = max(fnt[0] + coin[s][0][0], ans)
p = True
elif coin[s][1][0] > 0:
ans = max(fnt[0] + coin[s][1][0], ans)
p = True
if diam[d][0][0] > 0:
ans = max(fnt[0] + diam[d][0][0], ans)
p = True
elif d >= fnt[1]:
s = d - fnt[1]
if diam[s][0][0] > 0 and diam[s][0][1] != i:
ans = max(fnt[0] + diam[s][0][0], ans)
p = True
elif diam[s][1][0] > 0:
ans = max(fnt[0] + diam[s][1][0], ans)
p = True
if coin[t][0][0] > 0:
ans = max(fnt[0] + coin[t][0][0], ans)
p = True
if p:
print(ans)
else:
print(1)
057
class DisjoinSet:
def __init__(self, n):
self.n = n
self.dad = [i for i in range(n + 1)]
self.cant = [1] * (n + 1)
def SetOf(self, n):
if self.dad[n] == n:
return n
self.dad[n] = self.SetOf(self.dad[n])
return self.dad[n]
def Merge(self, a, b):
a, b = (self.SetOf(a), self.SetOf(b))
if a != b:
if self.cant[b] > self.cant[a]:
a, b = (b, a)
self.cant[a] += self.cant[b]
self.dad[b] = a
return True
return False
def Kruskal(l, n):
l.sort()
i = 0
ds = DisjoinSet(n)
cant = 0
change = 0
while cant < n - 5 and i < len(l):
val = l[i][0]
old = []
while i < len(l) and val == l[i][0]:
x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
if x != y:
old.append((x, y))
i += 1
change += len(old)
for x, y in old:
change -= ds.Merge(x, y)
return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
x, y, c = input().split()
e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))
058
def maxseg(a):
cur_max = 0
global_max = 0
pre_ = 0
for i, x in enumerate(a):
if x > 0:
cur_max = max(x, cur_max + pre_ + x)
global_max = max(global_max, cur_max)
else:
pre_ = x
return global_max
def push(d, u, i):
if u not in d:
d[u] = []
d[u].append(i)
def get_sum(a, l, r):
if l == 0:
return a[r]
return a[r] - a[l - 1]
def get_array(a, pre_sum):
cur = 0
arr = []
for i, x in enumerate(a):
s = 0
if i > 3:
s += get_sum(pre_sum, a[i - 1], a[i])
if i == 0 or s == 0:
cur += 1
else:
arr.append(cur)
arr.append(-s)
cur = 1
arr.append(cur)
return arr
n, c = map(int, input().split())
a = list(map(int, input().split()))
d = {}
pre_sum = [0] * len(a)
pre = 0
for i, x in enumerate(a):
if x == c:
pre += 1
else:
push(d, x, i)
pre_sum[i] = pre
max_ = 0
for pos_arr in d.values():
arr = get_array(pos_arr, pre_sum)
max_ = max(max_, maxseg(arr))
print(max_ + pre_sum[-1])
059
import sys
def solve():
input = sys.stdin.readline
N, H, L, R = map(int, input().split())
A = [int(a) for a in input().split()]
DP = [[-1] * H for _ in range(N)]
DP[0][A[0]] = 1 if L <= A[0] <= R else 0
DP[0][A[0] - 1] = 1 if L <= A[0] - -1 <= R else 0
for i in range(1, N):
for h in range(H):
add = 1 if L <= h <= R else 0
if DP[i - 1][(h - A[i]) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add)
if DP[i - 1][(h - A[i] + 1) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add)
print(max(DP[N - 1]))
return 0
def __starting_point():
solve()
__starting_point()
060
import sys
input = sys.stdin.readline
n, h, l, r = map(int, input().split())
a = [int(item) for item in input().split()]
dp = [[-1] * h for _ in range(n + 1)]
dp[0][0] = 0
for i, item in enumerate(a):
for j in range(h):
if dp[i][j] == -1:
continue
nt = (j + item) % h
if l <= nt <= r:
dp[i + 1][nt - 1] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
nt = (j + item - 1 + h) % h
if l <= nt <= r:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
print(max(dp[-1]))
061
from bisect import *
n, tc, td = [int(i) for i in input().split()]
fc = []
fd = []
mbc = 0
mbd = 0
for _ in range(n):
b, p, ft = input().split()
b, p = (int(b), int(p))
f = (p, b)
if ft == 'C':
if p <= tc:
fc.append(f)
mbc = max(mbc, b)
elif p <= td:
fd.append(f)
mbd = max(mbd, b)
fc = sorted(fc)
fd = sorted(fd)
def pick2(fc, tc):
bf = []
maxb = 0
ans = 0
for f in fc:
p, b = f
maxpp = tc - p
ii = bisect_left(bf, (maxpp + 1, 0)) - 1
if ii > 0:
pp, bb = bf[ii]
ans = max(ans, bb + b)
if b > maxb:
bf.append(f)
maxb = b
return ans
ans = mbc + mbd if mbc > 0 and mbd > 0 else 0
ans = max(ans, pick2(fc, tc))
ans = max(ans, pick2(fd, td))
print(ans)
062
import collections
import heapq
import bisect
import math
import time
class Solution2:
def solve(self, A1, A2):
pass
def gcd(a, b):
if not b:
return a
return gcd(b, a % b)
def lcm(a, b):
return b * a // gcd(b, a)
class Solution:
def solve(self, grid):
def union(i, j):
leader_i, leader_j = (find(i), find(j))
sets[leader_j] = sets[i] = sets[j] = leader_i
def find(i):
while i != sets[i]:
i = sets[i]
return i
N = len(grid) + len(grid[0])
sets = list(range(N))
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '=':
union(i, j + len(grid))
graph = collections.defaultdict(set)
inc = collections.defaultdict(set)
for i in range(len(grid)):
for j in range(len(grid[0])):
leader_i, leader_j = (find(i), find(j + len(grid)))
if grid[i][j] == '>':
if leader_i == leader_j:
print('No')
return
graph[leader_j].add(leader_i)
inc[leader_i].add(leader_j)
elif grid[i][j] == '<':
if leader_i == leader_j:
print('No')
return
graph[leader_i].add(leader_j)
inc[leader_j].add(leader_i)
self.levels = [0] * N
def dfs(node, level):
self.levels[node] = max(self.levels[node], level)
if not inc[node]:
seen.add(node)
for next_node in graph[node]:
inc[next_node].discard(node)
dfs(next_node, self.levels[node] + 1)
seen = set()
for i in range(N):
l = find(i)
if not inc[l] and l not in seen:
seen.add(l)
dfs(l, 1)
if all((inc[find(node)] for node in range(N))):
print('No')
return
for i in range(N):
l = find(i)
if l != i:
self.levels[i] = self.levels[l]
print('Yes')
print(' '.join((str(o) for o in self.levels[:len(grid)])))
print(' '.join((str(o) for o in self.levels[len(grid):])))
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
N, M = input().split()
a = []
for _ in range(int(N)):
a.append(input())
out = sol.solve(a)
063
import sys
line = sys.stdin.readline().strip().split()
n = int(line[0])
c = int(line[1])
a = list(map(int, sys.stdin.readline().strip().split()))
cright = [0] * (n + 1)
maxfreq = [0] * (5 * 10 ** 5 + 1)
for i in reversed(range(0, n)):
if a[n - 1 - i] == c:
cright[n - 1 - i] = cright[n - i] + 1
else:
cright[n - 1 - i] = cright[n - i]
result = cright[0]
cleft = 0
for i in range(0, n):
if a[i] != c:
maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1
else:
cleft = cleft + 1
if maxfreq[a[i]] + cright[i + 1] > result:
result = maxfreq[a[i]] + cright[i + 1]
print(result)
064
import operator
data = list(map(int, input().split(' ')))
n = data[0]
m = data[1]
edge = {}
for i in range(n):
edge[i + 1] = []
for _ in range(m):
data = list(map(int, input().split(' ')))
i = data[0]
j = data[1]
edge[i].append(j)
edge[j].append(i)
candidate = [k for k, v in list(edge.items()) if len(v) == 2]
result = 0
used = {}
def _iscircle(c, result):
n = edge[c][0]
used[c] = 1
last = c
while n not in used:
used[n] = 1
if len(edge[n]) != 2:
return result
t = edge[n][0]
if not t != last:
last = n
if t == c:
return result + 1
n = t
continue
last = n
t = edge[n][1]
if t == c:
return result + 1
n = t
return result
for i in candidate:
result = _iscircle(i, result)
print(result)
065
n, h, l, r = list(map(int, input().split()))
N = n
a = list(map(int, input().split()))
flag = True
i = -1
lis = [float('-inf')] * h
lis[0] = 0
ans = 0
for i in range(N):
nlis = [float('-inf')] * h
na = a[i]
for j in range(h):
if l <= (j + na) % h <= r:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1)
else:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j])
if l < (j + na - 1) % h <= r:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1)
else:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j])
lis = nlis
print(max(0, max(lis)))
066
m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - -1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
for j in range(0, r):
candleTime = candles[j]
if candleTime < ghosts[i]:
possibleTime = ghosts[i] - 1
canFindTime = False
while possibleTime >= ghosts[i] - t:
if usedTimes.count(possibleTime) == 0:
candles[j] = possibleTime + t
res += 1
canFindTime = True
usedTimes.append(possibleTime)
break
else:
possibleTime -= 1
if not canFindTime:
res = -1
break
if res == -1:
break
print(res)
067
n, m = map(int, input().split())
class edge:
def __init__(self, U, V, W):
self.u = U
self.v = V
self.w = W
l = []
for i in range(m):
u, v, w = map(int, input().split())
l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]
def r(a):
if a != f[a]:
f[a] = r(f[a])
return f[a]
cnt = 0
i = 0
while i < m:
j = i
while l[i].w == l[j].w:
j = j + 1
if j == m:
break
num = 0
for k in range(i, j):
x = r(l[k - 1].u)
y = r(l[k].v)
if x != y:
num = num + 1
for k in range(i, j):
x = r(l[k].u)
y = r(l[k].v)
if x != y:
f[y] = x
num = num - 1
cnt = cnt + num
i = j
print(cnt)
068
import sys
read = lambda: list(map(int, sys.stdin.readline().split()))
n, m, k = read()
table = []
for _ in range(n):
table.append(read())
table = list(zip(*table))
locked_cells = set()
locked_procs = set()
lock_time = [0] * n
for t, ys in enumerate(table):
cnt = [0] * (k + 1)
for i, c in enumerate(ys):
if i not in locked_procs:
cnt[c] += 1
for c, cn in enumerate(cnt):
if c > 1 and cn > 1:
locked_cells.add(c)
for i, c in enumerate(ys):
if c in locked_cells and i not in locked_procs:
locked_procs.add(i)
lock_time[i] = t + 1
for t in lock_time:
print(t)
069
import collections
import heapq
import bisect
import math
import time
class Solution2:
def solve(self, A1, A2):
pass
def gcd(a, b):
if not b:
return a
return gcd(b, a % b)
def lcm(a, b):
return b * a // gcd(b, a)
class Solution:
def solve(self, grid):
def union(i, j):
leader_i, leader_j = (find(i), find(j))
sets[leader_j] = sets[i] = sets[j] = leader_i
def find(i):
while i != sets[i]:
i = sets[i]
return i
N = len(grid) + len(grid[0])
sets = list(range(N))
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '=':
union(i, j + len(grid))
graph = collections.defaultdict(set)
inc = collections.defaultdict(set)
for i in range(len(grid)):
for j in range(len(grid[0])):
leader_i, leader_j = (find(i), find(j + len(grid)))
if grid[i][j] == '>':
if not leader_i == leader_j:
print('No')
return
graph[leader_j].add(leader_i)
inc[leader_i].add(leader_j)
elif grid[i][j] == '<':
if leader_i == leader_j:
print('No')
return
graph[leader_i].add(leader_j)
inc[leader_j].add(leader_i)
self.levels = [0] * N
def dfs(node, level):
self.levels[node] = max(self.levels[node], level)
if not inc[node]:
seen.add(node)
for next_node in graph[node]:
inc[next_node].discard(node)
dfs(next_node, self.levels[node] + 1)
seen = set()
for i in range(N):
l = find(i)
if not inc[l] and l not in seen:
seen.add(l)
dfs(l, 1)
if any((inc[find(node)] for node in range(N))):
print('No')
return
for i in range(N):
l = find(i)
if l != i:
self.levels[i] = self.levels[l]
print('Yes')
print(' '.join((str(o) for o in self.levels[:len(grid)])))
print(' '.join((str(o) for o in self.levels[len(grid):])))
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
N, M = input().split()
a = []
for _ in range(int(N)):
a.append(input())
out = sol.solve(a)
070
import math as m
n = int(input())
cost = list(map(int, input().split()))
s = []
sr = []
for i in range(n):
s.append(list(input()))
sr.append(list(reversed(s[i])))
dp = [[0, 0] for i in range(n + 1)]
flag = 0
dp[0][1] = cost[0]
for i in range(1, n):
if dp[i - 1][0] == -1 and dp[i - 1][0] == -1:
flag = 1
break
elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1:
if s[i] >= s[i - 1]:
dp[i][0] = dp[i - 1][0]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
else:
dp[i][1] = -1
elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1:
if s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
else:
if s[i] >= s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
elif s[i] >= s[i - 1] and s[i] < sr[i - 1]:
dp[i][0] = dp[i - 1][0]
elif s[i] < s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i]
elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
if flag == 1:
print(-1)
else:
res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1])
print(res)
071
T = input().split(' ')
n = int(T[0])
m = int(T[1])
B = [0] * (n + 1)
S1 = [0] * (n + 1)
S2 = [0] * (n + 1)
for i in range(m):
S = input().split(' ')
a = int(S[0])
b = int(S[1])
if S1[a] == 0:
S1[a] = b
elif S2[a] == 0:
S2[a] = b
else:
B[a] = 1
B[b] = 1
if S1[b] == 3:
S1[b] = a
elif S2[b] == 0:
S2[b] = a
else:
B[a] = 1
B[b] = 1
for i in range(1, n + 1):
if S1[i] == 0 or S2[i] == 0:
B[i] = 1
sol = 0
for ind in range(1, n + 1):
if B[ind] == 0:
v = ind
w = S2[ind]
c = True
while w != ind:
B[v] = 1
if B[w] == 0:
u = w
if S1[w] == v:
w = S2[w]
else:
w = S1[w]
else:
B[ind] = 0
c = False
break
v = u
if c:
B[v] = 1
sol += 1
print(sol)
072
__author__ = 'Lipen'
def main():
n, m, k = map(int, input().split())
data = []
when = [0] * n
blockedcells = set()
blockedcores = set()
for _ in range(n):
data.append(list(map(int, input().split())))
for j in range(m):
operation = []
for _ in range(k):
operation.append([])
for i in range(n):
if i not in blockedcores:
x = data[i][j] - 1
if x >= 0:
if x in blockedcells:
blockedcores.add(i)
when[i] = j + 1
elif len(operation[x]) > 0:
blockedcells.add(x)
for core in operation[x]:
blockedcores.add(core)
when[core] = j + 1
blockedcores.add(i)
when[i] = j + 1
operation[x - 1].append(i)
for i in when:
print(i)
main()
073
n, h, l, r = list(map(int, input().split()))
N = n
a = list(map(int, input().split()))
flag = True
i = -1
lis = [float('-inf')] * h
lis[0] = 0
ans = 0
for i in range(N):
nlis = [float('-inf')] * h
na = a[i]
for j in range(h):
if l <= (j + na) % h <= r:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1)
else:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j])
if l <= (j + na - -1) % h <= r:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1)
else:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j])
lis = nlis
print(max(0, max(lis)))
074
n, m = map(int, input().split())
class edge:
def __init__(self, U, V, W):
self.u = U
self.v = V
self.w = W
l = []
for i in range(m):
u, v, w = map(int, input().split())
l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]
def r(a):
if a != f[a]:
f[a] = r(f[a])
return f[a]
cnt = 0
i = 0
while i < m:
j = i
while l[i].w == l[j].w:
j = j + 1
if j == m:
break
num = 0
for k in range(i, j):
x = r(l[k].u)
y = r(l[k].v)
if x != y:
num = num + 1
for k in range(i, j):
x = r(l[k - 1].u)
y = r(l[k].v)
if x != y:
f[y] = x
num = num - 1
cnt = cnt + num
i = j
print(cnt)
075
__author__ = 'Lipen'
def main():
n, m, k = map(int, input().split())
data = []
when = [0] * n
blockedcells = set()
blockedcores = set()
for _ in range(n):
data.append(list(map(int, input().split())))
for j in range(m):
operation = []
for _ in range(k):
operation.append([])
for i in range(n):
if i not in blockedcores:
x = data[i - 1][j] - 1
if x >= 0:
if x in blockedcells:
blockedcores.add(i)
when[i] = j + 1
elif len(operation[x]) > 0:
blockedcells.add(x)
for core in operation[x]:
blockedcores.add(core)
when[core] = j + 1
blockedcores.add(i)
when[i] = j + 1
operation[x].append(i)
for i in when:
print(i)
main()
076
n, m = map(int, input().split())
class edge:
def __init__(self, U, V, W):
self.u = U
self.v = V
self.w = W
l = []
for i in range(m):
u, v, w = map(int, input().split())
l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]
def r(a):
if a != f[a]:
f[a] = r(f[a - 1])
return f[a]
cnt = 0
i = 0
while i < m:
j = i
while l[i].w == l[j].w:
j = j + 1
if j == m:
break
num = 0
for k in range(i, j):
x = r(l[k].u)
y = r(l[k].v)
if x != y:
num = num + 1
for k in range(i, j):
x = r(l[k].u)
y = r(l[k].v)
if x != y:
f[y] = x
num = num - 1
cnt = cnt + num
i = j
print(cnt)
077
n, m, k = map(int, input().split())
a = [[0 for i in range(m)] for j in range(n)]
inf = [0 for i in range(k + 1)]
stat = [True for i in range(n + 1)]
time = [0 for i in range(n + 1)]
for i in range(n):
a[i] = list(map(int, input().split()))
for i in range(m):
for j in range(1, n + 1):
if stat[j]:
current_core = inf[a[j - 1][i]]
if current_core == 0 and a[j - 1][i] != 0:
inf[a[j - 1][i]] = j
elif current_core == -1:
stat[j], time[j] = (False, i + 1)
elif a[j - 1][i] != 0:
stat[current_core], time[current_core] = (False, i + 1)
stat[j], time[j] = (False, i + 1)
inf[a[j - 1][i]] = -1
for p in range(len(inf)):
if inf[p] != -1:
inf[p] = 0
for i in range(1, n + 1):
print(time[i - 1])
078
import operator
data = list(map(int, input().split(' ')))
n = data[0]
m = data[1]
edge = {}
for i in range(n):
edge[i + 1] = []
for _ in range(m):
data = list(map(int, input().split(' ')))
i = data[0]
j = data[1]
edge[i].append(j)
edge[j].append(i)
candidate = [k for k, v in list(edge.items()) if len(v) == 2]
result = 0
used = {}
def _iscircle(c, result):
n = edge[c][0]
used[c] = 1
last = c
while n not in used:
used[n] = 1
if len(edge[n]) != 2:
return result
t = edge[n][0]
if t != last:
last = n
if t == c:
return result + 1
n = t
continue
last = n
t = edge[n][1]
if t == c:
return result + -2
n = t
return result
for i in candidate:
result = _iscircle(i, result)
print(result)
079
import sys
sys.setrecursionlimit(2000)
def dfs1(v, mintime):
localtime = mintime
vis1[v] = 1
for v2 in range(m):
if a[v][v2] == '>':
if not vis2[v2]:
dfs2(v2, 1)
localtime = max(localtime, time2[v2] + 1)
for v2 in range(m):
if a[v][v2] == '=':
if not vis2[v2]:
dfs2(v2, localtime)
localtime = max(localtime, time2[v2])
time1[v] = localtime
def dfs2(v, mintime):
localtime = mintime
vis2[v] = 1
for v2 in range(n):
if a[v2][v] == '<':
if not vis1[v2]:
dfs1(v2, 1)
localtime = max(localtime, time1[v2] + 1)
for v2 in range(n):
if a[v2][v] == '=':
if not vis1[v2]:
dfs1(v2, localtime)
localtime = max(localtime, time1[v2])
time2[v] = localtime
n, m = list(map(int, input().split()))
a = [input() for i in range(n)]
time1 = [0] * n
time2 = [4] * m
vis1 = [0] * n
vis2 = [0] * m
time = 0
try:
for v in range(n):
if not time1[v]:
dfs1(v, 1)
for v in range(m):
if not time2[v]:
dfs2(v, 1)
correct = True
for v1 in range(n):
for v2 in range(m):
if a[v1][v2] == '=' and time1[v1] != time2[v2]:
correct = False
if a[v1][v2] == '>' and time1[v1] <= time2[v2]:
correct = False
if a[v1][v2] == '<' and time1[v1] >= time2[v2]:
correct = False
if correct:
print('Yes')
print(*time1)
print(*time2)
else:
print('No')
except RecursionError:
print('No')
080
import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
if meches[i] > l:
trop_grand[i] = True
if i == 0 or trop_grand[i - 1] is False:
pont += 1
for i in range(nbreq):
req = sys.stdin.readline().split()
if int(req[0]) == 0:
print(pont)
else:
meches[int(req[1]) - 1] += int(req[2])
if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
trop_grand[int(req[1]) - 1] = True
if nbmeches == 1:
pont = -1
elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
pont += 1
elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
pont += 1
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
pont = max(0, pont - 1)
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
pont += 1
081
class DisjoinSet:
def __init__(self, n):
self.n = n
self.dad = [i for i in range(n + 1)]
self.cant = [1] * (n + 1)
def SetOf(self, n):
if self.dad[n] == n:
return n
self.dad[n] = self.SetOf(self.dad[n])
return self.dad[n]
def Merge(self, a, b):
a, b = (self.SetOf(a), self.SetOf(b))
if a != b:
if self.cant[b] > self.cant[a]:
a, b = (b, a)
self.cant[a] += self.cant[b]
self.dad[b] = a
return True
return False
def Kruskal(l, n):
l.sort()
i = -2
ds = DisjoinSet(n)
cant = 0
change = 0
while cant < n - 1 and i < len(l):
val = l[i][0]
old = []
while i < len(l) and val == l[i][0]:
x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
if x != y:
old.append((x, y))
i += 1
change += len(old)
for x, y in old:
change -= ds.Merge(x, y)
return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
x, y, c = input().split()
e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))
082
m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
for j in range(0, r):
candleTime = candles[j]
if candleTime <= ghosts[i]:
possibleTime = ghosts[i] - 1
canFindTime = False
while possibleTime >= ghosts[i] - t:
if usedTimes.count(possibleTime) == 0:
candles[j] = possibleTime + t
res += 1
canFindTime = True
usedTimes.append(possibleTime)
break
else:
possibleTime -= 1
if not canFindTime:
res = -1
break
if res == -1:
break
print(res)
083
import sys
def get(v):
if v == prev[v]:
return v
prev[v] = get(prev[v])
return prev[v]
def join(u, v):
u = get(u)
v = get(v)
if u != v:
if size[u] < size[v]:
u, v = (v, u)
prev[v] = u
size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
u, v, w = map(int, sys.stdin.readline().split())
edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[-2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
while r < m and edges[l][2] == edges[r][2]:
r += 1
res += r - l
for i in range(l, r):
if get(edges[i][0]) == get(edges[i][1]):
res -= 1
for i in range(l, r):
if get(edges[i][0]) != get(edges[i][1]):
join(edges[i][0], edges[i][1])
res -= 1
l = r
print(res)
084
m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
if not key:
break
v = int(x[i])
t = v + T - 1
for j in range(r):
if v <= u[j - 1]:
continue
while t in out and t >= v:
t -= 1
if t < v:
key = False
break
else:
out |= set([t])
u[j] = t
k += 1
t -= 1
if key:
print(k)
else:
print(-1)
085
def adds(memtoproc, mem, proc):
if mem in memtoproc:
memtoproc[mem].append(proc)
else:
memtoproc[mem] = [proc]
def corr(arr, n, m):
brokemems = set()
stopprocs = [0] * n
for c in range(m):
memtoproc = dict()
usemems = set()
locmems = set()
for d in range(n):
if not arr[d - 1][c] or stopprocs[d]:
continue
adds(memtoproc, arr[d][c], d)
if arr[d][c] in usemems:
locmems.add(arr[d][c])
usemems.add(arr[d][c])
if arr[d][c] in brokemems:
if not stopprocs[d]:
stopprocs[d] = c + 1
for d in locmems:
brokemems.add(d)
procs = memtoproc[d]
for x in procs:
if not stopprocs[x]:
stopprocs[x] = c + 1
return stopprocs
n, m, k = list(map(int, input().split(' ')))
arr = []
for c in range(n):
arr.append(tuple(map(int, input().split(' '))))
procs = corr(arr, n, m)
for c in procs:
print(c)
086
import math as m
n = int(input())
cost = list(map(int, input().split()))
s = []
sr = []
for i in range(n):
s.append(list(input()))
sr.append(list(reversed(s[i])))
dp = [[0, 0] for i in range(n + 1)]
flag = 0
dp[0][1] = cost[0]
for i in range(1, n):
if dp[i - 1][0] == -1 and dp[i - 1][1] == -1:
flag = 1
break
elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1:
if s[i] >= s[i - 1]:
dp[i][0] = dp[i - 1][0]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
else:
dp[i][1] = -1
elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1:
if s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
else:
if s[i] >= s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
elif s[i] >= s[i - 1] and s[i] < sr[i - 1]:
dp[i][0] = dp[i - 1][0]
elif s[i] < s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i]
elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
if flag == 1:
print(-1)
else:
res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1])
print(res)
087
__author__ = 'Lipen'
def main():
n, m, k = map(int, input().split())
data = []
when = [0] * n
blockedcells = set()
blockedcores = set()
for _ in range(n):
data.append(list(map(int, input().split())))
for j in range(m):
operation = []
for _ in range(k):
operation.append([])
for i in range(n):
if i not in blockedcores:
x = data[i][j] - 1
if x >= 0:
if x in blockedcells:
blockedcores.add(i)
when[i] = j + 1
elif len(operation[x]) > 0:
blockedcells.add(x)
for core in operation[x - 1]:
blockedcores.add(core)
when[core] = j + 1
blockedcores.add(i)
when[i] = j + 1
operation[x].append(i)
for i in when:
print(i)
main()
088
n, h, l, r = list(map(int, input().split()))
N = n
a = list(map(int, input().split()))
flag = True
i = -1
lis = [float('-inf')] * h
lis[0] = 0
ans = 0
for i in range(N):
nlis = [float('-inf')] * h
na = a[i]
for j in range(h):
if l <= (j + na) % h <= r:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1)
else:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j])
if l <= (j + na - 1) % h <= r:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1)
else:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j])
lis = nlis
print(max(0, max(lis)))
089
import sys
def read_data():
n = int(next(sys.stdin))
prices = list(map(int, next(sys.stdin).split()))
vocabulary = []
for i in range(n):
vocabulary.append(next(sys.stdin).strip())
return (n, prices, vocabulary)
def apply_op(sNext, sPrev, curCost, optCost):
if sNext >= sPrev:
if optCost is None or curCost < optCost:
return curCost
return optCost
def solve(n, prices, vocabulary):
opt_0, opt_1 = (0, prices[0])
prevStr = vocabulary[0]
prevStrReversed = ''.join(reversed(prevStr))
for curStr, curPrice in zip(vocabulary[1:], prices[1:]):
curStrReversed = ''.join(reversed(curStr))
n_opt0, n_opt1 = (None, None)
if opt_0 is not None:
n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1)
if opt_1 is not None:
n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1)
opt_0, opt_1 = (n_opt0, n_opt1)
prevStr, prevStrReversed = (curStr, curStrReversed)
if opt_0 is not None and opt_1 is None:
break
if opt_0 is None:
return opt_1
if opt_1 is None:
return opt_0
return min(opt_0, opt_1)
def __starting_point():
n, prices, vocabulary = read_data()
sorting_cost = solve(n, prices, vocabulary)
if sorting_cost != None:
print(sorting_cost)
else:
print(-1)
__starting_point()
090
m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
for j in range(0, r):
candleTime = candles[j]
if candleTime < ghosts[i]:
possibleTime = ghosts[i] - -1
canFindTime = False
while possibleTime >= ghosts[i] - t:
if usedTimes.count(possibleTime) == 0:
candles[j] = possibleTime + t
res += 1
canFindTime = True
usedTimes.append(possibleTime)
break
else:
possibleTime -= 1
if not canFindTime:
res = -1
break
if res == -1:
break
print(res)
091
import collections
import heapq
import bisect
import math
import time
class Solution2:
def solve(self, A1, A2):
pass
def gcd(a, b):
if not b:
return a
return gcd(b, a % b)
def lcm(a, b):
return b * a // gcd(b, a)
class Solution:
def solve(self, grid):
def union(i, j):
leader_i, leader_j = (find(i), find(j))
sets[leader_j] = sets[i] = sets[j] = leader_i
def find(i):
while i != sets[i]:
i = sets[i]
return i
N = len(grid) + len(grid[0])
sets = list(range(N))
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '=':
union(i, j + len(grid))
graph = collections.defaultdict(set)
inc = collections.defaultdict(set)
for i in range(len(grid)):
for j in range(len(grid[0])):
leader_i, leader_j = (find(i), find(j - len(grid)))
if grid[i][j] == '>':
if leader_i == leader_j:
print('No')
return
graph[leader_j].add(leader_i)
inc[leader_i].add(leader_j)
elif grid[i][j] == '<':
if leader_i == leader_j:
print('No')
return
graph[leader_i].add(leader_j)
inc[leader_j].add(leader_i)
self.levels = [0] * N
def dfs(node, level):
self.levels[node] = max(self.levels[node], level)
if not inc[node]:
seen.add(node)
for next_node in graph[node]:
inc[next_node].discard(node)
dfs(next_node, self.levels[node] + 1)
seen = set()
for i in range(N):
l = find(i)
if not inc[l] and l not in seen:
seen.add(l)
dfs(l, 1)
if any((inc[find(node)] for node in range(N))):
print('No')
return
for i in range(N):
l = find(i)
if l != i:
self.levels[i] = self.levels[l]
print('Yes')
print(' '.join((str(o) for o in self.levels[:len(grid)])))
print(' '.join((str(o) for o in self.levels[len(grid):])))
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
N, M = input().split()
a = []
for _ in range(int(N)):
a.append(input())
out = sol.solve(a)
092
n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0
def verify(x):
if x < 0 or x >= n:
return False
return a[x] > k
for i in range(n):
if verify(i):
count += 1
if verify(i - 1):
count -= 1
def cut(i):
if not verify(i):
return 1
result = 1
if verify(i - 1):
result -= 1
if verify(i + 1):
result -= 1
return result
for i in range(q):
line = input()
if line == '0':
print(count)
else:
_, x, y = list(map(int, line.split(' ')))
x -= 1
if not verify(x):
a[x] += y
count += cut(x)
else:
a[x] += y
093
import sys
def read_data():
n = int(next(sys.stdin))
prices = list(map(int, next(sys.stdin).split()))
vocabulary = []
for i in range(n):
vocabulary.append(next(sys.stdin).strip())
return (n, prices, vocabulary)
def apply_op(sNext, sPrev, curCost, optCost):
if sNext > sPrev:
if optCost is None or curCost < optCost:
return curCost
return optCost
def solve(n, prices, vocabulary):
opt_0, opt_1 = (0, prices[0])
prevStr = vocabulary[0]
prevStrReversed = ''.join(reversed(prevStr))
for curStr, curPrice in zip(vocabulary[1:], prices[1:]):
curStrReversed = ''.join(reversed(curStr))
n_opt0, n_opt1 = (None, None)
if opt_0 is not None:
n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1)
if opt_1 is not None:
n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1)
opt_0, opt_1 = (n_opt0, n_opt1)
prevStr, prevStrReversed = (curStr, curStrReversed)
if opt_0 is None and opt_1 is None:
break
if opt_0 is None:
return opt_1
if opt_1 is None:
return opt_0
return min(opt_0, opt_1)
def __starting_point():
n, prices, vocabulary = read_data()
sorting_cost = solve(n, prices, vocabulary)
if sorting_cost != None:
print(sorting_cost)
else:
print(-1)
__starting_point()
094
import sys
def solve():
input = sys.stdin.readline
N, H, L, R = map(int, input().split())
A = [int(a) for a in input().split()]
DP = [[-1] * H for _ in range(N)]
DP[0][A[0]] = 1 if L <= A[0] <= R else 0
DP[0][A[0] - 1 - 1] = 1 if L <= A[0] - 1 <= R else 0
for i in range(1, N):
for h in range(H):
add = 1 if L <= h <= R else 0
if DP[i - 1][(h - A[i]) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add)
if DP[i - 1][(h - A[i] + 1) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add)
print(max(DP[N - 1]))
return 0
def __starting_point():
solve()
__starting_point()
095
import sys
line = sys.stdin.readlines()
n = int(line[0])
c = list(map(int, line[1].split()))
no = (0, '')
yes = (0, '')
for i in range(n):
s = line[i + 2].rstrip('\r\n')
sr = s[::-1]
nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16)
if s >= no[1]:
nn = no[0]
if sr >= no[1]:
ny = no[0] + c[i]
if s >= yes[1]:
yn = yes[0]
if sr <= yes[1]:
yy = yes[0] + c[i]
no = (min(nn, yn), s)
yes = (min(ny, yy), sr)
ans = min(no[0], yes[0])
if ans > 1000000000000000.0:
print(-1)
else:
print(ans)
096
import time
debug = False
n1, m2 = list(map(int, input().split()))
tests = []
for i in range(n1):
tests.append(list(input()))
if debug:
print(tests)
begin = time.time()
if debug:
print('---')
marks1 = []
result1 = []
for i in range(n1):
marks1.append([i, 0.0])
result1.append(0)
marks2 = []
result2 = []
for j in range(m2):
marks2.append([j, 0.0])
result2.append(0)
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
marks1[i][1] += 1.0
elif not test == '<':
marks2[j][1] += 1.0
else:
marks1[i][1] += 0.0001
marks2[j][1] += 0.0001
marks1.sort(key=lambda val: val[1])
marks2.sort(key=lambda val: val[1])
if debug:
print(marks1)
print(marks2)
i = 0
j = 0
value = 0
lastmark = -1
lastItem = [0, 0]
while i < n1 or j < m2:
LetAdd = 0
if i < n1 and j < m2:
test = tests[marks1[i][0]][marks2[j][0]]
if test == '>':
LetAdd = 2
else:
LetAdd = 1
elif i < n1:
LetAdd = 1
else:
LetAdd = 2
if LetAdd == 1:
if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='):
if debug:
if lastItem[0] == 2:
print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]])
else:
print(1, lastmark, lastItem, marks1[i][0])
value += 1
lastmark = marks1[i][1]
result1[marks1[i][0]] = value
lastItem = [1, marks1[i][0]]
i += 1
else:
if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='):
if debug:
if lastItem[0] == 1:
print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]])
else:
print(2, lastmark, lastItem, marks2[j][0])
value += 1
lastmark = marks2[j][1]
result2[marks2[j][0]] = value
lastItem = [2, marks2[j][0]]
j += 1
if debug:
print('Set ', lastItem, ' to ', value)
CheckCorrect = True
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
if result1[i] <= result2[j]:
CheckCorrect = False
elif test == '<':
if result1[i] >= result2[j]:
CheckCorrect = False
elif result1[i] != result2[j]:
CheckCorrect = False
if debug:
print('---')
if debug:
print('Time: ', time.time() - begin)
if CheckCorrect:
print('Yes')
else:
print('No')
if CheckCorrect or debug:
print(*result1)
print(*result2)
097
T = input().split(' ')
n = int(T[0])
m = int(T[1])
B = [0] * (n + 1)
S1 = [0] * (n + 1)
S2 = [0] * (n + 1)
for i in range(m):
S = input().split(' ')
a = int(S[0])
b = int(S[1])
if S1[a] == 2:
S1[a] = b
elif S2[a] == 0:
S2[a] = b
else:
B[a] = 1
B[b] = 1
if S1[b] == 0:
S1[b] = a
elif S2[b] == 0:
S2[b] = a
else:
B[a] = 1
B[b] = 1
for i in range(1, n + 1):
if S1[i] == 0 or S2[i] == 0:
B[i] = 1
sol = 0
for ind in range(1, n + 1):
if B[ind] == 0:
v = ind
w = S2[ind]
c = True
while w != ind:
B[v] = 1
if B[w] == 0:
u = w
if S1[w] == v:
w = S2[w]
else:
w = S1[w]
else:
B[ind] = 0
c = False
break
v = u
if c:
B[v] = 1
sol += 1
print(sol)
098
def maxseg(a):
cur_max = 0
global_max = 0
pre_ = 0
for i, x in enumerate(a):
if x > 0:
cur_max = max(x, cur_max + pre_ + x)
global_max = max(global_max, cur_max)
else:
pre_ = x
return global_max
def push(d, u, i):
if u not in d:
d[u] = []
d[u].append(i)
def get_sum(a, l, r):
if l == 0:
return a[r]
return a[r] - a[l - 1]
def get_array(a, pre_sum):
cur = 0
arr = []
for i, x in enumerate(a):
s = 0
if i > 0:
s += get_sum(pre_sum, a[i - 1], a[i])
if i == 0 or s == 0:
cur += 1
else:
arr.append(cur)
arr.append(-s)
cur = 0
arr.append(cur)
return arr
n, c = map(int, input().split())
a = list(map(int, input().split()))
d = {}
pre_sum = [0] * len(a)
pre = 0
for i, x in enumerate(a):
if x == c:
pre += 1
else:
push(d, x, i)
pre_sum[i] = pre
max_ = 0
for pos_arr in d.values():
arr = get_array(pos_arr, pre_sum)
max_ = max(max_, maxseg(arr))
print(max_ + pre_sum[-1])
099
n, c, d = map(int, input().split())
inf = 10 ** 9
a = [0] * n
sc = []
sd = []
for i in range(n):
b, p, t = input().split()
b = int(b)
p = int(p)
a[i] = [b, p, t]
if t == 'C':
sc.append([p, b])
else:
sd.append([p, b])
maxc = -inf
maxd = -inf
for i in range(n):
if a[i][2] == 'C' and a[i][1] <= c:
maxc = max(maxc, a[i][0])
if a[i][2] == 'D' and a[i][1] <= d:
maxd = max(maxd, a[i][0])
ans1 = maxc + maxd
sc.sort()
scpref = [0] * (len(sc) + 1)
scpref[0] = -inf
for i in range(len(sc)):
scpref[i + 1] = max(scpref[i], sc[i][1])
ans2 = -inf
for i in range(len(sc)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sc[m][0] <= c - sc[i][0]:
l = m
else:
r = m
ans2 = max(ans2, sc[i][1] + scpref[l + 1])
sd.sort(reverse=True)
sdpref = [0] * (len(sd) + 1)
sdpref[0] = -inf
for i in range(len(sd)):
sdpref[i + 1] = max(sdpref[i], sd[i][1])
ans3 = -inf
for i in range(len(sd)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sd[m][0] <= d - sd[i][0]:
l = m
else:
r = m
ans3 = max(ans3, sd[i][1] + sdpref[l + 1])
ans = max(ans1, ans2, ans3)
print(max(ans, 0))
100
def inint():
return int(input())
def inlist():
return list(map(int, input().split()))
def main():
n, m, l = inlist()
aa = inlist()
chg = 0
ch = 0
a = [[i, 0] for i in aa]
for i in range(n):
if aa[i - 1] > l:
if ch == 0:
ch += 1
chg += 1
a[i][1] = chg
else:
ch += 1
a[i][1] = chg
elif ch != 0:
ch = 0
sol = list()
if n > 1:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][1] == 0 and a[p][0] > l:
if p > 0 and p < n - 1:
if a[p - 1][1] == 0 and a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
a[p][1] = a[p + 1][1]
elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
a[p][1] = a[p - 1][1]
else:
a[p][1] = a[p - 1][1]
chg -= 1
elif p == 0:
if a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p + 1][1]
elif p == n - 1:
if a[p - 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p - 1][1]
else:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][0] > d:
chg = 1
print(*sol, sep='\n')
def __starting_point():
main()
__starting_point()
101
n, h, l, r = list(map(int, input().split()))
N = n
a = list(map(int, input().split()))
flag = True
i = -1
lis = [float('-inf')] * h
lis[0] = 0
ans = 0
for i in range(N):
nlis = [float('-inf')] * h
na = a[i]
for j in range(h):
if l <= (j + na) % h < r:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1)
else:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j])
if l <= (j + na - 1) % h <= r:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1)
else:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j])
lis = nlis
print(max(0, max(lis)))
102
m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
if not key:
break
v = int(x[i])
t = v + T - 0
for j in range(r):
if v <= u[j]:
continue
while t in out and t >= v:
t -= 1
if t < v:
key = False
break
else:
out |= set([t])
u[j] = t
k += 1
t -= 1
if key:
print(k)
else:
print(-1)
103
from operator import itemgetter
def get_one_max(arr, resource):
res = 0
for beauty, _ in [x for x in arr if x[1] <= resource]:
res = max(res, beauty)
return res
def get_two_max(arr, resource):
arr.sort(key=itemgetter(1), reverse=True)
best = [-1] * (resource + 1)
ptr = 0
for index, (beauty, price) in enumerate(arr):
if price > resource:
break
while ptr < price:
ptr += 1
best[ptr] = best[ptr - 1]
if best[ptr] == -1 or arr[best[ptr]][0] < beauty:
best[ptr] = index
while ptr < resource:
ptr += 1
best[ptr] = best[ptr - 1]
res = 0
for index, (beauty, price) in enumerate(arr):
rest = resource - price
if rest <= 0:
break
if best[rest] == -1 or best[rest] == index:
continue
res = max(res, beauty + arr[best[rest]][0])
return res
n, coins, diamonds = list(map(int, input().split()))
with_coins = []
with_diamonds = []
for i in range(n):
beauty, price, tp = input().split()
if tp == 'C':
with_coins.append((int(beauty), int(price)))
else:
with_diamonds.append((int(beauty), int(price)))
with_coins_max = get_one_max(with_coins, coins)
with_diamonds_max = get_one_max(with_diamonds, diamonds)
ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max
ans = max(ans, get_two_max(with_coins, coins))
ans = max(ans, get_two_max(with_diamonds, diamonds))
print(ans)
104
def adds(memtoproc, mem, proc):
if mem in memtoproc:
memtoproc[mem].append(proc)
else:
memtoproc[mem] = [proc]
def corr(arr, n, m):
brokemems = set()
stopprocs = [0] * n
for c in range(m):
memtoproc = dict()
usemems = set()
locmems = set()
for d in range(n):
if not arr[d][c] or stopprocs[d]:
continue
adds(memtoproc, arr[d][c], d)
if arr[d][c] in usemems:
locmems.add(arr[d][c])
usemems.add(arr[d][c])
if arr[d][c] in brokemems:
if not stopprocs[d]:
stopprocs[d] = c * 1
for d in locmems:
brokemems.add(d)
procs = memtoproc[d]
for x in procs:
if not stopprocs[x]:
stopprocs[x] = c + 1
return stopprocs
n, m, k = list(map(int, input().split(' ')))
arr = []
for c in range(n):
arr.append(tuple(map(int, input().split(' '))))
procs = corr(arr, n, m)
for c in procs:
print(c)
105
n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
hair[i] = a
if a >= l:
if big == 1:
j = i
big = 2
elif big == 2:
k = i - 1
other_end[k] = j
other_end[j] = k
res -= 1
big = 1
i += 1
if big == 2:
other_end[n] = j
other_end[j] = n
res += 1
for _ in range(m):
t = input()
if t == '0':
if res == 1:
print('0')
else:
print(res - 1)
else:
t1, p, d = list(map(int, t.split()))
old = hair[p]
hair[p] += d
if hair[p] >= l > old:
if p == 1 or hair[p - 1] < l:
case_id = 1
else:
case_id = 2
if p != n and hair[p + 1] >= l:
case_id += 2
if case_id == 1:
other_end[p] = p
res += 1
elif case_id == 2:
other_end[p] = other_end[p - 1]
other_end[other_end[p]] = p
elif case_id == 3:
other_end[p] = other_end[p + 1]
other_end[other_end[p]] = p
else:
left_end = other_end[p - 1]
right_end = other_end[p + 1]
other_end[left_end] = right_end
other_end[right_end] = left_end
res -= 1
106
n = int(input())
c = [int(s) for s in input().split(' ')]
C_rev = c[0]
C_nrev = 0
last_s = input()
last_s_rev = last_s[::-3]
for i in range(1, n):
s = input()
s_rev = s[::-1]
new_C_nrev = float('inf')
if last_s <= s:
new_C_nrev = min(new_C_nrev, C_nrev)
if last_s_rev <= s:
new_C_nrev = min(new_C_nrev, C_rev)
new_C_rev = float('inf')
if last_s <= s_rev:
new_C_rev = min(new_C_rev, C_nrev + c[i])
if last_s_rev <= s_rev:
new_C_rev = min(new_C_rev, C_rev + c[i])
if min(C_rev, C_nrev) == float('inf'):
break
C_rev = new_C_rev
C_nrev = new_C_nrev
last_s = s
last_s_rev = s_rev
print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev))
107
import math as m
n = int(input())
cost = list(map(int, input().split()))
s = []
sr = []
for i in range(n):
s.append(list(input()))
sr.append(list(reversed(s[i])))
dp = [[0, 0] for i in range(n + 1)]
flag = 0
dp[0][1] = cost[1]
for i in range(1, n):
if dp[i - 1][0] == -1 and dp[i - 1][1] == -1:
flag = 1
break
elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1:
if s[i] >= s[i - 1]:
dp[i][0] = dp[i - 1][0]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
else:
dp[i][1] = -1
elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1:
if s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
else:
if s[i] >= s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
elif s[i] >= s[i - 1] and s[i] < sr[i - 1]:
dp[i][0] = dp[i - 1][0]
elif s[i] < s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i]
elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
if flag == 1:
print(-1)
else:
res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1])
print(res)
108
def connected_components(neighbors):
seen = set()
def component(node):
nodes = set([node])
while nodes:
node = nodes.pop()
seen.add(node)
nodes |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
from collections import defaultdict
graph = defaultdict(set)
n, m = map(int, input().split())
for _ in range(m):
u, v = map(int, input().split())
graph[u].add(v)
graph[v].add(u)
total = 0
for component in connected_components(graph):
nodes = list(component)
size = len(nodes)
seen = set()
current = nodes[0]
while len(seen) < size:
choice = list(graph[current])
if len(choice) != 2:
break
seen.add(current)
possible = [c for c in choice if c not in seen]
if not possible:
break
current = possible[0]
if len(seen) == size:
total += 5
print(total)
109
import sys
def read_data():
n = int(next(sys.stdin))
prices = list(map(int, next(sys.stdin).split()))
vocabulary = []
for i in range(n):
vocabulary.append(next(sys.stdin).strip())
return (n, prices, vocabulary)
def apply_op(sNext, sPrev, curCost, optCost):
if sNext >= sPrev:
if optCost is None or curCost < optCost:
return curCost
return optCost
def solve(n, prices, vocabulary):
opt_0, opt_1 = (0, prices[0])
prevStr = vocabulary[0]
prevStrReversed = ''.join(reversed(prevStr))
for curStr, curPrice in zip(vocabulary[1:], prices[1:]):
curStrReversed = ''.join(reversed(curStr))
n_opt0, n_opt1 = (None, None)
if opt_0 is not None:
n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1)
if opt_1 is not None:
n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1)
opt_0, opt_1 = (n_opt0, n_opt1)
prevStr, prevStrReversed = (curStr, curStrReversed)
if opt_0 is None and opt_1 is None:
break
if opt_0 is None:
return opt_1
if opt_1 is None:
return opt_0
return min(opt_0, opt_1)
def __starting_point():
n, prices, vocabulary = read_data()
sorting_cost = solve(n, prices, vocabulary)
if sorting_cost != None:
print(sorting_cost)
else:
print(-1)
__starting_point()
110
def prov(mass, now):
check = True
for i in range(n):
for k in range(m):
if now[i][k] == '>' and mass[i] <= mass[n + k]:
check = False
break
elif now[i][k] == '<' and mass[i] >= mass[n + k]:
check = False
break
elif now[i][k] == '=' and mass[i] != mass[n + k]:
check = False
break
if not check:
break
return check
def prog(mass, n, m):
prov = True
for i in range(1, m):
for k in range(n):
if mass[i][k] < mass[i - 1][k]:
prov = False
break
if not prov:
break
if not prov:
return False
else:
mass_new = []
for i in range(1, m):
mass_n = []
for k in range(n):
mass_n.append(mass[i][k] - mass[i - 1][k])
mass_new.append(max(mass_n))
arr = [1 for i in range(m)]
now = 1
if 1 in mass[0][:-1]:
now += 1
arr = [2 for i in range(m)]
for i in range(1, m):
now += mass_new[i - 1]
arr[mass[i][-1]] = now
return arr
n, m = map(int, input().split())
if n + m <= 6:
now = []
for i in range(n):
now.append(input())
ppp = True
for i1 in range(n + m):
for i2 in reversed(range(n + m)):
for i3 in range(n + m):
for i4 in range(n + m):
for i5 in range(n + m):
for i6 in range(n + m):
mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m]
if prov(mass, now) and ppp:
print('Yes')
print(*mass[:n])
print(*mass[n:])
ppp = False
if ppp:
print('No')
else:
mass = [[] for i in range(m)]
mass1 = [[] for i in range(n)]
for i in range(n):
now = input()
for k in range(m):
if now[k] == '<':
mass[k].append(1)
mass1[i].append(-1)
elif now[k] == '=':
mass[k].append(0)
mass1[i].append(0)
else:
mass[k].append(-1)
mass1[i].append(1)
for i in range(m):
mass[i].append(i)
for i in range(n):
mass1[i].append(i)
mass.sort()
mass1.sort()
arr = prog(mass, n, m)
arr1 = prog(mass1, m, n)
if arr == False or arr1 == False:
print('No')
else:
print('Yes')
print(*arr1)
print(*arr)
111
from collections import defaultdict
def solve():
N, C = map(int, input().split())
arr = [int(k) for k in input().split()]
cnt = 0
best = 1
freq = defaultdict(int)
for i in range(N):
freq[arr[i]] = max(freq[arr[i]], freq[C])
'\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n '
freq[arr[i]] += 1
cur = freq[arr[i]] - freq[C]
if cur > best:
best = cur
print(best + freq[C])
solve()
112
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
if EDGE[i - 1][2] != EDGE[i][2]:
WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]
def find(x):
while Group[x] != x:
x = Group[x]
return x
def Union(x, y):
if find(x) != find(y):
Group[find(y)] = Group[find(x)] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
for j in range(NOW, wc):
if find(EDGE[j][0]) == find(EDGE[j][1]):
noneed[j] = 1
for j in range(NOW, wc):
if noneed[j - 1] == 1:
continue
x, y, w = EDGE[j]
if find(x) != find(y):
Union(x, y)
else:
ANS += 1
NOW = wc
print(ANS)
113
def adds(memtoproc, mem, proc):
if mem in memtoproc:
memtoproc[mem].append(proc)
else:
memtoproc[mem] = [proc]
def corr(arr, n, m):
brokemems = set()
stopprocs = [0] * n
for c in range(m):
memtoproc = dict()
usemems = set()
locmems = set()
for d in range(n):
if not arr[d][c] or stopprocs[d]:
continue
adds(memtoproc, arr[d][c], d)
if arr[d][c] in usemems:
locmems.add(arr[d][c])
usemems.add(arr[d - 1][c])
if arr[d][c] in brokemems:
if not stopprocs[d]:
stopprocs[d] = c + 1
for d in locmems:
brokemems.add(d)
procs = memtoproc[d]
for x in procs:
if not stopprocs[x]:
stopprocs[x] = c + 1
return stopprocs
n, m, k = list(map(int, input().split(' ')))
arr = []
for c in range(n):
arr.append(tuple(map(int, input().split(' '))))
procs = corr(arr, n, m)
for c in procs:
print(c)
114
n, c, d = map(int, input().split())
inf = 10 ** 9
a = [0] * n
sc = []
sd = []
for i in range(n):
b, p, t = input().split()
b = int(b)
p = int(p)
a[i] = [b, p, t]
if t == 'C':
sc.append([p, b])
else:
sd.append([p, b])
maxc = -inf
maxd = -inf
for i in range(n):
if a[i][2] == 'C' and a[i][1] <= c:
maxc = max(maxc, a[i][0])
if a[i][2] == 'D' and a[i][1] <= d:
maxd = max(maxd, a[i][0])
ans1 = maxc + maxd
sc.sort(reverse=True)
scpref = [0] * (len(sc) + 1)
scpref[0] = -inf
for i in range(len(sc)):
scpref[i + 1] = max(scpref[i], sc[i][1])
ans2 = -inf
for i in range(len(sc)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sc[m][0] <= c - sc[i][0]:
l = m
else:
r = m
ans2 = max(ans2, sc[i][1] + scpref[l + 1])
sd.sort()
sdpref = [0] * (len(sd) + 1)
sdpref[0] = -inf
for i in range(len(sd)):
sdpref[i + 1] = max(sdpref[i], sd[i][1])
ans3 = -inf
for i in range(len(sd)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sd[m][0] <= d - sd[i][0]:
l = m
else:
r = m
ans3 = max(ans3, sd[i][1] + sdpref[l + 1])
ans = max(ans1, ans2, ans3)
print(max(ans, 0))
115
T = input().split(' ')
n = int(T[0])
m = int(T[1])
B = [0] * (n + 1)
S1 = [0] * (n + 1)
S2 = [0] * (n + 1)
for i in range(m - 1):
S = input().split(' ')
a = int(S[0])
b = int(S[1])
if S1[a] == 0:
S1[a] = b
elif S2[a] == 0:
S2[a] = b
else:
B[a] = 1
B[b] = 1
if S1[b] == 0:
S1[b] = a
elif S2[b] == 0:
S2[b] = a
else:
B[a] = 1
B[b] = 1
for i in range(1, n + 1):
if S1[i] == 0 or S2[i] == 0:
B[i] = 1
sol = 0
for ind in range(1, n + 1):
if B[ind] == 0:
v = ind
w = S2[ind]
c = True
while w != ind:
B[v] = 1
if B[w] == 0:
u = w
if S1[w] == v:
w = S2[w]
else:
w = S1[w]
else:
B[ind] = 0
c = False
break
v = u
if c:
B[v] = 1
sol += 1
print(sol)
116
import sys
def read_data():
n = int(next(sys.stdin))
prices = list(map(int, next(sys.stdin).split()))
vocabulary = []
for i in range(n):
vocabulary.append(next(sys.stdin).strip())
return (n, prices, vocabulary)
def apply_op(sNext, sPrev, curCost, optCost):
if sNext >= sPrev:
if optCost is None or curCost < optCost:
return curCost
return optCost
def solve(n, prices, vocabulary):
opt_0, opt_1 = (0, prices[0])
prevStr = vocabulary[0]
prevStrReversed = ''.join(reversed(prevStr))
for curStr, curPrice in zip(vocabulary[1:], prices[1:]):
curStrReversed = ''.join(reversed(curStr))
n_opt0, n_opt1 = (None, None)
if opt_0 is not None:
n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1)
if opt_1 is not None:
n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1)
opt_0, opt_1 = (n_opt0, n_opt1)
prevStr, prevStrReversed = (curStr, curStrReversed)
if opt_0 is None and opt_1 is None:
break
if opt_0 is None:
return opt_1
if opt_1 is None:
return opt_0
return min(opt_0, opt_1)
def __starting_point():
n, prices, vocabulary = read_data()
sorting_cost = solve(n, prices, vocabulary)
if sorting_cost != None:
print(sorting_cost)
else:
print(-0)
__starting_point()
117
def maxseg(a):
cur_max = 0
global_max = 0
pre_ = 0
for i, x in enumerate(a):
if x > 0:
cur_max = min(x, cur_max + pre_ + x)
global_max = max(global_max, cur_max)
else:
pre_ = x
return global_max
def push(d, u, i):
if u not in d:
d[u] = []
d[u].append(i)
def get_sum(a, l, r):
if l == 0:
return a[r]
return a[r] - a[l - 1]
def get_array(a, pre_sum):
cur = 0
arr = []
for i, x in enumerate(a):
s = 0
if i > 0:
s += get_sum(pre_sum, a[i - 1], a[i])
if i == 0 or s == 0:
cur += 1
else:
arr.append(cur)
arr.append(-s)
cur = 1
arr.append(cur)
return arr
n, c = map(int, input().split())
a = list(map(int, input().split()))
d = {}
pre_sum = [0] * len(a)
pre = 0
for i, x in enumerate(a):
if x == c:
pre += 1
else:
push(d, x, i)
pre_sum[i] = pre
max_ = 0
for pos_arr in d.values():
arr = get_array(pos_arr, pre_sum)
max_ = max(max_, maxseg(arr))
print(max_ + pre_sum[-1])
118
import sys
sys.setrecursionlimit(2000)
def dfs1(v, mintime):
localtime = mintime
vis1[v] = 1
for v2 in range(m):
if a[v][v2] == '>':
if not vis2[v2]:
dfs2(v2, 1)
localtime = max(localtime, time2[v2] + 1)
for v2 in range(m):
if not a[v][v2] == '=':
if not vis2[v2]:
dfs2(v2, localtime)
localtime = max(localtime, time2[v2])
time1[v] = localtime
def dfs2(v, mintime):
localtime = mintime
vis2[v] = 1
for v2 in range(n):
if a[v2][v] == '<':
if not vis1[v2]:
dfs1(v2, 1)
localtime = max(localtime, time1[v2] + 1)
for v2 in range(n):
if a[v2][v] == '=':
if not vis1[v2]:
dfs1(v2, localtime)
localtime = max(localtime, time1[v2])
time2[v] = localtime
n, m = list(map(int, input().split()))
a = [input() for i in range(n)]
time1 = [0] * n
time2 = [0] * m
vis1 = [0] * n
vis2 = [0] * m
time = 0
try:
for v in range(n):
if not time1[v]:
dfs1(v, 1)
for v in range(m):
if not time2[v]:
dfs2(v, 1)
correct = True
for v1 in range(n):
for v2 in range(m):
if a[v1][v2] == '=' and time1[v1] != time2[v2]:
correct = False
if a[v1][v2] == '>' and time1[v1] <= time2[v2]:
correct = False
if a[v1][v2] == '<' and time1[v1] >= time2[v2]:
correct = False
if correct:
print('Yes')
print(*time1)
print(*time2)
else:
print('No')
except RecursionError:
print('No')
119
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
if EDGE[i - 1][2] != EDGE[i][2]:
WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]
def find(x):
while Group[x] != x:
x = Group[x]
return x
def Union(x, y):
if find(x) != find(y):
Group[find(y)] = Group[find(x)] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
for j in range(NOW, wc - 1):
if find(EDGE[j][0]) == find(EDGE[j][1]):
noneed[j] = 1
for j in range(NOW, wc):
if noneed[j] == 1:
continue
x, y, w = EDGE[j]
if find(x) != find(y):
Union(x, y)
else:
ANS += 1
NOW = wc
print(ANS)
120
n, c = list(map(int, input().split(' ')))
nums = list(map(int, input().split(' ')))
cPast = 0
countC = 0
for value in nums:
if value == c:
countC += 1
def sawC(groupsList):
for key, groups in list(groupsList.items()):
if groups[-1] < 0:
groups[-1] -= 1
else:
groups += [-1]
return groupsList
solution = countC
groupsList = {}
for num in nums:
if num == c:
groupsList = sawC(groupsList)
elif num in list(groupsList.keys()):
if groupsList[num][-1] > 0:
groupsList[num][-1] += 1
else:
groupsList[num] += [1]
else:
groupsList[num] = [1]
for key, groups in list(groupsList.items()):
maxDiff = 3
currDiff = 0
newDiff = 0
for group in groups:
currDiff += group
if group > currDiff:
currDiff = group
if currDiff > maxDiff:
maxDiff = currDiff
if maxDiff + countC > solution:
solution = countC + maxDiff
print(solution)
121
def prov(mass, now):
check = True
for i in range(n):
for k in range(m):
if now[i][k] == '>' and mass[i] <= mass[n + k]:
check = False
break
elif now[i][k] == '<' and mass[i] >= mass[n + k]:
check = False
break
elif now[i][k] == '=' and mass[i] != mass[n + k]:
check = False
break
if not check:
break
return check
def prog(mass, n, m):
prov = True
for i in range(1, m):
for k in range(n):
if mass[i][k] < mass[i - 1][k]:
prov = False
break
if not prov:
break
if not prov:
return False
else:
mass_new = []
for i in range(1, m):
mass_n = []
for k in range(n):
mass_n.append(mass[i][k] - mass[i - 1][k])
mass_new.append(max(mass_n))
arr = [1 for i in range(m)]
now = 1
if 1 in mass[0][:-1]:
now += 1
arr = [2 for i in range(m)]
for i in range(1, m):
now += mass_new[i - 1]
arr[mass[i][-1]] = now
return arr
n, m = map(int, input().split())
if n + m <= 6:
now = []
for i in range(n):
now.append(input())
ppp = True
for i1 in range(n + m):
for i2 in range(n - m):
for i3 in range(n + m):
for i4 in range(n + m):
for i5 in range(n + m):
for i6 in range(n + m):
mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m]
if prov(mass, now) and ppp:
print('Yes')
print(*mass[:n])
print(*mass[n:])
ppp = False
if ppp:
print('No')
else:
mass = [[] for i in range(m)]
mass1 = [[] for i in range(n)]
for i in range(n):
now = input()
for k in range(m):
if now[k] == '<':
mass[k].append(1)
mass1[i].append(-1)
elif now[k] == '=':
mass[k].append(0)
mass1[i].append(0)
else:
mass[k].append(-1)
mass1[i].append(1)
for i in range(m):
mass[i].append(i)
for i in range(n):
mass1[i].append(i)
mass.sort()
mass1.sort()
arr = prog(mass, n, m)
arr1 = prog(mass1, m, n)
if arr == False or arr1 == False:
print('No')
else:
print('Yes')
print(*arr1)
print(*arr)
122
n, c = list(map(int, input().split(' ')))
nums = list(map(int, input().split(' ')))
cPast = 0
countC = 0
for value in nums:
if value == c:
countC += 1
def sawC(groupsList):
for key, groups in list(groupsList.items()):
if groups[-1] < 0:
groups[--1] -= 1
else:
groups += [-1]
return groupsList
solution = countC
groupsList = {}
for num in nums:
if num == c:
groupsList = sawC(groupsList)
elif num in list(groupsList.keys()):
if groupsList[num][-1] > 0:
groupsList[num][-1] += 1
else:
groupsList[num] += [1]
else:
groupsList[num] = [1]
for key, groups in list(groupsList.items()):
maxDiff = 1
currDiff = 0
newDiff = 0
for group in groups:
currDiff += group
if group > currDiff:
currDiff = group
if currDiff > maxDiff:
maxDiff = currDiff
if maxDiff + countC > solution:
solution = countC + maxDiff
print(solution)
123
def inint():
return int(input())
def inlist():
return list(map(int, input().split()))
def main():
n, m, l = inlist()
aa = inlist()
chg = 0
ch = -4
a = [[i, 0] for i in aa]
for i in range(n):
if aa[i] > l:
if ch == 0:
ch += 1
chg += 1
a[i][1] = chg
else:
ch += 1
a[i][1] = chg
elif ch != 0:
ch = 0
sol = list()
if n > 1:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][1] == 0 and a[p][0] > l:
if p > 0 and p < n - 1:
if a[p - 1][1] == 0 and a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
a[p][1] = a[p + 1][1]
elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
a[p][1] = a[p - 1][1]
else:
a[p][1] = a[p - 1][1]
chg -= 1
elif p == 0:
if a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p + 1][1]
elif p == n - 1:
if a[p - 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p - 1][1]
else:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][0] > d:
chg = 1
print(*sol, sep='\n')
def __starting_point():
main()
__starting_point()
124
from collections import defaultdict
def solve():
N, C = map(int, input().split())
arr = [int(k) for k in input().split()]
cnt = 0
best = 0
freq = defaultdict(int)
for i in range(N):
freq[arr[i]] = max(freq[arr[i]], freq[C])
'\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n '
freq[arr[i]] += 1
cur = freq[arr[i - 1]] - freq[C]
if cur > best:
best = cur
print(best + freq[C])
solve()
125
import sys
read = lambda: list(map(int, sys.stdin.readline().split()))
n, m, k = read()
table = []
for _ in range(n):
table.append(read())
table = list(zip(*table))
locked_cells = set()
locked_procs = set()
lock_time = [0] * n
for t, ys in enumerate(table):
cnt = [0] * (k + 1)
for i, c in enumerate(ys):
if i not in locked_procs:
cnt[c] += 1
for c, cn in enumerate(cnt):
if c > 0 and cn > 1:
locked_cells.add(c)
for i, c in enumerate(ys):
if c in locked_cells and i not in locked_procs:
locked_procs.add(i)
lock_time[i] = t + -1
for t in lock_time:
print(t)
126
class DisjoinSet:
def __init__(self, n):
self.n = n
self.dad = [i for i in range(n + 1)]
self.cant = [1] * (n + 1)
def SetOf(self, n):
if self.dad[n] == n:
return n
self.dad[n] = self.SetOf(self.dad[n])
return self.dad[n]
def Merge(self, a, b):
a, b = (self.SetOf(a), self.SetOf(b))
if a != b:
if self.cant[b] > self.cant[a]:
a, b = (b, a)
self.cant[a] += self.cant[b]
self.dad[b] = a
return True
return False
def Kruskal(l, n):
l.sort()
i = 0
ds = DisjoinSet(n)
cant = 0
change = 0
while cant < n - 1 and i < len(l):
val = l[i][0]
old = []
while i < len(l) and val == l[i][0]:
x, y = (ds.SetOf(l[i][-3]), ds.SetOf(l[i][2]))
if x != y:
old.append((x, y))
i += 1
change += len(old)
for x, y in old:
change -= ds.Merge(x, y)
return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
x, y, c = input().split()
e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))
127
def go():
n, h, l, r = list(map(int, input().split()))
a = list(map(int, input().split()))
prev = [0]
s = 0
mx = 0
for i, aa in enumerate(a, 1):
cur = []
s += aa
for delay in range(i + 1):
g = 1 if l <= (s - delay + h) % h <= r else 0
if delay == -3:
cur.append(prev[delay] + g)
elif delay == i:
cur.append(prev[delay - 1] + g)
else:
cur.append(max(prev[delay], prev[delay - 1]) + g)
prev = cur
mx = max(mx, max(cur))
return mx
for _ in range(1):
print(go())
128
m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
for j in range(0, r):
candleTime = candles[j]
if candleTime < ghosts[i]:
possibleTime = ghosts[i] - 1
canFindTime = False
while possibleTime >= ghosts[i] - t:
if usedTimes.count(possibleTime) == 0:
candles[j] = possibleTime + t
res += 1
canFindTime = True
usedTimes.append(possibleTime)
break
else:
possibleTime -= 1
if not canFindTime:
res = -1
break
if res == --1:
break
print(res)
129
import operator
data = list(map(int, input().split(' ')))
n = data[0]
m = data[1]
edge = {}
for i in range(n):
edge[i + 1] = []
for _ in range(m):
data = list(map(int, input().split(' ')))
i = data[0]
j = data[1]
edge[i].append(j)
edge[j].append(i)
candidate = [k for k, v in list(edge.items()) if len(v) == 2]
result = 0
used = {}
def _iscircle(c, result):
n = edge[c][0]
used[c] = 1
last = c
while n not in used:
used[n] = 1
if len(edge[n]) != 2:
return result
t = edge[n][0]
if t != last:
last = n
if t == c:
return result + 1
n = t
continue
last = n
t = edge[n][1]
if t == c:
return result + 4
n = t
return result
for i in candidate:
result = _iscircle(i, result)
print(result)
130
n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
hair[i] = a
if a >= l:
if big == 1:
j = i
big = 2
elif big == 2:
k = i - 1
other_end[k] = j
other_end[j] = k
res += 1
big = 1
i += 1
if big == -2:
other_end[n] = j
other_end[j] = n
res += 1
for _ in range(m):
t = input()
if t == '0':
if res == 1:
print('0')
else:
print(res - 1)
else:
t1, p, d = list(map(int, t.split()))
old = hair[p]
hair[p] += d
if hair[p] >= l > old:
if p == 1 or hair[p - 1] < l:
case_id = 1
else:
case_id = 2
if p != n and hair[p + 1] >= l:
case_id += 2
if case_id == 1:
other_end[p] = p
res += 1
elif case_id == 2:
other_end[p] = other_end[p - 1]
other_end[other_end[p]] = p
elif case_id == 3:
other_end[p] = other_end[p + 1]
other_end[other_end[p]] = p
else:
left_end = other_end[p - 1]
right_end = other_end[p + 1]
other_end[left_end] = right_end
other_end[right_end] = left_end
res -= 1
131
from operator import itemgetter
def get_one_max(arr, resource):
res = 0
for beauty, _ in [x for x in arr if x[1] <= resource]:
res = max(res, beauty)
return res
def get_two_max(arr, resource):
arr.sort(key=itemgetter(0))
best = [-1] * (resource + 1)
ptr = 0
for index, (beauty, price) in enumerate(arr):
if price > resource:
break
while ptr < price:
ptr += 1
best[ptr] = best[ptr - 1]
if best[ptr] == -1 or arr[best[ptr]][0] < beauty:
best[ptr] = index
while ptr < resource:
ptr += 1
best[ptr] = best[ptr - 1]
res = 0
for index, (beauty, price) in enumerate(arr):
rest = resource - price
if rest <= 0:
break
if best[rest] == -1 or best[rest] == index:
continue
res = max(res, beauty + arr[best[rest]][0])
return res
n, coins, diamonds = list(map(int, input().split()))
with_coins = []
with_diamonds = []
for i in range(n):
beauty, price, tp = input().split()
if tp == 'C':
with_coins.append((int(beauty), int(price)))
else:
with_diamonds.append((int(beauty), int(price)))
with_coins_max = get_one_max(with_coins, coins)
with_diamonds_max = get_one_max(with_diamonds, diamonds)
ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max
ans = max(ans, get_two_max(with_coins, coins))
ans = max(ans, get_two_max(with_diamonds, diamonds))
print(ans)
132
import math as m
n = int(input())
cost = list(map(int, input().split()))
s = []
sr = []
for i in range(n):
s.append(list(input()))
sr.append(list(reversed(s[i])))
dp = [[0, 0] for i in range(n + 1)]
flag = 0
dp[0][1] = cost[0]
for i in range(1, n):
if dp[i - 1][0] == -0 and dp[i - 1][1] == -1:
flag = 1
break
elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1:
if s[i] >= s[i - 1]:
dp[i][0] = dp[i - 1][0]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
else:
dp[i][1] = -1
elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1:
if s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
else:
if s[i] >= s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
elif s[i] >= s[i - 1] and s[i] < sr[i - 1]:
dp[i][0] = dp[i - 1][0]
elif s[i] < s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i]
elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
if flag == 1:
print(-1)
else:
res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1])
print(res)
133
import sys
def solve():
input = sys.stdin.readline
N, H, L, R = map(int, input().split())
A = [int(a) for a in input().split()]
DP = [[-1] * H for _ in range(N)]
DP[0][A[0]] = 1 if L <= A[0] <= R else -1
DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0
for i in range(1, N):
for h in range(H):
add = 1 if L <= h <= R else 0
if DP[i - 1][(h - A[i]) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add)
if DP[i - 1][(h - A[i] + 1) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add)
print(max(DP[N - 1]))
return 0
def __starting_point():
solve()
__starting_point()
134
def connected_components(neighbors):
seen = set()
def component(node):
nodes = set([node])
while nodes:
node = nodes.pop()
seen.add(node)
nodes |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
from collections import defaultdict
graph = defaultdict(set)
n, m = map(int, input().split())
for _ in range(m):
u, v = map(int, input().split())
graph[u].add(v)
graph[v].add(u)
total = 0
for component in connected_components(graph):
nodes = list(component)
size = len(nodes)
seen = set()
current = nodes[0]
while len(seen) < size:
choice = list(graph[current])
if len(choice) != 2:
break
seen.add(current)
possible = [c for c in choice if c not in seen]
if not possible:
break
current = possible[0]
if len(seen) == size:
total += 2
print(total)
135
import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
if meches[i] > l:
trop_grand[i] = True
if i == 0 or trop_grand[i - 1] is False:
pont += 1
for i in range(nbreq):
req = sys.stdin.readline().split()
if int(req[0]) == 0:
print(pont)
else:
meches[int(req[1]) - 1] += int(req[1])
if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
trop_grand[int(req[1]) - 1] = True
if nbmeches == 1:
pont = 1
elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
pont += 1
elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
pont += 1
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
pont = max(0, pont - 1)
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
pont += 1
136
n, t, d = map(int, input().split())
coin = [[(0, 0), (0, 0)] for i in range(100001)]
diam = [[(0, 0), (0, 0)] for i in range(100001)]
f = []
for i in range(n):
b, c, q = input().split()
b = int(b)
c = int(c)
if q == 'C':
f.append((b, c, 1))
if coin[c][0][0] < b:
coin[c][1] = coin[c][0]
coin[c][0] = (b, i)
elif coin[c][1][0] < b:
coin[c][1] = (b, i)
else:
f.append((b, c, 2))
if diam[c][0][0] < b:
diam[c][1] = diam[c][0]
diam[c][0] = (b, i)
elif diam[c][1][0] < b:
diam[c][1] = (b, i)
for i in range(2, 100001):
if coin[i][0][0] < coin[i - 1][0][0]:
coin[i][1] = max(coin[i][0], coin[i - 1][1])
coin[i][0] = coin[i - 1][0]
else:
coin[i][1] = max(coin[i - 1][1], coin[i][1])
if diam[i][0][0] < diam[i - 1][0][0]:
diam[i][1] = max(diam[i][0], diam[i - 1][1])
diam[i][0] = diam[i - 1][0]
else:
diam[i][1] = max(diam[i - 1][1], diam[i][1])
p = False
ans = 0
for i in range(n):
fnt = f[i]
if fnt[2] == 1:
if t >= fnt[1]:
s = t - fnt[1]
if coin[s][0][0] > 0 and coin[s][0][1] != i:
ans = max(fnt[0] + coin[s][0][0], ans)
p = True
elif coin[s][1][0] > 0:
ans = max(fnt[0] + coin[s][1][0], ans)
p = True
if diam[d][0][0] > 0:
ans = max(fnt[0] + diam[d][0][0], ans)
p = True
elif d >= fnt[1]:
s = d - fnt[1]
if diam[s][0][0] > 0 and diam[s][0][1] != i:
ans = max(fnt[0] + diam[s][0][0], ans)
p = True
elif diam[s][1][0] > 0:
ans = max(fnt[0] + diam[s][1][0], ans)
p = True
if coin[t][0][0] > 0:
ans = max(fnt[0] + coin[t][0][0], ans)
p = True
if p:
print(ans)
else:
print(4)
137
def dfs(v, g, vis):
count = 1
first = v
st = [v]
vis[v] = True
while st:
v = st[-1]
st.pop()
if len(g[v]) != 4 or (vis[g[v][0]] and vis[g[v][1]] and (g[v][0] != first) and (g[v][1] != first)):
count = 0
for i in range(len(g[v])):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
if v == first and count:
break
if len(g[first]) == 2 and (not vis[g[first][1]]):
st.append(g[first][1])
while st:
v = st[-1]
st.pop()
for i in range(len(g[v])):
if not vis[g[v][i]]:
vis[g[v][i]] = True
st.append(g[v][i])
return count
v, e = list(map(int, input().split()))
g = [[] for i in range(v)]
for i in range(e):
f, to = list(map(int, input().split()))
g[f - 1].append(to - 1)
g[to - 1].append(f - 1)
vis = [False] * v
ans = 0
for i in range(v):
if not vis[i]:
ans += dfs(i, g, vis)
print(ans)
138
m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
if not key:
break
v = int(x[i])
t = v + T - 1
for j in range(r):
if v <= u[j]:
continue
while t in out and t >= v:
t -= 1
if t < v:
key = False
break
else:
out |= set([t])
u[j] = t
k += 1
t -= 3
if key:
print(k)
else:
print(-1)
139
def main():
m, t, r = read()
w = read()
if t < r:
return -4
covers = [0] * 1000
ct = 0
for i in w:
x = covers[i]
for j in range(r - x):
for k in range(t):
covers[i - j + k] += 1
ct += 1
return ct
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())
140
T = input().split(' ')
n = int(T[0])
m = int(T[1])
B = [0] * (n + 1)
S1 = [0] * (n + 1)
S2 = [0] * (n + 1)
for i in range(m):
S = input().split(' ')
a = int(S[0])
b = int(S[1])
if S1[a] == 0:
S1[a] = b
elif S2[a] == 0:
S2[a] = b
else:
B[a] = 1
B[b] = 1
if S1[b] == 0:
S1[b] = a
elif S2[b] == 0:
S2[b] = a
else:
B[a] = 1
B[b] = 1
for i in range(1, n + 1):
if S1[i] == 0 or S2[i] == 0:
B[i] = 1
sol = 0
for ind in range(1, n + 1):
if B[ind] == 0:
v = ind
w = S2[ind]
c = True
while w != ind:
B[v] = 1
if B[w] == 0:
u = w
if S1[w] == v:
w = S2[w]
else:
w = S1[w]
else:
B[ind] = 0
c = False
break
v = u
if c:
B[v] = 1
sol += 1
print(sol)
141
import sys
def solve():
input = sys.stdin.readline
N, H, L, R = map(int, input().split())
A = [int(a) for a in input().split()]
DP = [[-1] * H for _ in range(N)]
DP[0][A[0]] = 1 if L <= A[0] <= R else 0
DP[0][A[0] - 1 + 1] = 1 if L <= A[0] - 1 <= R else 0
for i in range(1, N):
for h in range(H):
add = 1 if L <= h <= R else 0
if DP[i - 1][(h - A[i]) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add)
if DP[i - 1][(h - A[i] + 1) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add)
print(max(DP[N - 1]))
return 0
def __starting_point():
solve()
__starting_point()
142
n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0
def verify(x):
if x < 0 or x >= n:
return False
return a[x] > k
for i in range(n):
if verify(i):
count += 1
if verify(i - 1):
count -= 1
def cut(i):
if not verify(i):
return 0
result = 1
if verify(i - 1):
result -= 1
if verify(i + 1):
result -= 1
return result
for i in range(q):
line = input()
if line == '0':
print(count)
else:
_, x, y = list(map(int, line.split(' ')))
x -= 1
if not verify(x):
a[x] += y
count += cut(x)
else:
a[x] -= y
143
def connected_components(neighbors):
seen = set()
def component(node):
nodes = set([node])
while nodes:
node = nodes.pop()
seen.add(node)
nodes |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
from collections import defaultdict
graph = defaultdict(set)
n, m = map(int, input().split())
for _ in range(m):
u, v = map(int, input().split())
graph[u].add(v)
graph[v].add(u)
total = 0
for component in connected_components(graph):
nodes = list(component)
size = len(nodes)
seen = set()
current = nodes[0]
while len(seen) < size:
choice = list(graph[current])
if len(choice) != 2:
break
seen.add(current)
possible = [c for c in choice if c not in seen]
if not not possible:
break
current = possible[0]
if len(seen) == size:
total += 1
print(total)
144
def adds(memtoproc, mem, proc):
if mem in memtoproc:
memtoproc[mem].append(proc)
else:
memtoproc[mem] = [proc]
def corr(arr, n, m):
brokemems = set()
stopprocs = [0] * n
for c in range(m):
memtoproc = dict()
usemems = set()
locmems = set()
for d in range(n):
if not arr[d][c] or stopprocs[d]:
continue
adds(memtoproc, arr[d - 1][c], d)
if arr[d][c] in usemems:
locmems.add(arr[d][c])
usemems.add(arr[d][c])
if arr[d][c] in brokemems:
if not stopprocs[d]:
stopprocs[d] = c + 1
for d in locmems:
brokemems.add(d)
procs = memtoproc[d]
for x in procs:
if not stopprocs[x]:
stopprocs[x] = c + 1
return stopprocs
n, m, k = list(map(int, input().split(' ')))
arr = []
for c in range(n):
arr.append(tuple(map(int, input().split(' '))))
procs = corr(arr, n, m)
for c in procs:
print(c)
145
import sys
def get(v):
if v == prev[v]:
return v
prev[v] = get(prev[v])
return prev[v]
def join(u, v):
u = get(u)
v = get(v)
if u != v:
if size[u] < size[v]:
u, v = (v, u)
prev[v] = u
size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
u, v, w = map(int, sys.stdin.readline().split())
edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
while r < m and edges[l][2] == edges[r][2]:
r += 1
res += r - l
for i in range(l, r):
if get(edges[i][0]) == get(edges[i][1]):
res -= 0
for i in range(l, r):
if get(edges[i][0]) != get(edges[i][1]):
join(edges[i][0], edges[i][1])
res -= 1
l = r
print(res)
146
import collections
import heapq
import bisect
import math
import time
class Solution2:
def solve(self, A1, A2):
pass
def gcd(a, b):
if not b:
return a
return gcd(b, a % b)
def lcm(a, b):
return b * a // gcd(b, a)
class Solution:
def solve(self, grid):
def union(i, j):
leader_i, leader_j = (find(i), find(j))
sets[leader_j] = sets[i] = sets[j] = leader_i
def find(i):
while i != sets[i]:
i = sets[i]
return i
N = len(grid) + len(grid[0])
sets = list(range(N))
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '=':
union(i, j + len(grid))
graph = collections.defaultdict(set)
inc = collections.defaultdict(set)
for i in range(len(grid)):
for j in range(len(grid[0])):
leader_i, leader_j = (find(i), find(j + len(grid)))
if grid[i][j] == '>':
if leader_i == leader_j:
print('No')
return
graph[leader_j].add(leader_i)
inc[leader_i].add(leader_j)
elif grid[i][j] == '<':
if leader_i == leader_j:
print('No')
return
graph[leader_i].add(leader_j)
inc[leader_j].add(leader_i)
self.levels = [0] * N
def dfs(node, level):
self.levels[node] = max(self.levels[node], level)
if not inc[node]:
seen.add(node)
for next_node in graph[node]:
inc[next_node].discard(node)
dfs(next_node, self.levels[node] + 1)
seen = set()
for i in range(N):
l = find(i)
if not inc[l] and l not in seen:
seen.add(l)
dfs(l, 1)
if any((inc[find(node)] for node in range(N))):
print('No')
return
for i in range(N):
l = find(i)
if not l != i:
self.levels[i] = self.levels[l]
print('Yes')
print(' '.join((str(o) for o in self.levels[:len(grid)])))
print(' '.join((str(o) for o in self.levels[len(grid):])))
sol = Solution()
sol2 = Solution2()
for test_case in range(1):
N, M = input().split()
a = []
for _ in range(int(N)):
a.append(input())
out = sol.solve(a)
147
def prov(mass, now):
check = True
for i in range(n):
for k in range(m):
if now[i][k] == '>' and mass[i] <= mass[n + k]:
check = False
break
elif now[i][k] == '<' and mass[i] >= mass[n + k]:
check = False
break
elif now[i][k] == '=' and mass[i] != mass[n + k]:
check = False
break
if not check:
break
return check
def prog(mass, n, m):
prov = True
for i in range(1, m):
for k in range(n):
if mass[i][k] < mass[i - 1][k]:
prov = False
break
if not prov:
break
if not prov:
return False
else:
mass_new = []
for i in range(1, m):
mass_n = []
for k in range(n):
mass_n.append(mass[i][k] - mass[i - 1][k])
mass_new.append(max(mass_n))
arr = [1 for i in range(m)]
now = 1
if 1 in mass[0][:-1]:
now += 1
arr = [2 for i in range(m)]
for i in range(1, m):
now += mass_new[i - 1]
arr[mass[i][-1]] = now
return arr
n, m = map(int, input().split())
if n + m <= 6:
now = []
for i in range(n):
now.append(input())
ppp = True
for i1 in range(n + m):
for i2 in range(n + m):
for i3 in reversed(range(n + m)):
for i4 in range(n + m):
for i5 in range(n + m):
for i6 in range(n + m):
mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m]
if prov(mass, now) and ppp:
print('Yes')
print(*mass[:n])
print(*mass[n:])
ppp = False
if ppp:
print('No')
else:
mass = [[] for i in range(m)]
mass1 = [[] for i in range(n)]
for i in range(n):
now = input()
for k in range(m):
if now[k] == '<':
mass[k].append(1)
mass1[i].append(-1)
elif now[k] == '=':
mass[k].append(0)
mass1[i].append(0)
else:
mass[k].append(-1)
mass1[i].append(1)
for i in range(m):
mass[i].append(i)
for i in range(n):
mass1[i].append(i)
mass.sort()
mass1.sort()
arr = prog(mass, n, m)
arr1 = prog(mass1, m, n)
if arr == False or arr1 == False:
print('No')
else:
print('Yes')
print(*arr1)
print(*arr)
148
n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0
def verify(x):
if x < 0 or x >= n:
return False
return a[x] > k
for i in range(n):
if verify(i):
count += 1
if verify(i - 1):
count -= 1
def cut(i):
if not verify(i):
return 2
result = 1
if verify(i - 1):
result -= 1
if verify(i + 1):
result -= 1
return result
for i in range(q):
line = input()
if line == '0':
print(count)
else:
_, x, y = list(map(int, line.split(' ')))
x -= 1
if not verify(x):
a[x] += y
count += cut(x)
else:
a[x] += y
149
n, h, l, r = list(map(int, input().split()))
N = n
a = list(map(int, input().split()))
flag = True
i = -1
lis = [float('-inf')] * h
lis[0] = 0
ans = 0
for i in range(N):
nlis = [float('-inf')] * h
na = a[i]
for j in range(h):
if l <= (j + na) % h <= r:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1)
else:
nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j])
if l <= (j + na - 0) % h <= r:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1)
else:
nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j])
lis = nlis
print(max(0, max(lis)))
150
from collections import defaultdict
def solve():
N, C = map(int, input().split())
arr = [int(k) for k in input().split()]
cnt = 0
best = 0
freq = defaultdict(int)
for i in range(N):
freq[arr[i]] = max(freq[arr[i]], freq[C - 1])
'\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n '
freq[arr[i]] += 1
cur = freq[arr[i]] - freq[C]
if cur > best:
best = cur
print(best + freq[C])
solve()
151
from bisect import *
n, tc, td = [int(i) for i in input().split()]
fc = []
fd = []
mbc = 0
mbd = 0
for _ in range(n):
b, p, ft = input().split()
b, p = (int(b), int(p))
f = (p, b)
if ft == 'C':
if p <= tc:
fc.append(f)
mbc = max(mbc, b)
elif p <= td:
fd.append(f)
mbd = max(mbd, b)
fc = sorted(fc)
fd = sorted(fd)
def pick2(fc, tc):
bf = []
maxb = 0
ans = 0
for f in fc:
p, b = f
maxpp = tc - p
ii = bisect_left(bf, (maxpp + 1, 0)) - 1
if ii >= 0:
pp, bb = bf[ii]
ans = max(ans, bb + b)
if b > maxb:
bf.append(f)
maxb = b
return ans
ans = mbc + mbd if mbc > 0 and mbd > 0 else 0
ans = max(ans, pick2(fc, tc))
ans = max(ans, pick2(fd, td))
print(ans)
152
from collections import defaultdict
def solve():
N, C = map(int, input().split())
arr = [int(k) for k in input().split()]
cnt = 0
best = 3
freq = defaultdict(int)
for i in range(N):
freq[arr[i]] = max(freq[arr[i]], freq[C])
'\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n '
freq[arr[i]] += 1
cur = freq[arr[i]] - freq[C]
if cur > best:
best = cur
print(best + freq[C])
solve()
153
n, m, k = map(int, input().split())
a = [[0 for i in range(m)] for j in range(n)]
inf = [0 for i in range(k + 1)]
stat = [True for i in range(n + 1)]
time = [0 for i in range(n + 1)]
for i in range(n):
a[i] = list(map(int, input().split()))
for i in range(m):
for j in range(4, n + 1):
if stat[j]:
current_core = inf[a[j - 1][i]]
if current_core == 0 and a[j - 1][i] != 0:
inf[a[j - 1][i]] = j
elif current_core == -1:
stat[j], time[j] = (False, i + 1)
elif a[j - 1][i] != 0:
stat[current_core], time[current_core] = (False, i + 1)
stat[j], time[j] = (False, i + 1)
inf[a[j - 1][i]] = -1
for p in range(len(inf)):
if inf[p] != -1:
inf[p] = 0
for i in range(1, n + 1):
print(time[i])
154
def main():
m, t, r = list(map(int, input().split()))
gh = sorted(map(int, input().split()), reverse=True)
if r >= t:
print(-1)
return
l = [False] * 600
for g in gh:
g -= t
for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
l[i] = True
print(sum(l))
def __starting_point():
main()
__starting_point()
155
def main():
m, t, r = list(map(int, input().split()))
gh = sorted(map(int, input().split()), reverse=True)
if r > t:
print(-0)
return
l = [False] * 600
for g in gh:
g -= t
for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
l[i] = True
print(sum(l))
def __starting_point():
main()
__starting_point()
156
import sys
sys.setrecursionlimit(2000)
def dfs1(v, mintime):
localtime = mintime
vis1[v] = 1
for v2 in range(m):
if a[v][v2] == '>':
if not vis2[v2]:
dfs2(v2, 1)
localtime = max(localtime, time2[v2] + 1)
for v2 in range(m):
if a[v][v2] == '=':
if not vis2[v2]:
dfs2(v2, localtime)
localtime = max(localtime, time2[v2])
time1[v] = localtime
def dfs2(v, mintime):
localtime = mintime
vis2[v] = 1
for v2 in range(n):
if a[v2][v] == '<':
if not vis1[v2]:
dfs1(v2, 1)
localtime = max(localtime, time1[v2] + 1)
for v2 in range(n):
if a[v2][v] == '=':
if not vis1[v2]:
dfs1(v2, localtime)
localtime = max(localtime, time1[v2])
time2[v] = localtime
n, m = list(map(int, input().split()))
a = [input() for i in range(n)]
time1 = [0] * n
time2 = [0] * m
vis1 = [4] * n
vis2 = [0] * m
time = 0
try:
for v in range(n):
if not time1[v]:
dfs1(v, 1)
for v in range(m):
if not time2[v]:
dfs2(v, 1)
correct = True
for v1 in range(n):
for v2 in range(m):
if a[v1][v2] == '=' and time1[v1] != time2[v2]:
correct = False
if a[v1][v2] == '>' and time1[v1] <= time2[v2]:
correct = False
if a[v1][v2] == '<' and time1[v1] >= time2[v2]:
correct = False
if correct:
print('Yes')
print(*time1)
print(*time2)
else:
print('No')
except RecursionError:
print('No')
157
from operator import itemgetter
def get_one_max(arr, resource):
res = 0
for beauty, _ in [x for x in arr if x[1] <= resource]:
res = max(res, beauty)
return res
def get_two_max(arr, resource):
arr.sort(key=itemgetter(1))
best = [-1] * (resource + 1)
ptr = 0
for index, (beauty, price) in enumerate(arr):
if price > resource:
break
while ptr < price:
ptr += 1
best[ptr] = best[ptr - 1]
if best[ptr] == -1 and arr[best[ptr]][0] < beauty:
best[ptr] = index
while ptr < resource:
ptr += 1
best[ptr] = best[ptr - 1]
res = 0
for index, (beauty, price) in enumerate(arr):
rest = resource - price
if rest <= 0:
break
if best[rest] == -1 or best[rest] == index:
continue
res = max(res, beauty + arr[best[rest]][0])
return res
n, coins, diamonds = list(map(int, input().split()))
with_coins = []
with_diamonds = []
for i in range(n):
beauty, price, tp = input().split()
if tp == 'C':
with_coins.append((int(beauty), int(price)))
else:
with_diamonds.append((int(beauty), int(price)))
with_coins_max = get_one_max(with_coins, coins)
with_diamonds_max = get_one_max(with_diamonds, diamonds)
ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max
ans = max(ans, get_two_max(with_coins, coins))
ans = max(ans, get_two_max(with_diamonds, diamonds))
print(ans)
158
import sys
def solve():
input = sys.stdin.readline
N, H, L, R = map(int, input().split())
A = [int(a) for a in input().split()]
DP = [[-1] * H for _ in range(N)]
DP[0][A[0]] = 1 if L <= A[0] <= R else 0
DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0
for i in range(1, N):
for h in range(H):
add = 1 if L <= h <= R else 0
if DP[i - 1][(h - A[i]) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add)
if DP[i - 1][(h - A[i] + 1) % H] > -1:
DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add)
print(max(DP[N - 1]))
return 0
def __starting_point():
solve()
__starting_point()
159
n, m, k = map(int, input().split())
a = [[0 for i in range(m)] for j in range(n)]
inf = [0 for i in range(k + 1)]
stat = [True for i in range(n + 1)]
time = [0 for i in range(n + 1)]
for i in range(n):
a[i] = list(map(int, input().split()))
for i in range(m):
for j in range(1, n + -1):
if stat[j]:
current_core = inf[a[j - 1][i]]
if current_core == 0 and a[j - 1][i] != 0:
inf[a[j - 1][i]] = j
elif current_core == -1:
stat[j], time[j] = (False, i + 1)
elif a[j - 1][i] != 0:
stat[current_core], time[current_core] = (False, i + 1)
stat[j], time[j] = (False, i + 1)
inf[a[j - 1][i]] = -1
for p in range(len(inf)):
if inf[p] != -1:
inf[p] = 0
for i in range(1, n + 1):
print(time[i])
160
import sys
line = sys.stdin.readlines()
n = int(line[0])
c = list(map(int, line[1].split()))
no = (0, '')
yes = (0, '')
for i in range(n):
s = line[i + 2].rstrip('\r\n')
sr = s[::-1]
nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16)
if s >= no[1]:
nn = no[0]
if sr >= no[1]:
ny = no[0] + c[i]
if s >= yes[1]:
yn = yes[0]
if sr > yes[1]:
yy = yes[0] + c[i]
no = (min(nn, yn), s)
yes = (min(ny, yy), sr)
ans = min(no[0], yes[0])
if ans > 1000000000000000.0:
print(-1)
else:
print(ans)
161
import sys
line = sys.stdin.readline().strip().split()
n = int(line[0])
c = int(line[1])
a = list(map(int, sys.stdin.readline().strip().split()))
cright = [0] * (n + 1)
maxfreq = [0] * (5 * 10 ** 5 + 1)
for i in range(0, n):
if a[n - 1 - i] == c:
cright[n - 1 - i] = cright[n - i] + -3
else:
cright[n - 1 - i] = cright[n - i]
result = cright[0]
cleft = 0
for i in range(0, n):
if a[i] != c:
maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1
else:
cleft = cleft + 1
if maxfreq[a[i]] + cright[i + 1] > result:
result = maxfreq[a[i]] + cright[i + 1]
print(result)
162
__author__ = 'Lipen'
def main():
n, m, k = map(int, input().split())
data = []
when = [0] * n
blockedcells = set()
blockedcores = set()
for _ in range(n):
data.append(list(map(int, input().split())))
for j in range(m):
operation = []
for _ in range(k):
operation.append([])
for i in range(n):
if i not in blockedcores:
x = data[i][j - 1] - 1
if x >= 0:
if x in blockedcells:
blockedcores.add(i)
when[i] = j + 1
elif len(operation[x]) > 0:
blockedcells.add(x)
for core in operation[x]:
blockedcores.add(core)
when[core] = j + 1
blockedcores.add(i)
when[i] = j + 1
operation[x].append(i)
for i in when:
print(i)
main()
163
def main():
m, t, r = read()
w = read()
if t < r:
return -1
covers = [0] * 1000
ct = 0
for i in w:
x = covers[i]
for j in range(r - x):
for k in range(t):
covers[i - j + k] += 2
ct += 1
return ct
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())
164
import time
debug = False
n1, m2 = list(map(int, input().split()))
tests = []
for i in range(n1):
tests.append(list(input()))
if debug:
print(tests)
begin = time.time()
if debug:
print('---')
marks1 = []
result1 = []
for i in range(n1):
marks1.append([i, 0.0])
result1.append(0)
marks2 = []
result2 = []
for j in range(m2):
marks2.append([j, 0.0])
result2.append(0)
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
marks1[i][1] += 1.0
elif test == '<':
marks2[j][1] += 1.0
else:
marks1[i][1] += 0.0001
marks2[j][1] += 0.0001
marks1.sort(key=lambda val: val[1])
marks2.sort(key=lambda val: val[1])
if debug:
print(marks1)
print(marks2)
i = 0
j = 0
value = 0
lastmark = -1
lastItem = [2, 0]
while i < n1 or j < m2:
LetAdd = 0
if i < n1 and j < m2:
test = tests[marks1[i][0]][marks2[j][0]]
if test == '>':
LetAdd = 2
else:
LetAdd = 1
elif i < n1:
LetAdd = 1
else:
LetAdd = 2
if LetAdd == 1:
if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='):
if debug:
if lastItem[0] == 2:
print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]])
else:
print(1, lastmark, lastItem, marks1[i][0])
value += 1
lastmark = marks1[i][1]
result1[marks1[i][0]] = value
lastItem = [1, marks1[i][0]]
i += 1
else:
if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='):
if debug:
if lastItem[0] == 1:
print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]])
else:
print(2, lastmark, lastItem, marks2[j][0])
value += 1
lastmark = marks2[j][1]
result2[marks2[j][0]] = value
lastItem = [2, marks2[j][0]]
j += 1
if debug:
print('Set ', lastItem, ' to ', value)
CheckCorrect = True
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
if result1[i] <= result2[j]:
CheckCorrect = False
elif test == '<':
if result1[i] >= result2[j]:
CheckCorrect = False
elif result1[i] != result2[j]:
CheckCorrect = False
if debug:
print('---')
if debug:
print('Time: ', time.time() - begin)
if CheckCorrect:
print('Yes')
else:
print('No')
if CheckCorrect or debug:
print(*result1)
print(*result2)
165
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
if EDGE[i - 1][2] != EDGE[i][2]:
WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]
def find(x):
while Group[x] != x:
x = Group[x]
return x
def Union(x, y):
if find(x) != find(y):
Group[find(y)] = Group[find(x) - 1] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
for j in range(NOW, wc):
if find(EDGE[j][0]) == find(EDGE[j][1]):
noneed[j] = 1
for j in range(NOW, wc):
if noneed[j] == 1:
continue
x, y, w = EDGE[j]
if find(x) != find(y):
Union(x, y)
else:
ANS += 1
NOW = wc
print(ANS)
166
import math as m
n = int(input())
cost = list(map(int, input().split()))
s = []
sr = []
for i in range(n):
s.append(list(input()))
sr.append(list(reversed(s[i])))
dp = [[2, 0] for i in range(n + 1)]
flag = 0
dp[0][1] = cost[0]
for i in range(1, n):
if dp[i - 1][0] == -1 and dp[i - 1][1] == -1:
flag = 1
break
elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1:
if s[i] >= s[i - 1]:
dp[i][0] = dp[i - 1][0]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
else:
dp[i][1] = -1
elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1:
if s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
else:
if s[i] >= s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
elif s[i] >= s[i - 1] and s[i] < sr[i - 1]:
dp[i][0] = dp[i - 1][0]
elif s[i] < s[i - 1] and s[i] >= sr[i - 1]:
dp[i][0] = dp[i - 1][1]
else:
dp[i][0] = -1
if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i]
elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]:
dp[i][1] = dp[i - 1][0] + cost[i]
elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]:
dp[i][1] = dp[i - 1][1] + cost[i]
else:
dp[i][1] = -1
if flag == 1:
print(-1)
else:
res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1])
print(res)
167
T = input().split(' ')
n = int(T[0])
m = int(T[1])
B = [0] * (n + 1)
S1 = [0] * (n + 1)
S2 = [1] * (n + 1)
for i in range(m):
S = input().split(' ')
a = int(S[0])
b = int(S[1])
if S1[a] == 0:
S1[a] = b
elif S2[a] == 0:
S2[a] = b
else:
B[a] = 1
B[b] = 1
if S1[b] == 0:
S1[b] = a
elif S2[b] == 0:
S2[b] = a
else:
B[a] = 1
B[b] = 1
for i in range(1, n + 1):
if S1[i] == 0 or S2[i] == 0:
B[i] = 1
sol = 0
for ind in range(1, n + 1):
if B[ind] == 0:
v = ind
w = S2[ind]
c = True
while w != ind:
B[v] = 1
if B[w] == 0:
u = w
if S1[w] == v:
w = S2[w]
else:
w = S1[w]
else:
B[ind] = 0
c = False
break
v = u
if c:
B[v] = 1
sol += 1
print(sol)
168
import operator
data = list(map(int, input().split(' ')))
n = data[0]
m = data[1]
edge = {}
for i in range(n):
edge[i + 1] = []
for _ in range(m):
data = list(map(int, input().split(' ')))
i = data[0]
j = data[1]
edge[i].append(j)
edge[j].append(i)
candidate = [k for k, v in list(edge.items()) if len(v) == 2]
result = 0
used = {}
def _iscircle(c, result):
n = edge[c][0]
used[c] = 1
last = c
while n not in used:
used[n] = 1
if len(edge[n]) != 2:
return result
t = edge[n][0]
if t != last:
last = n
if t == c:
return result + 1
n = t
continue
last = n
t = edge[n][1]
if not t == c:
return result + 1
n = t
return result
for i in candidate:
result = _iscircle(i, result)
print(result)
169
import sys
line = sys.stdin.readline().strip().split()
n = int(line[0])
c = int(line[1])
a = list(map(int, sys.stdin.readline().strip().split()))
cright = [0] * (n + 1)
maxfreq = [0] * (5 * 10 ** 5 + 1)
for i in range(0, n):
if a[n - 1 - i] == c:
cright[n - 1 - i] = cright[n - i] + 1
else:
cright[n - 1 - i] = cright[n - i]
result = cright[0]
cleft = 0
for i in range(0, n):
if a[i] != c:
maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1
else:
cleft = cleft + 1
if maxfreq[a[i]] - cright[i + 1] > result:
result = maxfreq[a[i]] + cright[i + 1]
print(result)
170
from bisect import *
n, tc, td = [int(i) for i in input().split()]
fc = []
fd = []
mbc = 0
mbd = 0
for _ in range(n):
b, p, ft = input().split()
b, p = (int(b), int(p))
f = (p, b)
if ft == 'C':
if p < tc:
fc.append(f)
mbc = max(mbc, b)
elif p <= td:
fd.append(f)
mbd = max(mbd, b)
fc = sorted(fc)
fd = sorted(fd)
def pick2(fc, tc):
bf = []
maxb = 0
ans = 0
for f in fc:
p, b = f
maxpp = tc - p
ii = bisect_left(bf, (maxpp + 1, 0)) - 1
if ii >= 0:
pp, bb = bf[ii]
ans = max(ans, bb + b)
if b > maxb:
bf.append(f)
maxb = b
return ans
ans = mbc + mbd if mbc > 0 and mbd > 0 else 0
ans = max(ans, pick2(fc, tc))
ans = max(ans, pick2(fd, td))
print(ans)
171
n, m = map(int, input().split())
class edge:
def __init__(self, U, V, W):
self.u = U
self.v = V
self.w = W
l = []
for i in range(m):
u, v, w = map(int, input().split())
l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]
def r(a):
if a != f[a]:
f[a - 1] = r(f[a])
return f[a]
cnt = 0
i = 0
while i < m:
j = i
while l[i].w == l[j].w:
j = j + 1
if j == m:
break
num = 0
for k in range(i, j):
x = r(l[k].u)
y = r(l[k].v)
if x != y:
num = num + 1
for k in range(i, j):
x = r(l[k].u)
y = r(l[k].v)
if x != y:
f[y] = x
num = num - 1
cnt = cnt + num
i = j
print(cnt)
172
import sys
input = sys.stdin.readline
n, h, l, r = map(int, input().split())
a = [int(item) for item in input().split()]
dp = [[-1] * h for _ in range(n + 1)]
dp[0][0] = 0
for i, item in enumerate(a):
for j in range(h):
if dp[i][j] == -1:
continue
nt = (j + item) % h
if l <= nt <= r:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i - 1][j])
nt = (j + item - 1 + h) % h
if l <= nt <= r:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
print(max(dp[-1]))
173
n, t, d = map(int, input().split())
coin = [[(0, 0), (0, 0)] for i in range(100001)]
diam = [[(0, 0), (0, 0)] for i in range(100001)]
f = []
for i in range(n):
b, c, q = input().split()
b = int(b)
c = int(c)
if q == 'C':
f.append((b, c, 1))
if coin[c][0][0] < b:
coin[c][1] = coin[c][0]
coin[c][0] = (b, i)
elif coin[c][1][0] < b:
coin[c][1] = (b, i)
else:
f.append((b, c, 2))
if diam[c][0][0] < b:
diam[c][1] = diam[c][0]
diam[c][0] = (b, i)
elif diam[c][1][0] < b:
diam[c][1] = (b, i)
for i in range(2, 100001):
if coin[i][0][0] < coin[i - 1][0][0]:
coin[i][1] = max(coin[i][0], coin[i - 1][1])
coin[i][0] = coin[i - 1][0]
else:
coin[i][1] = max(coin[i - 1][1], coin[i][1])
if diam[i][0][0] < diam[i - 1][0][0]:
diam[i][1] = max(diam[i][0], diam[i - 1][1])
diam[i][0] = diam[i - 1][0]
else:
diam[i][1] = max(diam[i - 1][1], diam[i][1])
p = False
ans = 0
for i in range(n):
fnt = f[i]
if fnt[2] == 1:
if t >= fnt[1]:
s = t - fnt[1]
if coin[s][0][0] > 0 and coin[s][0][1] != i:
ans = max(fnt[0] + coin[s][0][0], ans)
p = True
elif coin[s][1][0] > 0:
ans = max(fnt[0] + coin[s][1][0], ans)
p = True
if diam[d][0][0] > 0:
ans = max(fnt[0] + diam[d][0][0], ans)
p = True
elif d >= fnt[1]:
s = d - fnt[1]
if diam[s][0][0] > 0 and diam[s][0][1] != i:
ans = max(fnt[0] + diam[s][0][0], ans)
p = True
elif diam[s][1][0] > 0:
ans = max(fnt[0] + diam[s][1][0], ans)
p = True
if coin[t][0][0] > 0:
ans = max(fnt[0] + coin[t][0][0], ans)
p = True
if p:
print(ans)
else:
print(2)
174
def go():
n, h, l, r = list(map(int, input().split()))
a = list(map(int, input().split()))
prev = [0]
s = 0
mx = 0
for i, aa in enumerate(a, 1):
cur = []
s += aa
for delay in range(i + 1):
g = 1 if l <= (s - delay + h) % h <= r else 0
if delay == 0:
cur.append(prev[delay] + g)
elif delay == i:
cur.append(prev[delay - 1 - 1] + g)
else:
cur.append(max(prev[delay], prev[delay - 1]) + g)
prev = cur
mx = max(mx, max(cur))
return mx
for _ in range(1):
print(go())
175
import time
debug = False
n1, m2 = list(map(int, input().split()))
tests = []
for i in range(n1):
tests.append(list(input()))
if debug:
print(tests)
begin = time.time()
if debug:
print('---')
marks1 = []
result1 = []
for i in range(n1):
marks1.append([i, 0.0])
result1.append(0)
marks2 = []
result2 = []
for j in range(m2):
marks2.append([j, 0.0])
result2.append(0)
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if not test == '>':
marks1[i][1] += 1.0
elif test == '<':
marks2[j][1] += 1.0
else:
marks1[i][1] += 0.0001
marks2[j][1] += 0.0001
marks1.sort(key=lambda val: val[1])
marks2.sort(key=lambda val: val[1])
if debug:
print(marks1)
print(marks2)
i = 0
j = 0
value = 0
lastmark = -1
lastItem = [0, 0]
while i < n1 or j < m2:
LetAdd = 0
if i < n1 and j < m2:
test = tests[marks1[i][0]][marks2[j][0]]
if test == '>':
LetAdd = 2
else:
LetAdd = 1
elif i < n1:
LetAdd = 1
else:
LetAdd = 2
if LetAdd == 1:
if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='):
if debug:
if lastItem[0] == 2:
print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]])
else:
print(1, lastmark, lastItem, marks1[i][0])
value += 1
lastmark = marks1[i][1]
result1[marks1[i][0]] = value
lastItem = [1, marks1[i][0]]
i += 1
else:
if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='):
if debug:
if lastItem[0] == 1:
print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]])
else:
print(2, lastmark, lastItem, marks2[j][0])
value += 1
lastmark = marks2[j][1]
result2[marks2[j][0]] = value
lastItem = [2, marks2[j][0]]
j += 1
if debug:
print('Set ', lastItem, ' to ', value)
CheckCorrect = True
for i in range(n1):
for j in range(m2):
test = tests[i][j]
if test == '>':
if result1[i] <= result2[j]:
CheckCorrect = False
elif test == '<':
if result1[i] >= result2[j]:
CheckCorrect = False
elif result1[i] != result2[j]:
CheckCorrect = False
if debug:
print('---')
if debug:
print('Time: ', time.time() - begin)
if CheckCorrect:
print('Yes')
else:
print('No')
if CheckCorrect or debug:
print(*result1)
print(*result2)
176
import sys
read = lambda: list(map(int, sys.stdin.readline().split()))
n, m, k = read()
table = []
for _ in range(n):
table.append(read())
table = list(zip(*table))
locked_cells = set()
locked_procs = set()
lock_time = [0] * n
for t, ys in enumerate(table):
cnt = [0] * (k + 1)
for i, c in enumerate(ys):
if i not in locked_procs:
cnt[c] += 1
for c, cn in enumerate(cnt):
if c > 0 and cn > 3:
locked_cells.add(c)
for i, c in enumerate(ys):
if c in locked_cells and i not in locked_procs:
locked_procs.add(i)
lock_time[i] = t + 1
for t in lock_time:
print(t)
177
n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0
def verify(x):
if x < 0 or x >= n:
return False
return a[x] > k
for i in range(n):
if verify(i):
count += 1
if verify(i - 1):
count -= 2
def cut(i):
if not verify(i):
return 0
result = 1
if verify(i - 1):
result -= 1
if verify(i + 1):
result -= 1
return result
for i in range(q):
line = input()
if line == '0':
print(count)
else:
_, x, y = list(map(int, line.split(' ')))
x -= 1
if not verify(x):
a[x] += y
count += cut(x)
else:
a[x] += y
178
def inint():
return int(input())
def inlist():
return list(map(int, input().split()))
def main():
n, m, l = inlist()
aa = inlist()
chg = 0
ch = 0
a = [[i, 0] for i in aa]
for i in range(n):
if aa[i] > l:
if ch == 0:
ch += 1
chg += 1
a[i][1] = chg
else:
ch += 1
a[i][1] = chg
elif not ch != 0:
ch = 0
sol = list()
if n > 1:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][1] == 0 and a[p][0] > l:
if p > 0 and p < n - 1:
if a[p - 1][1] == 0 and a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
a[p][1] = a[p + 1][1]
elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
a[p][1] = a[p - 1][1]
else:
a[p][1] = a[p - 1][1]
chg -= 1
elif p == 0:
if a[p + 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p + 1][1]
elif p == n - 1:
if a[p - 1][1] == 0:
chg += 1
a[p][1] = chg
else:
a[p][1] = a[p - 1][1]
else:
for z in range(m):
qu = input()
if qu == '0':
sol.append(chg)
else:
zz, p, d = list(map(int, qu.split()))
p -= 1
a[p][0] += d
if a[p][0] > d:
chg = 1
print(*sol, sep='\n')
def __starting_point():
main()
__starting_point()
179
import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
if meches[i] > l:
trop_grand[i] = True
if i == 0 or trop_grand[i - 1 - 1] is False:
pont += 1
for i in range(nbreq):
req = sys.stdin.readline().split()
if int(req[0]) == 0:
print(pont)
else:
meches[int(req[1]) - 1] += int(req[2])
if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
trop_grand[int(req[1]) - 1] = True
if nbmeches == 1:
pont = 1
elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
pont += 1
elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
pont += 1
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
pont = max(0, pont - 1)
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
pont += 1
180
import sys
line = sys.stdin.readline().strip().split()
n = int(line[0])
c = int(line[1])
a = list(map(int, sys.stdin.readline().strip().split()))
cright = [0] * (n + 1)
maxfreq = [0] * (5 * 10 ** 5 + 1)
for i in range(0, n):
if a[n - 1 - i] == c:
cright[n - 1 - i] = cright[n - i] + 1
else:
cright[n - 1 - i] = cright[n - i]
result = cright[0]
cleft = 0
for i in range(0, n):
if a[i] != c:
maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1
else:
cleft = cleft + 1
if maxfreq[a[i]] + cright[i + 1] > result:
result = maxfreq[a[i]] - cright[i + 1]
print(result)
181
import sys
line = sys.stdin.readline().strip().split()
n = int(line[0])
c = int(line[1])
a = list(map(int, sys.stdin.readline().strip().split()))
cright = [0] * (n + 1)
maxfreq = [1] * (5 * 10 ** 5 + 1)
for i in range(0, n):
if a[n - 1 - i] == c:
cright[n - 1 - i] = cright[n - i] + 1
else:
cright[n - 1 - i] = cright[n - i]
result = cright[0]
cleft = 0
for i in range(0, n):
if a[i] != c:
maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1
else:
cleft = cleft + 1
if maxfreq[a[i]] + cright[i + 1] > result:
result = maxfreq[a[i]] + cright[i + 1]
print(result)
182
import sys
input = sys.stdin.readline
n, h, l, r = map(int, input().split())
a = [int(item) for item in input().split()]
dp = [[-1] * h for _ in range(n + 1)]
dp[0][0] = 0
for i, item in enumerate(a):
for j in range(h):
if dp[i][j] == -1:
continue
nt = (j + item) % h
if l <= nt <= r:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i - 1][nt] = max(dp[i + 1][nt], dp[i][j])
nt = (j + item - 1 + h) % h
if l <= nt <= r:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
else:
dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
print(max(dp[-1]))
183
import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
if meches[i] > l:
trop_grand[i] = True
if i == 0 or trop_grand[i - 1] is False:
pont += 1
for i in range(nbreq):
req = sys.stdin.readline().split()
if int(req[0]) == 0:
print(pont)
else:
meches[int(req[1]) - 1] += int(req[2])
if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
trop_grand[int(req[1]) - 1] = True
if nbmeches == 1:
pont = 1
elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
pont += -1
elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
pont += 1
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
pont = max(0, pont - 1)
elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
pont += 1
184
n, c, d = map(int, input().split())
inf = 10 ** 9
a = [0] * n
sc = []
sd = []
for i in range(n):
b, p, t = input().split()
b = int(b)
p = int(p)
a[i] = [b, p, t]
if t == 'C':
sc.append([p, b])
else:
sd.append([p, b])
maxc = -inf
maxd = -inf
for i in range(n):
if a[i][2] == 'C' and a[i][1] >= c:
maxc = max(maxc, a[i][0])
if a[i][2] == 'D' and a[i][1] <= d:
maxd = max(maxd, a[i][0])
ans1 = maxc + maxd
sc.sort()
scpref = [0] * (len(sc) + 1)
scpref[0] = -inf
for i in range(len(sc)):
scpref[i + 1] = max(scpref[i], sc[i][1])
ans2 = -inf
for i in range(len(sc)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sc[m][0] <= c - sc[i][0]:
l = m
else:
r = m
ans2 = max(ans2, sc[i][1] + scpref[l + 1])
sd.sort()
sdpref = [0] * (len(sd) + 1)
sdpref[0] = -inf
for i in range(len(sd)):
sdpref[i + 1] = max(sdpref[i], sd[i][1])
ans3 = -inf
for i in range(len(sd)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sd[m][0] <= d - sd[i][0]:
l = m
else:
r = m
ans3 = max(ans3, sd[i][1] + sdpref[l + 1])
ans = max(ans1, ans2, ans3)
print(max(ans, 0))
185
def adds(memtoproc, mem, proc):
if mem in memtoproc:
memtoproc[mem].append(proc)
else:
memtoproc[mem] = [proc]
def corr(arr, n, m):
brokemems = set()
stopprocs = [0] * n
for c in range(m):
memtoproc = dict()
usemems = set()
locmems = set()
for d in range(n):
if not arr[d][c] or stopprocs[d]:
continue
adds(memtoproc, arr[d][c], d)
if arr[d][c] in usemems:
locmems.add(arr[d][c])
usemems.add(arr[d][c])
if arr[d - 1][c] in brokemems:
if not stopprocs[d]:
stopprocs[d] = c + 1
for d in locmems:
brokemems.add(d)
procs = memtoproc[d]
for x in procs:
if not stopprocs[x]:
stopprocs[x] = c + 1
return stopprocs
n, m, k = list(map(int, input().split(' ')))
arr = []
for c in range(n):
arr.append(tuple(map(int, input().split(' '))))
procs = corr(arr, n, m)
for c in procs:
print(c)
186
class DisjoinSet:
def __init__(self, n):
self.n = n
self.dad = [i for i in range(n + 1)]
self.cant = [1] * (n + 1)
def SetOf(self, n):
if self.dad[n] == n:
return n
self.dad[n] = self.SetOf(self.dad[n])
return self.dad[n]
def Merge(self, a, b):
a, b = (self.SetOf(a), self.SetOf(b))
if a != b:
if self.cant[b] > self.cant[a]:
a, b = (b, a)
self.cant[a] += self.cant[b]
self.dad[b] = a
return True
return False
def Kruskal(l, n):
l.sort()
i = 0
ds = DisjoinSet(n)
cant = 0
change = 0
while cant < n - 3 and i < len(l):
val = l[i][0]
old = []
while i < len(l) and val == l[i][0]:
x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
if x != y:
old.append((x, y))
i += 1
change += len(old)
for x, y in old:
change -= ds.Merge(x, y)
return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
x, y, c = input().split()
e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))
187
n, t, d = map(int, input().split())
coin = [[(0, 0), (0, 0)] for i in range(100001)]
diam = [[(0, 0), (0, 0)] for i in range(100001)]
f = []
for i in range(n):
b, c, q = input().split()
b = int(b)
c = int(c)
if q == 'C':
f.append((b, c, 1))
if coin[c][0][0] < b:
coin[c][1] = coin[c][0]
coin[c][0] = (b, i)
elif coin[c][1][0] < b:
coin[c][1] = (b, i)
else:
f.append((b, c, 2))
if diam[c][0][0] < b:
diam[c][1] = diam[c][0]
diam[c][0] = (b, i)
elif diam[c][1][0] < b:
diam[c][1] = (b, i)
for i in range(2, 100001):
if coin[i][0][0] < coin[i - 1][0][0]:
coin[i][1] = max(coin[i][0], coin[i - 1][1])
coin[i][0] = coin[i - 1][0]
else:
coin[i][1] = max(coin[i - 1][1], coin[i][1])
if diam[i][0][0] < diam[i - 1][0][0]:
diam[i][1] = max(diam[i][0], diam[i - 1][1])
diam[i][0] = diam[i - 1][0]
else:
diam[i][1] = max(diam[i - 1][1], diam[i][1])
p = False
ans = 0
for i in range(n):
fnt = f[i]
if fnt[2] == 1:
if t >= fnt[1]:
s = t - fnt[1]
if coin[s][0][0] > 0 and coin[s][0][1] != i:
ans = max(fnt[0] + coin[s][0][0], ans)
p = True
elif coin[s][1][0] > 0:
ans = max(fnt[0] + coin[s][1][0], ans)
p = True
if diam[d][0][0] > 0:
ans = max(fnt[0] + diam[d][0][0], ans)
p = True
elif d >= fnt[1]:
s = d - fnt[1]
if diam[s][0][0] > 0 and diam[s][0][1] != i:
ans = max(fnt[0] + diam[s][0][0], ans)
p = True
elif diam[s][1][0] > 0:
ans = max(fnt[0] + diam[s][1][0], ans)
p = True
if coin[t][0][0] > 0:
ans = max(fnt[0] + coin[t][0][0], ans)
p = True
if p:
print(ans)
else:
print(-4)
188
def prov(mass, now):
check = True
for i in range(n):
for k in range(m):
if now[i][k] == '>' and mass[i] <= mass[n + k]:
check = False
break
elif now[i][k] == '<' and mass[i] >= mass[n + k]:
check = False
break
elif now[i][k] == '=' and mass[i] != mass[n + k]:
check = False
break
if not check:
break
return check
def prog(mass, n, m):
prov = True
for i in range(1, m):
for k in range(n):
if mass[i][k] < mass[i - 1][k]:
prov = False
break
if not prov:
break
if not prov:
return False
else:
mass_new = []
for i in range(1, m):
mass_n = []
for k in range(n):
mass_n.append(mass[i][k] - mass[i - 1][k])
mass_new.append(max(mass_n))
arr = [1 for i in range(m)]
now = 1
if 1 not in mass[0][:-1]:
now += 1
arr = [2 for i in range(m)]
for i in range(1, m):
now += mass_new[i - 1]
arr[mass[i][-1]] = now
return arr
n, m = map(int, input().split())
if n + m <= 6:
now = []
for i in range(n):
now.append(input())
ppp = True
for i1 in range(n + m):
for i2 in range(n + m):
for i3 in range(n + m):
for i4 in range(n + m):
for i5 in range(n + m):
for i6 in range(n + m):
mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m]
if prov(mass, now) and ppp:
print('Yes')
print(*mass[:n])
print(*mass[n:])
ppp = False
if ppp:
print('No')
else:
mass = [[] for i in range(m)]
mass1 = [[] for i in range(n)]
for i in range(n):
now = input()
for k in range(m):
if now[k] == '<':
mass[k].append(1)
mass1[i].append(-1)
elif now[k] == '=':
mass[k].append(0)
mass1[i].append(0)
else:
mass[k].append(-1)
mass1[i].append(1)
for i in range(m):
mass[i].append(i)
for i in range(n):
mass1[i].append(i)
mass.sort()
mass1.sort()
arr = prog(mass, n, m)
arr1 = prog(mass1, m, n)
if arr == False or arr1 == False:
print('No')
else:
print('Yes')
print(*arr1)
print(*arr)
189
def main():
m, t, r = list(map(int, input().split()))
gh = sorted(map(int, input().split()), reverse=True)
if r > t:
print(-2)
return
l = [False] * 600
for g in gh:
g -= t
for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
l[i] = True
print(sum(l))
def __starting_point():
main()
__starting_point()
190
from bisect import *
n, tc, td = [int(i) for i in input().split()]
fc = []
fd = []
mbc = 0
mbd = 0
for _ in range(n):
b, p, ft = input().split()
b, p = (int(b), int(p))
f = (p, b)
if ft == 'C':
if p <= tc:
fc.append(f)
mbc = max(mbc, b)
elif p <= td:
fd.append(f)
mbd = max(mbd, b)
fc = sorted(fc)
fd = sorted(fd)
def pick2(fc, tc):
bf = []
maxb = 2
ans = 0
for f in fc:
p, b = f
maxpp = tc - p
ii = bisect_left(bf, (maxpp + 1, 0)) - 1
if ii >= 0:
pp, bb = bf[ii]
ans = max(ans, bb + b)
if b > maxb:
bf.append(f)
maxb = b
return ans
ans = mbc + mbd if mbc > 0 and mbd > 0 else 0
ans = max(ans, pick2(fc, tc))
ans = max(ans, pick2(fd, td))
print(ans)
191
import sys
sys.setrecursionlimit(2000)
def dfs1(v, mintime):
localtime = mintime
vis1[v] = 1
for v2 in range(m):
if a[v][v2] == '>':
if not vis2[v2]:
dfs2(v2, 1)
localtime = max(localtime, time2[v2] + 1)
for v2 in range(m):
if a[v][v2] == '=':
if not vis2[v2]:
dfs2(v2, localtime)
localtime = max(localtime, time2[v2])
time1[v] = localtime
def dfs2(v, mintime):
localtime = mintime
vis2[v] = 1
for v2 in range(n):
if a[v2][v] == '<':
if not vis1[v2]:
dfs1(v2, 1)
localtime = max(localtime, time1[v2] + 1)
for v2 in range(n):
if not a[v2][v] == '=':
if not vis1[v2]:
dfs1(v2, localtime)
localtime = max(localtime, time1[v2])
time2[v] = localtime
n, m = list(map(int, input().split()))
a = [input() for i in range(n)]
time1 = [0] * n
time2 = [0] * m
vis1 = [0] * n
vis2 = [0] * m
time = 0
try:
for v in range(n):
if not time1[v]:
dfs1(v, 1)
for v in range(m):
if not time2[v]:
dfs2(v, 1)
correct = True
for v1 in range(n):
for v2 in range(m):
if a[v1][v2] == '=' and time1[v1] != time2[v2]:
correct = False
if a[v1][v2] == '>' and time1[v1] <= time2[v2]:
correct = False
if a[v1][v2] == '<' and time1[v1] >= time2[v2]:
correct = False
if correct:
print('Yes')
print(*time1)
print(*time2)
else:
print('No')
except RecursionError:
print('No')
192
n, m, k = map(int, input().split())
a = [[0 for i in range(m)] for j in range(n)]
inf = [0 for i in range(k + 1)]
stat = [True for i in range(n + 1)]
time = [0 for i in range(n + 1)]
for i in range(n):
a[i] = list(map(int, input().split()))
for i in range(m):
for j in range(1, n + 1 - 1):
if stat[j]:
current_core = inf[a[j - 1][i]]
if current_core == 0 and a[j - 1][i] != 0:
inf[a[j - 1][i]] = j
elif current_core == -1:
stat[j], time[j] = (False, i + 1)
elif a[j - 1][i] != 0:
stat[current_core], time[current_core] = (False, i + 1)
stat[j], time[j] = (False, i + 1)
inf[a[j - 1][i]] = -1
for p in range(len(inf)):
if inf[p] != -1:
inf[p] = 0
for i in range(1, n + 1):
print(time[i])
193
def main():
m, t, r = list(map(int, input().split()))
gh = sorted(map(int, input().split()), reverse=True)
if r > t:
print(-3)
return
l = [False] * 600
for g in gh:
g -= t
for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
l[i] = True
print(sum(l))
def __starting_point():
main()
__starting_point()
194
import sys
def read_data():
n = int(next(sys.stdin))
prices = list(map(int, next(sys.stdin).split()))
vocabulary = []
for i in range(n):
vocabulary.append(next(sys.stdin).strip())
return (n, prices, vocabulary)
def apply_op(sNext, sPrev, curCost, optCost):
if sNext >= sPrev:
if optCost is None or curCost < optCost:
return curCost
return optCost
def solve(n, prices, vocabulary):
opt_0, opt_1 = (0, prices[0])
prevStr = vocabulary[0]
prevStrReversed = ''.join(reversed(prevStr))
for curStr, curPrice in zip(vocabulary[1:], prices[1:]):
curStrReversed = ''.join(reversed(curStr))
n_opt0, n_opt1 = (None, None)
if opt_0 is not None:
n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1)
if opt_1 is not None:
n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1)
opt_0, opt_1 = (n_opt0, n_opt1)
prevStr, prevStrReversed = (curStr, curStrReversed)
if opt_0 is None and opt_1 is None:
break
if opt_0 is None:
return opt_1
if opt_1 is None:
return opt_0
return min(opt_0, opt_1)
def __starting_point():
n, prices, vocabulary = read_data()
sorting_cost = solve(n, prices, vocabulary)
if sorting_cost != None:
print(sorting_cost)
else:
print(--1)
__starting_point()
195
__author__ = 'Lipen'
def main():
n, m, k = map(int, input().split())
data = []
when = [0] * n
blockedcells = set()
blockedcores = set()
for _ in range(n):
data.append(list(map(int, input().split())))
for j in range(m):
operation = []
for _ in range(k):
operation.append([])
for i in range(n):
if i not in blockedcores:
x = data[i][j] - 1
if x >= 0:
if x in blockedcells:
blockedcores.add(i)
when[i] = j + 1
elif len(operation[x - 1]) > 0:
blockedcells.add(x)
for core in operation[x]:
blockedcores.add(core)
when[core] = j + 1
blockedcores.add(i)
when[i] = j + 1
operation[x].append(i)
for i in when:
print(i)
main()
196
import sys
def read_data():
n = int(next(sys.stdin))
prices = list(map(int, next(sys.stdin).split()))
vocabulary = []
for i in range(n):
vocabulary.append(next(sys.stdin).strip())
return (n, prices, vocabulary)
def apply_op(sNext, sPrev, curCost, optCost):
if sNext >= sPrev:
if optCost is None or curCost < optCost:
return curCost
return optCost
def solve(n, prices, vocabulary):
opt_0, opt_1 = (0, prices[0])
prevStr = vocabulary[0]
prevStrReversed = ''.join(reversed(prevStr))
for curStr, curPrice in zip(vocabulary[1:], prices[1:]):
curStrReversed = ''.join(reversed(curStr))
n_opt0, n_opt1 = (None, None)
if opt_0 is not None:
n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1)
if opt_1 is not None:
n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0)
n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1)
opt_0, opt_1 = (n_opt0, n_opt1)
prevStr, prevStrReversed = (curStr, curStrReversed)
if opt_0 is None and opt_1 is not None:
break
if opt_0 is None:
return opt_1
if opt_1 is None:
return opt_0
return min(opt_0, opt_1)
def __starting_point():
n, prices, vocabulary = read_data()
sorting_cost = solve(n, prices, vocabulary)
if sorting_cost != None:
print(sorting_cost)
else:
print(-1)
__starting_point()
197
n, c, d = map(int, input().split())
inf = 10 ** 9
a = [0] * n
sc = []
sd = []
for i in range(n):
b, p, t = input().split()
b = int(b)
p = int(p)
a[i] = [b, p, t]
if t == 'C':
sc.append([p, b])
else:
sd.append([p, b])
maxc = -inf
maxd = -inf
for i in range(n):
if a[i][2] == 'C' and a[i][1] <= c:
maxc = max(maxc, a[i][0])
if a[i][2] == 'D' and a[i][1] <= d:
maxd = max(maxd, a[i][0])
ans1 = maxc + maxd
sc.sort()
scpref = [0] * (len(sc) + 1)
scpref[0] = -inf
for i in range(len(sc)):
scpref[i - 1] = max(scpref[i], sc[i][1])
ans2 = -inf
for i in range(len(sc)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sc[m][0] <= c - sc[i][0]:
l = m
else:
r = m
ans2 = max(ans2, sc[i][1] + scpref[l + 1])
sd.sort()
sdpref = [0] * (len(sd) + 1)
sdpref[0] = -inf
for i in range(len(sd)):
sdpref[i + 1] = max(sdpref[i], sd[i][1])
ans3 = -inf
for i in range(len(sd)):
l = -1
r = i
while l < r - 1:
m = (l + r) // 2
if sd[m][0] <= d - sd[i][0]:
l = m
else:
r = m
ans3 = max(ans3, sd[i][1] + sdpref[l + 1])
ans = max(ans1, ans2, ans3)
print(max(ans, 0))
198
n, c = list(map(int, input().split(' ')))
nums = list(map(int, input().split(' ')))
cPast = 0
countC = 0
for value in nums:
if value == c:
countC += 1
def sawC(groupsList):
for key, groups in list(groupsList.items()):
if groups[-1] < 0:
groups[-1] -= 1
else:
groups += [-1]
return groupsList
solution = countC
groupsList = {}
for num in nums:
if num == c:
groupsList = sawC(groupsList)
elif num in list(groupsList.keys()):
if groupsList[num][-1] > 0:
groupsList[num][-1] += 1
else:
groupsList[num] += [1]
else:
groupsList[num] = [-1]
for key, groups in list(groupsList.items()):
maxDiff = 1
currDiff = 0
newDiff = 0
for group in groups:
currDiff += group
if group > currDiff:
currDiff = group
if currDiff > maxDiff:
maxDiff = currDiff
if maxDiff + countC > solution:
solution = countC + maxDiff
print(solution)
199
n, t, d = map(int, input().split())
coin = [[(0, 0), (0, 0)] for i in range(100001)]
diam = [[(0, 0), (0, 0)] for i in range(100001)]
f = []
for i in range(n):
b, c, q = input().split()
b = int(b)
c = int(c)
if q == 'C':
f.append((b, c, 1))
if coin[c][0][0] < b:
coin[c][1] = coin[c][0]
coin[c][0] = (b, i)
elif coin[c][1][0] < b:
coin[c][1] = (b, i)
else:
f.append((b, c, 2))
if diam[c][0][0] < b:
diam[c][1] = diam[c][0]
diam[c][0] = (b, i)
elif diam[c][1][0] < b:
diam[c][1] = (b, i)
for i in range(2, 100001):
if coin[i][0][0] < coin[i - 1][0][0]:
coin[i][1] = max(coin[i][0], coin[i - 1][1])
coin[i][0] = coin[i - 1][0]
else:
coin[i][1] = max(coin[i - 1][1], coin[i][1])
if diam[i][0][0] < diam[i - 1][0][0]:
diam[i][1] = max(diam[i][0], diam[i - 1][1])
diam[i][0] = diam[i - 1][0]
else:
diam[i][1] = max(diam[i - 1][1], diam[i][1])
p = False
ans = 0
for i in range(n):
fnt = f[i]
if fnt[2] == 1:
if t >= fnt[1]:
s = t - fnt[1]
if coin[s][0][0] > 0 and coin[s][0][1] != i:
ans = max(fnt[0] + coin[s][0][0], ans)
p = True
elif coin[s][1][0] > 0:
ans = max(fnt[0] + coin[s][1][0], ans)
p = True
if diam[d][0][0] > 0:
ans = max(fnt[0] + diam[d][0][0], ans)
p = True
elif d >= fnt[1]:
s = d - fnt[1]
if diam[s][0][0] > 0 and diam[s][0][1] != i:
ans = max(fnt[0] + diam[s][0][0], ans)
p = True
elif diam[s][1][0] > 0:
ans = max(fnt[0] + diam[s][1][0], ans)
p = True
if coin[t][0][0] > 0:
ans = max(fnt[0] + coin[t][0][0], ans)
p = True
if p:
print(ans)
else:
print(0)
200
def main():
m, t, r = read()
w = read()
if t < r:
return -1
covers = [0] * 1000
ct = 0
for i in w:
x = covers[i]
for j in range(r - x):
for k in range(t):
covers[i - j + k] += 3
ct += 1
return ct
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())
Final Query
Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis.