Given set of line segments [Li , Ri] 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 number M (1 <= M <= 5000). Subsequent lines of input contain pairs of numbers Li and Ri (abs(Li), abs(Ri) <= 50000). Each pair is placed on separate line. Numbers in the pair are separated with space(s). List of pairs is ended with pair of integers “0 0”. i <= 100000
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 “0 0” 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.
Sample Input
1
-1 0
-5 -3
2 5
0 0
Sample Output
No solution
Hint
Huge input,scanf is recommended.
Sample input #2
1
-1 0
0 1
0 0
Sample output #2
1
0 1
思路:对于每一次找线段,我们都希望可以走到一个更远的距离,这样的话,我们对线段按照右端点由大到小排序,每次找出一个符合题意的线段就更新右端点,并且退出,直到左端点超过右端点。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
using namespace std;
const int maxx=1e5+100;
struct node{
int l,r;
bool operator<(const node &a)const{
return r>a.r;
}
}p[maxx];
int n,m;
inline bool cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
scanf("%d",&m);
n=0;
int x,y;
while(scanf("%d%d",&x,&y))
{
if(x==0&&y==0) break;
p[++n].l=x;p[n].r=y;
}
sort(p+1,p+1+n);
int s=0,e=m;
vector<node> q;
int flag=1;
while(s<e&&flag)
{
flag=0;
for(int i=1;i<=n;i++)
{
if(p[i].l<=s&&s<p[i].r)
{
s=p[i].r;
q.push_back(p[i]);
flag=1;
break;
}
}
}
if(s<e) printf("No solution\n");
else
{
printf("%d\n",q.size());
sort(q.begin(),q.end(),cmp);
for(int i=0;i<q.size();i++) printf("%d %d\n",q[i].l,q[i].r);
}
return 0;
}
努力加油a啊,(o)/~