fix: main ac
This commit is contained in:
@@ -9,9 +9,6 @@ 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;
|
||||
@@ -24,40 +21,13 @@ int main(){
|
||||
|
||||
sort(intervals.begin(), intervals.end(), comp);
|
||||
|
||||
// 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;
|
||||
int lastCovered = intervals[0].second;
|
||||
int count = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (lastCovered < intervals[i].second) {
|
||||
lastCovered = intervals[i].second;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (curInterval == j) curInterval++;
|
||||
else curInterval = j;
|
||||
}
|
||||
|
||||
cout << count << endl;
|
||||
|
||||
Reference in New Issue
Block a user