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 ...
随机推荐
- Backup--压缩备份和TDE
1>对启用TDE的数据库,压缩备份的备份文件大小与未压缩备份的备份文件大小差不多(压缩比为 1 ) 2>对启用TDE的数据库,压缩备份的备份时间远高于未压缩备份 2>对启用TDE的数 ...
- 用FileZilla服务器端和客户端实现本机与虚拟机之间文件上传和下载
1. FileZilla简介 2.准备工作3.安装 FileZilla Server和配置3.1.问题及解决方法3.2.添加目录3.3.测试FIP4.安装FileZilla Client5.连接服务器 ...
- “全栈2019”Java第七十五章:内部类持有外部类对象
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 【SSO单点系列】开篇
年底将至,忙碌了好几个月的项目也接近尾声了.在这个项目中,由于要和其他外系统做单点登录(SSO),整合其他系统的功能.在网上查询了相关资料后,最终选取了Yale大学发起的一个开源项目 CAS, 作为项 ...
- webpack构建多页面react项目(webpack+typescript+react)
目录介绍 src:里面的每个文件夹就是一个页面,页面开发相关的组件.图片和样式文件就存放在对应的文件夹下. tpl:里面放置模板文件,当webpack打包时为html-webpack-plugin插件 ...
- @functools.wrapes
保证被装饰函数的__name__属性不变
- BZOJ 2457 双端队列
Sherry 现在碰到了一个棘手的问题,有N个整数需要排序. Sherry 手头能用的工具就是若干个双端队列. 她需要依次处理这 N 个数,对于每个数, Sh ...
- [转] Vagrant入门
[From] https://www.cnblogs.com/davenkin/p/vagrant-virtualbox.html 简单地说,Vagrant让我们可以通过代码的方式快速地.可重复地创建 ...
- Django get_object ,get_queryset方法
Django提供了很多通用的基于类的视图(Class Based View),可以帮我们简化执行以下操作的代码.这些基于类的视图还提供了get_queryset, get_context_data和g ...
- Servlet入门小案例
案例一:tomcat9.jdk1.8 1.eclipse创建web项目 1)创建一个Dynamic web project,名字为Servlet_hjh 2)在src下创建一个包,为com.hjh.d ...