Problem J: Arithmetic Sequence

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1766  Solved: 299
[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.

题意: 给你一个序列 输出 序列中最长等差数列的长度

题解:

1.暴力 sort排序一下 然后暴力枚举每一个步长 坑点(注意相同的数  也就是等差可以为0)

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int n;
int mp[];
int exm;
int ans;
int q;
int maxn;
int main()
{
while(scanf("%d",&n)!=EOF)
{
ans=-;
maxn=-;
for(int i=;i<=;i++)
mp[i]=;
for(int i=;i<n;i++)
{
scanf("%d",&exm);
mp[exm]++;
if(ans<mp[exm])
ans=mp[exm];
if(maxn<exm)
maxn=exm;
}
for(int i=;i<=maxn;i++)
{
if(mp[i])
{
for(int d=;d<=maxn;d++)
{
int gg=i+d;
q=;
while(gg)
{
if(gg>maxn)
break;
if(mp[gg])
q++;
else
break;
gg=gg+d;
}
if(q>ans)
ans=q;
if(gg>maxn)
break;
}
}
}
cout<<ans<<endl;
}
return ;
}

2.dp处理 (yan代码) dp[i][j] 表示以i为结尾 j为等差的方法数目

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<vector>
#include<map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = +, M = 1e6+, mod = 1e9+,inf = 1e9;
typedef long long ll; int n,a[N];
int dp[N][N];
int main() {
while(scanf("%d",&n)!=EOF) {
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
for(int j=;j<=n;j++)
for(int i=;i<=;i++) dp[j][i] = ;
for(int i=;i<=n;i++) {
for(int j=;j<i;j++) {
dp[i][a[i]-a[j]] = max(dp[j][a[i]-a[j]]+,dp[i][a[i]-a[j]]);
}
}
int ans = ;
for(int j=;j<=n;j++)
for(int i=;i<=;i++) {
ans = max(ans,dp[j][i]);
}
printf("%d\n",ans);
}
return ;
}

华中农业大学第四届程序设计大赛网络同步赛 J的更多相关文章

  1. [HZAU]华中农业大学第四届程序设计大赛网络同步赛

    听说是邀请赛啊,大概做了做…中午出去吃了个饭回来过掉的I.然后去做作业了…… #include <algorithm> #include <iostream> #include ...

  2. (hzau)华中农业大学第四届程序设计大赛网络同步赛 G: Array C

    题目链接:http://acm.hzau.edu.cn/problem.php?id=18 题意是给你两个长度为n的数组,a数组相当于1到n的物品的数量,b数组相当于物品价值,而真正的价值表示是b[i ...

  3. 华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列

    Problem G: Array C Time Limit: 1 Sec  Memory Limit: 128 MB Description Giving two integers  and  and ...

  4. 华中农业大学第四届程序设计大赛网络同步赛 I

    Problem I: Catching Dogs Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1130  Solved: 292[Submit][St ...

  5. 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;

    1020: Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MB Submit:  ->打开链接<- Descriptio ...

  6. 华中农业大学第五届程序设计大赛网络同步赛-L

    L.Happiness Chicken brother is very happy today, because he attained N pieces of biscuits whose tast ...

  7. 华中农业大学第五届程序设计大赛网络同步赛-K

    K.Deadline There are N bugs to be repaired and some engineers whose abilities are roughly equal. And ...

  8. 华中农业大学第五届程序设计大赛网络同步赛-G

    G. Sequence Number In Linear algebra, we have learned the definition of inversion number: Assuming A ...

  9. 华中农业大学第五届程序设计大赛网络同步赛-D

    Problem D: GCD Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 179  Solved: 25[Submit][Status][Web B ...

随机推荐

  1. Linux实战教学笔记15:用户管理初级(下)

    第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...

  2. LNMP+HAProxy+Keepalived负载均衡 - 基础服务准备

    日志服务 修改日志服务配置并重启日志服务: ``` vim /etc/rsyslog.conf ``` 编辑系统日志配置,指定包含的配置文件路径和规则: ``` $IncludeConfig /etc ...

  3. python 一些基础知识

    Python 注释的原理: 原理:根据对象的引用计数器,对象创建会给对象一个引用计数器属性.如果该属性的值为0,那么该对象会被释放.创建一个字符串对象,但是没有任何引用,计数器为0. Python小整 ...

  4. ubuntu中使用apt命令安装ipython失败解决方案

    在最近使用ubuntu安装ipython时,出现如下报错: 出现这个问题,主要是因为apt还在运行,故解决方案为: 1.找到并且杀掉所有的apt-get 和apt进程 运行下面的命令来生成所有含有 a ...

  5. idea debug启动项目慢或者启动不了

    使用debug无法启动项目但是使用run就可以启动程序,而且启动比以前的debug模式快的多 原因: 启动不了的原因是在项目中的方法上打了断点,导致项目无法继续编译 取消方法断点就可以了 在idea官 ...

  6. spark中的RDD以及DAG

    今天,我们就先聊一下spark中的DAG以及RDD的相关的内容 1.DAG:有向无环图:有方向,无闭环,代表着数据的流向,这个DAG的边界则是Action方法的执行 2.如何将DAG切分stage,s ...

  7. CSS3 Flexbox(伸缩盒/弹性盒模型)可视化指南

    在http://css.doyoe.com/(CSS参考手册)中,本文对应其中的伸缩盒 引入 Flexbox布局官方称为CSS Flexible Box Layout Module是一个CSS3新的布 ...

  8. 3 网格 landing page

    0.大框架 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  9. setBackgroundResource和setImageResource的区别

    setBackgroundResource是设置view的背景图片 setImageResource是设置ImageView的图片 对于一个imagevewButton来说,你既可以setBackgr ...

  10. java.math.BigDecimal cannot be cast to java.lang.String解决方法

    从mysql数据库里取decimal(18,2)封装到Map<String,String>中 BigDecimal b = new BigDecimal(resultMap.get(&qu ...