当前位置:网站首页>cf:G. Count the Trains【sortedset + bisect + 模拟维持严格递减序列】
cf:G. Count the Trains【sortedset + bisect + 模拟维持严格递减序列】
2022-06-11 18:37:00 【白速龙王的回眸】

分析
这道题每次都需要求当前速度下,严格递减序列的长度
如果我们每次分开求,就会超时
所以我们要根据每次更改来更新新得递减序列的长度
首先我们根据一开始的速度得到一开始严格递减序列(我们记录的是idx)
然后我们开始修改,如果当前修改使得比前一个记录idx的val要严格小,我们将当前修改的id放入sortedset中
怎么看前一个记录的idx呢,通过bisect_right(s, x) - 1得到即可~
当然如果当前idx在一开头的位置,直接加入即可
加入了之后,要进行后续不服务严格递减的idx的删除
首先我们找到当前加入的x的下一个位置bisect_right(s,x)
然后用双指针找到需要删除的范围l和r 只要满足当前idx的val不小于x对应的val就r++
最后cut掉s[l:r]即可
这里就是sortedset中砍掉后面不符合要求的一段,直接del即可
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)
总结
cf中不能用sortedcontainers所以不知道对不对
思路是对的
然后list一种快速输出是print(*list)
然后sortedset可以add discard 和del[i:j]一般就是这几个用法
因为py默认的set是无序就很恼火!
边栏推荐
- 基于华为云图像识别标签实战
- Quanzhi T3 development board (4-core arm cortex-a7) - detailed explanation of logo display during system startup
- Teach you how to learn the first set and follow set!!!! Hematemesis collection!! Nanny level explanation!!!
- v-for循环遍历
- Specific methods for JS to realize full screen display
- BigDecimal基本使用与闭坑介绍
- Niuke's brush question -- judgment of legal bracket sequence
- 手把手教你学会FIRST集和FOLLOW集!!!!吐血收藏!!保姆级讲解!!!
- Introduction to basic use and pit closure of BigDecimal
- Construct enemy tanks
猜你喜欢

Common operations of Visio

2022-2023年西安交通大学管理学院MEM提前批面试网报通知

基于华为云图像识别标签实战

Signal processing and capture
MySQL in-depth and complete learning - stage 1 - overview of learning

视觉SLAM十四讲笔记-10-1

v-for循环遍历

Monitoring loss functions using visdom
Téléchargement et téléchargement des fichiers nécessaires au développement

Surveillance des fonctions de perte avec visdom
随机推荐
New project construction environment method
Monitoring loss functions using visdom
Uploading and downloading of necessary files in development
Introduction to basic use and pit closure of BigDecimal
Quanzhi Technology T3 Development Board (4 Core ARM Cortex - A7) - mqtt Communication Protocol case
牛客刷题——part6
Realize that you can continue to play
力扣刷题——二叉树的最近公共祖先
JS实现全屏展示的具体方法
Swagger2简单使用
5 minutes to understand the red, blue and purple in the attack and defense drill
WWDC22 开发者需要关注的重点内容
*Jetpack notes understanding of lifecycle ViewModel and livedata
Let our tanks move happily
非递归实现二叉树的前、中、后序遍历
Make a static tank
牛客刷题——不要二
* Jetpack 笔记 使用DataBinding
Force deduction questions -- create a string based on a binary tree
Visual slam lecture notes-10-1