华中农业大学第四届程序设计大赛网络同步赛 J
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的更多相关文章
- [HZAU]华中农业大学第四届程序设计大赛网络同步赛
听说是邀请赛啊,大概做了做…中午出去吃了个饭回来过掉的I.然后去做作业了…… #include <algorithm> #include <iostream> #include ...
- (hzau)华中农业大学第四届程序设计大赛网络同步赛 G: Array C
题目链接:http://acm.hzau.edu.cn/problem.php?id=18 题意是给你两个长度为n的数组,a数组相当于1到n的物品的数量,b数组相当于物品价值,而真正的价值表示是b[i ...
- 华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列
Problem G: Array C Time Limit: 1 Sec Memory Limit: 128 MB Description Giving two integers and and ...
- 华中农业大学第四届程序设计大赛网络同步赛 I
Problem I: Catching Dogs Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1130 Solved: 292[Submit][St ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- 华中农业大学第五届程序设计大赛网络同步赛-L
L.Happiness Chicken brother is very happy today, because he attained N pieces of biscuits whose tast ...
- 华中农业大学第五届程序设计大赛网络同步赛-K
K.Deadline There are N bugs to be repaired and some engineers whose abilities are roughly equal. And ...
- 华中农业大学第五届程序设计大赛网络同步赛-G
G. Sequence Number In Linear algebra, we have learned the definition of inversion number: Assuming A ...
- 华中农业大学第五届程序设计大赛网络同步赛-D
Problem D: GCD Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 179 Solved: 25[Submit][Status][Web B ...
随机推荐
- 基于mybatis设计简单信息管理系统---jsp页面
1.在设计编辑界面的时候需要有一个下拉的列表页,想要他指定到指定的值: <select id="categoryId" name="categoryId" ...
- ethereum(以太坊)(十二)--应用(二)__投票(基础总和)
编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约 pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; contract vo ...
- Centos7安装Mysql5.7并修改初始密码
1.CentOS 的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件. wget https://dev.mysql.com/get/mysql57-community-r ...
- BurpSuite 的使用
最好用的抓包软件, 不只是抓包软件 IE/Chrome中设置代理的方法是, 打开Internet选项面板->连接->局域网设置->取消勾选的使用自动配置脚本->勾选为LAN使用 ...
- Linux命令、权限
一.新建用户natasha,uid为1000,gid为555,备注信息为“master”: groupadd -g 555 natasha useradd -u 1000 -g 555 -c mast ...
- JSP---JSTL核心标签库的使用
JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...
- Windows usb设备正在使用中
每次插上u盘之后,弹出的时候,总是提示正在使用中.后来我发现了技巧, 1.打开任务管理器 2.打开底部的 打开资源监视器 按钮 3.然后点击磁盘 4.再次弹出usb 就可以弹出了.
- 如何在 Eclipse 中使用插件构建 PHP 开发环境[转]
原文出处: http://hykloud.com/2012/03/08/information_technology/how-setup-eclipse-php-pdt-remote-system-e ...
- Google Chrome 自定义协议(PROTOCOL)问题的处理
最近在使用谷歌浏览器的时候遇到了自定义协议(PROTOCOL)的问题,比较折腾,特此记录,希望我浪费生命换来的结果能够帮助读到此文的朋友少浪费一点宝贵的时间! 由于某些原因,电脑里一直没有安装阿里旺旺 ...
- 《数据结构与算法分析:C语言描述》复习——第五章“堆”——二叉堆
2014.06.15 22:14 简介: 堆是一种非常实用的数据结构,其中以二叉堆最为常用.二叉堆可以看作一棵完全二叉树,每个节点的键值都大于(小于)其子节点,但左右孩子之间不需要有序.我们关心的通常 ...