牛的大扫除

  题目大意:农夫有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. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  2. 单调队列 I

    2009国家集训队徐持衡的论文<浅谈几类背包问题>里提到的一个经典问题: 长度限制最大连续和问题: 给出长度为 n 的序列 X i ,求这个序列中长度不超过 Lmax 的最大连续和. Im ...

  3. android HDMI 清晰度 分辨率

    但改变分辨率时,发送广播即可: Intent intent_outputmode_change = new Intent(ACTION_OUTPUTMODE_CHANGE);     intent_o ...

  4. abstract 类 构造函数

    public abstract class CommonReq { private String TransNo { get; set; } public String SubmitData { ge ...

  5. 检测ADO.net拼接字符串中非法字符

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Refle ...

  6. cheerio, dom操作模块

    cheerio 为服务器特别定制的,快速.灵活.实施的jQuery核心实现. Introduction 将HTML告诉你的服务器 var cheerio = require('cheerio'), $ ...

  7. WEB开发中的页面跳转方法总结

    PHP header()函数跳转 PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向: $url = & ...

  8. php-fpm.conf两个至关重要的参数

    这里规定了PHP-CGI的连接.发送和读取的时间,300秒足够用了,因此我的服务器很少出现504 Gateway Time-out这个错误.最关键的是php-fpm.conf的设置,这个会直接导致50 ...

  9. Android SDK安装教程

    1.下载安装JDK(java 开发套件),天空软件站下载http://www.skycn.com/soft/3116.html(或 到java官网下载),下载后双击安装即可. 2.下载android ...

  10. 一张图解释Hadoop IPC

    基于hadoop2.6.2.... 一张图Server启动,Client访问..... RPC是IPC的一种,IPC还有另外一种LPC,相关请看参考中的3 使用hadoop ipc步骤: 1.定义RP ...