fix: updated AC solution

This commit is contained in:
2026-02-19 22:21:26 -03:00
parent 1d0494dc76
commit 81a0762a86
75 changed files with 1118737 additions and 1118750 deletions

View File

@@ -9,6 +9,9 @@ bool comp(pair<int, int> &a, pair<int, int> &b) {
return a.second > b.second;
}
bool intersecting(pair<int, int> &a, pair<int, int> &b) {
return a.second >= b.first;
}
int main(){
int n; cin >> n;
@@ -21,13 +24,40 @@ int main(){
sort(intervals.begin(), intervals.end(), comp);
int lastCovered = intervals[0].second;
int count = 1;
for (int i = 1; i < n; i++) {
if (lastCovered < intervals[i].second) {
lastCovered = intervals[i].second;
// first sort by start time, if two intervals start at the same time the one with the highest end time comes first
// always include the first interval
// keep track of the last included interval so far
// while there are intervals intersecting with the last included interval take the one with the highest ending value
//
// if there is some intersecting interval check if its end time is greater than the last included interval end time, if so include it and update count, else repeat process from the last intersecting interval
// if no intervals intersect with the last included interval then just process the next interval in order
// in both cases update the count by 1
int curInterval = 1, lastIncludedInterval = 0, count = 1;
while (curInterval < n) { // curInterval holds the first interval thaat was not processed yet
int bestIntersectingInterval = lastIncludedInterval, j = curInterval;
while (j < n && intersecting(intervals[lastIncludedInterval], intervals[j])) {
int bestEndTime = intervals[bestIntersectingInterval].second;
int endTime = intervals[j].second;
if (endTime > bestEndTime) {
bestIntersectingInterval = j;
}
j++;
}
if (bestIntersectingInterval != lastIncludedInterval) {
lastIncludedInterval = bestIntersectingInterval;
count++;
} else {
if (j == n) break; // this interval alredy covers every remaining interval
lastIncludedInterval = j;
count++;
}
if (curInterval == j) curInterval++;
else curInterval = j;
}
cout << count << endl;