POJ 2376 Cleaning Shifts
来源:互联网 发布:网吧知乎 编辑:程序博客网 时间:2024/05/29 12:35
题意:给n个区间,求可以覆盖区间[1,n]的最少区间数,根据题干,区间[1,2]、[3,4]可以认为是可衔接的
解题思路:贪心.将n个区间的起始点和终止点用结构体存储,并按照起始点排序。如果最开始的起始点>1就输出-1,否则定义temp、nowt,选出可以与temp相连接的所有区间中终止点最大的区间,用nowt更新终止点最大的区间,找到终止点最大的区间后将nowt也就是当前的终止点赋值给temp,这一步很必要,然后在新的temp的基础上继续选择可连接的终止点最大的区间,每选出一个区间ans++
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <set>using namespace std;struct P{ int st,ed;} p[25005];bool cmp(P p1,P p2){ if(p1.st==p2.st)return p1.ed<p2.ed; return p1.st<p2.st;}int main(){ int n,t; cin>>n>>t; for(int i=0; i<n; i++) { cin>>p[i].st>>p[i].ed; } sort(p,p+n,cmp); int flag=0; if(p[0].st>1)cout<<-1<<endl; else { int i=0,ans=0,nowt=0,temp=0; while(i<n&&nowt<t) { ans++; if(p[i].st>temp+1) { flag=1; break; } else { while(i<n&&p[i].st<=temp+1)//找出所有可以与temp连接的最长的区间 { if(p[i].ed>nowt) { nowt=p[i].ed; } i++; } temp=nowt; } } if(nowt<t||flag==1)cout<<-1<<endl; else cout<<ans<<endl; } return 0;}
阅读全文
0 0
- POJ 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ-2376-Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ-2376 Cleaning Shifts
- POJ 2376Cleaning Shifts
- POJ 2376 Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ - 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- poj 2376 Cleaning Shifts
- 表格与框架
- 获取手机中应用程序的包名
- [bzoj]4590: [Shoi2015]自动刷题机
- react实战:一个极简易的新闻站点
- JS的跨域的几种方式
- POJ 2376 Cleaning Shifts
- 华为提前批机试题目
- noip2003 数字游戏
- java数组转矩阵及部分操作
- J
- Ubuntu压缩、解压缩文件
- 瀑布流加载图片
- C++ 链接指示器:extern ”C"
- Ubuntu下使用桂电出校器(实验室版教程)