跳到正文

题解

ARC170B 题解

Register_int约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年1月21日
收录
2026年10月2日
更新
2026年11月21日
篇幅
约 2 分钟
获赞
4

版本

  1. v1当前

枚举左端点 ll,计算这样的区间有多少种后求和就能求出答案。不难发现右端点为 rr 满足条件时 r+1r+1 也满足条件,所以只要找到最小的 rr,总数即为 n−r+1n-r+1。

显然可以双指针解决,问题变为判定一个区间是否合法。由于值域较小,可以分别枚举 Ai,AkA_i,A_k,找出 AiA_i 第一次出现的位置、AkA_k 最后一次出现的位置,判断 Ai+Ak2\frac{A_i+A_k}2 是否有在两数见出现过即可。可以用 set 简单实现,时间复杂度 O(nV2log⁡n)O(nV^2\log n)。

AC 代码

C++
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 1e5 + 10;

int n, a[MAXN]; ll ans;

set<int> s[11];

inline 
bool check() {
	for (int i = 1; i <= 10; i++) if (s[i].size() >= 3) return 1;
	for (int i = 1, x; i <= 10; i++) {
		if (s[i].empty()) continue; x = *s[i].begin();
		for (int j = 1, k, y; j <= 10; j++) {
			if (i == j || s[j].empty()) continue; y = *s[j].rbegin();
			if ((i + j) % 2) continue; k = i + j >> 1;
			auto it = s[k].lower_bound(x);
			if (it != s[k].end() && *it < y) return 1;
		}
	}
	return 0;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1, j = 0; i <= n; i++) {
		for (; !check() && j <= n; j++, s[a[j]].insert(j));
		ans += n - j + 1, s[a[i]].erase(i);
	}
	printf("%lld", ans);
}
原文