一道dp[不太好写]
http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2281
Description
An arithmetic progression is a sequence of numbers a1, a2, ..., ak where the difference of consecutive members ai + 1 − ai is a constant 1 ≤ i ≤ k − 1 . For example, the sequence 5, 8, 11, 14, 17 is an arithmetic progression of length 5 with the common difference 3.
In this problem, you are requested to find the longest arithmetic progression which can be formed selecting some numbers from a given set of numbers. For example, if the given set of numbers is {0, 1, 3, 5, 6, 9}, you can form arithmetic progressions such as 0, 3, 6, 9 with the common difference 3, or 9, 5, 1 with the common difference -4. In this case, the progressions 0, 3, 6, 9 and 9, 6, 3, 0 are the longest.
Input
The input consists of a single test case of the following format.
n
v1 v2 ... vn
n is the number of elements of the set, which is an integer satisfying 2 ≤ n ≤ 5000 . Each vi(1 ≤ i ≤ n) is an element of the set,which is an integer satisfying 0 ≤ vi ≤ 109.vi's are all different, i.e.,vi ≠ vj if i ≠ j
Output
Output the length of the longest arithmetic progressions which can be formed selecting some numbers from the given set of numbers.
Sample Input
6
0 1 3 5 6 9
Sample Output
4
题意:求出序列从小到大排序之后能形成的最长等差数列的长度。
题解:dp,dp[i][j]表示i和j作为前两项时的数列长度,如果先枚举第一项(O(N)),然后第二项如果和第三项在第一项的同一侧的话就是(O(N^2)),整体复杂度是(O(N^3)),难以接受,而如果先枚举第二项,然后第一项和第三项在第二项两侧,根据a[j]+a[k]=2*a[i]可以将复杂度降为O(N^2)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[],dp[][];
int main(){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
if(i!=j)dp[i][j]=;
else dp[i][j]=;
}
}
sort(a+,a++n);
int ans=;
for(int i=n-;i>=;i--){
int j=i-;
int k=i+;
while(j>=&&k<=n){
if(a[j]+a[k]==*a[i]){dp[j][i]=dp[i][k]+;ans=max(ans,dp[j][i]);k++;j--;}
else if(a[j]+a[k]<*a[i]){k++;}
else {j--;}
}
}
cout<<ans<<endl;
return ;
}
一道dp[不太好写]的更多相关文章
- dp表模型-如何写出for循环动态规划
题目很肤浅.. 但是这件事我们要做.. 那么有一种方法叫做刷表法.. 当你发现这个问题具有最优子结构,重叠子问题时 那么这是一个dp问题是使用本方法的前提 画出该dp状态所对应的矩阵 画出转移关系线. ...
- 值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)
这是一道数据范围和评测时间水的可怕的题,只是思路有点难想,BUT假如你的思路清晰,完全了解怎么该做,那就算你写一个反LLL和反SLE都能A,如此水的一道题,你不心动吗? 下面贴出题目 Descript ...
- Kickstart Round D 2017 problem A sightseeing 一道DP
这是现场完整做出来的唯一一道题Orz..而且还调了很久的bug.还是太弱了. Problem When you travel, you like to spend time sightseeing i ...
- 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 一道DP
也是校赛学长出的一道题~想穿了很简单..但我还是听了学长讲才明白. 观察力有待提高. Problem D: YaoBIG’s extra homeworkTime LimitMemory Limit1 ...
- nyoj16矩形嵌套(第一道dp关于dag的题目)
http://acm.nyist.net/JudgeOnline/problem.php?pid=16 题意:有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c, ...
- 2018微软实习笔试一道dp题目总结
题意大概是说在一维数轴上起点和终点的距离是d,现在我们要从起点走到终点.每走一个单位长度消耗一个单位能量,初始时有K单位能量.同时在起点和终点之间分布一些加油站a1,a2,...an,给你加油站数量. ...
- python中一行字符串太多写不下时怎么写
‘123456789‘ 见此博客 https://www.cnblogs.com/wanderingzj/p/5244451.html str1=('123 '455' '789') 这样的话, ...
- dp 单调性优化总结
对于单调性优化其实更多的是观察dp的状态转移式子的单调性 进而用优先队列 单调队列 二分查找什么的找到最优决策 使时间更优. 对于这道题就是单调性优化的很好的例子 首先打一个暴力再说. f[i][j] ...
随机推荐
- 逆袭之旅DAY20.xia.师父指导.数据类型
2018-07-16 09:35:57 基础是王道 从码农--软件工程师--软件架构师 String 首字母大写,特殊引用类型,常量类 二.数组 存钱罐(只能放钱) 数据兼容 数组的长度(定义后长度 ...
- 摄像头录制视频并且保存成mp4
import cv2import numpy as npimport os cap = cv2.VideoCapture(1)#v4l2-ctl --list-devices 查看设备号,非正常中断时 ...
- JavaScript创建对象(三)——原型模式
在JavaScript创建对象(二)——构造函数模式中提到,构造函数模式存在相同功能的函数定义多次的问题.本篇文章就来讨论一下该问题的解决方案——原型模式. 首先我们来看下什么是原型.我们在创建一个函 ...
- 个人前端学习路线图与github优秀前端开发者的路线图推荐
1.个人目前学习的路线图 2.github优秀前端开发者的路线图推荐 打开github首页,在搜索框输入developer-roadmap,搜索github前端路线图 选择kamranahmedse/ ...
- spoj1811
题解: 后缀自动机 先把A的后缀自动机建好 然后用B再上面跑 如果不能转移就跳fail 如果可以就到下一个可行状态 代码: #include<bits/stdc++.h> using na ...
- poj3261
题解: 同bzoj1717 代码: #include<bits/stdc++.h> using namespace std; ,P2=,P=; int a1[P],num[P],a2[P] ...
- ssm框架整合中的双亲容器
SSM中Spring双亲容器的构造过程和XML加载顺序 Spring的父子容器问题和坑 Spring使用父子容器实现了很多功能,比如在Spring MVC中,展现层Bean位于一个子容器中,而业务层和 ...
- 2.BIND服务基础及域主服务器配置
一.BIND 现今使用最晚广泛的DNS服务器软件是BIND(Berkeley Internet Name Domain),最早由伯克利大学的一名学生编写,现在最新的版本是9,由ISC(Internet ...
- 【资料搜集】DirectX学习
[网站推荐:]GameRes游资网-游戏开发者门户 http://www.gameres.com/ [基础知识:] <游戏编程>第一部 基础篇 - GameRes.com http://d ...
- RabbitMQ 简单的消息发送与接收
RabbitMQ是建立在AMQP(Advanced Message Queuing Protocol,高级消息队列协议)基础上的,而AMQP是建立在TCP协议之上的. 因此,RabbitMQ是需要建立 ...