区间相交问题
2015-11-25 10:24
274 查看
#include <iostream> #include <fstream> #include <algorithm> using namespace std; /* 与活动安排问题类似,每次选取右端点坐标最小的闭区间,保留该闭区间,并将与其相交 的闭区间删除 */ struct Zone { int start; int end; bool operator < (const Zone &z) const { return end < z.end; } }; Zone zone[50]; int greedy(int n) { sort(zone, zone+n); int i; int count = 0; int temp = zone[0].end; for(i=1; i<n; i++) { if(zone[i].start <= temp) count++; else temp = zone[i].end; } return count; } int main() { ifstream fin("区间相交.txt"); cout << "输入区间数:"; int n; fin >> n; cout << n; cout << "\n输入各个区间端点:\n"; int i; for(i=0; i<n; i++) { fin >> zone[i].start >> zone[i].end; if(zone[i].start > zone[i].end) swap(zone[i].start, zone[i].end); cout << zone[i].start << " " << zone[i].end << endl; } cout << "去掉的最少闭区间数为:" << greedy(n); cout << endl; fin.close(); return 0; }
相关文章推荐
- c/c++中的strcpy问题
- Windows下MySQL zip版的简单安装
- Linux下SVN安装和配置
- poj 3041 Asteroids 最小顶点覆盖 && 二分匹配
- poj2376
- 递归调用,汉诺塔问题
- Spark初体验(1)--SparkPi详解
- 计量模块 Ceilometer 介绍及优化
- 取经人
- jQuery官网下载步骤
- 发送手机验证码
- Git详解之三 Git分支
- C#实现流程图设计器
- IOS 和 Android Unity游戏引擎的集成AdMob新版教程
- mac 下安装 plink
- Anaconda2-spyder中Ipython console显示connecting to kernel
- Unix目录结构的来历
- Android studio 2.0 预览版发布
- 【转】CSS网页布局DIV水平居中的各种方法
- windos7升级powershell到4.0版本步骤