P1668 [USACO04DEC] Cleaning Shifts S 题解
Planetary_system · · 题解
题面解释:
最小线段覆盖,段点不要求相交。
思路分析:
值域只有
从
这应该是最简洁的做法了吧,时间复杂度
AC Code:
#include<bits/stdc++.h>
using namespace std;
int n,t,to[1000010];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>t;
for(int i=1,x,y;i<=n;i++)
cin>>x>>y,to[x]=max(to[x],y);
for(int i=1;i<=t;i++)
to[i]=max(to[i],to[i-1]);
int now=1,ans=0;
while(now<=t){
if(to[now]<now){ans=-1;break;}
now=to[now]+1,ans++;
}cout<<ans;
return 0;
}