题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Farmer John is playing a famous and strategic card game with his dear cow Bessie. FJ has $N$ ($2\le N\le 2\cdot 10^5$) cards, conveniently numbered from $1$ to $N$. The $i$'th card costs $a_i$ ($1 \leq a_i \leq 10^9$) moolixir if FJ wants to play it.
His hand always consists of $H$ cards at any given time ($1\le H<N$). Initially, his hand consists of cards $1$ through $H$. The remaining cards are in a draw queue. Every time FJ plays a card in his hand, he will draw its replacement from the front of the draw queue to his hand. Then, FJ will put the card he just played to the back of the draw queue. Initially, cards $H+1$ through $N$ are arranged from the front to the back of the draw queue in that order.
In this game, time is measured in integer seconds. Initially, the game starts at time $0$ with FJ having $0$ moolixir. Immediately before each of integer times $t=1,2,3,\dots,$ moolixir increases by $1$. At each integer time, FJ may choose to play a card in his hand if its cost does not exceed FJ's current moolixir count, which subtracts FJ's current moolixir count by the card's cost.
FJ marks a subset of his cards $s_1, s_2, \ldots, s_k$ as win-conditions ($1\le k\le N$, $1\le s_i\le N$). If FJ has at least one win-condition in his hand, the next card he plays must be a win-condition.
He asks you $Q$ ($1 \leq Q \leq 2 \cdot 10^5$) queries. Each query is of the following form: what is the maximum number of win-conditions he could have placed down within $t$ time ($1 \leq t \leq 10^{18}$)?
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$ and $H$.The second line contains $N$ integers $a_1, a_2, \ldots, a_N$.
The third line contains an integer $k$, the number of win-conditions.
The fourth line contains $k$ distinct integers $s_1, s_2, \ldots, s_k$.
The fifth line contains an integer $Q$.
The following $Q$ lines each contain an integer $t$, the time to answer for each query.
OUTPUT FORMAT (print output to the terminal / stdout):
For each query, output the maximum number of win-conditions that FJ could've put down within $t$ time.SAMPLE INPUT:
6 3 2 4 3 5 7 6 2 1 4 6 1 2 3 7 10 1000000000000000
SAMPLE OUTPUT:
0 1 1 2 2 142857142857143
In this case, you start with card 1, a win condition on your hand. You can play it after you accumulate 2 elixir in 2 seconds. This means that just after t=1 you can play no cards, but after t=2 you can play your first card, which must be your win condition.
After t=3, it is still most optimal to play card 1 and have 1 elixir remaining, so the answer here is still 1.
You then draw card 4, which is also a win condition. You play it immediately after t=7, so you have played 2 win conditions at this time.
You then draw card 5 and have no win conditions in your hand. After t=10, even if you play card 3 with the 3 elixir you have, your number of win conditions does not change.
SCORING:
- Inputs 2-3: $N,Q\le 100$
- Inputs 4-5: $H=1$
- Inputs 6-11: No additional constraints.
Problem credits: Chongtian Ma
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 54.5k平均输出 token(含思考) ↓
- 22.9输出速度 tokens/s ↑
- 2378 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 5Pylint 问题 ↓
- 9.24Pylint 得分 ↑
- 13圈复杂度 ↓
- 51.2可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 49.4k平均输出 token(含思考) ↓
- 187.0输出速度 tokens/s ↑
- 264 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 9Pylint 问题 ↓
- 8.93Pylint 得分 ↑
- 14圈复杂度 ↓
- 49.0可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
0/1
解出的采样 ↑
- 0.0采样 1 · 答案错误
- 86.7k平均输出 token(含思考) ↓
- 108.2输出速度 tokens/s ↑
- 804 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- —时间复杂度(相对官方解)
- 7Pylint 问题 ↓
- 9.12Pylint 得分 ↑
- 16圈复杂度 ↓
- 54.9可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys from collections import deque from heapq import heappush, heappop from bisect import bisect_right def main(): it = iter(map(int, sys.stdin.buffer.read().split())) n = next(it) h = next(it) costs = [next(it) for _ in range(n)] k = next(it) is_win = [False] * (n + 1) for _ in range(k): is_win[next(it)] = True query_count = next(it) queries = [next(it) for _ in range(query_count)] del it queue_size = n - h period_length = queue_size + 1 draw_queue = deque(range(h + 1, n + 1)) win_hand = [] other_hand = [] for card in range(1, h + 1): item = (costs[card - 1], card) if is_win[card]: heappush(win_hand, item) else: heappush(other_hand, item) prefix_cost = [0] prefix_wins = [0] block_cost = [0] block_wins = [0] total_cost = 0 total_wins = 0 # Playing the cheapest currently legal card is optimal. After the first # queue_size plays, at most period_length more plays are needed before # the played-card sequence becomes periodic with period period_length. for step in range(1, queue_size + period_length + 1): if win_hand: cost, card = heappop(win_hand) else: cost, card = heappop(other_hand) gained_win = 1 if is_win[card] else 0 total_cost += cost total_wins += gained_win drawn = draw_queue.popleft() drawn_item = (costs[drawn - 1], drawn) if is_win[drawn]: heappush(win_hand, drawn_item) else: heappush(other_hand, drawn_item) draw_queue.append(card) if step <= queue_size: prefix_cost.append(total_cost) prefix_wins.append(total_wins) else: block_cost.append(block_cost[-1] + cost) block_wins.append(block_wins[-1] + gained_win) base_cost = prefix_cost[-1] base_wins = prefix_wins[-1] cycle_cost = block_cost[-1] cycle_wins = block_wins[-1] answers = [] for limit in queries: if limit < base_cost: plays = bisect_right(prefix_cost, limit) - 1 answers.append(prefix_wins[plays]) else: extra = limit - base_cost full_cycles, remainder = divmod(extra, cycle_cost) offset = bisect_right(block_cost, remainder) - 1 answers.append( base_wins + full_cycles * cycle_wins + block_wins[offset] ) sys.stdout.write("\n".join(map(str, answers))) if __name__ == "__main__": main()
import sys import heapq import bisect from collections import deque def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return idx = 0 N = data[idx] idx += 1 H = data[idx] idx += 1 a = [0] * (N + 1) for i in range(1, N + 1): a[i] = data[idx] idx += 1 k = data[idx] idx += 1 is_win = [False] * (N + 1) for _ in range(k): s = data[idx] idx += 1 is_win[s] = True Q = data[idx] idx += 1 queries = data[idx:idx + Q] win_heap = [] non_heap = [] for i in range(1, H + 1): if is_win[i]: heapq.heappush(win_heap, (a[i], i)) else: heapq.heappush(non_heap, (a[i], i)) queue = deque(range(H + 1, N + 1)) # In the eventual steady state, exactly M + 1 = N - H + 1 cards # participate in the cycle. L = N - H + 1 played = [] match_run = 0 # Simulate the optimal greedy sequence. # If a win-condition is in hand, play the cheapest one. # Otherwise, play the cheapest non-win. while True: if win_heap: _, card = heapq.heappop(win_heap) else: _, card = heapq.heappop(non_heap) played.append(card) drawn = queue.popleft() if is_win[drawn]: heapq.heappush(win_heap, (a[drawn], drawn)) else: heapq.heappush(non_heap, (a[drawn], drawn)) queue.append(card) pos = len(played) - 1 if pos >= L and played[pos] == played[pos - L]: match_run += 1 else: match_run = 0 if match_run >= L: break nseq = len(played) cycle_start = nseq - 2 * L transient_ids = played[:cycle_start] cycle_ids = played[cycle_start:cycle_start + L] def build_prefix(ids): pref_cost = [0] pref_win = [0] for c in ids: pref_cost.append(pref_cost[-1] + a[c]) pref_win.append(pref_win[-1] + (1 if is_win[c] else 0)) return pref_cost, pref_win trans_cost_pref, trans_win_pref = build_prefix(transient_ids) cycle_cost_pref, cycle_win_pref = build_prefix(cycle_ids) trans_cost = trans_cost_pref[-1] trans_wins = trans_win_pref[-1] cycle_cost = cycle_cost_pref[-1] cycle_wins = cycle_win_pref[-1] out = [] for t in queries: if t < trans_cost: p = bisect.bisect_right(trans_cost_pref, t) - 1 ans = trans_win_pref[p] else: rem = t - trans_cost full_cycles, rest = divmod(rem, cycle_cost) ans = trans_wins + full_cycles * cycle_wins p = bisect.bisect_right(cycle_cost_pref, rest) - 1 ans += cycle_win_pref[p] out.append(str(ans)) sys.stdout.write("\n".join(out)) if __name__ == "__main__": main()
import sys from heapq import heapify, heappop, heappush from bisect import bisect_right def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return n, h = data[0], data[1] ptr = 2 costs = data[ptr:ptr + n] ptr += n k = data[ptr] ptr += 1 is_win = bytearray(n) for card_id in data[ptr:ptr + k]: is_win[card_id - 1] = 1 ptr += k q = data[ptr] ptr += 1 queries = data[ptr:ptr + q] del data # Encoding (cost, id) as one integer makes heap operations faster. encoded = [costs[i] * n + i for i in range(n)] win_heap = [encoded[i] for i in range(h) if is_win[i]] nonwin_heap = [encoded[i] for i in range(h) if not is_win[i]] heapify(win_heap) heapify(nonwin_heap) draw_queue = list(range(h, n)) queue_front = draw_queue.pop queue_back = draw_queue.append pop_heap = heappop push_heap = heappush # A played card returns to the hand L plays after it was played. # Consequently, the type pattern and the greedy choices become periodic # with period P = L + 1. L = n - h P = L + 1 # All cards have become eligible by L + P plays. One additional # P-play period is enough for the cheapest cards to settle into # permanent phases. The following P-play block is therefore stable. base = L + 2 * P + 1 steps = base + P output_costs = [0] * steps output_is_win = bytearray(steps) win_costs = [] total_cost = 0 for step in range(steps): if win_heap: key = pop_heap(win_heap) played_win = 1 else: key = pop_heap(nonwin_heap) played_win = 0 card_id = key % n value = costs[card_id] output_costs[step] = value output_is_win[step] = played_win total_cost += value if played_win: win_costs.append(total_cost) incoming = queue_front() queue_back(card_id) if is_win[incoming]: push_heap(win_heap, encoded[incoming]) else: push_heap(nonwin_heap, encoded[incoming]) base_total_cost = sum(output_costs[:base]) base_win_count = sum(output_is_win[:base]) base_win_costs = win_costs[:base_win_count] # Costs, measured from the beginning of one stable period, at which # each win-condition in that period is played. cycle_win_prefix = [0] cycle_total_cost = 0 for step in range(base, base + P): cycle_total_cost += output_costs[step] if output_is_win[step]: cycle_win_prefix.append(cycle_total_cost) wins_per_cycle = len(cycle_win_prefix) - 1 answers = [] for t in queries: if t < base_total_cost: answer = bisect_right(base_win_costs, t) else: remaining = t - base_total_cost full_cycles, remainder = divmod(remaining, cycle_total_cost) answer = base_win_count + full_cycles * wins_per_cycle answer += bisect_right(cycle_win_prefix, remainder) - 1 answers.append(str(answer)) sys.stdout.write("\n".join(answers)) sys.stdout.write("\n") if __name__ == "__main__": solve()