牛的大扫除

  题目大意:农夫有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)的更多相关文章

  1. Cleaning Shifts POJ - 2376 (贪心题)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31194   Accepted: 7677 ...

  2. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  3. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  4. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

  5. 【原创】poj ----- 2376 Cleaning Shifts 解题报告

    题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K ...

  6. Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 ...

  7. POJ 2376 Cleaning Shifts【贪心】

    POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...

  8. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

  9. 【POJ - 2376】Cleaning Shifts(贪心)

    Cleaning Shifts Descriptions: 原文是English,我这就直接上Chinese了,想看原文的点一下链接哦 大表哥分配 N (1 <= N <= 25,000) ...

随机推荐

  1. UVA10118(记忆化搜索 + 好题)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19440 题意,4堆不同颜色的糖果,每堆N个,从堆上往下拿,放入一个最大装5个 ...

  2. POJ 2240Arbitrage(Floyd)

    E - Arbitrage Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...

  3. ci控制器写规范

    不需要后缀名 文件名全部小写 所有控制器需要直接或者间接继承CI_Controller 以下划线开头的方法为私有方法,不能被请求 protected private的方法不能被浏览器请求 ci方法名不 ...

  4. Ognl基本使用

    ---恢复内容开始--- Ognl默认是从“根”中取数据的 下面Demo中用的是 Ognl.getValue(String expression, Map context, Object root) ...

  5. Swift定义单例

    而在Swift中我们通过清晰的语法便能定义类变量: 通过static定义的类变量无法在子类重写,通过class定义的类变量则可在子类重写. struct SomeStructure { static ...

  6. ifconfig 工具

    ifconfig 工具 ifconfig 命令常用格式: 格式:ifconfig显示当前激活的网络接口信息. 格式:ifconfig {INTERFACE}显示指定网络接口的信息.比如:eth0, e ...

  7. mysql join详解

    下面是例子分析 表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 a20050114 5 a20050115 表B记录如下: bID bNa ...

  8. PHP定界符 heredoc

    <?php echo <<<EOT //如果这个后面有空格,报错... haha EOT; //如果这个后面有空格,报错[如果没有空格,就这样文件直接结束,同样报错,请在EOT ...

  9. 基于.NET的大型Web站点StackOverflow架构分析(转)

    Stack Overflow网址:http://stackoverflow.com/ 当前访问量:每月9500PV(每天300多万PV) 当前Alexa排名:149 所用.NET技术:C#.Visua ...

  10. JS 循环遍历JSON数据

    JSON数据如:{"options":"[{/"text/":/"王家湾/",/"value/":/" ...