跳到正文

题解

AT_abc369_c 题解

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

文章信息

发布
2024年9月1日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 2 分钟
获赞
6

版本

  1. v1当前

题目传送门

思路

开一个变量表示当前等差数列的长度,设其为 XX。设记录答案的变量为 SS。

对于第 ii 个点,若 Ai−Ai−1=Ai−1−Ai−2A_i-A_{i-1}=A_{i-1}-A_{i-2},说明等差数列长度增加,即 X←X+1X\gets X+1。否则将 XX 变为 22,因为任意 22 个数都会组成一个等差数列,并用等差数列求和公式更改 SS 的值,即 S←S+X2+X2S\gets S+\frac{X^2+X}{2}。

AC CODE

C++
#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=2e5+10;
int a[N];
int main(){
	int n=read();
	if(n==1)
		return printf("1\n"),0;
	for(int i=1;i<=n;++i)
		a[i]=read();
	int cnt=1;
	long long ans=n;
	for(int i=2;i<=n;++i)
		if(i>2&&a[i]-a[i-1]==a[i-1]-a[i-2])
			++cnt;
		else{
			ans+=(long long)cnt*(cnt-1)/2;
			cnt=2;
		}
	ans+=(long long)cnt*(cnt-1)/2;
	printf("%lld\n",ans);
	return 0;
}
原文