蓝桥杯3.8模拟赛2-5题

python 复制代码
n=int(input())
s="2026"
for i in range(1,n+1):
    line=""
    for j in range(i):
        line+=s[j%4]
    print(line)
python 复制代码
n,k=map(int,input().split())
s=input().strip()
stack=[]
for c in s:
    while k>0 and stack and stack[-1]<c:
        stack.pop()
        k-=1
    stack.append(c)
if k>0:
    stack=stack[:-k]
res=''.join(stack).lstrip('0')
print(res if res else'0')
python 复制代码
m, a = map(int, input().split())

# 递归:day = 当前天数,x = 第day-1项,y = 第day项
def dfs(day, x, y):
    # 先判断今天达不达标
    if y % m == a:
        return day
    
    # 出现 1,1 循环,永远找不到
    if x == 1 and y == 1 and day > 2:
        return -1
    
    # 递归下一天
    return dfs(day + 1, y, (x + y) % m)

# 初始:第1天=1,第2天=1,从第2天开始查
print(dfs(2, 1, 1))
python 复制代码
import heapq
n=int(input())
levels=[]
for _ in range(n):
    a,b=map(int,input().split())
    levels.append(a,b)
levels.sort()
heap=[]
total=0
for b,a in levels:
    heapq.heappush(heap,-a)
    total+=a
    if total>b:
        total-=(-heapq.heappop(heap))
print(len(heap))
python 复制代码
heapq.heappush(heap, a)   # 原始写法(小根堆)
heapq.heappush(heap, -a)
max_a = max(heap)   # 原始写法(暴力找最大,慢)
-heap[0]因为堆顶是 最小负数 → 取反就是 最大正数。
-heapq.heappop(heap)
python 复制代码
n = int(input())
levels = []

# 1. 读入所有关卡
for i in range(n):
    a, b = map(int, input().split())
    levels.append((a, b))

# 2. 贪心排序:按 b 从小到大排(核心!限制严的先打)
levels.sort(key=lambda x: x[1])

sum_a = 0    # 当前总体力
count = 0    # 答案:打过的关数
selected = [] # 保存打过的关卡

# 3. 暴力贪心:能打就打,不能打就算了(省三思路)
for a, b in levels:
    if sum_a <= b:          # 满足条件:当前体力 <= b
        selected.append(a)
        sum_a += a
        count += 1
    else:
        # 不能直接打 → 看看能不能替换掉之前最大的a(贪心优化)
        if selected and max(selected) > a:
            sum_a -= max(selected)
            selected.remove(max(selected))
            selected.append(a)
            sum_a += a

print(count)
  • sum_a -= max(...)→ 只做一件事:把总体力减少
  • selected.remove(max(...))→ 只做一件事:把列表里对应的关卡删掉
相关推荐
lifallen3 小时前
Flink Agents:Watermark 与事件时间 (Event Time) 在 Agent 算子中的演进分析
java·大数据·人工智能·语言模型·flink
报错小能手3 小时前
ios开发方向——swift并发进阶核心 @MainActor 与 DispatchQueue.main 解析
开发语言·ios·swift
量子炒饭大师3 小时前
【C++11】Cyber骇客的 亡骸剥离与右值重构 ——【右值引用 与 移动语义】(附带完整代码解析)
java·c++·重构·c++11·右值引用·移动语义
ZHANG13HAO3 小时前
Python 调用 Node.js(vm2 沙箱)完美方案:胶水层实战教程
开发语言·python·node.js
asdzx673 小时前
使用 C# 将 Excel 转换成高质量 JPG
开发语言·c#·excel
Rick19933 小时前
Java 接口高并发优化方案
java·性能优化·高并发
瑶总迷弟3 小时前
Python入门第7章:用户输入和 while 、for循环
开发语言·python·microsoft
曲幽3 小时前
FastAPI自动生成的API文档太丑?我花了一晚上把它改成了客户愿意付费的样子
python·fastapi·web·swagger·openapi·scalar·docs