蓝桥杯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(...))→ 只做一件事:把列表里对应的关卡删掉
相关推荐
lly20240621 小时前
建造者模式
开发语言
之歆21 小时前
Day20_PC 端电商商品详情页前端实战:从布局到放大镜与选项卡
开发语言·前端·javascript·css·less
SimonKing21 小时前
57K星标的开源AI视频神器:三分钟出片,零门槛
java·后端·程序员
带刺的坐椅21 小时前
一行代码干翻 Java 反射?EggG 流式反射调用让反射优雅到不可思议
java·反射·类型元数据·eggg
众创岛21 小时前
java环境配置(windows)
java·开发语言
光泽雨21 小时前
C# 扩展方法(Extension Method)在语法上的核心灵魂。
开发语言·c#
代码小书生21 小时前
shutil,一个文件操作的 Python 库!
开发语言·python·策略模式
啄缘之间21 小时前
10.【学习】SPI & UART 验证环境与测试用例
开发语言·经验分享·学习·fpga开发·测试用例·verilog
老码观察21 小时前
设计模式实战解读(六):装饰器模式——功能增强,不动原代码
java·设计模式·装饰器模式
yu859395821 小时前
基于MATLAB的层合板等效模量及极限强度计算实现
开发语言·matlab