Minimal coverage
Minimal coverage |
The Problem
Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M].
The Input
The first line is the number of test cases, followed by a blank line.
Each test case in the input should contains an integer M(1<=M<=5000), followed by pairs "Li Ri"(|Li|, |Ri|<=50000, i<=100000), each on a separate line. Each test case of input is terminated by pair "0 0".
Each test case will be separated by a single line.
The Output
For each test case, in the first line of output your programm should print the minimal number of line segments which can cover segment [0,M]. In the following lines, the coordinates of segments, sorted by their left end (Li), should be printed in the same format as in the input. Pair "0 0" should not be printed. If [0,M] can not be covered by given line segments, your programm should print "0"(without quotes).
Print a blank line between the outputs for two consecutive test cases.
Sample Input
2 1 -1 0 -5 -3 2 5 0 0 1 -1 0 0 1 0 0
Sample Output
0 1 0 1
题目大意:给你M的值和n条木棒的左右节点,求最少有多少根已知的木棒能覆盖[0,M]这个区间。
典型贪心里的最小覆盖问题,类似题目见:hdu 2088 。
因本人有一点左右不分(尤其是英语),所以直接打的拼音,莫见怪~O(∩_∩)O~
#include <iostream>
#include <cstring>
#include <cstdio>
#include<cmath>
#include <algorithm>
using namespace std;
struct point
{
int zuo;
int you;
friend bool operator <(const point a,const point b)//运算符'<'重载
{ if(a.zuo!=b.zuo)
return a.zuo<b.zuo;
else
return a.you>b.you;
}
}a[100010];
int main()
{
int T,M,i,j,k;
int cnt;
scanf("%d",&T);
while(T--)
{
scanf("%d",&M);
int t=0;
while(1)
{
scanf("%d%d",&a[t].zuo,&a[t].you);
if(!a[t].zuo&&!a[t].you)
break;
if(a[t].you>0||a[t].zuo>M)
++t;
}
sort(a,a+t);
if(a[0].zuo>0)
{
printf("0\n");
if(T)
printf("\n");
continue;
}
int cnt=0,s=-1;
int flag=0;
int sign[100010];
for(i=0;i<t;i++)
{
if(a[i].zuo<=cnt)
{
if(s==-1)
sign[++s]=i;
else
if(a[i].you>a[sign[s]].you)
sign[s]=i;
}
else
{
cnt=a[sign[s]].you;
sign[++s]=i;
}
if(a[sign[s]].you>=M)
{
flag=1;
break;
}
}
if(flag)
{
printf("%d\n",s+1);
for(i=0;i<=s;i++)
printf("%d %d\n",a[sign[i]].zuo,a[sign[i]].you);
}
else
printf("0\n");
if(T)
printf("\n");
}
return 0;
}