Love丶FFC's Blog

CCF-CSP_20180302:碰撞的小球

2019-12-01 09:30:29
阅读:841   •   评论:8
标签:,

问题描述

数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处。有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒。
  当小球到达线段的端点(左端点或右端点)的时候,会立即向相反的方向移动,速度大小仍然为原来大小。
  当两个小球撞到一起的时候,两个小球会分别向与自己原来移动的方向相反的方向,以原来的速度大小继续移动。
  现在,告诉你线段的长度L,小球数量n,以及n个小球的初始位置,请你计算t秒之后,各个小球的位置。提示  因为所有小球的初始位置都为偶数,而且线段的长度为偶数,可以证明,不会有三个小球同时相撞,小球到达线段端点以及小球之间的碰撞时刻均为整数。
  同时也可以证明两个小球发生碰撞的位置一定是整数(但不一定是偶数)。

输入格式

输入的第一行包含三个整数n, L, t,用空格分隔,分别表示小球的个数、线段长度和你需要计算t秒之后小球的位置。
  第二行包含n个整数a1, a2, …, an,用空格分隔,表示初始时刻n个小球的位置。

输出格式

输出一行包含n个整数,用空格分隔,第i个整数代表初始时刻位于ai的小球,在t秒之后的位置。

样例输入

3 10 5
4 6 8

样例输出

7 9 9

样例说明

初始时,三个小球的位置分别为4, 6, 8。

一秒后,三个小球的位置分别为5, 7, 9。

两秒后,第三个小球碰到墙壁,速度反向,三个小球位置分别为6, 8, 10。

三秒后,第二个小球与第三个小球在位置9发生碰撞,速度反向(注意碰撞位置不一定为偶数),三个小球位置分别为7, 9, 9。

四秒后,第一个小球与第二个小球在位置8发生碰撞,速度反向,第三个小球碰到墙壁,速度反向,三个小球位置分别为8, 8, 10。

五秒后,三个小球的位置分别为7, 9, 9。

样例输入

10 22 30
14 12 16 6 10 2 8 20 18 4

样例输出

6 6 8 2 4 0 4 12 10 2

数据规模和约定

对于所有评测用例,1 ≤ n ≤ 100,1 ≤ t ≤ 100,2 ≤ L ≤ 1000,0 < ai < L。L为偶数。
  保证所有小球的初始位置互不相同且均为偶数。

编程语言:Python

解题思想:

这道题的解题思路非常清晰,看了一遍题目就开始写,第一遍就AC了

1.对每个时刻每个球的位置进行一次判断,判断球是否在两个端点,以及球是否有碰撞(重合)情况

2.因为球每次运动都是1个距离,因此可以将向左运动进行-1,向右运动进行+1

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

IDE代码如下:

  1. NOB, Length, Time = map(int, input().split()) # 输入小球的个数,线段的长度,小球运动的时长
  2. Ball = [0 for i in range(NOB)]
  3. Ball = input().split()
  4. BallDirect = [1 for i in range(NOB)] # 小球运动的方向,初始为都为向右运动
  5. # 将列表的值转为int型
  6. for i in range(NOB):
  7. Ball[i] = int(Ball[i])
  8.  
  9. for i in range(Time):
  10. for j in range(NOB):
  11. if Ball[j] == 0:
  12. BallDirect[j] = 1
  13. elif Ball[j] == Length:
  14. BallDirect[j] = -1
  15. for k in range(j + 1, NOB):
  16. if Ball[j] == Ball[k]:
  17. BallDirect[j] *= -1
  18. BallDirect[k] *= -1
  19. for j in range(NOB):
  20. Ball[j] += BallDirect[j]
  21.  
  22. for i in range(NOB):
  23. if i != NOB - 1:
  24. print(Ball[i], end=" ")
  25. else:
  26. print(Ball[i])

评论板

共有 8 条评论

  1. Abupguele

    In one embodiment, reaction of the carbohydrate portion of a glycosylated antibody with either galactose oxidase or sodium meta periodate may yield carbonyl aldehyde and ketone groups in the protein that can react with appropriate groups on the drug Hermanson, Bioconjugate Techniques cialis no prescription Murfreesboro Pharmaceutical Nursing Supply Teva Pharmaceutical Industries Ltd

  2. Outlils

    April 13, 2022 at 9 45 pm cialis on sale in usa Cells were grown in 96 well plates at 4 10 3 cells per well and treated with 20 Ој M TMX and or 3 Ој M GMT for 48 h

  3. cheap jordans

    Read reviews and was a little hesitant since I had already inputted my order. or possibly but thank god, I had no issues. exactly like 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.
    cheap jordans https://www.realjordansretro.com/

  4. cheap jordans online

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

  5. invoimi

    B Hazard ratios of Oncotype DX usage stratified by tumor stage between 2004 and 2015 from univariate Cox regression models generic cialis from india

  6. invoimi

    legit cialis online First, what types of study designs, incentives, or methods can be used to encourage better evidence development for oncology orphan drugs

  7. gUVwaBi

    Breast Reconstruction with the free TRAM or DIEP flap patient selection, choice of flap, and outcome bluechew viagra

  8. CahPby

    Describe your experience with your nurse at Indiana Fertility Institute buy cialis usa Since the use of the minimal number of transgene copies that allow efficient recombination upon tamoxifen induction is desired, we only characterized for the two copy line the activity in absence of tamoxifen background in more detail

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名: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 -

已运行:

访问量:511363