华中农业大学第四届程序设计大赛网络同步赛 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 ...
随机推荐
- expect配合shell 实现自动分发秘钥文件
expect使用场景 有时候需要批量地执行一些操作,或者执行自动化的操作的时候,有些指令需要交互式地进行这就会有很多麻烦,linux下有一个程序交expect,它可以模拟键盘输入文本,省去人工干预交互 ...
- 日期格式兼容iOS
iOS不支持2016-02-11 12:21:12格式的日期 目前Safari可以支持的标准格式: MM-dd-yyyy yyyy/MM/dd MM/dd/yyyy MMMM dd, yyyy MMM ...
- JavaScript数组常用的方法
改变原数组: ※ push,pop,shif,unshift,sort,reverse ※ splice 不改变原数组: ※ concat,join→split,toString,slice push ...
- MySQL 如何生成月份表
MySQL 如何生成月份表 如果遇到按照月份统计信息的时候,常用的统计方式就是用month表去连接order表,下面就是生成月份表的过程 1.首先是建表 CREATE TABLE `sn_month` ...
- PHP使用redis(一)
1,connect 描述:实例连接到一个Redis.参数:host: string,port: int返回值:BOOL 成功返回:TRUE;失败返回:FALSE <?php $redis = ...
- 用Go实现RabbitMQ消息收发
// amqp.Dial accepts a string in the AMQP URI format and returns a new Connection over TCP using Pla ...
- 17-比赛1 D - IPC Trainers (贪心 + 优先队列)
题目描述 本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日程安排有 M 天,每天最多上一节课.第 i 名教练在第 Di 天到达,直到训练营结 ...
- Git-Git库管理
对象和引用哪里去了? 从GitHub上克隆一个示例版本库,这个版本库在"历史穿梭"一章就已经克隆过一次了,现在要重新克隆一份.为了和原来的克隆相区别,克隆到另外的目录.执行下面的命 ...
- Samba和NFS文件共享
SAMBA文件共享服务 通过Yum软件仓库来安装Samba服务程序 [root@zhangjh ~]# yum install samba -y Samba 配置文件注释信息较多,为了便于配置,因此先 ...
- imageX.exe
imageX 编辑ImageX 是一个命令行工具,原始设备制造商 (OEM) 和公司可以使用它来捕获.修改和应用基于文件的磁盘映像以进行快速部署.ImageX 可以使用 Windows 映像 (.wi ...