一道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] ...
随机推荐
- [IOS微信] PList文件解析,boost数据读取
最近在解析IOS版微信数据中的 mmsetting.archive 文件时,第一次接触到PList文件. 注:mmsetting.archive 不是一个标准的PList文件,其中含有汉字,并且很多 ...
- shell脚本分析二
Shell 基本运算符Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符原生bash不支持简单的数学运算,但是可以通过其他命令来实 ...
- Ubuntu16.04无法使用WiFi
本人联想431,安装ubuntu16.04 lts,打开之后没有wife,参考这个解决 http://blog.csdn.net/bubblem/article/details/53575017 U ...
- js图片时间和倒计时
图片时间原理 原理:使用定时器每秒获取时间,获取时间的时,分,秒,组成一个6位数的字符串,然后用charAt,截取出对应的字符,图片命名和数字相对应就ok拉 倒计时原理 原 ...
- Kafka.net使用编程入门(二)
1.首先创建一个Topic,命令如下: kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partit ...
- OOP⑶
/** * 学生类 * ctrl+o 查询本类的所有属性 和方法 */ public class Student { int age; // 年龄 String name; // 姓名 // 自己创建 ...
- OO作业总结报告3
规格化设计的发展史 下面部分来源:https://www.cnblogs.com/eggert/p/9098446.html: 随着计算机硬件的飞速发展,以及应用复杂度越来越高,软件规模越来越大,原有 ...
- pycharm模板
使用pycharm的模板 File - Settings (ctrl+alt+s) Editor - File and Code Templates - Python Script 可以使用部分变量. ...
- mysql 数据迁移
最近线上系统新挂了一次磁盘,需要将系统磁盘下的 mysql 数据目录迁移到 数据盘上. 经过一番考察,mysql在安装时,使用了预编译的二进制tar.gz包.共有两处配置了 datadir属性 /et ...
- 【Ctrl】 + 【Alt】 + 【F1~F6】 和 【Ctrl】 + 【Alt】 + 【T】打开的终端有什么不同?
ctrl +alt +Fn 打开的是模拟终端,简单说来,Linux系统一开机会自动打开6个模拟终端,然后自动切换到其中一个(一般来说是切换到图形界面的那个也就是说窗口管理器是在这6个模拟终端中运行的) ...