Minimal coverage POJ - 2620(贪心)

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)/~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值