题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Farmer John is expanding his farm! He has identified the perfect location in the Red-Black Forest, which consists of $N$ trees ($1 \le N \le 10^5$) on a number line, with the $i$-th tree at position $x_i$ ($-10^9 \le x_i \le 10^9$).
Environmental protection laws restrict which trees Farmer John can cut down to make space for his farm. There are $K$ restrictions ($1 \leq K \leq 10^5$) specifying that there must always be at least $t_i$ trees ($1 \leq t_i \leq N$) in the line segment $[l_i, r_i]$, including the endpoints ($-10^9 \le l_i \leq r_i \le 10^9$). It is guaranteed that the Red-Black Forest initially satisfies these restrictions.
Farmer John wants to make his farm as big as possible. Please help him compute the maximum number of trees he can cut down while still satisfying all the restrictions!
INPUT FORMAT (input arrives from the terminal / stdin):
Each input consists of $T$ ($1 \le T \le 10$) independent test cases. It is guaranteed that the sums of all $N$ and of all $K$ within an input each do not exceed $3 \cdot 10^5$.
The first line of input contains $T$. Each test case is then formatted as follows:
- The first line contains integers $N$ and $K$.
- The next line contains the $N$ integers $x_1, \dots, x_N$.
- Each of the next $K$ lines contains three space-separated integers: $l_i$, $r_i$ and $t_i$.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test case, output a single line with an integer denoting the maximum number of trees Farmer John can cut down.
SAMPLE INPUT:
3 7 1 8 4 10 1 2 6 7 2 9 3 7 2 8 4 10 1 2 6 7 2 9 3 1 10 1 7 2 8 4 10 1 2 6 7 2 9 3 1 10 4
SAMPLE OUTPUT:
4 4 3
For the first test case, Farmer John can cut down the first $4$ trees, leaving trees at $x_i = 2, 6, 7$ to satisfy the restriction.
For the second test case, the additional restriction does not affect which trees Farmer John can cut down, so he can cut down the same trees and satisfy both restrictions.
For the third test case, Farmer John can only cut down at most $3$ trees because there are initially $7$ trees but the second restriction requires him to leave at least $4$ trees uncut.
SCORING:
- Input 2: $N, K \le 16$
- Inputs 3-5: $N, K \le 1000$
- Inputs 6-7: $t_i = 1$ for all $i = 1, \dots, K$.
- Inputs 8-11: No additional constraints.
Problem credits: Tina Wang, Jiahe Lu, Benjamin Qi
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 7.7k平均输出 token(含思考) ↓
- 27.0输出速度 tokens/s ↑
- 289 秒延迟 ↓
- 0.85×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 11Pylint 问题 ↓
- 7.88Pylint 得分 ↑
- 5圈复杂度 ↓
- 54.0可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 12.1k平均输出 token(含思考) ↓
- 186.7输出速度 tokens/s ↑
- 65 秒延迟 ↓
- 0.84×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 11Pylint 问题 ↓
- 8.07Pylint 得分 ↑
- 9圈复杂度 ↓
- 41.9可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 16.8k平均输出 token(含思考) ↓
- 131.1输出速度 tokens/s ↑
- 129 秒延迟 ↓
- 0.65×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 8Pylint 问题 ↓
- 8.55Pylint 得分 ↑
- 10圈复杂度 ↓
- 65.1可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys from bisect import bisect_left, bisect_right def solve(): data = list(map(int, sys.stdin.buffer.read().split())) pos = 0 t_cases = data[pos] pos += 1 answers = [] for _ in range(t_cases): n, k = data[pos], data[pos + 1] pos += 2 trees = sorted(data[pos:pos + n]) pos += n restrictions = [] for _ in range(k): l, r, required = data[pos], data[pos + 1], data[pos + 2] pos += 3 # Convert coordinates to 1-based indices in the sorted tree array. left = bisect_left(trees, l) + 1 right = bisect_right(trees, r) restrictions.append((r, right, left, required)) restrictions.sort(key=lambda x: x[0]) bit = [0] * (n + 1) def bit_sum(index): result = 0 while index > 0: result += bit[index] index -= index & -index return result def bit_add(index): while index <= n: bit[index] += 1 index += index & -index # parent[i] leads to the rightmost unselected tree at or before i. parent = list(range(n + 1)) def find(index): while parent[index] != index: parent[index] = parent[parent[index]] index = parent[index] return index kept = 0 for _, right, left, required in restrictions: already_kept = bit_sum(right) - bit_sum(left - 1) needed = required - already_kept while needed > 0: chosen = find(right) bit_add(chosen) parent[chosen] = find(chosen - 1) kept += 1 needed -= 1 answers.append(str(n - kept)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": solve()
import sys import bisect def solve() -> None: data = list(map(int, sys.stdin.buffer.read().split())) it = iter(data) T = next(it) answers = [] for _ in range(T): N = next(it) K = next(it) xs = [next(it) for _ in range(N)] xs.sort() intervals = [] for _ in range(K): l = next(it) r = next(it) t = next(it) a = bisect.bisect_left(xs, l) + 1 b = bisect.bisect_right(xs, r) intervals.append((b, a, t)) intervals.sort() parent = list(range(N + 1)) bit = [0] * (N + 1) kept = 0 def find(x: int) -> int: root = x while parent[root] != root: root = parent[root] while parent[x] != x: nxt = parent[x] parent[x] = root x = nxt return root for b, a, t in intervals: selected_in_interval = 0 idx = b while idx > 0: selected_in_interval += bit[idx] idx -= idx & -idx idx = a - 1 while idx > 0: selected_in_interval -= bit[idx] idx -= idx & -idx need = t - selected_in_interval while need > 0: p = find(b) idx = p while idx <= N: bit[idx] += 1 idx += idx & -idx kept += 1 parent[p] = find(p - 1) need -= 1 answers.append(str(N - kept)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": solve()
import sys from bisect import bisect_left, bisect_right def solve(): data = iter(map(int, sys.stdin.buffer.read().split())) try: test_cases = next(data) except StopIteration: return answers = [] for _case in range(test_cases): n = next(data) k = next(data) positions = [next(data) for _ in range(n)] positions.sort() restrictions = [] for _ in range(k): l = next(data) r = next(data) required = next(data) # Convert the coordinate interval to a 1-based index interval. left = bisect_left(positions, l) + 1 right = bisect_right(positions, r) restrictions.append((right, left, required)) # Process intervals in increasing order of their right endpoint. restrictions.sort() # Predecessor DSU: find(v) is the largest not-yet-kept index <= v. # Index 0 is a sentinel. parent = list(range(n + 1)) def find(v): while parent[v] != v: parent[v] = parent[parent[v]] v = parent[v] return v # Fenwick tree containing 1 at each kept tree's index. bit = [0] * (n + 1) kept = 0 for right, left, required in restrictions: # Count kept trees strictly before left. before = 0 j = left - 1 while j: before += bit[j] j -= j & -j # All trees kept so far have index <= right, so this is the # number of kept trees in [left, right]. need = required - (kept - before) if need <= 0: continue # Keep the rightmost available trees in the interval. # Initial feasibility guarantees that these indices are >= left. p = find(right) while need > 0: q = find(p - 1) parent[p] = q j = p while j <= n: bit[j] += 1 j += j & -j kept += 1 need -= 1 p = q answers.append(str(n - kept)) sys.stdout.write("\n".join(answers) + "\n") if __name__ == "__main__": solve()