华中农业大学第四届程序设计大赛网络同步赛 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 ...
随机推荐
- js获取url参数方法
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- Sublime package control错误:There are no packages available for installation
查了很多资料都没有解决. 改host---无效 复制一个文件的什么的,我看到版本比我的旧,就没有用 终于最后一个解决了.最终解决方案 解决: 更新下Package Control就好了: prefer ...
- 笔记-cookie参数
笔记-cookie参数 cookie各个参数详解 定义和用法 setcookie()函数向客户端发送一个 HTTP cookie. cookie是由服务器发送到浏览器的变量.cookie 通常是服务器 ...
- MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':ge
数据库表里命名有这个字段,可怎么就是报错呢,大神的解释: 加上之后立马好用!!!
- 流量操控之SSH隧道与端口转发
目 录 第1章 概述... 3 1.1. 实现命令... 3 1.2. SSH隧道类型... 3 第2章 SSH隧道... ...
- AD15添加导入组件
- 手把手教你如何逐步安装OpenStack
[TechTarget中国原创] 尽管OpenStack官方提供的在线安装教程和分步向导能够为管理员提供很大帮助,但是依然存在很多不尽如人意的地方.因此在Ubuntu上安装OpenStack的过程当中 ...
- Web 开发的未来:React、Falcor 和 ES6
Web 开发的未来:React.Falcor 和 ES6 Widen是一家数字资产管理解决方案提供商.目前,其技术栈还非常传统,包括服务器端的Java.浏览器端的AngularJS.提供REST AP ...
- 基于Python的selenuim自动化测试尝试
工作这么多年了,终于狠下心好好开始学学自动化测试相关知识,揭开这层神秘的面纱. 困难重重,障碍很多,但好在每天都多少有点小收获. 很感谢一个QQ好友推荐的虫师,也非常感谢在这个契机读到了虫师编著的&l ...
- Python 3基础教程9-函数
本文介绍Python中的函数,主要了解如何定义一个函数,如何调用一个函数. 如果上面你不写调用函数这行代码,你运行后,是没有打印输出的.我们这里来,结合前面的if语句来定义一个,两个数比较,判断最大的 ...