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) ...
随机推荐
- c#创建ISS站点
private void CreateWebSite() { try { string installPath = "C:\\Program Files\\MyWeb"; stri ...
- 【HDU 5105】Math Problem
题意 f(x)=|ax3+bx2+cx+d| 求f(x)在L≤x≤R的最大值. 分析 参数有可能是0,注意分类讨论 1.当a=0时 b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间 ...
- Cocos2d-X3.0 刨根问底(三)----- Director类源码分析
上一章我们完整的跟了一遍HelloWorld的源码,了解了Cocos2d-x的启动流程.其中Director这个类贯穿了整个Application程序,这章随小鱼一起把这个类分析透彻. 小鱼的阅读源码 ...
- 40.Android之新手指引界面学习
我们经常可以看到打开新App会有新手指引界面,类似蒙板效果今天来学习.原理其实很简单,设置一个透明Activity或者Dialog,然后修改其属性即可.由于实现比较简单,就贴一部分代码. 1.在And ...
- BZOJ-2242 计算器 快速幂+拓展欧几里得+BSGS(数论三合一)
污污污污 2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2312 Solved: 917 [Submit][S ...
- UOJ150 运输计划
运输计划(transport.cpp/c/pas)[问题描述]公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n-1 条 双向 航道,每条航道建立在两个星球之间,这 n-1 条航道 ...
- 该如何理解AMD ,CMD,CommonJS规范--javascript模块化加载学习总结
是一篇关于javascript模块化AMD,CMD,CommonJS的学习总结,作为记录也给同样对三种方式有疑问的童鞋们,有不对或者偏差之处,望各位大神指出,不胜感激. 本篇默认读者大概知道requi ...
- [NOIP2009] 普及组
多项式输出 模拟 /*by SilverN*/ #include<algorithm> #include<iostream> #include<cstring> # ...
- Spring学习8- SSH需要的jar包
struts2 commons-logging-1.0.4.jar 主要用于日志处理 freemarker-2.3.8.jar 模板相关操作需要包 ognl-2.6.11.jar ognl表达示所需包 ...
- 修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot
问题描述: 1.机器:Linux主机,特别是主机为大内存,比如: 4G内存的使用pae内核的Ubuntu系统的dell电脑. 2.情况:使用VirtualBox安装Linux系统时,比如:通过Virt ...