Topic links
Test instructions
n number, two tolerance D1, D2
Sub-sequence conforming to requirements [L, R](1≤l≤r≤n) Number
Requirements: The presence of point I (L <= i <= R) makes A[l] ~a[i] is the D1 of tolerance arithmetic progression, a[i]~a[r] is the d2 of tolerance arithmetic progression
Idea: Enumerate each point to figure out the number of qualifying points to the left and right of each point (itself counted as l[i] and R[i]
L[i] * R[i] is the number of sub-sequences that meet the conditions at point I
The code is as follows:
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include < Algorithm>using namespace Std;typedef long long ll;const int n = 1e5+10;int N, d1, D2;ll A[n], l[n], r[n];ll ans, cnt;i NT main () {int I, J, K;while (~scanf ("%d%d%d", &n, &d1, &d2)) {for (i = 1; I <= n; i++) {scanf ("%i64d", &a[i ]);} if (D1 = = D2) {for (int i = 1; I <= n; i++) l[i] = 1;} else{l[1] = 1;for (int i = 2; I <= n; i++) {if ((A[i]-a[i-1] = = D1) {L[i] = l[i-1] + 1;} else l[i] = 1;}} Ans = 0;r[n] = 1;ans + = l[n] * r[n];for (int i = n-1; i > 0; i--) {if ((A[i+1]-a[i]) = = D2) {R[i] = r[i+1] + 1;} else R[i] = 1;ans + = l[i] * r[i];} printf ("%i64d\n", ans);} return 0;}
i ( 1 ≤ i ≤ n ) i ( 1 " span class= "Mo" id= "mathjax-span-32" style= "" >≤ ≤ ) in
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5400 Arithmetic Sequence