The coverage interval of each cow is given. The minimum number of cows can cover [1, T. (If you have a problem, for example, t = 10, 1-5, 6-10, all of them are covered, and the point is covered)
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define rep(i,n) for(int (i)=0;(i)<n;(i)++)using namespace std;struct Node{ int l,r;} cow[25010];bool cmp(Node a,Node b){ if(a.r==b.r) return a.l>b.l; return a.r>b.r;}int main(){ //freopen("d:\\Test.txt","r",stdin); int n,m; scanf("%d%d",&n,&m); rep(i,n) scanf("%d%d",&cow[i].l,&cow[i].r); sort(cow,cow+n,cmp); int ncount=1; int j=0; for(int i=0; i<n; i++) { if(cow[i].r<m) break; j++; } if(j==0) { cout<<"-1"<<endl; return 0; } int k=cow[j-1].l; int L=k; int i=j; while(i<n) { bool flag=false; if(cow[i].r<k-1) {cout<<"-1"<<endl;return 0;} while(i<n&&cow[i].r>=k-1) { if(L>cow[i].l) { L=cow[i].l; flag=true; } i++; } if(flag) ncount++; k=L; if(k==1) break; } if(k>1) cout<<"-1"<<endl; else cout<<ncount<<endl; return 0;}
Poj2376 cleaning shifts (Greedy)