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

2022 love analysis ― bank digitalization practice report

The sales volume has won the championship repeatedly. Is the secret of Wuling's success only low price?

图像批处理高斯滤波降噪+峰值信噪比计算
SQL injection tutorial: learn through examples

Record a failure caused by a custom redis distributed lock

Google gson usage details

元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))

Go operation excel library excel use

SVN version control branch and merge function use

Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation
随机推荐
SVN版本控制分支、合并功能使用
Leetcode 537. complex multiplication (netizens' thoughts, ashamed)
【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】
Travel (split points and layers)
Is it safe for Huatai Securities to open an account online? How to handle it?
I just test it
Google gson usage details
IDEA如何快速删除最近打开的项目
[Unity] 二维洞穴地图随机生成
Stack Title: basic calculator
Fiddler5+ lightning simulator 4.0 settings for app packet capturing
图像批处理高斯滤波降噪+峰值信噪比计算
旅行 (拆点分层)
How to modify Oracle functions?
What should I do when my blog is attacked by hackers?
C language enumeration types and unions
"Weilai Cup" 2022 Niuke summer multi school training camp 2 g.[link with monotonic subsequence] block structure
Typora expiration solution, what if typora can't open
NFT access tool premint was hacked and lost more than 370000 US dollars
华为无线设备WDS配置命令