-
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
08a15ab
commit 43e46c4
Showing
10 changed files
with
827 additions
and
341 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,26 @@ | ||
N, K = map(int, input().split()); A = [*map(int, input().split())]; f = lambda a: sum(a[i] > a[j] for i in range(len(a)) for j in range(i+1, len(a))) | ||
# if K == N, you can either reverse it or not | ||
if K == N: | ||
if (x:=f(A)) <= (y:=f(A[::-1])): print(x, 0) | ||
else: print(y, 1, '1'*N) | ||
exit(0) | ||
# otherwise, we can always sort it in (N-K)+2(K+1) = N+K+2 moves | ||
# take K-1 smallest and the i-th smallest where i = N-1, N-2, ..., K+1 | ||
# -> this will ensure that the N-(K+1) largest [K+1..N-1] will be in a relative sorted order | ||
# then take K+1 smallest minus the i-th smallest where i = 0, 1, 2, ..., K + flip these | ||
# -> this will clump [K+1..N-1] together while ordering [0..K] on the left side like it's insertion sort | ||
B = sorted((e,i) for i,e in enumerate(A)); M = []; F = '1'*K+'0'*(N-K) | ||
for i in range(N): A[B[i][1]] = i | ||
for i in range(N-1, K, -1): | ||
L, R, T = [], [], '' | ||
for j in A: | ||
if j < K-1 or j == i: L.append(j); T += '1' | ||
else: R.append(j); T += '0' | ||
A = L[::-1]+R; M.append(T) | ||
for i in range(K+1): | ||
L, R, T = [], [], '' | ||
for j in A: | ||
if j <= K and j != i: L.append(j); T += '1' | ||
else: R.append(j); T += '0' | ||
A = L+R; M.append(T); M.append(F) | ||
print(0, len(M), *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,71 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class DrillSergeant { | ||
public static void main(String[] args) throws IOException { | ||
Reader sc = new Reader(); | ||
PrintWriter pw = new PrintWriter(System.out); | ||
int N = sc.nextInt(), M = sc.nextInt(), tmp, Q; | ||
long Z = 0, X = 1_000_000_001; | ||
long[] P = {3, 32, 323, 3233}; | ||
Set<Long> H = new HashSet<Long>(); | ||
TreeSet<Long> T = new TreeSet<Long>(); | ||
while (M-- > 0) { | ||
int a = sc.nextInt(), b = sc.nextInt(); | ||
if (a > b) { tmp = b; b = a; a = tmp; } | ||
H.add(a*X+b); | ||
} | ||
Q = sc.nextInt(); | ||
while (Q-- > 0) { | ||
int c = sc.nextInt(); | ||
long x = sc.nextInt(), z = 0; | ||
Long px, sx, ppx, ssx; | ||
if (c == 1) { T.add(x); px = T.lower(x); sx = T.higher(x); } | ||
else { px = T.lower(x); sx = T.higher(x); T.remove(x); } | ||
if (px == null && sx == null) z += 3; | ||
else if (px != null && sx == null) { ppx = T.lower(px); z += H.contains(px*X+x) ? ((ppx == null || !H.contains(ppx*X+px)) ? 352 : 3233) : 3; } | ||
else if (px == null && sx != null) { ssx = T.higher(sx); z += H.contains(x*X+sx) ? ((ssx == null || !H.contains(sx*X+ssx)) ? 352 : 3233) : 3; } | ||
else { | ||
ppx = T.lower(px); ssx = T.higher(sx); | ||
z -= P[(ppx != null && H.contains(ppx*X+px) ? 2 : 0)+(H.contains(px*X+sx) ? 1 : 0)]; | ||
z -= P[(H.contains(px*X+sx) ? 2 : 0)+(ssx != null && H.contains(sx*X+ssx) ? 1 : 0)]; | ||
z += P[(ppx != null && H.contains(ppx*X+px) ? 2 : 0)+(H.contains(px*X+x) ? 1 : 0)]; | ||
z += P[(H.contains(x*X+sx) ? 2 : 0)+(ssx != null && H.contains(sx*X+ssx) ? 1 : 0)]; | ||
z += P[(H.contains(px*X+x) ? 2 : 0)+(H.contains(x*X+sx) ? 1 : 0)]; | ||
} | ||
Z += c == 1 ? z : -z; | ||
pw.println(Z); | ||
} | ||
pw.flush(); | ||
} | ||
static class Reader { | ||
final private int BUFFER_SIZE = 1 << 16; | ||
private DataInputStream din; | ||
private byte[] buffer; | ||
private int bufferPointer, bytesRead; | ||
public Reader() { | ||
din = new DataInputStream(System.in); | ||
buffer = new byte[BUFFER_SIZE]; | ||
bufferPointer = bytesRead = 0; | ||
} | ||
public int nextInt() throws IOException { | ||
int ret = 0; | ||
byte c = read(); | ||
while (c <= ' ') c = read(); | ||
boolean neg = (c == '-'); | ||
if (neg) c = read(); | ||
do { | ||
ret = ret * 10 + c - '0'; | ||
} while ((c = read()) >= '0' && c <= '9'); | ||
return neg ? -ret : ret; | ||
} | ||
private void fillBuffer() throws IOException { | ||
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); | ||
if (bytesRead == -1) buffer[0] = -1; | ||
} | ||
private byte read() throws IOException { | ||
if (bufferPointer == bytesRead) fillBuffer(); | ||
return buffer[bufferPointer++]; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Greedy Increasing Subsequences/greedyincreasingsubsequences.py
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,11 @@ | ||
import sys; input = sys.stdin.readline | ||
N = int(input()); R = [] | ||
for i in map(int, input().split()): | ||
lo, hi = 0, len(R) | ||
while lo<hi: | ||
if R[-1-(mi:=(lo+hi)//2)][-1] < i: lo = mi+1 | ||
else: hi = mi | ||
if lo == 0: R.append([i]) | ||
else: R[-lo].append(i) | ||
print(len(R)) | ||
for i in R: print(*i) |
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,75 @@ | ||
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**6 | ||
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 | ||
while (n:=int(input())): | ||
w = [input().strip() for _ in range(n)] | ||
if n == 1: print(w[0]); continue | ||
s = ''; m = [] | ||
for i in range(n): s += w[i]+chr(i+1); m.extend([i]*(len(w[i])+1)) | ||
N = len(s); sa = make_sa(s+'\0'); lcp = make_lcp(s, sa); r = [m[sa[i]] for i in range(N)]; cnt = [0]*n | ||
# whenever it includes {target} substrings, get min LCP among all included LCPs | ||
target = n//2+1; lo = 0; inc = 1; cnt[r[0]] = 1; a = [10**6]*2*N; best = (-1, []); has_hi = 0 | ||
for hi in range(1, N): | ||
cnt[r[hi]] += 1; inc += cnt[r[hi]] == 1; update(a, hi+N, lcp[hi]) | ||
while inc == target: # just need one substring for a fixed end | ||
b = rmq(a, lo+N, hi+1+N) | ||
if b > best[0]: best = (b, [lo]); has_hi = 1 | ||
elif b == best[0] and not has_hi: best[1].append(lo); has_hi = 1 | ||
elif b < best[0]: has_hi = 0 | ||
cnt[r[lo]] -= 1; inc -= cnt[r[lo]] == 0 | ||
lo += 1; update(a, lo+N, 10**6) | ||
if best[0]: | ||
for i in best[1]: print(s[sa[i]:sa[i]+best[0]]) | ||
else: print('?') | ||
print() |
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,44 @@ | ||
def get(l, r, interactive): | ||
global Z; Z[0] -= 1 | ||
assert Z[0] >= 0, interactive | ||
if interactive == True: print('ASK', l, r); return map(lambda x: x=='yes', input().strip().split()) | ||
else: S, A = interactive; return [l<=S<=r, l<=A<=r] | ||
|
||
def solve(L, R, interactive=True): | ||
global Z; Z = [11] | ||
while True: | ||
if R-L > 3: | ||
q1, q2, q3 = (L+round(i*(R-L)/4) for i in range(1, 4)); v1, v2 = get(L, q2, interactive); w1, w2 = get(q1, q3, interactive) | ||
if 2*v1+w1 == 2*v2+w2: | ||
L, R = (q3+1, q2+1, L, q1)[2*v1+w1], (R, q3, q1-1, q2)[2*v1+w1] | ||
if L == R: return (L, L) | ||
else: ls, rs, la, ra = (q3+1, q2+1, L, q1)[2*v1+w1], (R, q3, q1-1, q2)[2*v1+w1], (q3+1, q2+1, L, q1)[2*v2+w2], (R, q3, q1-1, q2)[2*v2+w2]; break | ||
else: | ||
m = (L+R)//2; v1, v2 = get(L, m, interactive) | ||
if v1 and v2: R = m | ||
elif not v1 and not v2: L = m+1 | ||
else: ls, rs = (L, m) if v1 else (m+1, R); la, ra = (L, m) if v2 else (m+1, R); break | ||
if L == R: return (L, L) | ||
while rs > ls or ra > la: | ||
ms = (ls+rs)//2; ma = (la+ra)//2 | ||
if ms < ma: | ||
if rs-ls < 2: ms += 1 | ||
v1, v2 = get(ms, ma, interactive) | ||
if v1: ls = ms | ||
else: rs = ms-1 | ||
if v2: ra = ma | ||
else: la = ma+1 | ||
else: | ||
if ra-la < 2: ma += 1 | ||
v1, v2 = get(ma, ms, interactive) | ||
if v1: rs = ms | ||
else: ls = ms+1 | ||
if v2: la = ma | ||
else: ra = ma-1 | ||
return (ls, la) | ||
|
||
print('GUESS', *solve(1, 500, True)), exit(0) | ||
for s in range(1, 501): | ||
for a in range(1, 501): | ||
assert (u:=solve(1, 500, (s, a))) == (s, a), (u, s, a) | ||
print('done', s, a); print() |
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,32 @@ | ||
from math import * | ||
|
||
def intersect(s1, s2): | ||
(p1, p2), (p3, p4) = s1, s2 | ||
(x1, y1), (x2, y2), (x3, y3), (x4, y4) = p1, p2, p3, p4 | ||
a, b, c = y2-y1, x1-x2, (y2-y1)*x1 - (x2-x1)*y1 | ||
d, e, f = y4-y3, x3-x4, (y4-y3)*x3 - (x4-x3)*y3 | ||
if a == b == 0: return ((x1, y1) if (x1, y1) == (x3, y3) else None) if d == e == 0 else ((x1, y1) if d*x1 + e*y1 == f and min(x3, x4) <= x1 <= max(x3, x4) and min(y3, y4) <= y1 <= max(y3, y4) else None) | ||
elif d == e == 0: return (x3, y3) if a*x3 + b*y3 == c and min(x1, x2) <= x3 <= max(x1, x2) and min(y1, y2) <= y3 <= max(y1, y2) else None | ||
else: | ||
det = b*d-a*e | ||
if det: | ||
x, y = (b*f-c*e)/det, (c*d-a*f)/det | ||
return (x, y) if min(x1, x2) <= x <= max(x1, x2) and min(y1, y2) <= y <= max(y1, y2) and min(x3, x4) <= x <= max(x3, x4) and min(y3, y4) <= y <= max(y3, y4) else None | ||
else: | ||
if a*f != c*d or b*f != c*e: return None | ||
else: | ||
p, q = min((x1, y1), (x2, y2)), max((x1, y1), (x2, y2)) | ||
r, s = min((x3, y3), (x4, y4)), max((x3, y3), (x4, y4)) | ||
if (p, q) == (r, s): i1, i2 = p, q | ||
elif p <= r <= q: i1, i2 = r, min(q, s) | ||
elif r <= p <= s: i1, i2 = p, min(s, q) | ||
else: i1 = i2 = None | ||
if i1 == i2 and i1 != None: return i1 | ||
elif i1: return None | ||
else: return None | ||
|
||
x1, y1, x2, y2, x3, y3, x4, y4 = map(int, input().split()); M = 10**8 | ||
dx1, dy1, dx2, dy2 = x2-x1, y2-y1, x4-x3, y4-y3 | ||
z = intersect(((x1, y1), (x1+M*dx1, y1+M*dy1)), ((x3, y3), (x3+M*dx2, y3+M*dy2))) | ||
if z: print(floor(z[0]), floor(z[1])) | ||
else: 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,20 @@ | ||
import sys; input = sys.stdin.readline; from array import * | ||
N = 200001; n, x, t = map(int, input().split()); INF = 2*N; h = array('i', map(int, input().split())); d = array('i', map(int, input().split())); dp = array('i', [INF]*n); x -= 1; dp[x] = 0; T = array('i', [INF]*2*N) | ||
|
||
# range update, point query | ||
def update(l, r, x): | ||
while l <= r: | ||
if l&1: T[l] = min(T[l], x) | ||
if r^1&1: T[r] = min(T[r], x) | ||
l = (l+1)>>1; r = (r-1)>>1 | ||
def query(x): | ||
z = INF | ||
while x >= 1: z = min(z, T[x]); x >>= 1 | ||
return z | ||
|
||
update(N, h[x]+d[x]+N, 0) | ||
for i in sorted(range(n), key=lambda x: h[x]): | ||
if h[i] >= h[x]: | ||
if i != x: dp[i] = min(dp[i], query(h[i]+N)+1) | ||
update(N, h[i]+d[i]+N, dp[i]) | ||
print(dp[0]*t if dp[0] != INF else -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,17 @@ | ||
import sys; input = sys.stdin.readline; from math import * | ||
R = {'Emerald': 0, '@': 1}; E = []; INF = float('inf') | ||
for _ in range(int(input())): | ||
for _ in range(int((input()))): | ||
p, a, q, b = input().strip().split() | ||
if a not in R: R[a] = len(R) | ||
if b not in R: R[b] = len(R) | ||
E.append((R[a], R[b], log(int(p))-log(int(q)))) | ||
D = [INF]*len(R); D[1] = 0 | ||
for i in range(2, len(R)): E.append((1, i, 0)) | ||
for _ in range(len(R)-1): | ||
for a, b, w in E: D[b] = min(D[b], D[a]+w) | ||
Z = D[0] | ||
for _ in range(len(R)-1): # could've checked more efficiently like /xyzzy but this works anyways' | ||
for a, b, w in E: D[b] = min(D[b], D[a]+w) | ||
if D[0] != Z: print('yes'), exit(0) | ||
print('no') |