当前位置:网站首页>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 !
边栏推荐
猜你喜欢

力扣刷题——二叉树的层序遍历

V-for loop traversal
开发中必备的文件的上传与下载

Non recursive traversal of binary tree

2022-2023 MEM pre approval interview notice of School of management, Xi'an Jiaotong University

Cool visualization tool: first introduction to processing

cf:F. Shifting String【字符串按指定顺序重排 + 分组成环(切割联通分量) + 各组最小相同移动周期 + 最小公倍数】

今天睡眠质量记录60分
2022 coming of age ceremony, to every college entrance examination student

牛客刷题——Fibonacci数列
随机推荐
Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case
cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】
Swagger2 easy to use
ubuntu 安装psql以及运行相关命令
Labelme for image data annotation
KMP! You deserve it!!! Run directly!
动态爆炸效果
kubernetes 二进制安装(v1.20.15)(九)收尾:部署几个仪表盘
炫酷的可视化工具:processing 初识
Let our tanks move happily
力扣刷题——二叉树的层序遍历Ⅱ
Flink CDC 在大健云仓的实践
MBA, EMBA, MPa, MEM, pre interview (pre interview) time batch of national colleges and universities has been released (continuously updated) - Wendu Management Institute
全志科技T3開發板(4核ARM Cortex-A7)——MQTT通信協議案例
让我们的坦克欢快的动起来吧
*Use of jetpack notes room
Financial bank_ Introduction to collection system
对‘g2o::VertexSE3::VertexSE3()’未定义的引用
Quanzhi T3 development board (4-core arm cortex-a7) - detailed explanation of logo display during system startup
BottomSheetDialog 使用详解,设置圆角、固定高度、默认全屏等