poj-2376--Cleaning Shifts--(贪心)
来源:互联网 发布:全球雾霾数据统计 编辑:程序博客网 时间:2024/06/10 14:02
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.
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.
* 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.
* 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.
* 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.
3 101 73 66 10
2
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.
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<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;typedef long long ll;#define pi acos(-1.0)#define inf 0x3f3f3f#define M 25005struct node{ int s,e;}p[M];int n,t;bool comp(const node & a,const node & b){ if(a.s!=b.s) return a.s<b.s; return a.e>b.e;}int main(){ int i; scanf("%d%d",&n,&t); for(i=1;i<=n;i++) scanf("%d%d",&p[i].s,&p[i].e); sort(p+1,p+1+n,comp); int num=1,it=p[1].e,temp; //使temp达到最远才确定当前区段 if(p[1].s>1) {printf("-1"); return 0;} for(i=2;i<=n;i++) { if(it+1<p[i].s) { num++; it=temp; } if(it+1>=p[i].s) { if(p[i].e>temp) temp=p[i].e; if(p[i].e==t) { num++; it=t; break; } } } if(it==t) printf("%d",num); else printf("-1"); return 0;}
阅读全文
0 0
- POJ 2376 - Cleaning Shifts(贪心)
- poj 2376 Cleaning Shifts(贪心)
- POJ 2376 Cleaning Shifts(贪心)
- POJ 2376 Cleaning Shifts (贪心)
- POJ 2376 Cleaning Shifts (贪心)
- POJ 2376 Cleaning Shifts(贪心)
- POJ 2376- Cleaning Shifts(贪心)
- POJ 2376 :Cleaning Shifts(贪心)
- [POJ 2376] Cleaning Shifts (区间贪心)
- POJ 2376 Cleaning Shifts(贪心)
- POJ 2376 Cleaning Shifts (贪心)
- poj 2376 Cleaning Shifts(贪心)
- poj-2376--Cleaning Shifts--(贪心)
- Cleaning Shifts(POJ 2376, 贪心)
- poj 2376 Cleaning Shifts( 贪心 )
- POJ 2376 Cleaning Shifts 贪心
- poj 2376 Cleaning Shifts( 贪心 )
- poj 2376 Cleaning Shifts 贪心
- imageloader
- Eclipse创建Mavenx项目抛错--maven-archetype-quickstart:1.1
- 常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LATEX2e 介绍”) 1、指数和下标可以用^和_后加相应字符来实现。比如: 2、平方根(square root)
- 使用hibernate框架制作学生信息查询(多表查询)
- 8.16-----japan
- poj-2376--Cleaning Shifts--(贪心)
- 网络安全之概述
- JS中的prototype
- hdoj 1515 Anagrams by Stack(dfs)
- 使用w3c生成xml文件
- 电脑通过wifi连接树莓派3
- animation.css的使用
- 剑指offer——24.二叉树中和为某一值的路径
- 机器学习校招笔记2:集成学习