题意:已知M,以及多个区间,求能覆盖[0,M]的最少的区间。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struct node 7 { 8 int l,r; 9 }q[100005],ans[100005]; 10 int cmp(node a,node b) 11 { 12 return a.r>b.r; 13 } 14 int main() 15 { 16 int t,M,num1,num2,s; 17 scanf("%d",&t); 18 while(t--) 19 { 20 num1=0; 21 num2=0; 22 s=0; 23 scanf("%d",&M); 24 memset(q,0,sizeof(q)); 25 memset(ans,0,sizeof(ans)); 26 while(~scanf("%d%d",&q[num1].l,&q[num1].r)) 27 { 28 if(q[num1].l+q[num1].r==0) 29 break; 30 num1++; 31 } 32 33 sort(q,q+num1,cmp); 34 while(s<M) 35 { 36 int i; 37 for(i=0;i<num1;++i) 38 { 39 if(q[i].l<=s&&q[i].r>s) 40 { 41 s=q[i].r; 42 ans[num2++]=q[i]; 43 break; 44 } 45 } 46 if(i==num1) 47 break; 48 } 49 if(s<M) 50 printf("0 "); 51 52 else 53 { 54 printf("%d ",num2); 55 for(int i=0;i<num2;++i) 56 printf("%d %d ",ans[i].l,ans[i].r); 57 } 58 if(t) 59 printf(" "); 60 } 61 return 0; 62 }