-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ba064b
commit a03d89a
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main() { | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
|
||
int x; | ||
cin >> x; | ||
multiset<int> st; | ||
set<int> s = {0, x}; | ||
st.insert(x - 0); | ||
|
||
int n; | ||
cin >> n; | ||
for (int i = 0; i < n; i++) { | ||
int t; | ||
cin >> t; | ||
s.insert(t); | ||
|
||
auto location = s.find(t); | ||
auto hup = next(location); // Get the next element correctly | ||
|
||
if (hup != s.end() && location != s.begin()) { | ||
auto hi = next(location); | ||
auto lo = prev(location); | ||
|
||
st.erase(st.find(*hi - *lo)); // Remove the old segment | ||
st.insert(*hi - t); // Insert new segment | ||
st.insert(t - *lo); | ||
} | ||
|
||
cout << *st.rbegin() << ' '; // Print the largest segment | ||
} | ||
return 0; | ||
} |