#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
struct Section {
int start;
int end;
Section(int s, int e) {
start = s;
end = e;
}
};
void printNonIntersectSections(list<Section> &nonIntersectSections) {
for (list<Section>::iterator i = nonIntersectSections.begin(); i != nonIntersectSections.end(); ++i)
{
cout << "[" << i->start << "," << i->end << "] ";
}
cout << endl;
}
bool compareSectionLength(Section left, Section right) {
return (left.end - left.start) < (right.end - right.start);
}
bool compareSectionStart(Section left, Section right) {
return left.start < right.start;
}
void findNonIntersectSections(vector<Section> §ions, list<Section> &nonIntersectSections) {
sort(sections.begin(), sections.end(), compareSectionStart);
vector<Section>::iterator selected = sections.begin();
while(selected != sections.end())
{
int sectionEnd = selected->end;
vector<Section>::iterator minEndIterator = sections.end();
for(vector<Section>::iterator i = selected; i != sections.end(); ++i)
{
cout << "start: " << i -> start << " end: " << i -> end << endl;
cout << sectionEnd << endl;
cout << nonIntersectSections.size() << endl;
if (nonIntersectSections.size() > 0)
{
if (nonIntersectSections.back().end <= i -> start && sectionEnd >= i -> end)
{
cout << "start: " << nonIntersectSections.back().start << " end: " << nonIntersectSections.back().end << endl;
minEndIterator = i;
sectionEnd = i -> end;
}
}
else
{
if (sectionEnd >= i -> end)
{
minEndIterator = i;
sectionEnd = i -> end;
}
}
}
if ( minEndIterator != sections.end())
{
selected = minEndIterator;
nonIntersectSections.push_back(*selected);
}
++selected;
}
}
int main(){
vector<Section> sections;
sections.push_back(Section(6, 8));
sections.push_back(Section(2, 4));
sections.push_back(Section(3, 5));
sections.push_back(Section(1, 5));
sections.push_back(Section(5, 9));
sections.push_back(Section(8, 10));
list<Section> nonIntersectSections;
findNonIntersectSections(sections, nonIntersectSections);
printNonIntersectSections(nonIntersectSections);
}