1303. Minimal Coverage

Given set of line segments [L i, R i] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer).

Input

First line of the input contains an integer M (1 ≤ M ≤ 5000). Subsequent lines of input contain pairs of integers L i  and R i  (−50000 ≤ L i  < R i  ≤ 50000). Each pair of coordinates is placed on separate line. Numbers in the pair are separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.

Output

Your program should print in the first line of output the power of minimal subset of segments which covers segment [0, M]. The list of segments of covering subset must follow. Format of the list must be the same as described in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.
If there is no covering subset then print “No solution” to output.

Samples

input output
1
-1 0
-5 -3
2 5
0 0
No solution
1
-1 0
0 1
0 0
1
0 1
**************************************************************************************
贪心(参考了别人的代码)
**************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 struct seg
 9 {
10     int s;
11     int e;
12   seg &operator =(seg &b)
13     {
14        s=b.s;
15        e=b.e;
16        return *this;
17     }
18 
19 }p[100010],ans[100010];
20 int M,i,j,k,t;
21 int m,n;
22 int min;
23 int gs;
24 void  init()
25 {
26     cin>>M;
27     i=0;
28     while(cin>>m>>n)
29      {
30          if(m==0&&n==0)
31           break;
32          p[i].s=m;
33          p[i++].e=n;
34      }
35      k=i;
36 }
37 bool cmp(seg a,seg b)
38  {
39      if(a.s<b.s)return true;
40      if(a.s>b.s)return false;
41      if(a.s==b.s)
42       {
43           if(a.e>b.e)return true;
44            else  return false;
45       }
46  }
47 int  main()
48 {
49     init();
50     sort(p,p+k,cmp);//头排序,尾次之
51     t=0;
52     for(i=1;i<k;i++)
53      if(p[i].s>p[t].s&&p[i].e>p[t].e)p[++t]=p[i];//删除被覆盖的线段
54     k=t;
55     p[k+1].s=M+1;//为了比较时不越界
56     p[k+1].e=M+1;
57     j=0;
58     int h=0;
59     for(i=0;i<=k;i++)
60      if(j<p[i+1].s&&p[i].s<=j)//满足条件即保证j不在下一条线上此举为了避免重复
61        {
62            j=p[i].e;
63            ans[++h]=p[i];//记录;
64         if(p[i].e>=M)//满足条件输出结果
65             {
66                 cout<<h<<endl;
67                 for(int g=1;g<=h;g++)
68                  cout<<ans[g].s<<' '<<ans[g].e<<endl;
69                 return 0;
70             }
71        }
72     cout<<"No solution"<<endl;
73     return 0;
74 
75 
76 
77 }
View Code
 

转载于:https://www.cnblogs.com/sdau--codeants/p/3234803.html

猜你喜欢

转载自blog.csdn.net/weixin_34250434/article/details/93432981
今日推荐