Greedy:Cleaning Shifts(POJ 2376)

题目大意:农夫有N只牛,这些牛要帮助打扫农舍,这些牛只能打扫固定位置(千万要注意这个位置不是连续的),每一段区间必须至少有一只牛打扫,问你至少需要多少只牛?(如果区间不能完全被覆盖,则输出-1)。
这一题有点像上次的萨鲁曼军队,就是一个区间贪婪算法,我们尽可能的由一个方向往前找到最大的区间就可以了(如果与本位置的endT的间隔相同,找最远的,贪婪的最基本的思想)。
这一题本身不难,但是特判很恶心,一不小心就要出错了,比如1 5 3 1这样的输入,就直接输出-1就可以了(一定要有牛打扫1),还有,更关键的是,区间是不连续的(也就是扫完了13,下一次还可以扫46),一定要注意这一点!
#include <iostream>
#include <functional>
#include <algorithm> using namespace std;
typedef struct cows_t
{
int start_T;
int end_T;
}Cow_Set; Cow_Set cows[];
void Search(const int, const int);
int fcmop(const void *a, const void *b)
{
return (*(Cow_Set *)a).start_T - (*(Cow_Set *)b).start_T;
} int main(void)
{
int N, T;
while (~scanf("%d%d", &N, &T))
{
for (int i = ; i < N; i++)
scanf("%d%d", &cows[i].start_T, &cows[i].end_T);
qsort(cows, N, sizeof(Cow_Set), fcmop);
Search(N, T);
} return ;
} void Search(const int N, const int T)
{
int et = , pos = , tmp_pos = -, ans = , tmp_maxt, j;
if (cows[].start_T != )
{
cout << - << endl;
return;
}
while ()
{
if (cows[pos].start_T > et + )//说明没有牛可以突破当前这个上一个时间界限,不用找了
{
cout << - << endl;
return;
}
ans++;//增加一头开始的那头牛
if (cows[pos].end_T >= T)
break;
tmp_maxt = ; tmp_pos = pos;
for (j = pos + ; j < N && (cows[j].start_T <= cows[pos].end_T + ); j++)//找到间隔比当前T还要长的牛(记得T可以间隔1)
{
if (cows[j].end_T - cows[pos].end_T >= tmp_maxt)//要带等号,尽量往前
{
tmp_pos = j;
tmp_maxt = cows[j].end_T - cows[pos].end_T;
}
}
if (tmp_pos == pos)
{
cout << - << endl;
return;
}
pos = tmp_pos;
et = cows[tmp_pos].start_T;
}
if (ans != )
printf("%d\n", ans);
else
cout << - << endl;
}

Greedy:Cleaning Shifts(POJ 2376)的更多相关文章
- Cleaning Shifts POJ - 2376 (贪心题)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31194 Accepted: 7677 ...
- 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 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- 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 ...
- Cleaning Shifts(POJ 2376 贪心)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15143 Accepted: 3875 ...
- POJ 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- 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 Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...
随机推荐
- 【UVA 1586】Ancient Cipher
题 题意 给你一个只含CHON的有机物的化学式如C6H5OH求相对分子质量 分析 ... 代码 switch #include<cstdio> #include<cctype> ...
- python 类型之 set
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- 【poj2187】 Beauty Contest
http://poj.org/problem?id=2187 (题目链接) 题意 求点集上两点间最长距离 Solution 凸包+旋转卡壳. 旋转卡壳是看起来很难,但是很好意会也很好实现的算法,但是要 ...
- POJ2288 Islands and Bridges
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- Record is locked by another user --Oracle行锁解锁
Oracle修改表中记录时出现record is locked by another user的问题 在操作表时没有commit,导致表被锁,只要执行下面两行语句,就可以了将行锁解锁了. 1. ...
- pthread_detach pthread_join pthread_create
pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...
- Jquery,ajax返回json数据后呈现到html页面的$.post方式。
------------------------------------------------------完整版------------------------------------------- ...
- stl 迭代器(了解)
STL 主要是由 containers(容器),iterators(迭代器)和 algorithms(算法)的 templates(模板)构成的. 对应于它们所支持的操作,共有五种 iterators ...
- C# 检测操作系统是否空闲,实现系统空闲后做一些操作
public class CheckComputerFreeState { /// <summary> /// 创建结构体用于返回捕获时间 /// </summary> [St ...
- 不错的linux下通用的java程序启动脚本
不错的linux下通用的java程序启动脚本(转载) 虽然写起动shell的频率非常不高...但是每次要写都要对付一大堆的jar文件路径,新加jar包也必须要修改起动shell. 在网上找到一个挺好的 ...