HZAU 21——Arithmetic Sequence——————【暴力 or dp】
Arithmetic Sequence
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1810 Solved: 311
[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
5
1 3 5 7 10
8
4 2 7 11 3 1 9 5
Sample Output
4
6
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.
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e3 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL;
int cnt[maxn], a[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(cnt,0,sizeof(cnt));
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
cnt[a[i]]++;
}
sort(a+1,a+1+n);
int ans = 1;
for(int i = 1; i <= n; ++i){ //enum the first item
if(cnt[a[i]] > n-i+1){
ans = max(ans, cnt[a[i]]);
break;
}
for(int j = 1; a[i] + j <= a[n]; ++j){
int d = j, c = a[i], len = 1;
while(cnt[c+d]){
c += d;
len++;
}
ans = max(ans, len);
}
}
printf("%d\n",ans); }
return 0;
}
dp做法:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e3 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL; int dp[maxn*2][maxn*2], a[2*maxn], idx[2*maxn]; //dp[i][j] meaning the length that ending up with a[i], common dif is j
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int Max = 0;
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
Max = Max < a[i] ? a[i]:Max;
}
sort(a+1,a+1+n);
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= Max; ++j){
dp[i][j] = 1;
}
}
memset(idx,0,sizeof(idx));
int res = 1;
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= Max; ++j){
if(a[i] > j){
dp[i][j] = dp[idx[a[i]-j]][j] + 1;
}
res = max(res, dp[i][j]);
}
idx[a[i]] = i;
}
printf("%d\n",res);
}
return 0;
}
HZAU 21——Arithmetic Sequence——————【暴力 or dp】的更多相关文章
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- (模拟)Arithmetic Sequence -- HDU -- 5400
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...
- hdu 5400 Arithmetic Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
- Arithmetic Sequence
Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
随机推荐
- SQL Server分页模板
SQL Server分页模板 WITH T AS ( SELECT ROW_NUMBER() OVER(ORDER BY AlbumId ) AS row_number, * FROM (SELECT ...
- Javascript:日期排班功能实现
背景: 近期,公司的产品经常会遇到日期排班类似的功能: 需求的排班日期长短不一:有些是两周,有些是四周:要求选中的时候有一个active的状态区分,另外要提供钩子获取选中日期的形如:[2018-04 ...
- python 使用跨平台文件锁
#encoding=utf-8 print '中国' #使用跨平台文件锁 import os if os.name == 'nt': import win32con,win32file,pywinty ...
- Logstash 性能及其替代方案
介绍 当谈及集中日志到 Elasticsearch 时,首先想到的日志传输(log shipper)就是 Logstash.开发者听说过它,但是不太清楚它具体是干什么事情的: 当深入这个话题时,我们才 ...
- ios 相机 自定义 相片的截取
前段时间公司需要做一个身份识别的功能,而系统相机无法满足要求,so自己自定义了. 上代码: .h文件 #import <UIKit/UIKit.h> #import <AVFound ...
- 动态sql语句和动态传入参数个数
1.可以将要传入的几个参数封装成一个实体类,然后将实体类作为一个参数传入到相应的方法中,这时候就需要这sqlMapper.xml文件中对传入的字段利用<if test=""& ...
- 两种 js下载文件的步骤
----------------------------------引用地址链接------------------------------------------------- http://www ...
- BFC概念和作用,触发条件
1.概念,全称是block format context,块级格式化上下文 2.触发条件 根元素 float属性不为none position为absolute或fixed display为inlin ...
- 2016级算法第四次上机-F.AlvinZH的最“长”公共子序列
940 AlvinZH的最"长"公共子序列 思路 DP,难题. \(dp[i][j]\) :记录A的前i个字符与B的前j个字符变成相同需要的最小操作数. 初始化:dp[i][0] ...
- Mondrian Schema Workbench 概念及常用参数
Schema Schema 定义了一个多维数据库.包含了一个逻辑模型,而这个逻辑模型的目的是为了书写 MDX 语言的查询语句.这个逻辑模型实际上提供了这几个概念: Cubes (立方体).维度( Di ...