ZOJ 3518 Unsafe Factor

题目链接:ZOJ 3518 Unsafe Factor

开始以为卡的是时间,写了一个树状数组成段更新,用了两个数组,内存超了,然后试了一下直接模拟,居然过了==。。

整一个数组,长度为最大点数,表示点的经过次数,读入段的时候把数组这一段每个点加一,最后点为2的说明两个纸带在这一点都坏了,1表示只有一个在这一点坏了,0表示两个纸带在这一点都没有坏,然后扫一遍所有点,求最大连续点值为1的长度。

用map时间1500MS,用数组240MS, STL好慢, 不过能省一半内存。

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAX_N = 10000000 + 100;

int L, n1, n2;
int _map[MAX_N];

int main()
{
    while(scanf("%d%d%d", &L, &n1, &n2) != EOF)
    {
        memset(_map, 0, sizeof(_map));
        int n = n1 + n2;
        int a, b;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &a, &b);
            for(int j = a; j <= b; j++)
                _map[j]++;
        }
        int res = 0, _max = 0;
        for(int i = 0; i <= L; i++)
        {
            if(_map[i] != 0)
            {
                if(_map[i] == 1)
                    res++;
                else
                {
                    _max = max(_max, res);
                    res = 0;
                }
            }
            else
            {
                _max = max(_max, res);
                res = 0;
            }
        }
        printf("%d\n", _max);

    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>

using namespace std;

int L, n1, n2;
map <int , int> _map;

int main()
{
    while(scanf("%d%d%d", &L, &n1, &n2) != EOF)
    {
        _map.clear();
        int n = n1 + n2;
        int a, b;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &a, &b);
            for(int j = a; j <= b; j++)
            {
                if(_map.count(j))
                    _map[j] = 2;
                else
                    _map[j] = 1;
            }
        }
        int res = 0, _max = 0;
        for(int i = 0; i <= L; i++)
        {
            if(_map.count(i))
            {
                if(_map[i] == 1)
                    res++;
                else
                {
                    _max = max(_max, res);
                    res = 0;
                }
            }
            else
            {
                _max = max(_max, res);
                res = 0;
            }
        }
        printf("%d\n", _max);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值