CSES Problem Set
Traffic Lights
There is a street of length whose positions are numbered . Initially there are no traffic lights, but sets of traffic lights are added to the street one after another.
Your task is to calculate the length of the longest passage without traffic lights after each addition.
Input
The first input line contains two integers and : the length of the street and the number of sets of traffic lights.
Then, the next line contains integers : the position of each set of traffic lights. Each position is distinct.
Output
Print the length of the longest passage without traffic lights after each addition.
Constraints
Input:
Output:
Your task is to calculate the length of the longest passage without traffic lights after each addition.
Input
The first input line contains two integers and : the length of the street and the number of sets of traffic lights.
Then, the next line contains integers : the position of each set of traffic lights. Each position is distinct.
Output
Print the length of the longest passage without traffic lights after each addition.
Constraints
Input:
8 3
3 6 2
Output:
5 3 3
int x,n;cin>>x>>n;
set<pii>s;multiset<int>sz;s.insert({1,x});sz.insert(x);
for (int i = 0; i < n; i++){ int q; cin>>q; auto it=s.upper_bound({q,INT_MAX}); if(it!=s.begin())--it; pii p=*it; s.erase(it); s.insert({p.first,q}); s.insert({q+1,p.second}); auto itt=sz.find(p.second-p.first+1); sz.erase(itt); sz.insert(q-p.first+1); sz.insert(p.second-q); cout<<*sz.rbegin()<<" ";}
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.