当前位置:网站首页>G. Count the trains (thought set + two points)
G. Count the trains (thought set + two points)
2022-07-26 01:48:00 【to cling】
Codeforces Round #797 (Div. 3)
Problem
There are n Coach compartment , Each car can reach a maximum speed driven by its own engine , Record in a sequence { a i } \{a_i\} { ai} in . The number of the carriage from left to right is 1 To n.
Now let these carriages move to the left as fast as possible , It is required that the actual speed of the carriage on the right cannot exceed that of the carriage on the left . In this way, several continuous cars with the same speed will be formed , Call such a section a train . For example, the sequence a=[10,13,5,2,6] The actual running speed of the corresponding carriage is [10,10,5,2,2], To form the 3 Train Festival .
When driving in the carriage , Received in turn m Bar information , Each message contains two numbers k and d, It means the first one k The top speed of the car has decreased due to the aging of the engine d. Please maintain the total number of trains in this process , Output each time you receive a message , The changes of each information to the array are saved .
Solution
- It is required that the actual speed of the carriage on the right should not exceed the actual speed of the carriage on the left ,
that , Eventually, a monotonic decreasing queue containing the first carriage will be formed .
The final number of trains is the length of the strictly decreasing sequence of the queue . - We store the subscript of this sequence in set in , For each inquiry , find set The largest of j Satisfy j ≤ k j \leq k j≤k
- If j = k, Insert set
- If a [ j ] > a [ k ] a[j] > a[k] a[j]>a[k] (a[k] Is the modified value ), Insert set
- Maintain the monotonicity of the queue after insertion , So we need to put The subscript is greater than j Of And Its value Greater than or equal to a[k] The deletion of .
Make the queue always strictly monotonically decreasing . - here set The size of is the result of this modification
- Time complexity O ( m l o g n ) O(mlogn) O(mlogn)
Code
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
set<int> s;
for (int i = 1; i <= n; i++)
if (s.empty() || a[i] < a[*s.rbegin()])
s.insert(i);
int k, d;
while (m--)
{
cin >> k >> d;
a[k] -= d;
auto it = s.upper_bound(k);
if (it == s.begin()) s.insert(k);
else
{
it = prev(it);
if (*it == k || a[*it] > a[k])
s.insert(k);
}
while (1)
{
it = s.upper_bound(k);
if (it != s.end() && a[*it] >= a[k])
s.erase(it);
else break;
}
cout << sz(s) << " ";
}
cout << "\n";
}
return 0;
}
边栏推荐
- IDEA如何快速删除最近打开的项目
- Digital transformation behind the reshaping growth of catering chain stores
- “蔚来杯“2022牛客暑期多校训练营2 个人题解集合
- Basic version of Google browser debugging tool (I)
- “蔚来杯“2022牛客暑期多校训练营2 I.[let fat tension] 矩阵乘法 J.[Link with Arithmetic Progression]线性回归
- 图像批处理高斯滤波降噪+峰值信噪比计算
- Silicon Valley classroom - official account cloud on demand Silicon Valley classroom microservice project practical notes
- What is cross site scripting (XSS)?
- Summary after reading "poor dad and rich dad"
- Understand Linglong platform unified access service from simple to deep Monet
猜你喜欢

Analysis of zeromq

Cross Site Request Forgery (CSRF): impact, examples, and Prevention
![[unity] random generation of two-dimensional cave map](/img/ec/7433f6e942fc3d0b03cac37ac87e7b.png)
[unity] random generation of two-dimensional cave map

How idea can quickly delete recently opened projects

Record a failure caused by a custom redis distributed lock

【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】

Understand Linglong platform unified access service from simple to deep Monet

销量连连夺冠,五菱的成功秘诀只有低价吗?

Silicon Valley classroom - official account cloud on demand Silicon Valley classroom microservice project practical notes

What should I do when my blog is attacked by hackers?
随机推荐
我mysql to mysql数据表同步,代码上只有写在第一个顺序上的生效 其余的不生效,这个可能是
Maximum side length of elements and squares less than or equal to the threshold (source: leetcode)
Mysql_ Note2
[unity] random generation of two-dimensional cave map
Dijkstra find the shortest path
Qtreewidget dotted line setting
How idea can quickly delete recently opened projects
ABC find 4-cycle (pigeon nest theorem)
在Anaconda 中安装和使用R
“蔚来杯“2022牛客暑期多校训练营2 H.[Take the Elevator] 维护线段
How to display numbers / English time in Excel
Three modes of CPU
Google gson usage details
“蔚来杯“2022牛客暑期多校训练营2 K.[Link with Bracket Sequence I] 括号序列 DP
There is no setter method in grpc list under flutter. How to use related attributes
FFT is used to estimate the image resampling factor after interpolation
怎么使用宝塔面板把node全栈项目部署到服务器上
快速创建题目文件夹
“蔚来杯“2022牛客暑期多校训练营2 D.[Link with Game Glitch] 二分答案+SPFA判环
Leetcode 537. complex multiplication (netizens' thoughts, ashamed)