[算法入门经典] 区间最少覆盖问题 UVA 10020 - Minimal coverage

按算法入门经典的思路去模拟就好。

#include <stdio.h>  
#include <string.h>  
#include <math.h>  
#include <stdlib.h>  
#include <algorithm>  
#include <iostream>  
#include <set>  
#include <map>  
#include <queue>  
#include <stack>
#include <assert.h>
using namespace std;
struct ak
{
    int L, R;//分别代表区间左,右
}s[100001], ss[100001], tmp;
int cmp(const void *a, const void *b)
{
    struct ak c = *(struct ak *)a;
    struct ak d = *(struct ak *)b;
    return c.L - d.L;
}
int main()  
{  
    //freopen("test0.in", "r", stdin);  
    //freopen("test0.out", "w", stdout);  
    int m, k, kk, T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &m);
        k = 0;
        while(~scanf("%d %d", &s[k].L, &s[k].R), s[k].L || s[k].R)
        {
            k++;
        }
        qsort(s, k, sizeof(s[0]), cmp);
        int temp = 0, maxx = 0, cnt = 0;//初始化区间起始位置,初始化计数
        kk = 0;
        if(s[0].L <= temp)//按L从小到大排序后,如果第一个区间的L > temp
        {                 //那一定无法覆盖[0,m]区间了
            cnt = 1;
            for(int i = 0 ; i < k && maxx < m; i++)
            {
                if(s[i].L > temp && s[i].R > maxx)//如果L > temp 并且 R > maxx
                {                                 //那么需要更新temp开始位置,并且计数+1
                    temp = maxx;
                    ++cnt;
                    ss[kk++] = tmp;
                }
                if(s[i].L <= temp)
                {
                    if(s[i].R > maxx)   
                    {
                        maxx = s[i].R;
                        tmp.L = s[i].L;
                        tmp.R = s[i].R;
                    }
                }
                else if(s[i].R > maxx)//如果L > temp 并且 R > maxx 
                {                     //意味着不能完整覆盖,直接break
                    cnt = 0;
                    break;
                }
            }
        }
        if(kk == 0 || tmp.L != ss[kk-1].L || tmp.R != ss[kk-1].R)
        {
            ss[kk++] = tmp;
        }
        if(cnt)
        {
            printf("%d\n", cnt);
            for(int i = 0; i < cnt; i++)
            {
                printf("%d %d\n", ss[i].L, ss[i].R);
            }
        }
        else
        {
            printf("0\n");
        }
        printf(T?"\n":"");
    }
    return 0;  
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值