华中农业大学第四届程序设计大赛网络同步赛 Arithmetic Sequence
来源:互联网 发布:电脑cpu评测软件 编辑:程序博客网 时间:2024/05/07 01:11
题目链接:http://acm.hzau.edu.cn/problem.php?cid=1009&pid=9
Problem J: Arithmetic Sequence
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1823 Solved: 317
[Submit][Status][Web Board]
Description
Giving a number sequence A with length n, you should choosing m numbers from A(ignore the order) which can form an arithmetic sequence and make m as large as possible.
Input
There are multiple test cases. In each test case, the first line contains a positive integer n. The second line contains n integers separated by spaces, indicating the number sequence A. All the integers are positive and not more than 2000. The input will end by EOF.
Output
For each test case, output the maximum as the answer in one line.
Sample Input
51 3 5 7 1084 2 7 11 3 1 9 5
Sample Output
46
HINT
In the first test case, you should choose 1,3,5,7 to form the arithmetic sequence and its length is 4.
In the second test case, you should choose 1,3,5,7,9,11 and the length is 6.
题意很简单,给我们一个序列,问我们从这个序列中选出一些数能构成的最长的等差序列的长度为多少。
这个问题我们用DP来解决,首先因为我们取数是不用关注它的顺序的,所以我们就可以先排个序,然后我们用dp[i][j]表示取到第i个数的时候间距为j的长度的等差数列最长的长度,所以我们有状态转移方程dp[i][dif] = dp[j][dif]+1
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 2000+5;int a[maxn];int dp[maxn][maxn];int main(){int n;while(scanf("%d",&n)!=EOF){for(int i=0; i<n; i++) scanf("%d",&a[i]);if(n <= 1){printf("%d\n",n);continue;}sort(a,a+n);int ans = 1;for(int i=0; i<maxn; i++)for(int j=0; j<maxn; j++)dp[i][j] = 1;for(int i=1; i<n; i++){for(int j=0; j<i; j++){int dif = a[i]-a[j];dp[i][dif] = dp[j][dif]+1;ans = max(ans, dp[i][dif]);}}//for(int i=0; i<n; i++) printf("%d ",dp[i][0]);printf("%d\n",ans);}}
- Problem J: Arithmetic Sequence——华中农业大学第四届程序设计大赛网络同步赛题目
- 华中农业大学第四届程序设计大赛网络同步赛Problem J: Arithmetic Sequence
- 华中农业大学第四届程序设计大赛网络同步赛 Arithmetic Sequence
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
- 华中农业大学第四届程序设计大赛网络同步赛题目
- 华中农业大学第四届程序设计大赛网络同步赛 J
- 华中农业大学第四届程序设计大赛网络同步赛 C
- 华中农业大学第四届程序设计大赛网络同步赛 F
- 华中农业大学第四届程序设计大赛网络同步赛
- 华中农业大学第四届程序设计大赛网络同步赛解题报告
- hzau华中农业大学第四届程序设计大赛网络同步赛F.LCS
- 华中农业大学第四届程序设计大赛网络同步赛Problem I: Catching Dogs
- Contest1009 - 华中农业大学第四届程序设计大赛网络同步赛C,H,J
- 周赛题 (华中农业大学第四届程序设计大赛网络同步赛)
- 华中农业大学第四届程序设计大赛网络同步赛 D题 Robbing Red Packets
- 华中农业大学第五届程序设计大赛网络同步赛
- 华中农业大学第五届程序设计大赛网络同步赛 H MathematicalGame
- 华中农业大学第五届程序设计大赛网络同步赛 C Friends
- SM2算法第八篇:SSL Socket通信详解
- VS2010安装MSDN(转载)
- Android之解决aidl里面函数参数太大不能跨进程通信的问题
- Sublime Text2插件之 - JSON格式化
- Python模拟登陆
- 华中农业大学第四届程序设计大赛网络同步赛 Arithmetic Sequence
- spring——控制反转简单例子
- 第十一周项目——Time类中的运算符重载
- cv::Mat& dst 这名话从语法是怎么理解?
- js中使用for 循环和 for in 遍历数组区别
- OpenGL ES Shader相关API 总结【5】——VBO与VAO的作用与关系
- css图片变色变暗变亮
- 第12周项目1——实现复数类的运算符重载
- 数据挖掘实习面经