题目链接:hdu 5400 Arithmetic Sequence
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
int N, D1, D2, A[maxn];
int main () {
while (scanf("%d%d%d", &N, &D1, &D2) == 3) {
for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
ll ans = N;
int l = 1, r = 1;
while (l <= N) {
while (r < N && A[r] + D1 == A[r+1]) r++;
while (r < N && A[r] + D2 == A[r+1]) r++;
if (r == l) {
r++, l++;
continue;
}
int n = r - l;
ans = ans + 1LL * n * (n+1) / 2;
l = r;
}
printf("%lld\n", ans);
}
return 0;
}

本文介绍了解决HDU5400 Arithmetic Sequence问题的算法,通过双指针技巧和数学分析,实现了O(N)的时间复杂度。详细解释了如何在数组中寻找符合条件的连续子序列,并计算这些子序列的数量。
968

被折叠的 条评论
为什么被折叠?



