(模拟)Arithmetic Sequence -- HDU -- 5400
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5400
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 875 Accepted Submission(s): 386
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.
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).
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h> #define N 100005
#define LL long long int a[N], dp[N]; int main()
{
int n, d1, d2; while(scanf("%d%d%d", &n, &d1, &d2)!=EOF)
{
int i; for(i=; i<=n; i++)
scanf("%d", &a[i]); memset(dp, , sizeof(dp)); for(i=; i<n; i++)
{
if(a[i+]==a[i]+d1)
dp[i+] = ;
else if(a[i+]==a[i]+d2)
dp[i+] = ;
else dp[i+] = ;
} LL ans=, tmp=; for(i=; i<=n; i++)
{
if(dp[i]==)
{
if(dp[i-]==) tmp = ;
else tmp++; ans = ans + tmp +;
}
else if(dp[i]==)
{
tmp++;
ans = ans + tmp + ;
}
else
{
ans ++;
tmp = ;
}
} printf("%lld\n", ans); }
return ;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 110000 int a[N]; int main()
{
int n, i, x, y, d1, d2; while(scanf("%d%d%d", &n, &d1, &d2)!=EOF)
{
__int64 s1, s2, sum; s1 = s2 = sum = ;
memset(a, , sizeof(a)); scanf("%d", &x); for(i=; i<n; i++)
{
scanf("%d", &y);
a[i] = y-x;
x = y;
} for(i=; i<n; i++)
{
if(a[i]==d1)
{
if(a[i-]!=d1) s1 = ; s1++;
sum += s1; ///sum加上当前公差为的d1序列长度
s2 = ; ///s2进行清零
}
else if(a[i]==d2)
{
s2++;
sum += s1 + s2; ///加上公差为d2和前半段为d1后半段为d2的序列长度
}
else
s1 = s2 = ;
} printf("%I64d\n", sum+n); ///n是只有一个元素的时候
} return ;
}
(模拟)Arithmetic Sequence -- HDU -- 5400的更多相关文章
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- hdu 5400 Arithmetic Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- HZAU 21——Arithmetic Sequence——————【暴力 or dp】
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1810 Solved: 311[Submit][Status] ...
- hdu 5400(思路题)
Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
随机推荐
- 手写简易WEB服务器
今天我们来写一个类似于Tomcat的简易服务器.可供大家深入理解一下tomcat的工作原理,本文仅供新手参考,请各位大神指正!首先我们要准备的知识是: Socket编程 HTML HTTP协议 服务器 ...
- unittest框架出报告乱码的问题解决
跟着上面的步骤修改好后,unittest断言写法要写成下面这样才能展示非乱码
- 学习笔记之Python 3 教程
Python 3 教程 http://www.runoob.com/python3/python3-tutorial.html Python的3.0版本,常被称为Python 3000,或简称Py3k ...
- springboot的interceptor(拦截器)的应用
一.SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先 ...
- 有了 itchat, python 调用微信个人号从未如此简单(新增 py3 支持)
itchat 是一个开源的微信个人号接口. 近期完成了 py3 与文档的完善,欢迎各位使用与测试. 使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人. 当然,该 api 的使用远不止 ...
- eclipse自动生成.apt_generated、factory path
- Java Reference & ReferenceQueue一览
Overview The java.lang.ref package provides more flexible types of references than are otherwise ava ...
- Kubernetes v1.10.x HA 全手动安装教程(TL;DR)
转自 https://www.kubernetes.org.cn/3814.html 本篇延续过往手动安装方式来部署 Kubernetes v1.10.x 版本的 High Availability ...
- go_goroutine and channel
协程Coroutine 轻量级”线程“ 非抢占式多任务处理,由协程主动交出控制权(如果程序中间没有print,程序会一直霸占着调度器) 编译器/解释器/虚拟机层面的多任务 多个协程可能在一个或多个线程 ...
- 数论知识总结——史诗大作(这是一个flag)
1.快速幂 计算a^b的快速算法,例如,3^5,我们把5写成二进制101,3^5=3^1*1+3^2*2+3^4*1 ll fast(ll a,ll b){ll ans=;,a=mul(a,a)))a ...