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)
相关推荐
Eloudy2 小时前
LLM 公司提供的 token api 服务输出内容的特点
人工智能
DeepAgent2 小时前
AI Agent 工程实践(03):Memory 设计——Agent 到底应该记住什么?
人工智能
懂懂笔记2 小时前
老龄化加速下的中国答案:东软用AI思维重构“人生下半场”
人工智能·智慧养老
VIP_CQCRE2 小时前
用 Ace Data Cloud 快速接入 OpenAI Chat Completions:对话、流式、多轮和多模态一篇打通
python·ai·openai·api·教程
GitCode官方2 小时前
基于《人工智能 智能体互联》国标的 AIP 开源项目在 AtomGit 正式开源
人工智能·开源·atomgit
万象AI实验室2 小时前
GPT-5.6 正式上线,我们挖到了 3 个隐藏玩法!
人工智能
运行时记录2 小时前
prompt-optimizer skill
算法
万法若空2 小时前
【数据结构-哈希表】哈希表原理
数据结构·算法·散列表
IpdataCloud2 小时前
AI生成的Avalon恶意框架怎么防?用IP离线库识别模块化C2通信
运维·人工智能·网络协议·tcp/ip·ip
FreeBuf_2 小时前
公开GitHub Issue可诱骗AI泄露私有数据,绕过防护仅需一词
人工智能·github·issue