当前位置:网站首页>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;
}
边栏推荐
- 推荐⼀款超好⽤的UI⾃动化⼯具: UiAutomator2!
- Installing and using R in Anaconda
- [Unity] 二维洞穴地图随机生成
- Integer data type in C language (do you really understand it)
- Zombie‘s Treasure Chest(枚举)
- “蔚来杯“2022牛客暑期多校训练营2 G.[Link with Monotonic Subsequence] 分块构造
- Prime Ring Problem
- The SQL script generated by powerdispatcher model runs incorrectly
- There is no setter method in grpc list under flutter. How to use related attributes
- Network layer 2 and layer 3 forwarding
猜你喜欢

Leetcode 537. complex multiplication (netizens' thoughts, ashamed)

There is no setter method in grpc list under flutter. How to use related attributes

Google gson usage details

Handler message mechanism - FWK layer

What should I do when my blog is attacked by hackers?

01. MySQL transaction isolation level and concurrent database access

推荐系统-协同过滤在Spark中的实现

Recommend a super good UI automation tool: uiautomator2!
![[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
随机推荐
劳驾问一下各位老师 oracle 到pg cdc oracle 那边字段大写 pg 这边小写 同
Go operation excel library excel use
"Weilai Cup" 2022 Niuke summer multi school training camp 2 i.[let fat tension] matrix multiplication j.[link with arithmetic progression] linear regression
AUTOCAD——计算面积的方法
我mysql to mysql数据表同步,代码上只有写在第一个顺序上的生效 其余的不生效,这个可能是
华为无线设备WDS配置命令
Stack Title: basic calculator
2022 love analysis ― bank digitalization practice report
【独立站建设】shopify卖家:学会这几点,网上商店销量翻倍!
旅行 (拆点分层)
Software group verification
推荐⼀款超好⽤的UI⾃动化⼯具: UiAutomator2!
Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation
pdf. JS introduction
When everything can be metauniverse, the development of metauniverse seems to have entered a new stage of development
言语理解中心理解总结
8. Learn Mysql to create data tables
Prime Ring Problem
There is no setter method in grpc list under flutter. How to use related attributes
"Weilai Cup" 2022 Niuke summer multi school training camp 2 h.[take the elevator] maintenance section