Minimal coverage _最小区间覆盖 UVA 10020

题目:

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


题目大意

 给定一个数字t,做t组测试

每组测试给定一个数字m,随后给出任意个区间,当输入0 0时,结束区间输入。

要求从给定区间中找出几组数来覆盖[0,m] 

AC代码:

/*给定一个端点n,再给一定数量的区间[li,ri] ,问覆盖[0,n]( m初始为0 )最少需要多少个区间,如果不能输出0。。
为了能使一个区间覆盖的更多,将所有区间按右端点降序排序,如果一个区间[l,r] 覆盖了0(start) ,
则说明[0,r]已经被覆盖,所以令0(start)=r,剩下需要被覆盖的为[r,n] (在r<n的情况下,若r>=n,则说明[m,n]被完全覆盖,直接结束)
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
 
int t;
int start, end, qn, outn;
struct M {
    int start;
    int end;
} q[100005], out[100005];
 
int cmp (M a, M b) {//按最大能覆盖到排序
    return a.end > b.end;
}
int main() {
    scanf("%d", &t);
    while (t --) {
		qn = 0; outn = 0; start = 0;
		scanf("%d", &end);
		while (~scanf("%d%d", &q[qn].start, &q[qn].end) && q[qn].start + q[qn].end)	qn ++;
		sort(q, q + qn, cmp);
		while (start < end)
		 {
			int i;
	 	   	for (i = 0; i < qn; i ++) 
			{
				if (q[i].start <= start && q[i].end > start)
				{
			    	start = q[i].end;//更新区间
			    	out[outn ++] = q[i];
		    		break;
				}
   		 	}
		    if (i == qn) break;//如果没有一个满足条件的区间,直接结束。
		 }
		
		if (start < end) printf("0\n");
		else 
		{
		    printf("%d\n", outn);
		    for (int i = 0; i < outn; i ++)
			printf("%d %d\n", out[i].start, out[i].end);
		}
		
		if (t) printf("\n");
    }
    return 0;
}

 /*给定一个端点n,再给一定数量的区间[li,ri] ,问覆盖[0,n]( m初始为0 )最少需要多少个区间,如果不能输出0。。
为了能使一个区间覆盖的更多,将所有区间按右端点降序排序,如果一个区间[l,r] 覆盖了0(start) ,
则说明[0,r]已经被覆盖,所以令0(start)=r,剩下需要被覆盖的为[r,n] (在r<n的情况下,若r>=n,则说明[m,n]被完全覆盖,直接结束)
*/

自己代码 ,实在是找不出什么错误,但是就是WA,大佬们帮帮忙看一看错误在哪

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
struct M {
    int x;
    int y;
} a[100005], b[100005];
int cmp (M a, M b) {//按最大能覆盖到排序
    return a.y>b.y;
}
int cmp1 (M a, M b) {//按最大能覆盖到排序
    return a.x<b.x;
}
//sort(q,q+qn,cmp);
int main()
{
	int t;
	for(int qq=0;qq<100005;qq++)
	{
		a[qq].x=0;
		a[qq].y=0;
	}
	cin>>t;
	while(t--)
	{
		//memset( &a, 0, sizeof(a) ); 
		//flushall();
		stack<M> show; 
		int m;
		cin>>m;
		int xx,yy;
		int i=0;
		while(cin>>xx>>yy)
		{
			if(xx==0 && yy==0) break;
			a[i].x=xx;a[i].y=yy;
			b[i].x=xx;b[i].y=yy;
			i++;
		}
		sort(a,a+i,cmp);
		sort(b,b+i,cmp1);
		int s=i;
	/*	for(i=0;i<s;i++)
		{
			cout<<a[i].x<<" "<<a[i].y<<endl;
		}
		cout<<endl<<endl;*/
		if(a[0].y<m || b[0].x>0) 
		{
			cout<<"0"<<endl;
		}
		else
		{
			int start=a[0].x,end=a[0].y;
			show.push(a[0]);
			if(a[0].x<=0) 
			{
				
				cout<<"1"<<endl;
				cout<<show.top().x<<" "<<show.top().y<<endl;
				cout<<endl;
				continue;//
			} 
			for(i=1;i<s;i++)
			{
				if(a[i].x<=start && a[i].y>=start)
				{
					if(a[i].y>=m) //&&a[i].x>0
					{
						show.pop();			
					}
					if(a[i].x<=0) 
					{
						show.push(a[i]);
						break;
					} 
					show.push(a[i]);
					start=a[i].x;
					//end=a[i].y; 
				}
			}
			if(show.top().x>0) cout<<"0"<<endl;
			else
			{
				cout<<show.size()<<endl;
				while(!show.empty())
				{
            		cout<<show.top().x<<" "<<show.top().y<<endl;
            		show.pop();
        		}	
			}
		//	show.pop();
				
		}
		//cout<<endl;
		if (t) cout<<endl;
	/*	while(!show.empty())
				{
            		
            		show.pop();
        		}*/	
		
	} 
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/baidu_41907100/article/details/84673955
今日推荐