hdu 5400(思路题)

Arithmetic Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1445    Accepted Submission(s): 632

Problem Description

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.

Input

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).

Output

For each test case, print the answer.

Sample Input

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

Sample Output

12
5

Author

xudyh

对每个数先预处理出左右能够达到的最远距离。然后当d1!=d2时,用乘法原理得到区间数(左边有l[i]个区间,右边有r[i]个区间,左右就是l[i]*r[i]个区间)

当d1==d2 时,左右会算重,只算左边就好了。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100005;
int a[N];
LL l[N],r[N],ans; ///l[i]记录第i个数左边与其成方差为d1的数的个数,r[i]记录第i个数右边与其方差为
                  ///d2的数的个数(包括自身)
int main()
{
    int n,d1,d2;
    while(scanf("%d%d%d",&n,&d1,&d2)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        l[1] = 1,r[n]=1;
        ans = 0;
        for(int i=2;i<=n;i++){ ///预处理
            if(a[i]==a[i-1]+d1){
                l[i] = l[i-1]+1;
            }else l[i] = 1;
        }
        for(int i=n-1;i>=0;i--){
            if(a[i]+d2==a[i+1]){
                r[i] = r[i+1]+1;
            }else r[i]=1;
        }
        if(d1==d2){
            for(int i=1;i<=n;i++){
            ans+=l[i];
        }
        }
        else
            for(int i=1;i<=n;i++){ ///乘法原理
           // printf("%lld %lld\n",l[i],r[i]);
            ans+=l[i]*r[i];
        }
        printf("%lld\n",ans);
    }
}
时间: 2024-12-19 03:08:47

hdu 5400(思路题)的相关文章

hdu 4908(思路题)

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1614    Accepted Submission(s): 566 Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing

hdu 5063(思路题-反向操作数组)

Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 842    Accepted Submission(s): 288 Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n. T

hdu 5101(思路题)

Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1599    Accepted Submission(s): 443 Problem Description One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting

hdu 4859(思路题)

Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1171    Accepted Submission(s): 402 Problem Description Recently, Goffi is interested in squary partition of integers.

HDU 5301 思路题

给出n*m的矩阵,里面有一个坏点,不覆盖这个坏点的矩阵填满n*m的矩阵,使得这些矩阵的最大面积最小,并输出最小面积 先把矩阵转换为n<=m的矩阵,并把坏点通过镜像转移到左上方 ans=MAX(矩阵最中心点到两边距离的最小值,MIN(坏点下方的点到矩阵左端和下端的最小值)); #include "stdio.h" #include "string.h" int ans,n,m,x,y; int Max(int a,int b) { if (a<b) ret

hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题

Spring3与Hibernate4整合时出现了nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider. hibernate3的时候,用spring来控制sessionfactory用的可以是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,因为用的是hibernate4所以照猫画

51nod P1305 Pairwise Sum and Divide ——思路题

久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305<< 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = i+1 to A.length sum = sum + Floor(

hdu 2837 坑题。

Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1414    Accepted Submission(s): 291 Problem Description Assume that f(0) = 1 and 0^0=1. f(n) = (n%10)^f(n/10) for all n bigger than ze

hdu 4883 思维题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 566    Accepted Submission(s): 267 Problem Description TIANKENG manages a