贪心算法
2016-03-07 10:34
357 查看
1 活动安排问题
#include <iostream> #include <vector> // vector #include <iterator> //ostream_iterator #include <algorithm> //stable_sort using namespace std; // 活动信息 class Activity { public: Activity(int beg, int end) { m_tEnd = end; m_tBegin = beg; m_tShow = true; } friend bool operator < (const Activity & a,const Activity &b) { return a.m_tEnd < b.m_tEnd; } bool operator >= (const Activity & b) { return this->m_tBegin >= b.m_tEnd; } friend ostream & operator << (ostream & out, const Activity & act){ if ( act.m_tShow == true) { out << act.m_tBegin << " " << act.m_tEnd << endl; } return out; } private: int m_tEnd; int m_tBegin; bool m_tShow; //是否被安排 public: friend class Arrange; //友元类,可以更改本类属性 }; //活动安排类 class Arrange { public: Arrange(vector<Activity> &actVec){ m_actVec = actVec; } void arrangeActivity() { // 按照结束时间早晚排序 _sortWithEnd(); auto comp = m_actVec.begin(); for (auto it = m_actVec.begin()+1; it != m_actVec.end(); it++) { if (*it >= *comp) { comp = it; } else it->m_tShow = false; } // 输出 copy(m_actVec.begin(), m_actVec.end(), ostream_iterator<Activity>(cout, "")); } private: vector<Activity> m_actVec; private: void _sortWithEnd() { stable_sort(m_actVec.begin(), m_actVec.end()); // copy(m_actVec.begin(), m_actVec.end(), ostream_iterator<Activity>(cout,"")); } }; int main() { vector<Activity> actVec; actVec.push_back(Activity(1, 4)); actVec.push_back(Activity(3, 5)); actVec.push_back(Activity(0, 6)); actVec.push_back(Activity(5, 7)); actVec.push_back(Activity(3, 8)); actVec.push_back(Activity(5, 9)); actVec.push_back(Activity(6, 10)); actVec.push_back(Activity(8, 11)); actVec.push_back(Activity(8, 12)); actVec.push_back(Activity(2, 13)); actVec.push_back(Activity(12, 14)); Arrange arr(actVec); arr.arrangeActivity(); return 0; }
相关文章推荐
- Microsoft visual C++ 2010学习版中C语言基本数据类型所占字节大小(系统为64位win8.1)
- Cookie禁用了,Session还能用吗?
- 访问WebRoot
- 软件/环境的注册码
- 推荐大家使用字符串直接赋值
- Mat操作
- dllhost.exe占用内存过高的问题
- 网络状态都正常,ping 网关能ping通,但不正常,出现“?”,并且不能进行域名解析
- 网络状态都正常,ping 网关能ping通,但不正常,出现“?”,并且不能进行域名解析
- 使用AndroidMultiChannelBuildTool进行多渠道批量打包
- Java包装类 & Java7的 compare()方法
- linux下文件被删除的恢复
- iOS高效添加圆角效果实战讲解
- 生成Kindle可读的mobi和PDF电子书
- key_buffer_size
- CentOS怎样强制卸载PHP以及自定义安装PHP
- VW Jetta A6
- mysql Got error 28 from storage engine错误,Mysql导致tmp目录空间耗尽问题
- JSP学习资料
- 深度优先搜索