HDU5400——Arithmetic Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5400
/************************************************ * Author :Powatr * Created Time :2015-8-18 13:00:59 * File Name :1005.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int a[MAXN]; int pre1[MAXN], pre2[MAXN]; int main(){ int n, d1, d2; while(~scanf("%d%d%d", &n, &d1, &d2)){ for(int i = 1; i <= n; i++) scanf("%d", &a[i]); memset(pre1, 0, sizeof(pre1)); memset(pre2, 0, sizeof(pre2)); for(int i = 1; i < n; i++){ if(a[i+1] - a[i] == d1) pre1[i+1] = pre1[i] + 1; } for(int i = n; i >= 2; i--){ if(a[i] - a[i-1] == d2) pre2[i-1] = pre2[i] + 1; } if(d1 == d2){ int num = 1; ll ans = 0; for(int i = 1; i < n; i++){ if(a[i+1] - a[i] == d1){ num++; } else{ ans += 1ll*num*(num+1)/2; num = 1; } } ans += 1ll*num*(num+1)/2; printf("%I64d\n", ans); continue; } ll ans = 0; for(int i = 1; i <= n; i++) ans += (1ll*(pre1[i]+1) * 1ll*(pre2[i]+1)); printf("%I64d\n", ans); } return 0; }