File size: 9,720 Bytes
d517dc9 | 1 2 3 4 5 | {"type": "session", "harness": "opencode", "id": "misc-0188", "name": "Competitive Programming"}
{"type": "message", "message": {"role": "system", "content": "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."}}
{"type": "message", "message": {"role": "user", "content": "**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.**\n\nBessie has recently taken an interest in magic and needs to collect mana for a\nvery important spell. Bessie has $N$ ($1\\le N\\le 18$) mana pools, the $i$th of\nwhich accumulates $m_i$ mana per second ($1\\le m_i\\le 10^8$). The pools are\nlinked by a collection of $M$ ($0\\le M\\le N(N-1)$) directed edges\n$(a_i,b_i,t_i)$, meaning that she can travel from $a_i$ to $b_i$ in $t_i$\nseconds ($1\\le a_i, b_i\\le N$, $a_i\\neq b_i$, $1\\le t_i\\le 10^9$). Whenever\nBessie is present at a pool, she can collect all the mana stored at that\nlocation, emptying it. At time $0$, all mana pools are empty, and Bessie can\nselect any pool to start at.\n\nAnswer $Q$ ($1\\le Q\\le 2\\cdot 10^5$) queries, each specified by two integers $s$\nand $e$ ($1\\le s\\le 10^9$, $1\\le e\\le N$). For each query, determine the maximum\namount of mana Bessie can collect in $s$ seconds if she must be at mana pool $e$\nat the end of the $s$th second.\n\nINPUT FORMAT (input arrives from the terminal / stdin):\nFirst line contains $N$ and $M$.\n\nNext line contains $m_1,m_2,\\dots, m_N$.\n\nNext $M$ lines contain $a_i,b_i,t_i$. No ordered pair $(a_i,b_i)$ appears more\nthan once in the input.\n\nNext line contains $Q$.\n\nNext $Q$ lines contain two integers $s$ and $e$.\n\nOUTPUT FORMAT (print output to the terminal / stdout):\n$Q$ lines, one for each query.\n\nSAMPLE INPUT:\n2 1\n1 10\n1 2 10\n4\n5 1\n5 2\n100 1\n100 2\nSAMPLE OUTPUT: \n5\n50\n100\n1090\n\nFirst query: Bessie takes 5 mana from pool 1 after 5 seconds.\n\nSecond query: Bessie takes 50 mana from pool 2 after 5 seconds.\n\nThird query: Bessie takes 100 mana from pool 1 after 100 seconds.\n\nFourth query: Bessie takes 90 mana from pool 1 after 90 seconds and 1000 mana\nfrom pool 2 after 100 seconds.\n\nSAMPLE INPUT:\n4 8\n50000000 100000000 20000000 70000000\n1 2 20\n2 1 50\n2 3 90\n1 3 40\n3 1 10\n4 1 25\n1 4 5\n4 3 70\n3\n8 3\n1000000000 1\n500000 4\nSAMPLE OUTPUT: \n160000000\n239999988050000000\n119992550000000\n\nAn example where Bessie is able to collect much larger amounts of mana.\n\nSCORING:\nInputs 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.\n\n\nProblem credits: Benjamin Qi"}}
{"type": "message", "message": {"role": "assistant", "content": "import sys\n\ndef main():\n data = sys.stdin.buffer.read().split()\n it = iter(data)\n N = int(next(it))\n M = int(next(it))\n m = [int(next(it)) for _ in range(N)]\n INF = 10**30\n dist = [[INF]*N for _ in range(N)]\n for i in range(N):\n dist[i][i] = 0\n for _ in range(M):\n a = int(next(it))-1\n b = int(next(it))-1\n t = int(next(it))\n if t < dist[a][b]:\n dist[a][b] = t\n # Floyd-Warshall\n for k in range(N):\n dk = dist[k]\n for i in range(N):\n di = dist[i]\n dik = di[k]\n if dik == INF:\n continue\n for j in range(N):\n nd = dik + dk[j]\n if nd < di[j]:\n di[j] = nd\n Q = int(next(it))\n queries = []\n for qi in range(Q):\n s = int(next(it))\n e = int(next(it))-1\n queries.append((s, e, qi))\n\n FULL = 1 << N\n # W for mask\n Wmask = [0]*FULL\n for mask in range(1, FULL):\n lb = mask & -mask\n i = lb.bit_length()-1\n Wmask[mask] = Wmask[mask ^ lb] + m[i]\n\n # dp[mask][last] = (bestD, bestval, bestvalD, bestvalval?) We'll store two candidates:\n # best_minD: (D, val); best_maxval: (D, val)\n # Use arrays of size N for each mask? Memory: FULL*N = 262144*18 = 4.7M entries; each a tuple heavy but okay maybe.\n # We'll store dict of last -> (minD,minval,maxD,maxval) only for reachable.\n dp = [None]*FULL\n for i in range(N):\n dp[1<<i] = {i: (0, 0, 0, 0)} # (minD, minval, maxD, maxval) init minD=0 val=0; max same\n # iterate masks\n for mask in range(1, FULL):\n cur = dp[mask]\n if cur is None:\n continue\n W = Wmask[mask]\n for last, (minD, minval, maxD, maxval) in list(cur.items()):\n # try extend to nxt\n for nxt in range(N):\n if mask & (1<<nxt):\n continue\n d = dist[last][nxt]\n if d >= INF:\n continue\n nmask = mask | (1<<nxt)\n nD = minD + d # for minD path\n nval_minpath = minval - d * W\n # for maxval path\n nD2 = maxD + d\n nval_maxpath = maxval - d * W\n # update dp[nmask][nxt]\n if dp[nmask] is None:\n dp[nmask] = {}\n entry = dp[nmask].get(nxt)\n if entry is None:\n dp[nmask][nxt] = (nD, nval_minpath, nD2, nval_maxpath)\n else:\n o_minD, o_minval, o_maxD, o_maxval = entry\n # keep min D\n if nD < o_minD or (nD == o_minD and nval_minpath > o_minval):\n o_minD, o_minval = nD, nval_minpath\n # keep max val\n if nval_maxpath > o_maxval or (nval_maxpath == o_maxval and nD2 < o_maxD):\n o_maxD, o_maxval = nD2, nval_maxpath\n dp[nmask][nxt] = (o_minD, o_minval, o_maxD, o_maxval)\n\n # Build per-e candidate lines\n # For each e, list of (D, W, val)\n cands_by_e = [[] for _ in range(N)]\n for mask in range(1, FULL):\n cur = dp[mask]\n if cur is None:\n continue\n W = Wmask[mask]\n for last, (minD, minval, maxD, maxval) in cur.items():\n cands_by_e[last].append((minD, W, minval))\n if (maxD, maxval) != (minD, minval):\n cands_by_e[last].append((maxD, W, maxval))\n\n # Li Chao tree over compressed s values (global)\n xs = sorted(set(s for s,_,_ in queries))\n # map s to index\n # We'll process per e\n ans = [0]*Q\n for e in range(N):\n qs = [(s, qi) for (s, ee, qi) in queries if ee == e]\n if not qs:\n continue\n qs.sort()\n cands = sorted(cands_by_e[e], key=lambda x: x[0])\n # Li Chao over xs_e (unique s for this e) but use global xs indices? We'll use local xs_e\n xs_e = sorted(set(s for s,_ in qs))\n # Li Chao tree arrays\n size = 1\n while size < len(xs_e):\n size <<= 1\n # store lines as (m,b) or None; use arrays\n tree_m = [0]*(2*size)\n tree_b = [None]*(2*size) # None means no line\n def eval_line(idx, x):\n m = tree_m[idx]; b = tree_b[idx]\n if b is None:\n return None\n return m*x + b\n def update(node, l, r, m_new, b_new):\n if tree_b[node] is None:\n tree_m[node] = m_new\n tree_b[node] = b_new\n return\n mid = (l+r)//2\n xm = xs_e[mid]\n cur_val = tree_m[node]*xm + tree_b[node]\n new_val = m_new*xm + b_new\n if new_val > cur_val:\n # swap\n tree_m[node], m_new = m_new, tree_m[node]\n tree_b[node], b_new = b_new, tree_b[node]\n if l == r:\n return\n # decide side\n xl = xs_e[l]\n # compare at left endpoint\n if m_new*xl + b_new > tree_m[node]*xl + tree_b[node]:\n update(node*2, l, mid, m_new, b_new)\n else:\n update(node*2+1, mid+1, r, m_new, b_new)\n def query(node, l, r, idx, x):\n res = None\n if tree_b[node] is not None:\n res = tree_m[node]*x + tree_b[node]\n if l == r:\n return res\n mid = (l+r)//2\n if idx <= mid:\n q = query(node*2, l, mid, idx, x)\n else:\n q = query(node*2+1, mid+1, r, idx, x)\n if q is None:\n return res\n if res is None or q > res:\n return q\n return res\n\n # map s to index\n import bisect\n ptr = 0\n for s, qi in qs:\n while ptr < len(cands) and cands[ptr][0] <= s:\n D, W, val = cands[ptr]\n update(1, 0, size-1, W, val)\n ptr += 1\n idx = bisect.bisect_left(xs_e, s)\n # idx should valid\n if idx >= len(xs_e):\n idx = len(xs_e)-1\n qval = query(1, 0, size-1, idx, s)\n if qval is None:\n # fallback: at least pool e alone? should have D=0 candidate\n qval = m[e]*s\n ans[qi] = qval\n\n # Output in original order\n out = []\n for qi in range(Q):\n out.append(str(ans[qi]))\n sys.stdout.write(\"\\n\".join(out) + (\"\\n\" if out else \"\"))\n\nif __name__ == \"__main__\":\n main()"}}
|