【POJ - 2376】Cleaning Shifts(贪心)
Cleaning Shifts
Descriptions:
原文是English,我这就直接上Chinese了,想看原文的点一下链接哦
每只奶牛只在一些时间段有空。奶牛如果选择某一段时间,则必须完成整段时间的工作
你的任务是帮助FJ安排一些奶牛,使每段时间至少有一只奶牛被安排来做这件事。并且奶牛数应尽可能小。如果不可能办到,输出-1
Input
* 第一行:N和T
* 第二行至N+1行: 每一行包括奶牛能工作的开始和结束时间。闭区间。
Output
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
题目链接:
https://vjudge.net/problem/POJ-2376
给定N个小区间以及区间起点终点,求能用它们覆盖区间[1,T]的最小组合。
贪心策略是从左往右,尽量选择长度最大的区间。
首先对所有奶牛排序,按照开始时间排序。
然后更新起点=终点+1,搜索剩下的奶牛中能够覆盖这个起点同时终点最远的那一头,更新终点。
AC代码;
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
int N,T;
struct cow
{
int s;//开始时间start的缩写
int e;//结束时间end的缩写
int no;
bool operator<(const cow &c)const//按开始时间从大到小排序
{
return s<c.s;
}
}; int main()
{
while(cin>>N>>T)
{
cow cows[];//在这里设置数组就不用每次都重置数组了
for(int i=; i<N; ++i)
cin>>cows[i].s>>cows[i].e;
sort(cows,cows+N);//排序
// for(int i=0; i<N; i++)
// cout<<cows[i].s<<" "<<cows[i].e<<endl;
int over=;//结束时间
int pos=;//位置
int sum=;//一共几头牛
while(over<T)
{
int start=over+;//每次的开始时间都是上一次的结束时间+1
// cout<<"***"<<start<<endl;
for(int i=pos; i<N; ++i)
{
if(cows[i].s<=start)//能够覆盖起始点
{
if(cows[i].e>over)
over=max(over,cows[i].e);//将结束时间延长到最远
}
else
{
//不能覆盖起点,说明上一头牛的终点就是最远点,需要换一头牛了
// cout<<cows[i].s<<" "<<cows[i].e<<endl;
pos=i;
break;
}
}
// 没找到这样的牛,失败
if(start>over)
{
cout<<-<<endl;
break;
}
//找到了,+1
else
++sum;
}
if(over>=T)//正常跳出循环,输出sum即可
cout<<sum<<endl;
}
}
【POJ - 2376】Cleaning Shifts(贪心)的更多相关文章
- 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 (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- 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【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- 【原创】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 ...
- ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...
随机推荐
- 基于jquery 全选、反选、各行换色、单击行选中事件实现代码
<script language="javascript"> $(document).ready(function(){ //各行换色 $('table tr:odd' ...
- Codeforces Round #148 (Div. 1)
A wool sequence 表示一个序列中能够找到一个连续的子区间使得区间异或值为0 那么求的是不含这样的情况的序列个数 题目中数据范围是.在0~2^m - 1中选n个数作为一个序列 n和m都是1 ...
- 《Unix网络编程》中的错误处理函数
#include "net.h" #include <syslog.h> // syslog() int daemon_proc; static void err_do ...
- OpenStack Live Migration
About live migration of KVM virtual machines with NFS storage, from Mirantis blog: click this link w ...
- nfs 挂载错误
[ 147.080000] svc: failed to register lockdv1 RPC service (errno 146). [ 147.090000] lockd_up: makes ...
- Vue生命周期方法。
- POJ 1182 食物链(并查集)
题目链接 经过宝哥的讲解,终于对这种问题有了进一步的理解.根据flag[x]和flag[y]求flag[tx]是最关键的了. 0吃1,1吃2,2吃0. 假设flag[tx] = X; 那么X + fl ...
- Spark SQL is a Spark module for structured data processing.
http://spark.apache.org/docs/latest/sql-programming-guide.html
- HDOJ 5045 Contest
状压DP.. . . Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- IE67 下 setattribute class 失效
解决办法.将class 换成 className ,同理.ff不能识别className,将其换成class element.setAttribute("class"," ...