求助,玄关
查看原帖
求助,玄关
1066484
XuZile楼主2024/7/25 13:48
#include <bits/stdc++.h>
using namespace std;
int n;
long long sum;
int h[500010],g[500010];
int main(){
	cin >> n;
	getchar();
	for(int i=1;i<=n;i++){
		char ch=getchar();
		if(ch=='H') h[i]++;
		else g[i]++;
	}
	for(int i=1;i<=n;i++){
		h[i]=h[i-1]+h[i];
		g[i]=g[i-1]+g[i];
	}
	for(int i=1;i<=n;i++){
		int t_h2=lower_bound(h+i,h+n+1,h[i-1]+2)-h;
		int t_g2=lower_bound(g+i,g+n+1,g[i-1]+2)-g;
		int t=n-max(t_h2,t_g2)+1;
		int t_h3=lower_bound(h+i,h+n+1,h[i-1]+3)-h;
		int t_g=lower_bound(g+i,g+n+1,g[i-1]+1)-g;
		if(t_h3<t_g&&t_g-t_h3>t) t=t_g-t_h3;
		int t_g3=lower_bound(g+i,g+n+1,g[i-1]+3)-g;
		int t_h=lower_bound(h+i,h+n+1,h[i-1]+1)-h;
		if(t_g3<t_h&&t_h-t_g3>t) t=t_h-t_g3;
		sum+=t;
	}
	cout << sum;
	return 0;
}

自我感觉没有错可只过了第一个点 样例二:

50
GGGGGGGGGGGGGGGGGGGGGGGGGHGGGGGGGGGGGGGGGGGGGGGGGG

647

2024/7/25 13:48
加载中...