HZAU 21——Arithmetic Sequence——————【暴力 or dp】
Arithmetic Sequence
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1810 Solved: 311
[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.
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e3 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL;
int cnt[maxn], a[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(cnt,0,sizeof(cnt));
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
cnt[a[i]]++;
}
sort(a+1,a+1+n);
int ans = 1;
for(int i = 1; i <= n; ++i){ //enum the first item
if(cnt[a[i]] > n-i+1){
ans = max(ans, cnt[a[i]]);
break;
}
for(int j = 1; a[i] + j <= a[n]; ++j){
int d = j, c = a[i], len = 1;
while(cnt[c+d]){
c += d;
len++;
}
ans = max(ans, len);
}
}
printf("%d\n",ans); }
return 0;
}
dp做法:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 1e3 + 30;
const LL INF = 0x3f3f3f3f;
const LL mod = 9973;
typedef long long LL;
typedef unsigned long long ULL; int dp[maxn*2][maxn*2], a[2*maxn], idx[2*maxn]; //dp[i][j] meaning the length that ending up with a[i], common dif is j
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int Max = 0;
for(int i = 1; i <= n; ++i){
scanf("%d",&a[i]);
Max = Max < a[i] ? a[i]:Max;
}
sort(a+1,a+1+n);
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= Max; ++j){
dp[i][j] = 1;
}
}
memset(idx,0,sizeof(idx));
int res = 1;
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= Max; ++j){
if(a[i] > j){
dp[i][j] = dp[idx[a[i]-j]][j] + 1;
}
res = max(res, dp[i][j]);
}
idx[a[i]] = i;
}
printf("%d\n",res);
}
return 0;
}
HZAU 21——Arithmetic Sequence——————【暴力 or dp】的更多相关文章
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- (模拟)Arithmetic Sequence -- HDU -- 5400
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...
- hdu 5400 Arithmetic Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
- Arithmetic Sequence
Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
随机推荐
- 一行代码搞定Dubbo接口调用
本文来自网易云社区 作者:吕彦峰 在工作中我们经常遇到关于接口测试的问题,无论是对于QA同学还是开发同学都会有远程接口调用的需求.针对这种问题我研发了一个工具包,专门用于远程Dubbo调用,下面就让我 ...
- Mysql内置功能《四》存储过程
存储过程 一 存储过程介绍 存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql 使用存储过程的优点: #1. 用于替代程序写的SQL语句,实 ...
- tf-idf sklearn
第一步:语料转化为词袋向量 step 1. 声明一个向量化工具vectorizer: 本文使用的是CountVectorizer,默认情况下,CountVectorizer仅统计长度超过两个字符的词, ...
- python 小点
python中列表不能除以列表,列表不能除以整数.浮点数. numpy数组可以实现数组除以整数.
- is 与 == 的区别;小数据池; 编码与解码
1, is 与 == 的区别 == 比较的是两边的值 is 比较的是两边的地址 id () 2,小数据池(在终端中) 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址 ...
- scrapy框架基于CrawlSpider的全站数据爬取
引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...
- php面向对象编程_1
1, php面向对象编程的三大特征: (1) 封装性,封装就是把抽象出的数据和对数据的操作封装在一起,数据被保护在内部,程序的其它部分只有通过被授权的操作(成员方法)才能对数据进行操作. (2) 继承 ...
- [ActionScript 3.0] 自定义右键菜单
将自定义右键菜单的一些属性和方法归纳到AddRightMenu.as,通过实例化此类,调用相关方法即可测试! package { import flash.display.Sprite; import ...
- Ionic2的CLI的命令行
http://blog.csdn.net/qq_33315185/article/details/68067747 在我们开发Ionic app的时候 CLI 是一个非常重要的工具.CLI包含了很多开 ...
- POJ1475 Pushing Boxes(双搜索)
POJ1475 Pushing Boxes 推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...