POJ 2376 Cleaning Shifts 区间覆盖问题
http://poj.org/problem?id=2376
题目大意:
给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间。
思路:
贪心,按区间的起点找满足条件的并且终点尽量大的。
一开始排序还考虑了起点一样终点要大的,想了想没必要,因为只后都是直接扫描满足条件的。。
注意的是比如【4.5】下一次可以直接【6,10】这样。。。这一步坑了我好久。。。
后来一直WA,改了老半天。。。发现是大于等于少写了个等号,,,我去面壁。。。我要蹲墙角。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=25000+10;
struct cow{
int s,e;//start time and end time
bool operator <(const cow &x)const{
// if(s==x.s) // 也没必要这么排序。。
// return e>x.e;
return s<x.s;
}
}a[MAXN]; int main()
{
int n,T;
while(~scanf("%d%d",&n,&T))
{
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].s,&a[i].e); sort(a,a+n);
int s=0,e=0; //s必须从0开始,因为下面判断是s+1
int ans=0;
for(int i=0;i<n;i++)
{
if(s>=T) break; //少写了个等于。改了一小时。。。。。我去面壁
bool find=false;
for(int j=i;j<n;j++) //找到满足条件的结束时间最大的
{
if(a[j].s>s+1) break;
if(a[j].e>e) {
e=a[j].e;
i=j;
}
find=true;
}
if(!find)
{
ans=-1;
break;
}
ans++;
s=e;
}
if(s<T) ans=-1;
printf("%d\n",ans);
}
return 0;
}
POJ 2376 Cleaning Shifts 区间覆盖问题的更多相关文章
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- poj 3171 Cleaning Shifts(区间的最小覆盖价值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2743 Accepted: 955 De ...
随机推荐
- BZOJ——T 1113: [Poi2008]海报PLA
http://www.lydsy.com/JudgeOnline/problem.php?id=1113 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: ...
- iterator的使用和封个问题
这篇文章的内容还是不错的: http://www.cnblogs.com/zhuyf87/archive/2012/12/08/2808290.html for (vector<int>: ...
- [Python] Find available methods and help in REPL
For example you want to know what methods are available in Python for String, you can do : dir(" ...
- poj Transferring Sylla(怎样高速的推断一个图是否是3—连通图,求割点,割边)
Transferring Sylla 首先.什么是k连通图? k连通图就是指至少去掉k个点使之不连通的图. 题目: 题目描写叙述的非常裸.就是给你一张图要求你推断这图是否是3-连通图. 算法分析: / ...
- 稀疏表示字典的显示(MATLAB实现代码)
本文主要是实现论文--基于稀疏表示的图像超分辨率<Image Super-Resolution Via Sparse Representation>中的Figure2.通过对100000个 ...
- 61.node.js开发错误——Error: Connection strategy not found
转自:https://blog.csdn.net/fd214333890/article/details/53457145
- Js经典实例收集
跨浏览器添加事件 //跨浏览器添加事件 function addEvent(obj,type,fn){ if(obj.addEventListener){ obj.addEventListener(t ...
- Active Data Guard
ADG INTRODUCE Active Data Guard(ADG)是ORACLE 11g企业版的新特性,需要单独的License.可以打开Physical standby至read only模式 ...
- ifconfig---配置和显示Linux内核中网络接口
ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数.用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在.要想将上述的配置信息永远的存的电脑里,那就要修改网卡 ...
- 【MinGW】【C语言环境搭建】
问题 安装MinGW配置环境变量后终端输入gcc -v出错 解决 Win10下环境变量最后不用加分号