一、题目

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. 
Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. 
Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

* Line 1: Two space-separated integers: N and T 
* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 
INPUT DETAILS: 
There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 
OUTPUT DETAILS: 
By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

二、思路&心得

  • 利用贪心算法,先按开始时间将所有数据进行排序,然后每次选择结束时间最大的即可

三、代码

#include<cstdio>
#include<algorithm>
#define MAX_N 25005
using namespace std;typedef pair<int, int> P;int N, T;int cnt;P a[MAX_N];bool cmp(P a, P b) {if (a.first < b.first) return true;else return false;
}   int solve() {sort(a, a + N, cmp);bool found = false;int begin = 0, end = 0;for (int i = 0; i < N; i ++) {if (a[i].first <= begin + 1) {if (!found) found = true;if (a[i].second > end) {end = a[i].second;if (end == T) return ++ cnt;}continue;}if (!found ) return -1;begin = end;found = false;cnt ++;i --;}return -1;
}int main() {scanf("%d %d", &N, &T);cnt = 0;for (int i = 0; i < N; i ++) {scanf("%d %d", &a[i].first, &a[i].second);}printf("%d\n", solve());            return 0;
} 

转载于:https://www.cnblogs.com/CSLaker/p/7281249.html

【贪心算法】POJ-2376 区间问题相关推荐

  1. 贪心算法无重叠区间c语言,贪心算法之区间问题.md

    --- title: 贪心算法之区间问题 tags: - Leetcode categories: - Leetcode author: 四叶草 top: false abbrlink: 26230 ...

  2. java区间合并_贪心算法:合并区间

    ❝ 最近文章阅读量少了很多啊打卡也少了, 是不是年底了很多录友在忙期末考试啊,哈哈. 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: intervals = [[1,3],[2,6], ...

  3. 贪心算法——选择不相交区间问题

    题目描述:设有n个活动的集合,其中每个活动都要求使用同一个资源,而在同一时间内只有一个活动能够使用这一资源,每个活动i都有一个要求使用该资源的起始时间si和一个结束时间fi(si<fi),如果选 ...

  4. 贪心算法-----poj 3253 Fence Repair(切木板)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  5. leetcode 贪心_leetcode题解(贪心算法)

    定义 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择. 也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 通常贪心算法的代码会非常短而且思路也非常 ...

  6. 4.贪心算法 含例题

    文章目录 贪心算法 一.一个基本的贪心算法问题:区间调度问题 二.区间调度的推广:多个资源下的贪心算法 三.最小延迟调度--交换论证 四.最优超高速缓存问题 五.图最短路径问题 六.最小生成树问题 七 ...

  7. _32LeetCode代码随想录算法训练营第三十二天-贪心算法 | 738.单调递增的数字 、714.买卖股票的最佳时机含手续费、968.监控二叉树

    _32LeetCode代码随想录算法训练营第三十二天-贪心算法 | 738.单调递增的数字 .714.买卖股票的最佳时机含手续费.968.监控二叉树 题目列表 738.单调递增的数字 714.买卖股票 ...

  8. 贪心算法—区间调度 电影节(POJ 4151)

    贪心算法--区间选取问题 或是区间调度问题 本文解决一个很经典的贪心算法问题 Interval Scheduling(区间调度问题).给你很多形如[start,end]的闭区间,请你设计一个算法,算出 ...

  9. 贪心算法—建立雷达(POJ 1328)

    贪心算法--区间选点问题 这也是贪心算法的经典问题,一般情况为:有n个闭区间[ai,bj],取尽量少的点,使得每个区间内都至少有一个点. 分析 如果区间i内已经有一个点被取到,则称此区间已经被满足. ...

最新文章

  1. pandas使用str函数和startswith函数,筛选dataframe中(start with)以特定前缀开头的数据列(selecting columns begin with certain
  2. vba和python哪个好学-Python或将取代VBA,成为Excel官方脚本语言???
  3. linux内核杂记(16)-系统调用(3)
  4. high severity error mysql_pg将运行日志导入表中的方法及时区问题解决
  5. c++整理程序 dev_C编程从入门到实践:C语言开发工具详解(2)
  6. 课堂笔记——Data Mining(1)
  7. 基于ansj_seg和nlp-lang的简单nlp工具类
  8. 使用Word 2007写blog
  9. python基础9-常见难点
  10. 微软职位内部推荐-Senior Engineering Lead
  11. 解决:点击鼠标右键无新建选项卡
  12. Egret引擎基础教程I(下载安装创建第一个项目)
  13. 东方卫视携微软小冰打造人工智能新闻节目引热议
  14. 阅读软件怎么添加书源_【看书听书】两款神器软件,我已深深的为之折服了
  15. 有哪些技术可以自动拨打电话
  16. 计算机网络实验IP数据报分片,实验报告三IP数据分片.doc
  17. 编程计算图形的面积(圆形,正方形,长方形)
  18. SVN服务端和客户端搭建教程
  19. 黑马程序员:移动web
  20. Java创建二维三维数组的几种方式

热门文章

  1. 三维点云学习(3)2- K-Means
  2. Active Directory 域服务(AD DS)
  3. 基于系统的流量控制(Qos)
  4. no python application found_为什么我会得到“uWSGI Error Python application not found”?
  5. mysql解压版超详细安装以及各种避坑指南
  6. 根据共享文件夹的权限进行自动映射网络驱动器
  7. java工具类使用逗号切割字符串_【java】分割字符串工具类,霸气 jdk自带的
  8. 参考文献如何居中_一线教师如何写教育教学论文?
  9. 技术篇-符号制作-线符号制作
  10. SpringCloudAlibaba--Seata简单案例