Download misc/misc-0188.jsonl from winapiadmin/tinycodinglm: direct link, hf CLI and curl.
- Browser
- Download file 9.72 kB
-
https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/7fe6853bcbe998c7aec3aa0f7086f584cc091586/misc/misc-0188.jsonl
- Command line
-
hf download hf://datasets/winapiadmin/tinycodinglm@7fe6853bcbe998c7aec3aa0f7086f584cc091586/misc/misc-0188.jsonl
-
curl -L -o misc-0188.jsonl https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/7fe6853bcbe998c7aec3aa0f7086f584cc091586/misc/misc-0188.jsonl
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing...
You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing the final solution.
Note: The time limit for this problem is 5s, 2.5 times the default. The memory limit for this problem is 512MB, twice the default.
Bessie has recently taken an interest in magic and needs to collect mana for a
very important spell. Bessie has $N$ ($1\le N\le 18$) mana pools, the $i$th of
which accumulates $m_i$ mana per second ($1\le m_i\le 10^8$). The pools are
linked by a collection of $M$ ($0\le M\le N(N-1)$) directed edges
$(a_i,b_i,t_i)$, meaning that she can travel from $a_i$ to $b_i$ in $t_i$
seconds ($1\le a_i, b_i\le N$, $a_i\neq b_i$, $1\le t_i\le 10^9$). Whenever
Bessie is present at a pool, she can collect all the mana stored at that
location, emptying it. At time $0$, all mana pools are empty, and Bessie can
select any pool to start at.
Answer $Q$ ($1\le Q\le 2\cdot 10^5$) queries, each specified by two integers $s$
and $e$ ($1\le s\le 10^9$, $1\le e\le N$). For each query, determine the maximum
amount of mana Bessie can collect in $s$ seconds if she must be at mana pool $e$
at the end of the $s$th second.
INPUT FORMAT (input arrives from the terminal / stdin):
First line contains $N$ and $M$.
Next line contains $m_1,m_2,\dots, m_N$.
Next $M$ lines contain $a_i,b_i,t_i$. No ordered pair $(a_i,b_i)$ appears more
than once in the input.
Next line contains $Q$.
Next $Q$ lines contain two integers $s$ and $e$.
OUTPUT FORMAT (print output to the terminal / stdout):
$Q$ lines, one for each query.
SAMPLE INPUT:
2 1
1 10
1 2 10
4
5 1
5 2
100 1
100 2
SAMPLE OUTPUT:
5
50
100
1090
First query: Bessie takes 5 mana from pool 1 after 5 seconds.
Second query: Bessie takes 50 mana from pool 2 after 5 seconds.
Third query: Bessie takes 100 mana from pool 1 after 100 seconds.
Fourth query: Bessie takes 90 mana from pool 1 after 90 seconds and 1000 mana
from pool 2 after 100 seconds.
SAMPLE INPUT:
4 8
50000000 100000000 20000000 70000000
1 2 20
2 1 50
2 3 90
1 3 40
3 1 10
4 1 25
1 4 5
4 3 70
3
8 3
1000000000 1
500000 4
SAMPLE OUTPUT:
160000000
239999988050000000
119992550000000
An example where Bessie is able to collect much larger amounts of mana.
SCORING:
Inputs 3-4: $N\le 10, Q\le 100$Inputs 5-9: $N\le 10$Inputs 10-14: $Q\le 100$Inputs 15-17: $N = 16$Inputs 18-20: $N = 17$Inputs 21-24: No additional constraints.
Problem credits: Benjamin Qi
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
N = int(next(it))
M = int(next(it))
m = [int(next(it)) for _ in range(N)]
INF = 10**30
dist = [[INF]*N for _ in range(N)]
for i in range(N):
dist[i][i] = 0
for _ in range(M):
a = int(next(it))-1
b = int(next(it))-1
t = int(next(it))
if t < dist[a][b]:
dist[a][b] = t
# Floyd-Warshall
for k in range(N):
dk = dist[k]
for i in range(N):
di = dist[i]
dik = di[k]
if dik == INF:
continue
for j in range(N):
nd = dik + dk[j]
if nd < di[j]:
di[j] = nd
Q = int(next(it))
queries = []
for qi in range(Q):
s = int(next(it))
e = int(next(it))-1
queries.append((s, e, qi))
FULL = 1 << N
# W for mask
Wmask = [0]*FULL
for mask in range(1, FULL):
lb = mask & -mask
i = lb.bit_length()-1
Wmask[mask] = Wmask[mask ^ lb] + m[i]
# dp[mask][last] = (bestD, bestval, bestvalD, bestvalval?) We'll store two candidates:
# best_minD: (D, val); best_maxval: (D, val)
# Use arrays of size N for each mask? Memory: FULL*N = 262144*18 = 4.7M entries; each a tuple heavy but okay maybe.
# We'll store dict of last -> (minD,minval,maxD,maxval) only for reachable.
dp = [None]*FULL
for i in range(N):
dp[1<<i] = {i: (0, 0, 0, 0)} # (minD, minval, maxD, maxval) init minD=0 val=0; max same
# iterate masks
for mask in range(1, FULL):
cur = dp[mask]
if cur is None:
continue
W = Wmask[mask]
for last, (minD, minval, maxD, maxval) in list(cur.items()):
# try extend to nxt
for nxt in range(N):
if mask & (1<<nxt):
continue
d = dist[last][nxt]
if d >= INF:
continue
nmask = mask | (1<<nxt)
nD = minD + d # for minD path
nval_minpath = minval - d * W
# for maxval path
nD2 = maxD + d
nval_maxpath = maxval - d * W
# update dp[nmask][nxt]
if dp[nmask] is None:
dp[nmask] = {}
entry = dp[nmask].get(nxt)
if entry is None:
dp[nmask][nxt] = (nD, nval_minpath, nD2, nval_maxpath)
else:
o_minD, o_minval, o_maxD, o_maxval = entry
# keep min D
if nD < o_minD or (nD == o_minD and nval_minpath > o_minval):
o_minD, o_minval = nD, nval_minpath
# keep max val
if nval_maxpath > o_maxval or (nval_maxpath == o_maxval and nD2 < o_maxD):
o_maxD, o_maxval = nD2, nval_maxpath
dp[nmask][nxt] = (o_minD, o_minval, o_maxD, o_maxval)
# Build per-e candidate lines
# For each e, list of (D, W, val)
cands_by_e = [[] for _ in range(N)]
for mask in range(1, FULL):
cur = dp[mask]
if cur is None:
continue
W = Wmask[mask]
for last, (minD, minval, maxD, maxval) in cur.items():
cands_by_e[last].append((minD, W, minval))
if (maxD, maxval) != (minD, minval):
cands_by_e[last].append((maxD, W, maxval))
# Li Chao tree over compressed s values (global)
xs = sorted(set(s for s,_,_ in queries))
# map s to index
# We'll process per e
ans = [0]*Q
for e in range(N):
qs = [(s, qi) for (s, ee, qi) in queries if ee == e]
if not qs:
continue
qs.sort()
cands = sorted(cands_by_e[e], key=lambda x: x[0])
# Li Chao over xs_e (unique s for this e) but use global xs indices? We'll use local xs_e
xs_e = sorted(set(s for s,_ in qs))
# Li Chao tree arrays
size = 1
while size < len(xs_e):
size <<= 1
# store lines as (m,b) or None; use arrays
tree_m = [0]*(2*size)
tree_b = [None]*(2*size) # None means no line
def eval_line(idx, x):
m = tree_m[idx]; b = tree_b[idx]
if b is None:
return None
return m*x + b
def update(node, l, r, m_new, b_new):
if tree_b[node] is None:
tree_m[node] = m_new
tree_b[node] = b_new
return
mid = (l+r)//2
xm = xs_e[mid]
cur_val = tree_m[node]*xm + tree_b[node]
new_val = m_new*xm + b_new
if new_val > cur_val:
# swap
tree_m[node], m_new = m_new, tree_m[node]
tree_b[node], b_new = b_new, tree_b[node]
if l == r:
return
# decide side
xl = xs_e[l]
# compare at left endpoint
if m_new*xl + b_new > tree_m[node]*xl + tree_b[node]:
update(node*2, l, mid, m_new, b_new)
else:
update(node*2+1, mid+1, r, m_new, b_new)
def query(node, l, r, idx, x):
res = None
if tree_b[node] is not None:
res = tree_m[node]*x + tree_b[node]
if l == r:
return res
mid = (l+r)//2
if idx <= mid:
q = query(node*2, l, mid, idx, x)
else:
q = query(node*2+1, mid+1, r, idx, x)
if q is None:
return res
if res is None or q > res:
return q
return res
# map s to index
import bisect
ptr = 0
for s, qi in qs:
while ptr < len(cands) and cands[ptr][0] <= s:
D, W, val = cands[ptr]
update(1, 0, size-1, W, val)
ptr += 1
idx = bisect.bisect_left(xs_e, s)
# idx should valid
if idx >= len(xs_e):
idx = len(xs_e)-1
qval = query(1, 0, size-1, idx, s)
if qval is None:
# fallback: at least pool e alone? should have D=0 candidate
qval = m[e]*s
ans[qi] = qval
# Output in original order
out = []
for qi in range(Q):
out.append(str(ans[qi]))
sys.stdout.write("\n".join(out) + ("\n" if out else ""))if name == "main":
main()