Minimal Coverage(ural1303,区间贪心)

来源:互联网 发布:社交网络的坏处 编辑:程序博客网 时间:2024/05/18 03:31

http://acm.timus.ru/problem.aspx?space=1&num=1303

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13756

Minimal Coverage

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

Description

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.

Sample Input

input output

1

-1 0

-5 -3

2 5

0 0

No solution

1

-1 0

0 1

0 0

1

0 1

Source

Problem Source: II Collegiate Students Urals Programming Contest. Yekaterinburg, April 3-4, 1998 

解析:

题意:给出若干段区间,问是否可以找到最少段区间将[0,m]覆盖,输出区间个数以及按照

递增的顺序输出这些区间

思路:

区间的贪心,为保证得到最优解,先对区间进行排序处理,删掉被覆盖区间;

然后从左到右找到可拼接的区间,直至边界跨过或在m为止

思路参考自:http://hi.baidu.com/chingfantsou/item/5e1d530e66cd04803d42e26e

#include<string.h>#include<math.h>#include<stdio.h>#include<algorithm>#include <iostream>using namespace std;const int maxn=100000+10;struct Line{int s;int t;}line[maxn],temp[maxn];bool cmp(Line a,Line b){return a.s<b.s||(a.s==b.s&&a.t>b.t);}int main(){    int m,k,i,j,ans;    int n,cnt,l,r;    scanf("%d",&m);     cnt=0;    for(;;)    {    scanf("%d%d",&l,&r);    if(l==0&&r==0)    break;    line[++cnt].s=l;    line[cnt].t=r;}    sort(line+1,line+cnt+1,cmp);    j=1;    for(i=2;i<=cnt;i++)//将互不覆盖的线段存起来    if(line[i].s>line[j].s&&line[i].t>line[j].t)     line[++j]=line[i];     n=j;     ans=0;     line[n+1].s=m+1;     line[n+1].t=m+1;     k=0;//第一个临界     for(i=1;i<=n;i++)     if(line[i+1].s>k&&line[i].s<=k)     {     k=line[i].t;     temp[++ans]=line[i];     if(line[i].t>=m)     {     printf("%d\n",ans);     for(j=1;j<=ans;j++)      printf("%d %d\n",temp[j].s,temp[j].t);      return 0;     }     }     printf("No solution\n");    return 0;}