(模拟)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 ...
随机推荐
- Sql2008 全文索引 简明教程
在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了. 下面简明的介绍如何使用Sql2008 全文索引 一.检查 ...
- cesium初始化参数
var viewer = new Cesium.Viewer('cesiumContainer',{ animation:false, //动画控制不显示 //baseLayerPicker:fals ...
- android studio 3.0.1使用笔记(二)
发布前如何生成正式签名的APK? 一,查看APK签名方法:??Preferences->Android->Build可以查看到这个默认keystore文件的位置. 二,新建正式签名过程: ...
- 温故而知新-array_walk和sizeof和array_count_values()和extract()
1 array_walk对数组的每一个元素应用任何函数 用户自定义函数中的第一个参数指定为引用:&$value,来改变数组元素的值 如果对一个参数使用取地址,那么会改变数组元素的值 2 siz ...
- 学python着几个要搞清楚WSGI和uWSGI区别
1 WSGI是一种通信协议 2 uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信. 3 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器.
- Pthreads 读写锁
▶ 使用读写锁来限制同一数据多线程读写.若任何线程拥有读锁,则其他任何请求写锁的线程将阻塞在其写锁函数的调用上:若任何线程拥有写锁,则其他任何请求读锁和写锁的线程将阻塞在其对应的锁函数上,相当于将读与 ...
- win8 机器硬盘异响
win8系统安装在ssd上,挂了一块希捷2T的机器硬盘做数据存储用,经常发生异响.类似通电断电的声音.因为硬盘的APM节能使磁头归位造成的声音. 使用CrystalDiskInfo软件禁用APM,异响 ...
- 【CentOS 6.5】 Qt Creator 启动失败
在CentOS 6.5中 点击 [应用程序]->[编程]->Qt Creator , 没有反应,Creator没有启动,转而进入Shell cd /opt/Qt5.2.1/Tools/Qt ...
- sublime text3最新版本注册码(build 3143)
—– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...
- Nginx 图片服务器搭建
安装Nginx >yum install -y nginx 安装vsftpd http://www.cnblogs.com/eason-d/p/9057389.html 2: 创建目录 /us ...