蓝桥杯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(...))→ 只做一件事:把列表里对应的关卡删掉
相关推荐
小羊没烦恼!3 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
俊昭喜喜里3 天前
java中的继承和多态的区别
java
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
胡写代码3 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖3 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
长沙三为智能科技3 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python