Arithmetic Sequence(dp)
Arithmetic Sequence
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 51 Solved: 19
[Submit][Status][Web Board]
Description
Giving a number sequence A with length n, you should choosingm 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 nintegers separated by spaces, indicating the number sequenceA. 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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[][];
int a[];
int main(){
int N;
while(~scanf("%d", &N)){
for(int i = ; i < N; i++){
scanf("%d", a + i);
}
sort(a, a + N);
int ans = ;
for(int i = ; i < N; i++){
for(int k = ; k <= ; k++){
dp[i][k] = ;
}
}
for(int i = ; i < N; i++){
for(int j = ; j < i; j++){
int d = a[i] - a[j];
dp[i][d] = dp[j][d] + ;
ans = max(ans, dp[i][d] + );
}
}
printf("%d\n", ans);
}
return ;
}
java:
import java.util.Arrays;
import java.util.Scanner; public class ArithmeticSequence{
static int[][] dp = new int[][];
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int N;
while(cin.hasNext()){
N = cin.nextInt();
int[] a = new int[N];
for(int i = ; i < a.length; i++){
a[i] = cin.nextInt();
}
Arrays.sort(a);
int ans = ;
for(int i = ; i < N; i++){
for(int k = ; k <= ; k++){
dp[i][k] = ;
}
}
for(int i = ; i < a.length; i++){
for(int j = ; j < i; j++){
int d = a[i] - a[j];
dp[i][d] = dp[j][d] + ;
ans = Math.max(ans, dp[i][d] + );
}
}
System.out.println(ans);
}
}
}
dp;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[];
int a[];
int main(){
int N;
while(~scanf("%d", &N)){
for(int i = ; i < N; i++){
scanf("%d", a + i);
}
sort(a, a + N);
int ans = ;
for(int i = ; i <= ; i++){
for(int k = ; k <= ; k++){
dp[k] = ;
}
for(int j = ; j < N; j++){
if(a[j] >= i)dp[a[j]] = max(dp[a[j] - i] + , dp[a[j]]);
else dp[a[j]] = ;
ans = max(ans, dp[a[j]]);
}
}
printf("%d\n", ans);
}
return ;
}
Arithmetic Sequence(dp)的更多相关文章
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- cf13C Sequence(DP)
题意: N个数.a1...aN. 对于每个数而言,每一步只能加一或减一. 问最少总共需要多少步使得新序列是非递减序列. N (1 ≤ N ≤ 5000) 思路: *一个还不知道怎么证明的结论(待证): ...
- codeforces 486 E. LIS of Sequence(dp)
题目链接:http://codeforces.com/contest/486/problem/E 题意:给出n个数,如果一个数满足不属于最长递增序列,那么输出1,如果属于最长递增序列但是不属于所有最长 ...
- URAL 1183 Brackets Sequence(DP)
题目链接 题意 : 给你一串由括号组成的串,让你添加最少的括号使该串匹配. 思路 : 黑书上的DP.dp[i][j] = min{dp[i+1][j-1] (sh[i] == sh[j]),dp[i] ...
- 【ZJOI2017 Round1练习】D8T2 sequence(DP)
题意: 思路: #include <algorithm> #include <iostream> #include <cstring> #include <c ...
- Atcoder E - RGB Sequence(dp)
题目链接:http://arc074.contest.atcoder.jp/tasks/arc074_c 题意:一共有3种颜色,红色,绿色,蓝色.给出m个要求l,r,x表示在区间[l,r]内要有x种不 ...
- 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...
- 【UVa】Wavio Sequence(dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- Go程序GC优化经验分享
http://1234n.com/?post/yzsrwa 最近一段时间对<仙侠道>的服务端进行了一系列针对GC的调优,这里跟各位分享一下调优的经验. 游戏第一次上线的时候,大部分精力都投 ...
- neural style论文解读
相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 概述 在艺术领域,艺术家 ...
- python进阶之路4.2---装饰器
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Javascript高级程序设计读书笔记(第六章)
第6章 面向对象的程序设计 6.2 创建对象 创建某个类的实例,必须使用new操作符调用构造函数会经历以下四个步骤: 创建一个新对象: 将构造函数的作用域赋给新对象: 执行构造函数中的代码: 返回新 ...
- 短信猫编程的一些资料1(At指令发送短信)
现在正在做TC35的项目, 下面分享一下这几天在网上找到的资料: 手机 SMS PDU 格式参考手册 1.相关的GSM AT指令 与SMS有关的GSM AT指令(from GSM0 ...
- 关于DIPS的MVC 4.0项目发布与在IIS 7.0上的部署的方法
本人技术笨拙,今天在发布DIPS的MVC4.0项目,并部署到IIS上,遇到各种问题.在查询相关资料后,最终得以解决,所以想把这个过程记录下来. 首先是MVC4.0项目的发布: 打开你的VS2012网站 ...
- js中完数的输出
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- C# ITextShap 生成PDF 下载
using iTextSharp.text; using iTextSharp.text.pdf; //创建 Document Document pdfDoc = new Document(new R ...
- Java系列--第二篇 基于Maven的Android开发HelloAndroidWorld
曾经写过一篇Android环境配置的随笔,个人感觉特繁琐,既然有Maven,何不尝试用用Maven呢,经网上搜索这篇文章但不限于这些,而做了一个基于Maven的Android版的Hello Andro ...
- 关于Windows8.1更新后Sql Server服务消失的处理办法
前言 微软在17日发布了windows8.1,兴致勃勃地花了半天的时间更新了,不过不知所云的是,在20日又被卸下Windows Store.此为背景. 影响 更新完毕做开发的时候,发现SqlServe ...