Ural_1303

區間覆蓋問題,選取最少的線段,覆蓋一個區間.

首先,區間外的線段不予考慮,按線段左點排序,從起點開始覆蓋,在左點在起點左邊的點中,選取右點最右的點,之後將該右點作爲新的起點直至覆蓋整個區間爲止,如果找不到新的線段右點在起點右邊,說明不能完全覆蓋


#include <cstdio>
#include <string>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
int m,p,a,b;

struct xd{
	int s,e;
	bool operator < (const xd& a)const{//按照開始座標排序 
		return s<a.s;	
	}
}ss[100005];
int vis[100005];//標記是否在結果中 
int main(){
	p=0;
	memset(vis,0,sizeof vis);
	
	scanf("%d",&m);
	while(scanf("%d%d",&a,&b)){
		if(a==0&&b==0)break;
		if(b<=0||a>=m||a==b)continue; 
		ss[p].s=a,ss[p].e=b;
		p++;
	}
	
	sort(ss,ss+p);
	
	int flag=1,ed=0,pos=0,tp=0;
	
	while(ed<m){
		int ned=0,ni=0;
		while(pos<p&&ss[pos].s<=ed){
			if(ss[pos].e>ned){//在起始點在該點左邊的點中,選擇終止點最靠右的點 
				ned=ss[pos].e;
				ni=pos;	
			}
			pos++;
		}	
		if(ned==0){flag=0;break;}//沒有增量,失敗,退出 
		
		ed=ned;//將該點作爲新的起始點並標記結果 
		vis[ni]=1;
		tp++;
	}
	
	if(p==0||flag==0){
		printf("No solution\n");	
	}else{
		printf("%d\n",tp);
		for(int i=0;i<p;i++){
			if(vis[i])printf("%d %d\n",ss[i].s,ss[i].e);
		}
	}
    system("pause");
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章