当前位置:网站首页>Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]
Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]
2022-06-11 18:47:00 【Review of the white speed Dragon King】

analysis
This problem needs to be solved at the current speed every time , Strictly decreasing the length of the sequence
If we ask each time separately , It will time out
So we need to update the length of the new decreasing sequence according to each change
First, we get a strictly decreasing sequence at the beginning according to the speed at the beginning ( What we record is idx)
Then we began to modify , If the current modification makes the previous record idx Of val Be strict and small , We will modify the current id Put in sortedset in
What do you think of the previous record idx Well , adopt bisect_right(s, x) - 1 Just get it ~
Of course, if at present idx At the beginning , Just join in
After joining , Follow up services that do not strictly decrease idx The deletion of
First, we find the currently joined x Next position of bisect_right(s,x)
Then use the double pointer to find the range to be deleted l and r As long as you meet the current idx Of val Not less than x Corresponding val Just r++
Last cut fall s[l:r] that will do
Here is the sortedset Cut out the following paragraph that does not meet the requirements , direct del that will do
ac code(maybe)
from bisect import bisect_left, bisect_right
from sortedcontainers import SortedSet
for _ in range(int(input())):
#print('eg:')
empty = input()
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
s = SortedSet(list()) # store the head(idx) of every train
for i in range(n):
if not s or a[i] < a[s[-1]]: # keep decreasing
s.add(i)
#print(s)
ans = []
for i in range(m):
x, k = n, m = list(map(int, input().split()))
x -= 1 # start from 0
a[x] -= k
# find the id right before x in s
idx = bisect_right(s, x) - 1
#print(idx)
# if x at the very beginning
if idx == 0:
s.add(x)
# if a[x] less than a[idx], add it
elif a[x] < a[idx]:
s.add(x)
# delete the following id which val >= a[x]
# keep strictly decreasing
#print(s)
l = r = bisect_right(s, x)
while True:
#print(r)
if r == len(s) or a[s[r]] < a[x]:
break
r += 1
# delete sth
#print(l, r)
del s[l: r]
# print len
ans.append(len(s))
print(*ans)
summary
cf You can't use sortedcontainers So I don't know if it's right
The idea is right
then list A quick output is print(*list)
then sortedset Sure add discard and del[i:j] These are the general usages
because py default set If it's disorder, it's annoying !
边栏推荐
- Niuke's brush question -- judgment of legal bracket sequence
- 为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由
- Make a static tank
- 疫情下远程办公沟通心得|社区征文
- 给你一个项目,你将如何开展性能测试工作?
- 牛客刷题——求最小公倍数
- Construct enemy tanks
- Teach you how to learn the first set and follow set!!!! Hematemesis collection!! Nanny level explanation!!!
- * Jetpack 笔记 使用DataBinding
- 在 SAP Kyma 上部署一个 Go MSSQL API Endpoint
猜你喜欢
随机推荐
Two methods for matlab to save imshow drawing pictures to a specified folder
How to manually execute workflow on SAP BTP
V-for loop traversal
leetcode:剑指 Offer 56 - II. 数组中数字出现的次数 II【简单排序】
Force buckle 34 finds the first and last positions of elements in a sorted array
非递归实现二叉树的前、中、后序遍历
Niu Ke swipes the question -- converting a string to an integer
2022-2023年西安交通大学管理学院MEM提前批面试网报通知
uni-app 慕客热搜项目实战(一)tabBar的制作
Niuke brush questions part6
cf:E. Price Maximization【排序 + 取mod + 双指针+ 配对】
Niu Ke's question -- finding the least common multiple
SQL injection vulnerability learning 1: phpstudy integrated environment building DVWA shooting range
Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons
Dynamic explosion effect
Force deduction questions -- create a string based on a binary tree
力扣刷题——根据二叉树创建字符串
7-3 组合问题(*)
实现可以继续上局
Flink CDC 在大健云仓的实践






