码迷,mamicode.com
首页 > 其他好文 > 详细

uva10020-最小覆盖

时间:2016-08-17 10:18:47      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

题目链接 http://acm.hust.edu.cn/vjudge/problem/19688

 

解题思路

区间的最小覆盖问题

 

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX_LEN 100050
using namespace std;
typedef struct line {
    int l, r;
}Line;
Line lines[MAX_LEN];
Line cpy[MAX_LEN];
Line ans[MAX_LEN];
int m, n;
bool compare(Line x, Line y)
{
    return x.l<y.l;
}
void Solve()
{
    int start=0, ansp=0;
    int j=0;
    bool flag = true;
    while(j<n) {
        for(int i=j; i<n; i++) {
            cpy[i] = lines[i];
            if(cpy[i].l<start) cpy[i].l = start;
        }
        int maxr=0, now = -1;
        while(j<n && cpy[j].l==start) {
            if(cpy[j].r == start) { j++; continue; }
            else if(cpy[j].r > maxr) {
                maxr = cpy[j].r; now=j;
                j++;
            }
            else j++;
        }
    //    if(j<n && cpy[j].l!=start) j++;
        if(now!=-1) {
            start = lines[now].r;
            ans[ansp++] = lines[now];
        }
        else { flag = false; break; }
        if(maxr>=m) break;
    }
    if(flag) {
        printf("%d\n", ansp);
        for(int i=0; i<ansp; i++) printf("%d %d\n", ans[i].l, ans[i].r);
    }
    else printf("0\n");
}
int main()
{
    int cases;
    scanf("%d", &cases);
    while(cases--) {
        scanf("%d", &m);
        int l, r;
        char ch; 
        n = 0;
        scanf("%d%d", &l, &r);
        while(!(l==0&&r==0)) {
            lines[n].l = l; lines[n].r = r; n++;
            scanf("%d%d", &l, &r);
        }
        sort(lines, lines+n, compare);
        Solve();
        if(cases) printf("\n");
    }
    return 0;
}

 

uva10020-最小覆盖

标签:

原文地址:http://www.cnblogs.com/ZengWangli/p/5778629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!