Subject address: Ural 1303
First, sort by the Left endpoint of each line segment, and then set a start point S. Each time, the largest right endpoint is found from the line segment whose start point is less than or equal to S. And use the right endpoint as the new starting point S. Scan it from left to right.
The Code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int INF=0x3f3f3f3f;int a[100000];struct node{ int l, r, ll, rr;} fei[100000];int cmp(node x, node y){ return x.l<y.l;}int main(){ int m, i, j, n, l, r, s, max1, cnt=0, tot, pos, flag, flag1, ll, rr; scanf("%d",&m); while(scanf("%d%d",&ll, &rr)!=EOF&&(ll||rr)) { if(rr<=0||ll>=m) continue ; fei[cnt].ll=ll; fei[cnt].rr=rr; l=ll;r=rr; if(l<0) l=0; if(r>m) r=m; fei[cnt].l=l; fei[cnt++].r=r; } sort(fei,fei+cnt,cmp); s=0; tot=0; if(cnt==0) { printf("No solution\n"); } else { for(i=0; i<cnt;) { max1=-1; flag=0; while(fei[i].l<=s&&i<cnt) { flag=1; if(max1<fei[i].r) { max1=fei[i].r; pos=i; } i++; } if(!flag) break; if(s<max1) { a[tot++]=pos; s=max1; } } if(i<cnt||s<m) puts("No solution"); else { printf("%d\n",tot); for(i=0; i<tot; i++) { printf("%d %d\n",fei[a[i]].ll,fei[a[i]].rr); } } } return 0;}
Ural 1303 minimal coverage (Greedy)