Description
一天有T(1≤T≤10^6)个时段.约翰正打算安排他的N(1≤N≤25000)只奶牛来值班,打扫
打扫牛棚卫生.每只奶牛都有自己的空闲时间段Si,Ei,只能把空闲的奶牛安排出来值班.而且,每个时间段必需有奶牛在值班. 那么,最少需要动用多少奶牛参与值班呢?如果没有办法安排出合理的方案,就输出-1.
Input
第1行:N,T.
第2到N+1行:Si,Ei.
Output
最少安排的奶牛数.
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
样例说明
奶牛1和奶牛3参与值班即可.
题解
贪心,每次取能取到的最远点。
代码
#include<bits/stdc++.h>
#define N 500005
#define ll long long
#define inf 1000000009
#define mod 2010516623LL
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,T,R,i,ans;
struct node{int l,r;}a[25005];
bool cmp(node a,node b){return a.l<b.l;}
int main()
{
n=read();T=read();
for (i=1;i<=n;i++) a[i].l=read(),a[i].r=read();
sort(a+1,a+n+1,cmp);
i=1;
while (R!=T&&i<=n)
{
int r=0;
for (;i<=n&&a[i].l<=R+1;i++) r=max(r,a[i].r);
ans++;R=r;
if (R==0) return puts("-1"),0;
}
if (R!=T) puts("-1");else printf("%d",ans);
return 0;
}