Love丶FFC's Blog

CCF-CSP_20170302:学生排队

2019-11-27 09:49:19
阅读:695   •   评论:8
标签:,

问题描述

体育老师小明要将自己班上的学生按顺序排队。他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面,然后进行多次调整。一次调整小明可能让一位同学出队,向前或者向后移动一段距离后再插入队列。
  例如,下面给出了一组移动的例子,例子中学生的人数为8人。
  0)初始队列中学生的学号依次为1, 2, 3, 4, 5, 6, 7, 8;
  1)第一次调整,命令为“3号同学向后移动2”,表示3号同学出队,向后移动2名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 5, 3, 6, 7, 8;
  2)第二次调整,命令为“8号同学向前移动3”,表示8号同学出队,向前移动3名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 5, 8, 3, 6, 7;
  3)第三次调整,命令为“3号同学向前移动2”,表示3号同学出队,向前移动2名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 3, 5, 8, 6, 7。
  小明记录了所有调整的过程,请问,最终从前向后所有学生的学号依次是多少?
  请特别注意,上述移动过程中所涉及的号码指的是学号,而不是在队伍中的位置。在向后移动时,移动的距离不超过对应同学后面的人数,如果向后移动的距离正好等于对应同学后面的人数则该同学会移动到队列的最后面。在向前移动时,移动的距离不超过对应同学前面的人数,如果向前移动的距离正好等于对应同学前面的人数则该同学会移动到队列的最前面。

输入格式

输入的第一行包含一个整数n,表示学生的数量,学生的学号由1到n编号。

第二行包含一个整数m,表示调整的次数。

接下来m行,每行两个整数p, q,如果q为正,表示学号为p的同学向后移动q,如果q为负,表示学号为p的同学向前移动-q。

输出格式

输出一行,包含n个整数,相邻两个整数之间由一个空格分隔,表示最终从前向后所有学生的学号。

样例输入

8
3
3 2
8 -3
3 -2

样例输出

1 2 4 3 5 8 6 7

评测用例规模与约定

对于所有评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 1000,所有移动均合法。

编程语言:Python

解题思想:

1.输入一条调整指令就进行一次调整

2.对向前调整和向后调整判断后进行覆盖操作

时间复杂度:O(n^3)

IDE代码如下:

  1. Number = int(input()) # 输入学生的人数
  2. Queue = [int(i) for i in range(1, Number + 1)] # 生成队伍列表
  3.  
  4. Adjust = int(input()) # 输入调整的次数
  5. for i in range(Adjust):
  6. AdjustStudentID, AdjustDistance = map(int, input().split()) # 输入调整位置的学号和调整距离
  7. for j in range(Number): # 遍历队伍
  8. if Queue[j] == AdjustStudentID: # 找到该学号学生对应的位置
  9. if AdjustDistance >= 0: # 向后调整
  10. Key = Queue[j] # 保留该位置的学号
  11. for k in range(j, j + AdjustDistance):
  12. Queue[k] = Queue[k + 1]
  13. elif AdjustDistance < 0: # 向前调整
  14. Key = Queue[j] # 保留该位置的学号
  15. for k in range(j, j + AdjustDistance, -1):
  16. Queue[k] = Queue[k - 1]
  17. Queue[j + AdjustDistance] = Key # 覆盖完毕后再将该位置的学号进行替换
  18. break
  19.  
  20. # 按题目要求输出
  21. for i in range(Number):
  22. if i != Number - 1:
  23. print(Queue[i], end=" ")
  24. else:
  25. print(Queue[i])
  26.  

评论板

共有 8 条评论

  1. Jorcift

    cialis 5 mg best price usa Clinical findings This disease can affect young and old cats age range 4 14 years but younger cats are more commonly affected

  2. louis vuitton outlet

    Read reviews and was a little hesitant since I had already inputted my order. maybe but thank god, I had no issues. for instance the received item in a timely matter, they are in new condition. in any event so happy I made the purchase. Will be definitely be purchasing again.
    louis vuitton outlet https://www.cheaplouisvuittonsale.com/

  3. cheap jordans for sale

    Read reviews and was a little hesitant since I had already inputted my order. aka but thank god, I had no issues. which includes received item in a timely matter, they are in new condition. blue jays so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans for sale https://www.cheapretrojordan.com/

  4. original louis vuittons outlet

    Read reviews and was a little hesitant since I had already inputted my order. and even but thank god, I had no issues. themselves received item in a timely matter, they are in new condition. direction so happy I made the purchase. Will be definitely be purchasing again.
    original louis vuittons outlet https://www.louisvuittonsoutletonline.com/

  5. jordans for cheap

    Read reviews and was a little hesitant since I had already inputted my order. or possibly a but thank god, I had no issues. such as received item in a timely matter, they are in new condition. direction so happy I made the purchase. Will be definitely be purchasing again.
    jordans for cheap https://www.realjordansretro.com/

  6. intuido

    levitra voltaren fitil fiyat 2020 Service providers around the world, and particularly inEurope where roaming charges are extremely high, must evolvetheir communications services rapidly to ensure that they stayrelevant and competitive how to buy cialis

  7. intuido

    Assuming equivalent biopotency between the gonadotrophin preparations, an assumption borne out by the literature Hedon et al can you buy priligy

  8. JbrqLYn

    Basal cell carcinoma is a type of skin cancer that causes a lump, bump or lesion to form on the outside layer of your skin epidermis propecia otc

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:496167