当前位置:网站首页>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 !
边栏推荐
- 非递归实现二叉树的前、中、后序遍历
- kubernetes 二进制安装(v1.20.15)(八)部署 网络插件
- New project construction environment method
- 牛客刷题——求最小公倍数
- Niu Ke's questions -- two sorting methods
- kubernetes 二进制安装(v1.20.15)(九)收尾:部署几个仪表盘
- Introduction to basic use and pit closure of BigDecimal
- 2023年西安交通大学管理学院MPAcc提前批面试网报通知
- 平衡搜索二叉树——AVL树
- An adaptive chat site - anonymous online chat room PHP source code
猜你喜欢
Mysql深入完全学习---阶段1---学习总述

全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例

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

「案例分享」基于 AM57x+ Artix-7 FPGA开发板——PRU开发手册详解
Uploading and downloading of necessary files in development

Niu Ke swipes the question -- converting a string to an integer

*Jetpack notes understanding of lifecycle ViewModel and livedata

Uni app Muke hot search project (I) production of tabbar

Specific methods for JS to realize full screen display

*Use of jetpack notes room
随机推荐
Niu Ke brushes the question - no two
KMP! You deserve it!!! Run directly!
牛客刷题——不要二
Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解
Téléchargement et téléchargement des fichiers nécessaires au développement
SAP BTP 上 workflow 和 Business Service 的 project 管理
Force buckle 31 next arrangement
Dynamic explosion effect
Do you know that public fields are automatically filled in
MySQL in-depth and complete learning - stage 1 - overview of learning
牛客刷题——求最小公倍数
Construct enemy tanks
2022-2023年西安交通大学管理学院MEM提前批面试网报通知
Realize that you can continue to play
New project construction environment method
手把手教你学会FIRST集和FOLLOW集!!!!吐血收藏!!保姆级讲解!!!
美国通胀率8.6%创41年历史新高!通胀高烧不退?股票、加密市场先跌为敬!
SQL注入漏洞学习之一:phpstudy集成环境搭建DVWA靶场
map和set