【POJ - 2376】Cleaning Shifts (贪心)

题干:

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.

题目大意:

    农夫有N头牛。现在他想让一些牛去做家务。然后他把一天分成T个时间点,也就是一天的时间点是区间[1,T]。他想要任何一个时间点都有牛在做家务。现在给出每头牛的工作时间,问你能否用最少的牛满足他的要求,即用最少的时间段覆盖掉这一天([1,T])。如果不能满足则输出-1,否则输出最少的牛数量。

分析:贪心题。题目意思很明显是求最少的区间覆盖掉大区间。先对这些时间段排好序,这个排序应该是没什么问题的。然后呢,第一头牛肯定要选,就从这头牛开始,选取下一头牛。下一头牛怎么选取呢?即在满足条件的牛里面(注意:满足条件的牛是只要开始工作时间 start>=cow[0].y+1 即可),选取右边界值最大的那个,因为这样子能够覆盖掉最多的时间段。以此类推,故贪心法求之。

解题报告:

           代码太多,已经分不清了。

AC代码1:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct co{int beg;int end;
};
bool cmp(co a,co b){if(a.beg==b.beg) {return a.end<b.end;}return a.beg<b.beg;
}
int main()
{int n,t;cin >> n>> t;co cow[25005];//若定义在全局变量  即 struct中   那么会有自动补全功能存在 如果这样定义 则没有 for(int i=1;i<=n;i++){scanf("%d%d",&cow[i].beg,&cow[i].end);}int cur=0;int low;int pos=1;int ans=0;sort(cow+1,cow+n+1,cmp);while(cur<=t){		//行吗 low=cur+1;int i; 
//		for(i=pos;cow[i].beg<=low&&cow[i].end>=low;i++){//这样写不可以!!只能i<=n  然后再for里面用if来判断是否结束虚幻  或者继续循环 这样更好一些 
//			cur=max(cur,cow[i].end);
//		}
//		pos=i;for(i=pos;i<=n;i++){if(cow[i].beg<=low&&cow[i].end>=low){cur=max(cur,cow[i].end);}else if (cow[i].beg>low) {pos=i;break;} }if(low>cur) {break;}else {++ans;}}if(cur>=t) cout << ans << endl;else cout << -1 << endl;return 0 ;} 

 

 

AC代码2:

#include<iostream>
#include<cstdio>
#include<algorithm>const int MAX= 25000 + 5;
typedef long long ll;
struct co{int start;int end;
} cow[MAX];
//ll a[]
bool cmp( const co & a1,const co & a2){if(a1.start==a2.start) {return a1.end > a2.end;}return a1.start<a2.start;
}
using namespace std;
int main()
{int n,t;int cur=0;scanf("%d%d",&n,&t);int sum=0;for(int i=0;i<n;i++){scanf("%d%d",&cow[i].start,&cow[i].end);} sort(cow,cow+n,cmp);int po=0;//从第po头牛开始找 
//	int maxx=0;//其实就是cur while(cur<t){//如果当前的最大值还是小于t的话 //int flag=0;int min=cur+1;for(int i=po;i<n;i++) {if(cow[i].start<=min&&cow[i].end>=min){//不能用cur也不能cur+1 !! 因为cur不能变 但是这样cur就变了! //	flag=1;cur=max(cur,cow[i].end);//maxx=max(maxx,cow[i].end);} else if(cow[i].start>min){//	flag=1;po=i;//下一次从第i个开始找break; }}//	if(flag==0) break;if(min>cur) break;else ++sum;}if(cur>=t) cout << sum << endl;else cout << -1<<endl;return 0 ;} 

AC代码3:

#include <iostream>  
#include <string.h>  
#include <stdio.h>  
#include <algorithm>  
using namespace std;  
struct A  
{  int left,right;  
};  
int cmp(const A &a,const A &b)  
{  if(a.left==b.left)  return a.right<b.right;  return a.left <b.left;  
}  
int main()  
{  A a[25005],b[25005];  int m,n,c;  scanf("%d%d",&n,&c);for(int i=0; i<n; i++)  {  scanf("%d%d",&a[i].left,&a[i].right);  }  sort(a,a+n,cmp);  /*for(int i=0;i<n;i++) cout << a[i].left <<" "<< a[i].right<<endl;*/  int max=0;  int sum=0;  int top=0;  while(max<c)  {  int min=max+1;  for(int i=top; i<n; i++)  if(a[i].left<=min&&a[i].right>=min)  max=max>a[i].right?max:a[i].right;  else if(a[i].left>min)  {  top=i;  break;  }  if(min>max)  break;  else  sum++;  }  if(max>=c)  cout << sum <<endl;  else  cout << "-1\n";   return 0;  
}  

 AC代码4:

#include <iostream>  
#include <string.h>  
#include <stdio.h>  
#include <algorithm>  
using namespace std;  
struct A  
{  int left,right;  
};  
int cmp(const A &a,const A &b)  
{  if(a.left==b.left)  return a.right<b.right;  return a.left <b.left;  
}  
int main()  
{  A a[25005],b[25005];  int m,n,c;  scanf("%d%d",&n,&c);for(int i=0; i<n; i++)  {  scanf("%d%d",&a[i].left,&a[i].right);  }  sort(a,a+n,cmp);  /*for(int i=0;i<n;i++) cout << a[i].left <<" "<< a[i].right<<endl;*/  int max=0;  int sum=0;  int top=0;  while(max<c)  {  int min=max+1;  for(int i=top; i<n; i++)  if(a[i].left<=min&&a[i].right>=min)  max=max>a[i].right?max:a[i].right;  else if(a[i].left>min)  {  top=i;  break;  }  if(min>max)  break;  else  sum++;  }  if(max>=c)  cout << sum <<endl;  else  cout << "-1\n";   return 0;  
}  

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442010.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql sql 片段_MySQL代码片段

1.[代码][SQL]代码--导出为xml文件mysql -X -uroot -proot -e "use testa;select * from test_tb;" > /opt/test.xml--导出为csv文件--fields terminated 分割记录中每个字段的字符--optionally enclosed 包围每个字段的字符--lines terminated 每行结束的字符--P…

【HDU - 4990】 Reading comprehension (构造+矩阵快速幂)

题干&#xff1a; Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include <cstring> #include <cmath> #include &…

finereport文本框如何实现多值查询_如何实现参数级联查询

参数级联查询是查询控件之间的一种互动方式&#xff0c;比如在某个下拉框选定选项后&#xff0c;另一个下拉框里的选项范围会随之变化。润乾报表提供了多种编辑风格&#xff0c;每种编辑风格都有丰富的属性&#xff0c;以此为基础实现参数级联查询也很简单。下面就通过一个例子…

【HDU - 5015 】233 Matrix (矩阵快速幂)

题干&#xff1a; In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 233…

【HDU - 2899】 Strange fuction(二分或三分,求导)

题干&#xff1a; Now, here is a fuction: F(x) 6 * x^78*x^67*x^35*x^2-y*x (0 < x <100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<T<100) which means the number of…

php mysql html标签_HTML标签格式化PHP和MySQL

我有这个MySQL语句Select type.type, color.color, ShotName, Item.name, Item.Item_idFrom typeInner Join ItemOn type.type_id Item.type_idInner Join colorOn color.color_id Item.color_idWhere Item.state0 And Item.offline 0Group By color.color, Item.name, type.o…

【CF#706B】 Interesting drink (二分)

题干&#xff1a; 瓦西里喜欢在努力工作后休息&#xff0c;所以你可能经常在附近的一些酒吧见到他。他喜欢 "Beecola"&#xff0c;可以从 n 个不同的商店买到。在第 i 个商店的价格为 xi 元。 瓦西里计划购买他最喜欢的饮料 q 次。在第 i 天他能花 mi 元。他想知道每…

mysql datetime month不走索引_like百分号加前面一定不走索引吗?一不小心就翻车,关于mysql索引那些容易错的点...

like百分号加前面一定不走索引吗&#xff1f;正常来讲&#xff0c;我们都知道在mysql的like查询中&#xff0c;百分号加在关键词后面是走索引的&#xff0c;比如 select * like "张三%"&#xff0c;而百分号在前面是不走索引的&#xff0c;比如 select * like "…

ACM竞赛、数论内容常用的定理(求解(a/b)%c,乘法逆元,费马小定理)

如果b与c互素&#xff0c;则(a/b)%ca*b^((c)-1)%c其中是欧拉函数。或者(a/b)%ca*b^(c-2)%c 如果b与c不互素&#xff0c;则(a/b)%c(a%bc)/b 对于b与c互素和不互素都有(a/b)%c(a%bc)/b成立 乘法逆元用扩展欧几里得定理&#xff1a; 例题&#xff1a;ZOJ - 3609 题干&#xf…

自定义菜单url不能带_微服务架构【SpringBoot+SpringCloud+VUE】五 || 实战项目微信公众号自定义开发...

本章主要讲解微信公众号自定义菜单、微信网页开发、模板消息推送等功能的实现&#xff1b;发福利了&#xff0c;下方关注公众号&#xff0c;就能免费获取项目源码1、自定义菜单开发前需要了解以下几点&#xff1a;1、微信公众号的自定义菜单最多包括3个一级菜单&#xff0c;每个…

C语言编程中关于负数的%运算的判定。

如果 % 两边的操作数都为正数&#xff0c;则结果为正数或零&#xff1b;如果 % 两边的操作数都是负数&#xff0c;则结果为负数或零。C99 以前&#xff0c;并没有规定如果操作数中有一方为负数&#xff0c;模除的结果会是什么。C99 规定&#xff0c;如果 % 左边的操作数是正数&…

mnesia mysql性能,Mnesia数据库的存储容量是多少?

Some places state 2GB period. Some places state it depends up the number of nodes.解决方案Quite large if your question is "whats the storage capacity of an mnesia database made up of a huge number of disc_only_copies tables" - youre largely limit…

无数种求逆元的方法总结

乘法逆元 对于缩系中的元素&#xff0c;每个数a均有唯一的与之对应的乘法逆元x&#xff0c;使得ax≡1(mod n) 一个数有逆元的充分必要条件是gcd(a,n)1&#xff0c;此时逆元唯一存在 逆元的含义&#xff1a;模n意义下&#xff0c;1个数a如果有逆元x&#xff0c;那么除以a相当于…

【CF#2A】Winner(模拟 STL-map)☆

题干&#xff1a; The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation be…

python两个元组相加_《第5章 Python 列表与元组》5.1.3 序列相加(Adding)!

《高中信息技术 Python编程》 教学案《第5章 Python 列表与元组》 5.1.3 序列相加(Adding)&#xff01;06:151 #使用加号可以进行序列的连接操作&#xff0c;输入如下&#xff1a;2 >>>[1,2,3][4,5,6]3 [1,2,3,4,5,6]4 >>>a[1,2]5 >>>b[5,6]6 >&…

STL之 set简略介绍。

set常用函数及其讲解 构造set集合的主要目的是为了快速检索&#xff0c;使用set前&#xff0c;需要在程序头文件中包含声明“#include<set>”。 set集合容器实现了红黑树&#xff08;Red-Black Tree&#xff09;的平衡二叉检索树的的数据结构&#xff0c;在插入元素时&a…

mysql 按时间查询优化_mysql如何按时间查询优化

mysql按时间查询优化的方法&#xff1a;1、【register_time】字段是datetime类型&#xff0c;转换为日期再匹配&#xff0c;需要查询出所有行进行过滤&#xff1b;2、可以利用在【register_time】字段上建立索引&#xff0c;查询极快。本教程操作环境&#xff1a;windows7系统、…

【HDU - 5688 】Problem D(STL+map)

题干&#xff1a; 度熊所居住的 D 国&#xff0c;是一个完全尊重人权的国度。以至于这个国家的所有人命名自己的名字都非常奇怪。一个人的名字由若干个字符组成&#xff0c;同样的&#xff0c;这些字符的全排列的结果中的每一个字符串&#xff0c;也都是这个人的名字。例如&am…

列举python中常用的数据类型_Python基础知识 变量和简单数据类型

在本章节中&#xff0c;将介绍Python程序中会使用到的各种数据类型&#xff0c;以及如何在程序中使用变量来表示这些数据。其中用到的一些例子均来自《Python编程从入门到实践 第2版》。一、变量1. 变量是标签变量是可以赋给值的标签&#xff0c;也可以说变量指向特定的值。mes…

【HDU - 1412】 {A} + {B} (STL + set)

题干&#xff1a; 给你两个集合&#xff0c;要求{A} {B}. 注:同一个集合中不会有两个相同的元素. Input 每组输入数据分为三行,第一行有两个数字n,m(0<n,m<10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元…