【学习笔记】2021.10.9 - zhengru IOI 七连测 Day6

时间:2021-10-10
本文章向大家介绍【学习笔记】2021.10.9 - zhengru IOI 七连测 Day6,主要包括【学习笔记】2021.10.9 - zhengru IOI 七连测 Day6使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

T1聚会

正解

思路

  • 简单题,开栈暴力存储每个 1 的位置,然后暴力向两边拓展更新答案即可。

  • 当拓展到另一个 1 时应该立即停止,因为再进行拓展一定会多上一个 两个 1 之间的距离,一定不会更优,没有意义。

  • 至此,每一段区间最多被遍历两次,复杂度 \(O(Tn)\) ,可以通过。

代码

屑代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int T,n,ans[500233],stk[500233],tot,Case=0;
char ch[500233];
long long ouout;
inline int R(){
	int x=0,f=1;char c='c';
	while(c>'9'||c<'0'){f=f*(c=='-'?-1:1);c=getchar();}
	while(c<='9'&&c>='0'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
inline void gechch(){
	char c='c';int pos=0;
	while(c!='1'&&c!='0') c=getchar();
	while((++pos)&&(c=='1'||c=='0')){ans[pos]=((c=='1')?0*(stk[++tot]=pos):1919810);c=getchar();}
	return;
}
int main(){
	T=R();
	while(T--){
		tot=0;ouout=0;
		n=R();
		gechch();
		for(register int i=1;i<=tot;++i){
			for(register int j=stk[i]-1;ans[j];--j){if(ans[j]<=stk[i]-j) break;ans[j]=stk[i]-j;}
			for(register int j=stk[i]+1;ans[j];++j) ans[j]=j-stk[i];
		}
		for(register int i=1;i<=n;++i) ouout+=ans[i];
		printf("Case #%d: %lld\n",++Case,ouout);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/Konjac-Binaries/p/15389007.html