uva10020-最小覆盖

题目链接 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;
}

 

转载于:https://www.cnblogs.com/ZengWangli/p/5778629.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值