华中农业大学第四届程序设计大赛网络同步赛 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 ...
随机推荐
- 一张思维导图带你梳理HashMap相关知识
HashMap可以说是java中最常见也是最重要的key-value存储结构类,很多程序员可能经常用,但是不一定清楚这个类背后的数据结构和相关操作原理,为了复习HashMap相关的知识,今天花了一天的 ...
- 事件监听和window.history以及自定义创建事件
1.事件监听window.addEventListener方法: Window.addEventListener(event, function, useCapture); useCapture:表示 ...
- 柱状图多系列php动态实现(ec)
<?php require_once 'data.php'; $arr1=$a->sum('answer','ask_id=1'); $arr2=$a->sum('answer',' ...
- 微信小程序禁止下拉_解决小程序下拉出现空白的情况
微信小程序禁止下拉 在微信小程序中,用力往下拉动,页面顶部会出现一段空白的地方. 产品的需求不太允许这么做,会影响用户体验,查看文档发现可以使用enablePullDownRefresh这属性来实现, ...
- C指针(1)——指针在数组中的应用(程序讲解)
2-1.c数组指针的定义: #include <stdio.h> int main() { char str[]="China Beijing Fujian"; //定 ...
- poj 3087 直接模拟
题意:意思就是,s1,和s2两堆牌,然后先s2一张再s1,最后会出现一个s12序列,例如s1 AHAH S2 HAHA 然后s12为HAAHHAAH,然后前面一部分给s1,后面一部分给s2,然后再重复 ...
- BitLocker:如何启用网络解锁
TechNet 库Windows ServerWindows Server 2012 R2 和 Windows Server 2012服务器角色和技术安全和保护BitLockerBitLocker 中 ...
- Jenkins拾遗--第三篇(用户权限管理)
采访过很多实用Jenkins的同学,发现Jenkins的安全是一个很薄弱的地方.很多公司用作生产部署的Jenkins安全管理都不是很规范,就更别提测试用的Jenkins了. 其实Jenkins是一个很 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目5
2014-04-28 22:44 题目:猜数字游戏.四个数字,每个都是0~9之间.你每猜一次,我都告诉你,有多少个位置和数字都对(全对),有多少个位置错数字对(半对).比如“6309”,你猜“3701 ...
- 通过slf4j/log4j的MDC/NDC 实现日志追踪
在分布式系统或者较为复杂的系统中,我们希望可以看到一个客户请求的处理过程所涉及到的所有子系统\模块的处理日志. 由于slf4j/log4j基本是日志记录的标准组件,所以slf4j/log4j成为了我的 ...