51Nod 1055 最长等差数列 (dp+哈希)
基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题
N个不同的正整数,找出由这些数组成的最长的等差数列。
例如:1 3 5 6 8 9 10 12 13 14
等差子数列包括(仅包括两项的不列举)
1 3 5
1 5 9 13
3 6 9 12
3 8 13
5 9 13
6 8 10 12 14
其中6 8 10 12 14最长,长度为5。
Input
第1行:N,N为正整数的数量(3 <= N <= 10000)。
第2 - N+1行:N个正整数。(2<= A[i] <= 10^9)
Output
最长等差数列的长度。
Input示例
10
1
3
5
6
8
9
10
12
13
14
Output示例
5
#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<hash_map>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int maxn = 10002;
short dp[10002][10002];
int a[10002];
int n;
const int BUCKET_SIZE = 42839;
const int NEXT_POS = 7;
typedef struct _my_hash
{
int _bucket[BUCKET_SIZE];
int _key[BUCKET_SIZE];
_my_hash()
{
memset(_bucket, 0, sizeof(_bucket[0]) * BUCKET_SIZE);
memset(_key, 0, sizeof(_key[0]) * BUCKET_SIZE);
}
void insert(int key, int value)
{
int mod = key % BUCKET_SIZE;
while (_key[mod] != 0)
{
mod = (mod + NEXT_POS) % BUCKET_SIZE;
}
_bucket[mod] = value;
_key[mod] = key;
}
int find(int key)
{
int origin = key % BUCKET_SIZE;
int mod = key % BUCKET_SIZE;
while (_key[mod] != 0)
{
if (_key[mod] == key)
{
return _bucket[mod];
}
mod = (mod + NEXT_POS) % BUCKET_SIZE;
if (mod == origin)
{
return -1;
}
}
return -1;
}
}my_hash_t;
my_hash_t h;
short d(int j,int k) {
if(dp[j][k]>=0) return dp[j][k];
dp[j][k]=2;
int ai=a[j]-(a[k]-a[j]);
int i=h.find(ai);
if(i<1) return dp[j][k];
dp[j][k]=d(i,j)+1;
return dp[j][k];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLIN
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++) h.insert(a[i],i);
short ans=-1;
for(int j=1;j<n;j++) {
for(int k=j+1;k<=n;k++) {
int i=h.find(2*a[j]-a[k]);
if(i<0) continue;
dp[j][k]=dp[i][j]+1;
ans = max(ans,dp[j][k]);
}
}
cout<<ans+2<<endl;
}
51Nod 1055 最长等差数列 (dp+哈希)的更多相关文章
- 51 nod 1055 最长等差数列(dp)
1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 ...
- 51nod 1055 最长等差数列
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1055 题意: 思路:先固定一个位置,然后从该中心点出发向两边扫,确实很难 ...
- 【51Nod】1055 最长等差数列 动态规划
[题目]1055 最长等差数列 [题意]给定大小为n的互不不同正整数集合,求最长等差数列的长度.\(n \leq 10000\). [算法]动态规划 两个数之间的差是非常重要的信息,设\(f_{i,j ...
- 51nod-1055-最长等差数列(dp+优化)
1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 收藏 关注 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 ...
- 51nod 1055:最长等差数列
1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 收藏 取消关注 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 ...
- 51nod1055 最长等差数列
完全一脸懵逼!.dp[i][j]表示i,j为相邻的两项的最大值.两个指针两边扫的思想好劲啊这个!%%% #include<cstdio> #include<cstring> # ...
- 51Nod - 1055:最长等差数列 (求最长的等差数列)
N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 10 12 13 14 等差子数列包括(仅包括两项的不列举) 1 3 5 1 5 9 13 3 6 9 12 ...
- 『最长等差数列 线性DP』
最长等差数列(51nod 1055) Description N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 10 12 13 14 等差子数列包括(仅包括两项的不 ...
- 51nod 1376 最长上升子序列的数量 | DP | vector怒刷存在感!
51nod 1376 最长上升子序列的数量 题解 我们设lis[i]为以位置i结尾的最长上升子序列长度,dp[i]为以位置i结尾的最长上升子序列数量. 显然,dp[i]要从前面的一些位置(设为位置j) ...
随机推荐
- crm--rbac权限组件使用步骤
本人的权限组件码云地址:https://gitee.com/shiguanggege/rbac 里面有文档详细介绍权限组件的使用步骤
- 使用python的ctypes库实现内存的动态申请和释放
1.申请前内存占用情况 2.申请内存 from ctypes import * import time #在这里申请1G的内存,单位k mem = create_string_buffer(1024* ...
- Array Product CodeForces - 1042C (细节)
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> ...
- 关于CPU的一些操作(CPU设置超频)
常见的几种CPU模式: .ondemand:系统默认的超频模式,按需调节,内核提供的功能,不是很强大,但有效实现了动态频率调节,平时以低速方式运行,当系统负载提高时候自动提高频率.以这种模式运行不会因 ...
- js中数组的定义方法及注意事项(转)
1.数组的创建 var name= new Array(); //创建一个数组 name[0]="zhangsan"; //给数组赋值 name[1]="lisi&q ...
- div布局(持续更新)
1. 效果: 代码: <!DOCTYPE html> <html> <head> <meta name="viewport" conten ...
- Http请求头缓存设置方法
1.直接在.aspx页面中设置最直接的,在.aspx页面中添加一行如下代码: <%@ OutputCache Duration="3600" VaryByParam=&quo ...
- 你不知道的css各类布局(四)之响应式布局
响应式布局 概念 响应式布局指的是同一页面在不同屏幕尺寸下有不同的布局 布局特点 响应式设计的目标是确保一个页面在所有终端上(各种尺寸的PC.手机.手表.冰箱的Web浏览器等等)都能显示出令人满意的效 ...
- shiro学习(三,shiro加密)
shiro加密 使用MD5加密 认证 //自定义的Realm 域 public class CustomRealmSecret extends AuthorizingRealm { @Overrid ...
- 养成一个SQL好习惯
要知道sql语句,我想我们有必要知道sqlserver查询分析器怎么执行我么sql语句的,我么很多人会看执行计划,或者用profile来监视和调优查询语句或者存储过程慢的原因,但是如果我们知道查询分析 ...