2024蓝桥杯每日一题(双指针)

一、第一题:牛的学术圈

解题思路:双指针+贪心

仔细思考可以知道,写一篇综述最多在原来的H指数的基础上+1,所以基本方法可以是先求出原始的H指数,然后分类讨论怎么样提升H指数。

【Python程序代码】

python 复制代码
n,l = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
res,suml,sumr = 0,0,0
for i in range(n):
    sumr = n-i
    if a[i]>sumr:
        if i==0:break
        if a[i-1]+1>=sumr+1 and l>=1:
            sumr+=1
            break
        break
    if a[i]==sumr:
        j,c = i,1
        if i == 0: break
        while j+1<n and a[j+1]==a[i]:
            j += 1
            c += 1
        if a[i-1]==a[i] and l>=c+1:
            sumr+=1
            break
if l==0 and a[n-1]==0:print(0)
else:print(sumr)

二、第二题:最长连续不重复子序列

思路:双指针

双指针模板题

【Python程序代码】

python 复制代码
n = int(input())
a = [0] + list(map(int,input().split()))
mp = [0]*(100010)
res,j=1,1
for i in range(1,n+1):
    if mp[a[i]]:
        res = max(res,i-j)
        while a[j]!=a[i]:
            mp[a[j]]-=1
            j +=1
        j += 1
    else:
        mp[a[i]]+=1
res = max(res,i-j+1)
print(res)

三、第三题: 数组元素的目标和

解题思路:双指针

双指针模板题

【Python程序代码】

python 复制代码
n,m,x = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
j = m-1
res = []
for i in range(n):
    if a[i]+b[j]<x:continue
    if a[i]+b[j]==x:
        res.append((i,j))
        continue
    if a[i]+b[j]>x:
        while j>0 and a[i]+b[j]>=x:
            if a[i]+b[j]==x:
                res.append((i,j))
            j -= 1
for i in res:
    print(i[0],i[1])

四、第四题: 判断子序列

解题思路:双指针

双指针模板题

【Python程序代码】

python 复制代码
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
j = 0
for i in range(m):
    if j==n:break
    if b[i]==a[j]:
        j += 1
if j==n:print("Yes")
else:print("No")

五、第五题: 日志统计

解题思路:双指针+滑动窗口

双指针+滑动窗口模板题

【Python程序代码】

python 复制代码
from collections import defaultdict
n,d,k = map(int,input().split())
mp = defaultdict(int)
a,res = [],[]
for i in range(n):
    ts,id = map(int,input().split())
    a.append([ts,id])
a.sort()
j,pret = 0,0
for i in range(n):
    teps,tepd = a[i]
    if teps - a[j][0]>=d:
        while teps - a[j][0]>=d:
            mp[a[j][1]]-=1
            j += 1
        pret = a[j][0]
    mp[tepd] += 1
    if mp[tepd]>=k:
         res.append(tepd)
res = list(set(res))
res.sort()
for i in res:print(i)
相关推荐
不是万能的豆学长3 小时前
filetidy 文件整理助手:一键归类 + 规范重命名(Python 零依赖单文件工具)
大数据·开发语言·python·技术美术
gCode Teacher 格码致知5 小时前
Pandas教学-18:number与numeric的区别
python·pandas
xsd202411186 小时前
3D双目相机 vs 面阵激光雷达:机器人的“眼睛“到底怎么选?
人工智能
梦想不只是梦与想7 小时前
Python 中subprocess 模块:执行外部命令
python·subprocess·调用外部命令
懂点Tech8 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
2601_965619698 小时前
把开源项目Changelog自动做成更新视频,AI工作流
人工智能·开源·音视频
Json____8 小时前
PHP 突发严重安全漏洞,AI 开始改变网络安全的攻防方式
人工智能·web安全·php·it学习·wwwoop.com
徐小夕8 小时前
花了3个月,我们做了一款 AI 版的“飞书文档”
前端·算法·github
Robert--cao8 小时前
移动底盘与机械臂联合规划:开源算法与工程实现
算法
fxss8 小时前
我用AI给娃写了个拼音闯关大冒险
人工智能