-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb10946
commit 543bfa7
Showing
11 changed files
with
1,148 additions
and
572 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import sys; input = sys.stdin.readline; from array import * | ||
n = int(input()); s = input().strip(); m = [input().strip() for _ in range(n)]; k = {'<': (0, -1), '>': (0, 1), '^': (-1, 0), 'v': (1, 0)}; s = [k[x] for x in s]; z = 0; w = array('i', ii:=[-1]*(n**3)); v = [] | ||
for t in range(n*n): | ||
if m[t//n][t%n] == 'R': break | ||
while True: | ||
r = t//n; c = t%n; x = z%len(s); tt = t*n+x | ||
if w[tt] != -1: break | ||
w[tt] = z | ||
if m[r+s[x][0]][c+s[x][1]] != '#': v.append((z, s[x])); t = (r+s[x][0])*n+c+s[x][1] | ||
z += 1 | ||
v2 = [x[1] for x in v if x[0] >= w[tt]] | ||
for i in range(1, len(v2)+1): | ||
if len(v2)%i: continue | ||
ok = 1 | ||
for j in range(len(v2)//i): | ||
for k in range(i): | ||
if v2[k] != v2[i*j+k]: ok = 0; break | ||
if not ok: break | ||
if ok: print(i), exit(0) | ||
print(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
B = 38; N = 5*B+2; P = print | ||
P(N); P('vv<<<^^^>>'+'v^'*(N//2-5)); P('#'*N); P('#'+'#.###'*B+'#'); P('##'+'....#'*B) | ||
for _ in range(N//2-4): P('#..#.'*B+'##'); P('##.#.'*B+'##') | ||
P('#'+'..#..'*B+'#'); P('##.'+'#'*(N-6)+'.##'); P('#'+'.'*(N-4)+'R##'); P('##'+'#.'*(N//2-2)+'##'); P('#'*N) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
def make_sa(s): | ||
n = len(s) | ||
sa = list(range(n)) | ||
ra = [ord(s[i]) for i in range(n)] | ||
k, maxi = 1, max(300, n) | ||
while k < n: | ||
for kk in [k, 0]: | ||
c = [0]*maxi | ||
for i in range(n): c[ra[i+kk] if i+kk<n else 0] += 1 | ||
ss, temp = 0, [0]*n | ||
for i in range(maxi): t = c[i]; c[i] = ss; ss += t | ||
for i in range(n): | ||
idx = ra[sa[i]+kk] if sa[i]+kk < n else 0 | ||
temp[c[idx]] = sa[i] | ||
c[idx] += 1 | ||
sa = temp | ||
temp, r = [0]*n, 0 | ||
temp[sa[0]] = r | ||
for i in range(1, n): | ||
r += ra[sa[i]] != ra[sa[i-1]] or ra[sa[i]+k] != ra[sa[i-1]+k] | ||
temp[sa[i]] = r | ||
ra = temp | ||
if ra[sa[n-1]] == n-1: break | ||
k *= 2 | ||
return sa[1:] | ||
|
||
def make_lcp(s, sa): | ||
n = len(s) | ||
ra = [0]*n | ||
for i in range(n): ra[sa[i]] = i | ||
k = 0 | ||
lcp = [0]*(n-1) | ||
for i in range(n): | ||
if ra[i] == n-1: k = 0; continue | ||
j = sa[ra[i]+1] | ||
while i + k < n and j + k < n and s[i+k] == s[j+k]: k += 1 | ||
lcp[ra[i]] = k | ||
if k: k -= 1 | ||
return [0]+lcp | ||
|
||
# https://stackoverflow.com/questions/31106459/how-to-adapt-fenwick-tree-to-answer-range-minimum-queries/34602284#34602284 | ||
def update(a, i, x): | ||
a[i] = x | ||
while i > 1: i //= 2; a[i] = min(a[2*i], a[2*i+1]) | ||
def rmq(a, i, j): | ||
x = 10**9 | ||
while i < j: | ||
if i%2 == 0: i //= 2 | ||
else: x = min(x, a[i]); i = i//2 + 1 | ||
if j%2 == 0: j //= 2 | ||
else: x = min(x, a[j-1]); j //= 2 | ||
return x | ||
|
||
import sys; input = sys.stdin.readline; from array import * | ||
s = input().strip(); n = len(s); sa = make_sa(s+'\0'); lcp = make_lcp(s, sa); r = array('i', [0]*2*n); m = array('i', [0]*n) | ||
for i in range(n): m[sa[i]] = i; update(r, i+n, lcp[i]) | ||
for _ in range(int(input())): a, b = map(int, input().split()); print(rmq(r, min(m[a], m[b])+n+1, max(m[a], m[b])+n+1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from math import *; INF = 10**9 | ||
|
||
def hungarian(mat): | ||
if len(mat) > len(mat[0]): mat = [*map(list, zip(*mat))] | ||
n = len(mat)+1; m = len(mat[0])+1; ans = 0; ii = [0]*max(m, n) | ||
mtc = [*ii]; u = [*ii]; v = [*ii]; w = [*ii]; c = 0 | ||
mat = [[*ii], *([0]+r for r in mat)] | ||
for i in range(1, n): | ||
mtc[0] = i; mi = [INF]*m; vis = [*ii] | ||
while 1: | ||
vis[c] = 1; d = INF; c2 = 0 | ||
for j in range(1, m): | ||
if vis[j]: continue | ||
if (cur:=mat[mtc[c]][j]-u[mtc[c]]-v[j]) < mi[j]: mi[j] = cur; w[j] = c | ||
if mi[j] < d: d = mi[j]; c2 = j | ||
for j in range(m): | ||
if vis[j]: u[mtc[j]] += d; v[j] -= d | ||
else: mi[j] -= d | ||
if mtc[(c:=c2)] == 0: break | ||
while 1: | ||
mtc[c] = mtc[w[c]] | ||
if (c:=w[c]) == 0: break | ||
for i in range(1, m): | ||
if mtc[i]: ans += mat[mtc[i]][i] | ||
return 10**(2-ans) if ans < 5000 else 0 | ||
|
||
n = int(input()); p = [[*map(lambda x: 2-log10(int(x)) if int(x) else INF, input().split())] for _ in range(n)] | ||
print(hungarian(p)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import sys; input = sys.stdin.readline | ||
p, q, n = map(int, input().split()); r = [[] for _ in range(p)] | ||
for _ in range(n): a, b = map(int, input().split()); r[a-1].append(b-1) | ||
for i in r: i.sort() | ||
sr = 0 | ||
while True: | ||
cr = sr | ||
while cr < p and not r[cr]: cr += 1 | ||
if cr == p: break | ||
cr2 = cr+1 | ||
while cr2 < p and not r[cr2]: cr2 += 1 | ||
cs = cr if cr2 < p else p-1; x = 0 | ||
for i in r[cr][:-1]: print(sr+1, x+1, cs+1, i+1); x = i+1 | ||
if r[cr]: print(sr+1, x+1, cs+1, q) | ||
sr = cr+1 | ||
if sr == p: break | ||
print(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import sys; input = sys.stdin.readline; from array import * | ||
m, u, d = map(float, input().split()); h = []; ss = []; cc = []; u = round(u*60); m = round(m*100); d = round(d) | ||
for _ in range(d): b, s, z, c = input().split(); h.append(b); ss.append(round(int(s)*eval(z)*60)); cc.append(round(float(c)*100)) | ||
q = [(1201*m+u, -1) for i in range(d) if cc[i] <= m and ss[i] <= u]; p = array('b', [-1]*1201*1001) | ||
for mu, pp in q: | ||
if p[mu] != -1: continue | ||
mm = mu//1201; uu = mu%1201; p[mu] = pp | ||
if mm == uu == 0: | ||
x = [0]*d; y = 0 | ||
while p[y] != -1: x[p[y]] += 1; y += 1201*cc[p[y]]+ss[p[y]] | ||
for j in range(d): | ||
if x[j]: print(h[j], x[j]) | ||
exit(0) | ||
for j in range(d): | ||
if cc[j] <= mm and ss[j] <= uu: q.append((1201*(mm-cc[j])+uu-ss[j], j)) | ||
print('IMPOSSIBLE') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
R, C = map(int, input().split()); print(' _'*C) | ||
if R == 1: print('|'+'_ '*(C-1)+'_|'), exit(0) | ||
if C == 1: print('| |\n'*(R-1)+'|_|'), exit(0) | ||
# edge case if R is odd and C is even, need to add bar in middle | ||
for i in range(R-1): print('|'+'_ '*(C//2-1)+['_ ', [' |', ' '][i==(R-1)//2]][R%2 and C%2==0]+' '+' _'*(C-C//2-1)+'|') | ||
print('|'+'_ '*(C//2-1)+'_'+' |'[R%2 and C%2==0]+'_ '*(C-C//2-1)+'_|') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import sys; input = sys.stdin.readline | ||
n, x = map(int, input().split()); g = [[] for _ in range(n)]; s = [(x, -1)]; m = [] | ||
for _ in range(n-1): a, b = map(int, input().split()); g[a].append(b); g[b].append(a) | ||
while s: | ||
u, p = s.pop() | ||
for v in g[u]: | ||
if v != p: s.append((v, u)) | ||
if len(g[u]) == 1: m.append(u) | ||
print((len(m)+1)//2); y = len(m)//2 | ||
for i in range((len(m)+1)//2): print(m[i], m[(i+y)%len(m)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sys; input = sys.stdin.readline | ||
t = n = int(input()); k = sorted(int(input()) for _ in range(1<<n)); c = {}; a = [] | ||
if 0 not in k: print('impossible'), exit(0) | ||
for i in k: | ||
if i not in c: c[i] = 0 | ||
c[i] += 1 | ||
c[0] -= 1 | ||
for i in range(1, 1<<n): | ||
m = k[i] | ||
if c[m]: | ||
c[m] -= 1; a.append(m); z = 1; t -= 1; u = {} | ||
for j in range(i+1, 1<<n): | ||
if c[k[j]]: | ||
if k[j] not in u: u[k[j]] = 0 | ||
u[k[j]] += 1; c[k[j]] -= 1 | ||
if k[j]+m in c and c[k[j]+m]: c[k[j]+m] -= 1; z += 1 | ||
if z != 1<<t: print('impossible'), exit(0) | ||
for i in u: c[i] += u[i] | ||
print(*a) |