Non-boring sequences UVA - 1608

题目传送门

题意:给你长度为n的一个序列,如果这个序列的任何一个连续子序列都有至少一个出现了一次的数字就说这个序列是一个不无聊的序列,反之就是一个无聊的序列。

思路:这个题就是求出来每一个数字所在位置离他左右最近的相同的数字,然后进行搜索就可以了,从两边开始向中间搜索。

PS:这个题目遇到一个奇怪的问题,这个做法的复杂度应该是O(nlogn),但是花了2000+ms,我就一直在找原因,然后最后发现原因竟然是用memset把数组的所有元素都赋值为-1的问题,把memset改成了循环时间就只剩400ms了。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define MAXN 200010
#define MAXE 210
#define INF 1000000010
#define MOD 1000000007
#define LL long long
#define pi acos(-1.0)

using namespace std;

int arr[MAXN];
int PRE[MAXN];
int NEXT[MAXN];
typedef pair<int, int> P;
map<int, int> num;

bool check(int l, int r) {
  if (l >= r)
    return true;
  int left = l, right = r;
  while (left <= right) {
    if (NEXT[left] > r && PRE[left] < l) {
      return check(l, left - 1) && check(left + 1, r);
    } 
    if (NEXT[right] > r && PRE[right] < l) {
      return check(l, right - 1) && check(right + 1, r);
    }
    left++;
    right--;
  }
  return false;
}

int main() {
  std::ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int kase = 1; kase <= T; ++kase) {
    num.clear();
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> arr[i];
    }
    for (int i = 0; i <= n; ++i) {
      NEXT[i] = n + 1;
      PRE[i] = -1;
    }
    for (int i = 0; i < n; ++i) {
      if (num.count(arr[i])) {
        PRE[i] = num[arr[i]];
        NEXT[num[arr[i]]] = i;
        num[arr[i]] = i;
      } else {
        num[arr[i]] = i;
      }
    }
    if (check(0, n - 1))
      cout << "non-boring\n";
    else
      cout << "boring\n";
  }
  return 0;
}

/*
4
5
1 2 3 4 5
5
1 1 1 1 1
5
1 2 3 2 1
5
1 1 2 1 1
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值