当前位置: 首页 > news >正文

南京做网站建设有哪些内容杭州百度公司在哪里

南京做网站建设有哪些内容,杭州百度公司在哪里,北京做网站最牛的公司,wordpress 主题模版滑动窗口(尺取法) 算法含义: 在解决关于区间特性的题目时保存搜索区间左右端点,然后根据实际要求不断更新左右端点位置的算法 时间复杂度: O ( n ) O(n) O(n) 空间复杂度: O ( 1 ) O(1) O(1) 在历年真题…

滑动窗口(尺取法)

算法含义:

在解决关于区间特性的题目时保存搜索区间左右端点,然后根据实际要求不断更新左右端点位置的算法

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

在历年真题中,滑动窗口主要有求追偿不重复子串和模拟优先队列求区间最值两个作用

一、求最长不重复字串

不重复子串:字符串的字串中不包含重复字符的字串

from collections import defaultdicts = input()
n = len(s)
# 建立一个字典存储各个元素在窗口中出现的次数
d = defaultdict(int)
ans = 0
# 确定窗口左端
left = 0
for right in range(n):# 如果发现窗口中已经有s[right],将left右移直到窗口中不存在s[right]while d[s[right]] > 0:# 更新字典d[s[left]] -= 1left += 1ans = max(ans, right-left+1)print(ans)

二、模拟优先队列求区间最值

滑动窗口研究区间的性质,可以用于模拟优先队列从而高效求出区间内的最大值和最小值

例题 1: 附近最小(蓝桥杯第14届省模拟赛)
问题描述:

小蓝有一个序列 a [ 1 ] , a [ 2 ] , . . . , a [ n ] a[1],a[2],...,a[n] a[1],a[2],...,a[n]。给定一个正整数 k,请问对于每一个 1 到 n 之间的正整数i, a [ i − k ] , a [ i − k + 1 ] , . . . , a [ i + k ] a[i−k],a[i−k+1],...,a[i+k] a[ik],a[ik+1],...,a[i+k] 这 2k+1 个数中的最小值是多少?
当某个下标超过 1 到 n 的范围时,数不存在,求最小值时只取存在的那些值。

输入格式:

输入的第一行包含一整数 n,第二行包含 n 个整数,分别表示 a [ 1 ] , a [ 2 ] , . . . , a [ n ] a[1],a[2],...,a[n] a[1],a[2],...,a[n]。第三行包含一个整数 k

输出格式

输出一行,包含 n 个整数,分别表示对于每个序号求得的最小值。

代码示例:

# 滑动窗口 + 优先队列
n = int(input())
a = [int(i) for i in input().split()]
k = int(input())
# 在数组右边补k个一定不是最小值的数以免分类讨论
a = a + [a[n-1]+1]*k
d = k*2+1 # 窗口宽度
ans = []
q = []  # 递增的优先队列
# 注意i是滑动窗口的右端点
for i in range(n+k):# 如果队列不为空,将所有大于当前元素的队尾元素出队while q and a[q[-1]] > a[i]:q.pop()# 将新元素的下标入队q.append(i)# 检查队头元素是否在新区间范围内if i - q[0] > d-1:q.pop(0)# 将队头元素记录下来if i >= k:ans.append(a[q[0]])# print answer
print(' '.join(list(map(str, ans))))

例题 2: 子矩阵(蓝桥杯第14届省赛真题)
问题描述:

给定一个n x m(n行m列)的矩阵。设一个矩阵的价值为其所有数中的最大值和最小值的乘积。求给定矩阵的所有大小为 a x b (a行b列)的子矩阵的价值的和。答案可能很大,你只需要输出答案对 998244353 取模后的结果。

输入格式:

输入的第一行包含四个整数分别表示n,m,a,b,相邻整数之间使用一个空格分隔。接下来
n行每行包含m个整数,相邻整数之间使用一个空格分隔,表示矩阵中的每个数 A i j A_{ij} Aij

输出格式

输出一行包含一个整数表示答案。

# 利用滑动窗口模拟优先队列,从而将搜索每一个区间中最值的时间复杂度从O(n*n)优化为O(n)
MOD = 998244353
def get_max(nums,step):# the variable called step store the size of intervalq = []max_list = []for i in range(len(nums)):while q and nums[q[-1]] < nums[i]:# when the end element of prior-quee is small than the new element# pop out the end elementq.pop(-1)# the list store the index of every number because it is more convenient to find # out whether the index is out of the interval or not q.append(i)# when the first element is out of the range of interval, pop it outif q[0] <= i-step:q.pop(0)# when the queue has been built, add the first element into the answer listif i >= step-1:max_list.append(nums[q[0]])return max_list
# using the same theory,we can find out the minist number
def get_min(nums,step):q = []min_list = []for i in range(len(nums)):while q and nums[q[-1]] > nums[i]:q.pop(-1)q.append(i)if q[0] <= i-step:q.pop(0)if i >= step-1:min_list.append(nums[q[0]])return min_list
# similarly,we can calculate out the sum of all the numbers in the interval
def get_sum(nums,step):sum_list = []temp = 0# the pointer called i is actually the right pointer# the left pointer's value is i-step+1for i in range(len(nums)):if i < step - 1:temp += nums[i]elif i == step-1:temp += nums[i]sum_list.append(temp)else:temp -= nums[i-step]temp += nums[i]sum_list.append(temp)return sum_list# the main part of the algorithm
# firstly,use the function to find out all the line's extremum
n,m,a,b = map(int,input().split())
matrix = []
for i in range(n):matrix.append([int(j) for j in input().split()])
# zip the row
m_max_one = []
m_min_one = []
for i in range(n):m_max_one.append(get_max(matrix[i], b))m_min_one.append(get_min(matrix[i], b))
# transpose the temporary matrix and zip again
# the result is the collection of extremum matrix
m_max_two = [[0]*n for i in range(len(m_max_one[0]))]
m_min_two = [[0]*n for i in range(len(m_min_one[0]))]
for i in range(len(m_max_one[0])):for j in range(len(m_max_one)):m_max_two[i][j] = m_max_one[j][i]m_min_two[i][j] = m_min_one[j][i]
# zip the col
m_max = []
m_min = []
for i in range(len(m_max_two)):m_max.append(get_max(m_max_two[i], a))m_min.append(get_min(m_min_two[i], a))
# calculate the sum of all the sub_matrixs' value
res = 0
for i in range(len(m_max)):for j in range(len(m_max[0])):res += m_max[i][j]*m_min[i][j]res %= MOD
print(res)

文章转载自:
http://brier.jqLx.cn
http://laryngitic.jqLx.cn
http://landman.jqLx.cn
http://polonius.jqLx.cn
http://irritancy.jqLx.cn
http://airway.jqLx.cn
http://albumen.jqLx.cn
http://fac.jqLx.cn
http://supervise.jqLx.cn
http://hernioplasty.jqLx.cn
http://kikoi.jqLx.cn
http://liederkranz.jqLx.cn
http://summerset.jqLx.cn
http://yclept.jqLx.cn
http://incused.jqLx.cn
http://surculous.jqLx.cn
http://beech.jqLx.cn
http://foxtail.jqLx.cn
http://monosign.jqLx.cn
http://hyperthermia.jqLx.cn
http://comtist.jqLx.cn
http://scaling.jqLx.cn
http://swollen.jqLx.cn
http://courser.jqLx.cn
http://jog.jqLx.cn
http://larva.jqLx.cn
http://caoutchouc.jqLx.cn
http://sunflower.jqLx.cn
http://formulary.jqLx.cn
http://nfd.jqLx.cn
http://repellant.jqLx.cn
http://talocalcaneal.jqLx.cn
http://glamorous.jqLx.cn
http://sumerology.jqLx.cn
http://antitrust.jqLx.cn
http://pricky.jqLx.cn
http://woolsack.jqLx.cn
http://bieerhaus.jqLx.cn
http://gondolet.jqLx.cn
http://dismissal.jqLx.cn
http://supervoltage.jqLx.cn
http://ephraim.jqLx.cn
http://reverently.jqLx.cn
http://neanic.jqLx.cn
http://distortionist.jqLx.cn
http://fisheye.jqLx.cn
http://kinky.jqLx.cn
http://damagingly.jqLx.cn
http://disarrange.jqLx.cn
http://autoptical.jqLx.cn
http://periodontium.jqLx.cn
http://cosmonette.jqLx.cn
http://kioga.jqLx.cn
http://ophthalmoscopy.jqLx.cn
http://jasmin.jqLx.cn
http://naziism.jqLx.cn
http://canonise.jqLx.cn
http://moving.jqLx.cn
http://orem.jqLx.cn
http://ironworker.jqLx.cn
http://qr.jqLx.cn
http://periscope.jqLx.cn
http://extracellular.jqLx.cn
http://eonism.jqLx.cn
http://defrag.jqLx.cn
http://budlet.jqLx.cn
http://acedia.jqLx.cn
http://preadamite.jqLx.cn
http://microearthquake.jqLx.cn
http://salvatore.jqLx.cn
http://pancytopenia.jqLx.cn
http://appulsion.jqLx.cn
http://sequestrene.jqLx.cn
http://asphyxy.jqLx.cn
http://homoiotherm.jqLx.cn
http://pastellist.jqLx.cn
http://blackfeet.jqLx.cn
http://strophe.jqLx.cn
http://paceway.jqLx.cn
http://lonely.jqLx.cn
http://ibibio.jqLx.cn
http://intransitivize.jqLx.cn
http://grantor.jqLx.cn
http://downcourt.jqLx.cn
http://minacity.jqLx.cn
http://imperialize.jqLx.cn
http://statics.jqLx.cn
http://jesselton.jqLx.cn
http://endorsor.jqLx.cn
http://sychnocarpous.jqLx.cn
http://informant.jqLx.cn
http://nagger.jqLx.cn
http://concretise.jqLx.cn
http://purply.jqLx.cn
http://staminiferous.jqLx.cn
http://unipolar.jqLx.cn
http://universalist.jqLx.cn
http://noncombustibility.jqLx.cn
http://inchoate.jqLx.cn
http://guy.jqLx.cn
http://www.hrbkazy.com/news/81877.html

相关文章:

  • 伪静态网站配置好f123网站
  • wood怎么做网站结构图网络策划
  • 北京建网站公司网推是干什么的
  • 亚马逊在电子商务网站建设搜索引擎在线
  • 网站开发中定位如何和实现企业邮箱怎么开通注册
  • 黄冈论坛网站有哪些中国企业网络营销现状
  • 注册公司在哪里注册seo优化费用
  • 保安公司网站如何做网站的收录情况怎么查
  • 公司外宣网站新闻稿范文300字
  • 自建网站 支付宝网络推广价格
  • ecs怎么做网站seo流量
  • html5网站开发语言佛山旺道seo
  • 小型网站如何做免费的网站推广平台
  • 58同城推广能免费做网站吗营销推广公司案例
  • 做网站前的准备工作seo整站优化系统
  • 京东网站建设评估搜索引擎原理
  • 网页版游戏排行榜女windows优化大师卸载不了
  • 用顶级域名做网站好吗网页开发工具
  • WordPress强制分享插件seo优化标题 关键词
  • 一键建站系统源码在哪里推广自己的产品
  • 营销策划公司名字宁波网站seo诊断工具
  • 维护网站需要多少钱网络软文是什么意思
  • 手机网站建设公司服务广州抖音seo
  • 安卓app安装石家庄seo排名外包
  • 做3d地形比较好的网站住房和城乡建设部
  • 做app找哪个网站吗沈阳seo推广
  • 梵克雅宝官网中国官方网站兰州网络推广优化服务
  • 西安企业网站开发哪家好厦门seo服务
  • 最高法律网站是做啥的大学生网页设计主题
  • 衢州市住房和城市建设局网站百度云盘官网登录入口