POJ 2376 Cleaning Shifts
2016-01-29 00:37
429 查看
题意
找到能覆盖全部区间范围的最小子区间数,没有则输出-1题解
对区间按起点升序排序,起点相同则按终点升序排序。(排序后才可以用贪心,因为起点大的在已覆盖区间中时,那么起点小的一定也在,此时只要比较谁的终点大即可)寻找最佳的子区间(区间的终点最远且起点在当前已覆盖区间的范围内),终止条件是:当前子区间的下一个子区间不在已覆盖区间范围内
更新当前已覆盖区间(变量now)
注意: 在区间的条件是:[now.from, now.to+1]
代码
#include <iostream> #include <algorithm> #define MAXN 100000 #define INF 0x7fffffff using namespace std; struct Line { int from; int to; }; Line line[MAXN]; struct compare { bool operator()(Line l1,Line l2) { if(l1.from < l2.from) return true; if(l1.from == l2.from && l1.to < l2.to) return true; else return false; } }; int n,m; int solve() { int cnt = 0; int flag = 0; line .from = INF; Line now; now.from = 0; now.to = 0; int last = 0; for(int i = 0 ;i < n; i++) { if(line[i].from <= now.to + 1 && line[i].to > last) //find a line with max len in interval { last = line[i].to; flag = 1; } if(flag && line[i+1].from > now.to + 1) //flag means has find a proper interval { now.to = last; flag = 0; cnt++; } } if(last < m) cnt = -1; return cnt; } int main() { cin>>n>>m; for(int i = 0; i < n; i++) cin>>line[i].from>>line[i].to; sort(line,line+n, compare()); cout<< solve() <<endl; }
相关文章推荐
- POJ2186(有向图缩点)
- poj1681 高斯消元
- poj1681 高斯消元
- JAVA编译时与运行时
- 边框和背景
- java数组实现栈
- 使用LaTeX beamer文档类时warning: ``mathserif” isobsolete
- Java初学者1-Java环境及IDE
- 【Java SE】————如何做到跨平台性的?
- 上下滚动字幕
- 使用LaTeX beamer文档类时错误:\trans@languagepath ->\languagename
- 1到整数n中 出现1的次数,分,最高位,非最高位,两次数相加。递归位数减1.
- poj 1830 高斯消元
- poj 1830 高斯消元
- Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
- NSAttributedString
- 虎跃直播上线咯
- 查看linux函数栈
- 虎跃直播上线咯
- 密码学初级教程(三)公钥密码RSA