poj-2376 Cleaning Shifts (排序+贪心)
http://poj.org/problem?id=2376
john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时间和结束时间,现在让我们找出在每个时间点都有牛打扫的情况下,所用牛越少越好,不能满足输出-1.
首先按起点排序,起点相同就按结束时间长的排序,然后贪心,每次选择时满足 当前牛的开始时间<=上一头牛的结束时间加1,并且当前牛的结束时间最大的一个。
注意 : 不必覆盖只要能连接即可。(1 3 4 6) 是可以的!!!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 1010
using namespace std; struct point
{
int x,y;
}p[];
bool cmp(const point &a,const point &b)
{
if(a.x!=b.x) return a.x<b.x;
else return a.y>b.y;
}
int main()
{
//Read();
//Write()
int n,t;
while(~scanf("%d%d",&n,&t))
{
for(int i=;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
sort(p,p+n,cmp);
//for(int i=0;i<n;i++)
// printf("%d %d\n",p[i].x,p[i].y);
if(p[].x!=) {printf("-1\n");continue;}
if(p[].y==t) {printf("1\n");continue;}
int m=p[].y,max=p[].y,ans=,j=;
bool flag;
while()
{
flag=;
for(int i=j+;i<n;i++)
{
if(p[i].x<=m+&&p[i].y>max)
{
flag=;
j=i;
max=p[i].y;
}
}
if(!flag) break;
//printf("%d %d\n",p[j].x,p[j].y);
ans++;
if(max==t)
{
printf("%d\n",ans);
flag=;
break;
}
m=max;
}
if(!flag||max<t) printf("-1\n");
}
return ;
}
poj-2376 Cleaning Shifts (排序+贪心)的更多相关文章
- POJ 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...
- poj 2376 Cleaning Shifts(贪心)
Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...
- 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 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- 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 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
随机推荐
- git创建和删除远程分支
问题描述: 使用git创建和删除远程分支 问题解决: (1)git创建本地分支 注: 如上所示,使用命令 git branch -a ...
- 剑指offer--面试题21--相关
题目:设计包含min函数的栈,pop(),push(),min()的时间复杂度均为O(1) 自己所写代码如下:(写‘栈’的代码还是有些不熟练!) #include <iostream> u ...
- Fiddler 过滤 css,图片等请求url 正则表达式
设置步骤: 1.勾选 Request Headers 中的 Hide if url contains 过滤项 2.贴入下方正则表达式 REGEX:(?insx)/[^?/]*.(css|ico|jpg ...
- 动态调用WCF服务
动态调用WCF服务,只需要提供*.svc地址, 1:命名空间: using System.ServiceModel.Channels;using System.ServiceModel; 2:创建访问 ...
- dedecms还原数据时要选对备份目录 不然会提示function文件出错
小李子最近在学习dedecms,在网上下载了一个二次开发的系统,顺利安装后想要还原一下作者的备份数据,可一直没有成功,让ytkah查看一下什么情况.进到后台,点击还原,提示/e/class/funct ...
- 如何快速查看将C反汇编的代码
查看反汇编主要的思路在于将 流程,处理,算法 区分开来.1 函数调用:原C代码: int sum(int, int);int main(){ int c = sum(1, 2); printf(&qu ...
- 驱动笔记 - Makefile
ifneq ($(KERNELRELEASE),) obj-m := hello.ohello-objs := main.o add.o else KDIR := /lib/modules/2.6.1 ...
- yum rpm 命令 linux
centos的软件安装大致可以分为两种类型: [centos]rpm文件安装,使用rpm指令 类似[ubuntu]deb文件安装,使用dpkg指令 [centos]yum安装 类似[ubuntu ...
- eclipse 自动 注释
在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...
- sql Server2005 master损坏处理
一.准备条件 a) 假设Master 数据库Hung:关闭SQL Server 服务(关闭MSSQLSERVER,SQL Server Agent其他的没有影响),然后剪切C:/Pro ...