跳到正文

题解

Xterfusion

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

文章信息

发布
2026年10月4日
收录
2026年10月23日
更新
2026年11月12日
篇幅
约 2 分钟
题目
P17590

版本

  1. v1当前

题目大意

给你一个 01 序列,要求你选择一些 1 的数字,使得这些数字的下标有一个分界值,其中分界值前面的下标间隔为分界值后面的下标间隔的两倍。

思路

注意到 nn 很小,只有 10001000,所以可以想的暴力一点。

直接枚举所有的分界值 pp,然后枚举下标间隔 xx,然后暴力在数组里跳。

枚举的复杂度为 O(n2)O(n ^ 2),而在数组里跳的渐进复杂度约为 O(log⁡n)O(\log n),时间复杂度约为 O(n2log⁡n)O(n ^ 2\log n),可以接受。

AC code

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

#define int long long
const int N = 2e3 + 10;
int n, a[N], ans = -1e9, sum;

signed main()
{
//	freopen("1.in", "r", stdin);
//	freopen("1.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		char c;
		cin >> c;
		if(c == 'X') a[i] = 1; // 将输入进行01转化 
		sum += a[i];
	}
	if(sum == 0) cout << sum, exit(0);
	for(int p = 1; p <= n; p++) // 枚举p 
	{
		if(!a[p]) continue; // 不是1直接跳过 
		for(int x = 1; x + p <= n; x++) // 枚举x,x+p是因为 需要保证至少走一步 
		{
			int cnt = 0, id = p; // cnt表示向右跳了几步,id表示当前跳到了第id个 
			while(id <= n && a[id]) // 向右跳  
			{
				cnt++;
				id += x;
			}
			if(cnt < 2) continue; // 小坑点,题目要求右边小于k,所以至少有两个 
			int cnt2 = 0; // 向左跳了几步 
			id = p;
			while(id >= 1 && a[id]) // 向左跳 
			{
				cnt2++;
				id -= 2 * x;
			}
			ans = max(ans, cnt2 + cnt - 1); // 统计答案,-1是因为p点被重复算了 
		}
	}
	cout << ans << endl;
	return 0;
}
原文