CF1168B Good Triple

CF1168B Good Triple

给定一个 \(01\)\(S\) ,求有多少个区间 \([l,\ r]\) ,满足至少有一对 \((x,\ k)\) 使得 \(l\leq x<x+2k\leq r\)\(S_x=S_{x+k}=S_{x+2k}\)

\(|S|\leq3\times10^5\)

贪心,暴力


正难则反,考虑统计有多少个区间不满足条件

手算 / 口胡 可得不满足条件的区间长度一定很小,因此可以暴力枚举左端点,找到最长的不满足条件的区间并统计答案

时间复杂度 \(O(nk)\)

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 3e5 + 10;
int n, tot;
char a[maxn];

int main() {
  scanf("%s", a + 1);
  n = strlen(a + 1);
  for (int l = 1; l <= n; l++) {
    for (int j = 3; j < 20; j++) {
      int r = l + j - 1;
      if (r > n) {
        tot += n - l + 1;
        break;
      }
      bool flg = 0;
      for (int x = l; x < r - 1 && !flg; x++) {
        for (int k = 1; x + k + k <= r; k++) {
          if (a[x] == a[x + k] && a[x + k] == a[x + k + k]) {
            flg = 1; break;
          }
        }
      }
      if (flg) {
        tot += r - l;
        break;
      }
    }
  }
  printf("%I64d", 1ll * n * (n + 1) / 2 - tot);
  return 0;
}
posted @ 2019-06-10 19:13  cnJuanzhang  阅读(162)  评论(0编辑  收藏  举报