#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include<string>
using namespace std;
struct Section {
string name;
float start;
float end;
Section(string a,float s, float e) {
name =a;
start = s;
end = e;
}
};
void printNonIntersectSections(list<Section> &nonIntersectSections) {
for (list<Section>::iterator i = nonIntersectSections.begin(); i != nonIntersectSections.end(); ++i)
{
cout<<i->name;
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 minEnd = 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;
minEnd = i;
sectionEnd = i -> end;
}
}
else
{
if (sectionEnd >= i -> end)
{
minEnd = i;
sectionEnd = i -> end;
}
}
}
if ( minEnd != sections.end())
{
selected = minEnd;
nonIntersectSections.push_back(*selected);
}
++selected;
}
}
int main(){
vector<Section> sections;
sections.push_back(Section("高数",800, 930));
sections.push_back(Section("电子商务",830, 1000));
sections.push_back(Section("数据结构",930, 1200));
sections.push_back(Section("计算机基础",1000, 1100));
sections.push_back(Section("C语言",1130, 1230));
list<Section> nonIntersectSections;
findNonIntersectSections(sections, nonIntersectSections);
printNonIntersectSections(nonIntersectSections);
}