HDU 5400 Arithmetic Sequence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.