#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
struct Section
{
string name;
double start;
double end;
Section(string n,int s, int e)
{
name=n;
start = s;
end = e;
}
};
void printNonIntersectSections(list<Section> &nonIntersectSections)
{
for (list<Section>::iterator i = nonIntersectSections.begin(); i != nonIntersectSections.end(); ++i)
{
cout<<i->name<<" "<<i->start<<"~"<<i->end<<endl;
}
cout << endl;
}
void printNonIntersectSections(vector<Section> §ions)
{
for (vector<Section>::iterator i = sections.begin(); i != sections.end(); ++i)
{
cout<<i->name<<" "<<i->start<<"~"<<i->end<<endl;
}
cout << endl;
}
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)
{
if (nonIntersectSections.size() > 0)
{
if (nonIntersectSections.back().end <= i -> start && sectionEnd >= i -> end)
{
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("高数",8.00, 9.30));
sections.push_back(Section("电子商务",8.30, 10.00));
sections.push_back(Section("数据结构", 9.30, 12.00));
sections.push_back(Section("计算机基础", 10.00, 11.00));
sections.push_back(Section("C语言", 11.30, 12.30));
list<Section> nonIntersectSections;
findNonIntersectSections(sections, nonIntersectSections);
cout<<"全部课程如下:"<<endl<<endl;
printNonIntersectSections(sections);
cout<<"选中的课程有:"<<endl<<endl;
printNonIntersectSections(nonIntersectSections);
}