답안 #652890

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
652890 2022-10-25T02:35:24 Z lto5 Non-boring sequences (CERC12_D) C++14
0 / 1
5000 ms 3460 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;

template <typename T> inline void read (T &x) {
  char ch;
  while (!isdigit(ch = getchar()) && ch != '-') ;
  bool sign = ch == '-';
  x = isdigit(ch) ? ch - '0' : 0;
  while (isdigit(ch = getchar()))
    x = x * 10 + ch - '0';
  if (sign)
    x = -x;
}

template <typename T> inline void write_pos (T x) {
  if (x > 9) {
    write_pos(x / 10);
  }
  putchar(x % 10 + '0');
}

template <typename T> inline void write (T x) {
  if (x < 0)
    putchar('-'), x = -x;
  write_pos(x);
}

int n;
int a[N];
int b[N];
int cnt[N];
int pos[N];
int low[N], high[N];

void read_input() {
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    b[i] = a[i];
  }
}

bool boring (int L, int R) {
  if (L >= R)
    return false;

  int ptrL = L, ptrR = R;
  int turn = 0;

  while (ptrL <= ptrR) {
    if (!turn) {
      if (low[ptrL] < L && high[ptrL] > R)
        return boring(L, ptrL - 1) || boring(ptrL + 1, R);
      ++ptrL;
    }
    else {
      if (low[ptrR] < L && high[ptrR] > R)
        return boring(L, ptrR - 1) || boring(ptrR + 1, R);
      --ptrR;
    }
  }

  return true;
}

void solve() {
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
  }

  for (int i = 1; i <= n; i++) {
    pos[a[i]] = 0;
  }

  for (int i = 1; i <= n; i++) {
    low[i] = pos[a[i]];
    pos[a[i]] = i;
  }

  for (int i = 1; i <= n; i++) {
    pos[a[i]] = n + 1;
  }

  for (int i = n; i >= 1; i--) {
    high[i] = pos[a[i]];
    pos[a[i]] = i;
  }

  puts(!boring(1, n) ? "non-boring" : "boring");
}

int main() {
//  freopen("NONBORING.INP", "r", stdin);
//  freopen("NONBORING.OUT", "w", stdout);

  srand(time(NULL));
  int t;
  read(t);

  while (t--) {
    read_input();
    solve();
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 972 KB Output is correct
2 Execution timed out 5017 ms 3460 KB Time limit exceeded
3 Halted 0 ms 0 KB -