3389: [Usaco2004 Dec]Cleaning Shifts安排值班
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 462 Solved: 175
[ Submit][ Status][ Discuss]
Description
一天有T(1≤T≤10^6)个时段.约翰正打算安排他的N(1≤N≤25000)只奶牛来值班,打扫
打扫牛棚卫生.每只奶牛都有自己的空闲时间段[Si,Ei](1≤Si≤Ei≤T),只能把空闲的奶牛安排出来值班.而且,每个时间段必需有奶牛在值班. 那么,最少需要动用多少奶牛参与值班呢?如果没有办法安排出合理的方案,就输出-1.
Input
第1行:N,T.
第2到N+1行:Si,Ei.
Output
最少安排的奶牛数.
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
所有时间段按x从小到大排序,x相同按y从大到小排
之后贪心,必选第一个,假设第一个的覆盖区间为[1, y],那么后面所有起点在[1, y+1]内的线段找个y最大的选上,以此类推到最后一个时间段,中间如果有断或者第一个区间不从1开始就是-1
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct Line
{
int x, y;
bool operator < (const Line &b) const
{
if(x<b.x || x==b.x && y>b.y)
return 1;
return 0;
}
}Line;
Line s[25005];
int main(void)
{
int n, len, i, ans, now, bet;
scanf("%d%d", &n, &len);
len++;
for(i=1;i<=n;i++)
{
scanf("%d%d", &s[i].x, &s[i].y);
s[i].y++;
}
sort(s+1, s+n+1);
if(s[1].x!=1)
{
printf("-1\n");
return 0;
}
bet = now = s[1].y, ans = 1;
for(i=1;i<=n;i++)
{
if(s[i].x<=now)
bet = max(bet, s[i].y);
else
{
if(bet<s[i].x)
{
printf("-1\n");
return 0;
}
ans++;
now = bet;
bet = max(bet, s[i].y);
}
}
if(now<len)
{
if(bet<len)
printf("-1\n");
else
printf("%d\n", ans+1);
}
else
printf("%d\n", ans);
return 0;
}