您的位置:首页 > 产品设计 > UI/UE

hdu 5400 Arithmetic Sequence

2015-08-21 11:20 561 查看
点击打开题目链接

[align=left]Problem Description[/align]
A sequence b1,b2,⋯,bn
are called (d1,d2)-arithmetic
sequence if and only if there exist i(1≤i≤n)
such that for every j(1≤j<i),bj+1=bj+d1
and for every j(i≤j<n),bj+1=bj+d2.

Teacher Mai has a sequence a1,a2,⋯,an.
He wants to know how many intervals [l,r](1≤l≤r≤n)
there are that al,al+1,⋯,ar
are (d1,d2)-arithmetic
sequence.

[align=left]Input[/align]
There are multiple test cases.

For each test case, the first line contains three numbers
n,d1,d2(1≤n≤105,|d1|,|d2|≤1000),
the next line contains n
integers a1,a2,⋯,an(|ai|≤109).

[align=left]Output[/align]
For each test case, print the answer.

[align=left]Sample Input[/align]

5 2 -2
0 2 0 -2 0
5 2 3
2 3 3 3 3


[align=left]Sample Output[/align]

12
5


题意:

第一行n,d1,d2,表示n个数,d1和d2代表等差

第二行n个数。

叫你任选一个区间[l,r],要求这个区间中能找到一个点i,(l<=i<=r)使得前半部分[l,i]是以d1为等差的等差数列,后半部分[i,r]是以d2为等差的等差数列,问你有多少不同的区间。

这个题要注意当d1==d2时,前半部分和后半部分构成等差,所以计算时可能会算重,还有就是中间过程计算时会超int,全程用long long就行。

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#define ll long long
#define maxn 100005
using namespace std;
ll a[maxn],l[maxn],r[maxn];
int main()
{
ll n,d1,d2;
while(scanf("%lld%lld%lld",&n,&d1,&d2)!=EOF)
{
for(ll i=1;i<=n;i++)
scanf("%lld",&a[i]);
l[1]=1;
for(ll i=2;i<=n;i++)
{
if(a[i-1]+d1==a[i]) l[i]=l[i-1]+1;
else l[i]=1;
}
r
=1;
for(ll i=n-1;i>=1;i--)
{
if(a[i]+d2==a[i+1]) r[i]=r[i+1]+1;
else r[i]=1;
}
ll ans=0;
for(ll i=1;i<=n;i++)
{
if(d1==d2) ans+=r[i]-1;
else ans+=(l[i]-1)*(r[i]-1)+l[i]+r[i]-2;
}
printf("%lld\n",ans+n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: