【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 ...
随机推荐
- Dos 改动IP 地址
1.改动 ip地址 子网掩码 默认网关 netsh interface ip set address "本地连接" static 192.168.1.23 255.255.255. ...
- iOS应用崩溃日志分析 iOS应用崩溃日志揭秘
转自:http://www.raywenderlich.com/zh-hans/30818/ios%E5%BA%94%E7%94%A8%E5%B4%A9%E6%BA%83%E6%97%A5%E5%BF ...
- UBUntu 软件 源配置方法
近期公司产品须要添加一个功能,就是版本号自己主动更新.使用apt-get 实现. apt-get 软件源配置的方法,參见本人资源里的共享.以下是代码中作为升级的一部分. FILE *fp; ...
- boost::serialization 用基类指针转存派生类(错误多多,一波三折)
boost::serialization 也支持c++的多态,这样我们就能够通过使用基类的指针来转存派生类, 我们接着上一篇( boost::serialization(2)序列化基类 )的样例来看: ...
- TinyXML中类分析
TiXmlElement: 对应于XML的元素,定义了对element的相关操作 成员函数: TiXmlElement (const char * in_value); TiXmlElement( c ...
- adb pull 与 push
adb pull <remote> <local> Copies a specified file from an emulator/device instance to yo ...
- Android 通过Application 传递数据
</pre><pre> package com.example.ApplicationTest; import android.app.Application; /** * C ...
- block-循环引用
在ARC机制下,app的内存管理由操作系统进行管理,不须要程序猿手动的管理内存,方便了开发.虽然,自己主动释放内存非常方便.可是并不是绝对安全,绝对不会产生内存泄露. 大部分导致iOS对象无法按预期释 ...
- Mongo.setReadPref(mode, tagSet) primaries and secondaries are treated equivalently. 读优先级策略
https://docs.mongodb.com/manual/reference/method/Mongo.setReadPref/#Mongo.setReadPref Mongo.setReadP ...
- Marking as slave lost.
Spark on Yarn提交任务时报ClosedChannelException解决方案_服务器应用_Linux公社-Linux系统门户网站 http://www.linuxidc.com/Linu ...