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