Problem Killer(递推)
来源:互联网 发布:先锋软件职业学院图片 编辑:程序博客网 时间:2024/05/17 02:21
Link:http://acm.hdu.edu.cn/showproblem.php?pid=5328
Problem Killer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 153 Accepted Submission(s): 61
Problem Description
You are a "Problem Killer", you want to solve many problems.
Now you haven problems, the i -th problem's difficulty is represented by an integer ai (1≤ai≤109 ).
For some strange reason, you must choose some integerl and r (1≤l≤r≤n ), and solve the problems between the l -th and the r -th, and these problems' difficulties must form an AP (Arithmetic Progression) or a GP (Geometric Progression).
So how many problems can you solve at most?
You can find the definitions of AP and GP by the following links:
https://en.wikipedia.org/wiki/Arithmetic_progression
https://en.wikipedia.org/wiki/Geometric_progression
Now you have
For some strange reason, you must choose some integer
So how many problems can you solve at most?
You can find the definitions of AP and GP by the following links:
https://en.wikipedia.org/wiki/Arithmetic_progression
https://en.wikipedia.org/wiki/Geometric_progression
Input
The first line contains a single integer T , indicating the number of cases.
For each test case, the first line contains a single integern , the second line contains n integers a1,a2,⋯,an .
T≤104,∑n≤106
For each test case, the first line contains a single integer
Output
For each test case, output one line with a single integer, representing the answer.
Sample Input
251 2 3 4 6101 1 1 1 1 1 2 3 4 5
Sample Output
46
Source
2015 Multi-University Training Contest 4
分析:该题求的是区间中连续的数组元素所能构成等差数列或等比数列的最长长度是多少。主要解题思想是用f1[i]、f2[i]表示以a[i]结尾的最长等差数列、最长等比数列的长度,然后 利用等差数列、等比数列的性质,可得到若能构成等差数列或等比数列,则相应的有递推公式f1[i]=f1[i-1]+1或f2[i]=f2[i-1]+1。详见代码注释。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<map>#include<cmath>#define LL long long#define MAXN 1000010using namespace std;LL a[MAXN],f1[MAXN],f2[MAXN];//f1[i]、f2[i]表示以a[i]结尾的最长等差数列、最长等比数列的长度 int main(){int t,n,i,j;LL ans;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++){scanf("%lld",&a[i]);}if(n<=2){ans=n;}else{ans=2;for(i=1;i<=n;i++){f1[i]=2;f2[i]=2;}for(i=3;i<=n;i++){if(a[i-1]*2==a[i-2]+a[i])//等差数列性质 {f1[i]=f1[i-1]+1;}if(a[i]*a[i-2]==a[i-1]*a[i-1])// 等比数列性质 {f2[i]=f2[i-1]+1;}ans=max(ans,max(f1[i],f2[i]));}}printf("%lld\n",ans);}return 0;}
0 0
- Problem Killer(递推)
- hdu 6143 Killer Names (组合数,递推)
- hdu 5328 Problem Killer(水题)
- hdu 5328 Problem Killer (水)
- HDU 5328 Problem Killer(简单DP)
- uva10401Injured Queen Problem(递推)
- hdu-2606 Renovation Problem(递推)
- uva 11898 - Killer Problem
- UVA - 11898 Killer Problem
- UVA11898 - Killer Problem(暴力)
- HDU 5328 Problem Killer
- HDU5328.Problem Killer
- hdu5328 Problem Killer
- 【HDOJ 5328】 Problem Killer
- HDU 5328 Problem Killer
- hdu 5328 Problem Killer
- hdu 5328 Problem Killer
- HDU 5238 Problem Killer
- 【读书笔记】iOS-UIFont-动态下载系统提供的多种中文字体网址
- hdu 5317 多校第三场
- A20 操作GPIO口
- 用apache文件上传组件commons-io-2.4.jar commons-fileupload-1.2.1.jar进行文件上传
- android沉浸式状态栏实现细节
- Problem Killer(递推)
- json字符串转换为多级Map -list-map的形式
- 现代应用密码学中椭圆曲线求点集E以及点乘算法的java代码实现
- SQL Server 2008 Windows身份验证改为混合模式身份验证
- easyUI Tabs
- 通过JDBC/ODBC方法连接SQL Servers与MyEclipe(SQL)
- windows下Mysql解决Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
- 很好的网站
- hdoj 2032 杨辉三角