python数据结构与算法练习-队列

  • Printer Queue
    • python实现
    • 需要注意的知识点:

Printer Queue

链接: link.

The only printer in the computer science students’ union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output.

Because some jobs are more important than others, the Hacker General has invented and implemented a simple priority system for the print job queue. Now, each job is assigned a priority between 1 and 9 (with 9 being the highest priority,

and 1 being the lowest), and the printer operates as follows.

◎ The first job J in queue is taken from the queue.

◎ If there is some job in the queue with a higher priority than job J, thenmove J to the end of the queue without printing it.

◎ Otherwise, print job J (and do not put it back in the queue).

In this way, all those importantmuffin recipes that the Hacker General is printing get printed very quickly. Of course, those annoying term papers that others are printing may have to wait for quite some time to get printed, but that’s life.

Your problem with the new policy is that it has become quite tricky to determine when your print job will actually be completed. You decide to write a program to figure this out. The program will be given the current queue (as a list of priorities) as well as the position of your job in the queue, and must then calculate how long it will take until your job is printed, assuming that no additional jobs will be added to the queue. To simplifymatters, we assume that printing a job always takes exactly one minute, and that adding and removing jobs from the queue is instantaneous.

输入格式
One line with a positive integer: the number of test cases (at most 100). Then for each test case:

◎ One line with two integers n and m, where n is the number of jobs in the queue (1 ≤ n ≤ 100) and m is the position of your job (0 ≤ m ≤ n −1). The first position in the queue is number 0, the second is number 1, and so on.

◎ One linewith n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The first integer gives the priority of the first job, the second integer the priority of the second job, and so on.

输出格式
For each test case, print one line with a single integer; the number of minutes until your job is completely printed, assuming that no additional print jobs will arrive.

样例

input
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
output
1
2
5

题意:本题题意就是在队列中优先级高的数值先输出的情况下,给定一个目标值以及目标队列,直到输出目标值时需要的次数。(一个输出记为一次)

思路
1.创建双端队列
2.用while循环,判定条件队列不为空即可
3.对于popleft(),如果其值小于当前最大值,则放队尾
4.如果等于最大值 则出队,计数加一

遇到的问题
1.队列中的值有与给定值相同的值的时候,怎么确定出队的是给定的值
2.怎么确定j及时变化的队列中当前优先级的最大值

(这两点是我自己做题时遇到的问题,特此记录以免忘记)
解决方案

  1. 采用zip记录给定队列以及目标值的下标,在将元组列表存为队列,这样while跳出循环的条件即可将popleft()[0][0]和popleft()[0]分别与当前最大值和目标值分别对比,满足即跳出循环。
  2. 每循环一次就把给定的输入列表排序一次,当最大值被打印出一次,便将最大值pop(),并重新排序列表,这样保证每次列表的[-1]都是当前最大值。

python实现

from collections import deque
case_n = int(input())while True:try:n,m = map(int,input().split())l = list(map(int,input().split()))def cul_time(n, m, l):num = []qu = deque()for i in range(n):num.append(i)zipped = zip(l, num)for i in zipped:qu.append(i)# qu = [(1,0),(2,1)(3,2)(4,3)]stop = (l[m], m)# stop = (3,2)sum = 0while len(qu) > 0:l.sort()ma = l[-1]if qu[0][0] == ma and qu[0] == stop:  # 跳出条件 当前最大值等于给定的目标值sum += 1breakif qu[0][0] == ma:qu.popleft()del (l[-1])sum += 1elif qu[0][0] < ma:qu.append(qu.popleft())return sumprint(cul_time(n, m, l))except:break

需要注意的知识点:

这里有用到了如下结构解题:一直处理多个案例到不再有输入
while True:try:''''''except:break

仅记录刷题过程以及需要注意的知识点,方便自己复习。

python数据结构与算法练习-Printer Queue相关推荐

  1. python数据结构和算法 时间复杂度分析 乱序单词检测 线性数据结构 栈stack 字符匹配 表达式求值 queue队列 链表 递归 动态规划 排序和搜索 树 图

    python数据结构和算法 参考 本文github 计算机科学是解决问题的研究.计算机科学使用抽象作为表示过程和数据的工具.抽象的数据类型允许程序员通过隐藏数据的细节来管理问题领域的复杂性.Pytho ...

  2. Python天天美味(32) - python数据结构与算法之堆排序

    1. 选择排序 选择排序原理是先选出最小的数,与第一个数交换,然后从第二个数开始再选择最小的数与第二个数交换,-- def selection_sort(data):     for i in ran ...

  3. python数据结构与算法13_python 数据结构与算法 (13)

    python 数据结构与算法 (13) 选择排序 (Selection sort) 是? 种简单直观的排序算法. 它的? 作原理如 下.? 先在未排序序列中找到最?(?)元素, 存放到排序序列的起始位 ...

  4. python leetcode_leetcode 介绍和 python 数据结构与算法学习资料

    for (刚入门的编程)的高中 or 大学生 leetcode 介绍 leetcode 可以说是 cs 最核心的一门"课程"了,虽然不是大学开设的,但基本上每一个现代的高水平的程序 ...

  5. Python数据结构与算法(二)栈和队列

    本系列总结了python常用的数据结构和算法,以及一些编程实现. 参考书籍:<数据结构与算法 Python语言实现> [美]Michael T.Goodrich, Roberto Tama ...

  6. Python数据结构与算法(一)列表和元组

    本系列总结了python常用的数据结构和算法,以及一些编程实现. 参考书籍:<数据结构与算法 Python语言实现> [美]Michael T.Goodrich, Roberto Tama ...

  7. Python 数据结构与算法——快排

    Python 数据结构与算法--选取算法(TopK) 如果说快速选取法所代表的是剪枝式的遍历操作--在递归树中找出一条通往第 k<script type="math/tex" ...

  8. Python 数据结构与算法——从二分图到寻找最大排列(Maximum Permutation)

    假设现在有 8 位有着特殊癖好的人去买票看电影,其中有一部分人得到了自己喜欢的座位,但大多数人并不满意.现在的问题是,如果这些人各自都有自己喜欢的座位(喜欢的座位有重叠,这是进行最大排列的前提,否则无 ...

  9. Python数据结构与算法(4.1)——递归

    Python数据结构与算法(4.1)--递归 0. 学习目标 1 递归 1.1 递归的基本概念 1.2 递归的重要性 1.3 递归三原则 1.4 递归的应用 2 递归示例 2.1 列表求和 2.2 汉 ...

最新文章

  1. java和C++ C比较
  2. 【jQuery插件】用jQuery Masonry快速构建一个pinterest网站布局(转)
  3. iOS native集成Weex js文件 不显示提示框问题
  4. 让Vs2013 完美支持EF6.1 Code First with Oracle 2015年12月24日更新
  5. 快讯|MIT遥控机器人;日本推出带摄像头的智能AI马桶;德国在线零售商Zalando引进拣货机器人等...
  6. jQuery慢慢啃之事件对象(十一)
  7. Spring 3.1,Cloud Foundry和本地开发
  8. 论文浅尝 | GEOM-GCN: Geometric Graph Convolutional Networks
  9. python笔记之Cmd模块
  10. 5动态显示图片_单片机入门 数码管的静态显示和动态显示 壁纸
  11. bison、lex版本不同造成的问题
  12. 如何选择数据可视化工具?
  13. Dubbo源码分析(一)Dubbo与Spring集成实例
  14. python基础之列表生成式和生成器
  15. Android 用 platform.pk8 和 platform.x509.pem 生成 keystore 系统签名文件
  16. CAD制图教程之CAD中怎么画剪刀楼梯?
  17. 计算机设备自动关机,怎样设置电脑自动关机时间,电脑设置自动关机时间-
  18. 【每日一题】P1551 亲戚
  19. mysql中signed是什么类型_mysql|unsigned 与 signed 类型
  20. Hadoop生态圈(十三)- Namenode元数据管理及各组件工作机制

热门文章

  1. 单总线CPU设计(现代时序)(HUST)
  2. 全球及中国数字每周可编程时间开关行业研究及十四五规划分析报告
  3. 公信宝CEO黄敏强:区块链让数据所有权回归大众
  4. opencv给图片加滑动条调节窗口大小
  5. 流利说懂你英语结班学习总结
  6. 首汽约车携手AWS,发布出行行业首个定制智能语音解决方案
  7. 禁用一个右下角广告弹窗的程序 popwndexe.exe
  8. 移动端:M站和APP的区别
  9. 手机上怎么打字才能快?
  10. MAC-将视频转成GIF