avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

Counting Stars-Inter-Uni Programming Contest

  2024-10-03        
字数统计: 299字   |   阅读时长: 1min

Description:

https://interunia.unswcpmsoc.com/task/Counting%20Stars/

Thinking:

  • Given a set of star positions, we need to calculate the minimum number of stars required to explain these positions.
  • Meteors (i.e., moving stars) move from left to right, and from high to low (x coordinates increase, y coordinates decrease), without moving horizontally or vertically.
  • Each meteor may appear in multiple positions (because it moves), and the final cumulative image will show all the positions it has passed through.
  • The positions of fixed stars remain unchanged.

Therefore, we need to maintain a list of the last y-coordinate of the current chain.

  1. Sort the points: Sort them by increasing x coordinates.
  2. Initialization: Create an empty list last_y to store the last y-coordinate of each chain.
  3. Traverse the set of points:
    • For each point (x, y):
      • Use bisect_right to find the first position in last_y that is greater than the current y.
      • If the index is less than the length of last_y, it means there is an existing chain that can accommodate the current point, so we update the last y-coordinate of that chain to the current y.
      • If the index is equal to the length of last_y, it means no suitable chain is found, so we need to create a new chain and add the current y to last_y.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import bisect

n = int(input())
stars = []

for _ in range(n):
x, y = map(int, input().split())
stars.append((x, y))

# 按 x 坐标递增排序
stars.sort(key=lambda x: (x[0],))

last_y = []

for x, y in stars:
idx = bisect.bisect_right(last_y, y)
if idx < len(last_y):
last_y[idx] = y # 更新链的最后一个 y 坐标
else:
last_y.append(y) # 创建新的链

print(len(last_y))
  • Python
  • Contest
  • Binary Search

扫一扫,分享到微信

微信分享二维码
Horse Racing-Inter-Uni Programming Contest
9021_TUT_4
目录
  1. 1. Description:
  2. 2. Thinking:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号