牛的大扫除

  题目大意:农夫有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. 【codevs1257】 打砖块

    http://codevs.cn/problem/1257/ (题目链接) 题意 在等腰三角形上打砖块,总共有m发炮弹,每块砖有一个权值,求打出的最大权值 Solution 今天考试题,考场上的2个小 ...

  2. 洛谷P1656 炸铁路

    题目描述 因为某国被某红色政权残酷的高压暴力统治.美国派出将军uim,对该国进行战略性措施,以解救涂炭的生灵. 该国有n个城市,这些城市以铁路相连.任意两个城市都可以通过铁路直接或者间接到达. uim ...

  3. C语言学习-01第一个C语言程序

    一 C语言的历史 C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. 尽管C语言提供 ...

  4. eclipse中Preferences的一些设置

    1.在Eclipse里面设置了java文件保存时自动格式化,在java->Code Style->Formatter里设置了自定义的格式化的样式,这样每次保存后都会自动格式化代码,用了一段 ...

  5. spark中操作hdfs

    1 获取路径 val output = new Path("hdfs://master:9000/output/"); val hdfs = org.apache.hadoop.f ...

  6. javascript学习随笔(二)原型prototype

    JavaScript三类方法: 1.类方法:2.对象方法:3.原型方法;注意三者异同 例: function People(name){ this.name=name; //对象方法 this.Int ...

  7. Hbase Shell常用命令

    hbase shell常用的操作命令有create,describe,disable,drop,list,scan,put,get,delete,deleteall,count,status等,通过h ...

  8. pthread_cancel

    #include <pthread.h> #include <stdio.h> #include<stdlib.h> #include <unistd.h&g ...

  9. Java中使用split、sort函数

    public static void main(String[] args) { // TODO Auto-generated method stub String str = null ; Scan ...

  10. 使用Adivisor配置增强处理

    使用Adivisor配置增强处理 实现步骤: 1.通过MethodBeforeAdivice接口实现前置增强处理 public class ServiceBeforeAdvisor implement ...