dp(最长升序列)
#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
int v[] , w[] , dp[] , a[]; int main()
{
int n ;
while(cin >> n)
{
for(int i = ; i <= n ; i++)
{
cin >> a[i] ;
dp[i] = ;
}
for(int i = ; i <= n ; i++)
{
for(int j = ; j < i ; j++)
{
//dp[i]记录以i结尾的前i个最长上升子序列
if(a[j] < a[i]) // dp[j] 代表前j个子序列的最长上升子序列
{
dp[i] = max(dp[i] , dp[j] + ); // 如果第i个数大于第j个数,就在前j个最长子序列的个数上加一再比较
}
}
}
int max1 = ; // 注意不要认为dp[n]就是整个序列的最大子序列。他只是n为结尾的最大子序列
for(int i = ; i <= n ; i++)//例如 : 1 2 3 100 1 .这里的dp[n] = 1 ;
{
if(max1 < dp[i])
max1 = dp[i] ;
}
cout << max1 << endl ; } return ;
}
优化:另开一个数组dp该数组长度始终是进行到某点的最长长度,且该数组是递增的,但不一定是该序列的最长上升序列,例如:3 5 1 ,该数组为1 5。
该数组有两种操作:1、如果a【i】大于dp的最后一个元素,则直接加到dp数组末尾。
2、如果小于等于,对dp数组中的元素进行二分查找找到大于a[i]的下标,并将其替换。
note:lower_bound找到第一个大于等于a[i]的下标,upper_bound找到第一个大于a[i]的下标。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[];
int dp[]; int main()
{
/*#ifdef ONLINE_JUDGE
#else
freopen("D:/c++/in.txt", "r", stdin);
freopen("D:/c++/out.txt", "w", stdout);
#endif*/
int n ;
scanf("%d" , &n);
for(int i = ; i <= n ; i++)
{
scanf("%d" , &a[i]);
}
int l = ;
dp[l++] = a[];
for(int i = ; i <= n ; i++)
{
if(a[i] > dp[l-])
{
dp[l++] = a[i];
}
else{
int index = lower_bound(dp , dp+l , a[i]) - dp;//upper_bound找到第一个大于a【i】的下标,一样ac
dp[index] = a[i];
}
}
cout << l << endl ; return ;
}
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[];
int dp[]; int halfsearch(int *a , int len , int x)//upper_bound实现
{
int l = , r = len , mid = (l+r) >> ;
while(r >= l)
{
if(a[mid] > x)//目标在左边
{
r = mid - ;
}
if(a[mid] <= x)//不取等就TLE.
{
l = mid + ;
}
mid = (l + r) >> ;
}
return l ;
} int main()
{
/*#ifdef ONLINE_JUDGE
#else
freopen("D:/c++/in.txt", "r", stdin);
freopen("D:/c++/out.txt", "w", stdout);
#endif*/
int n ;
scanf("%d" , &n);
for(int i = ; i <= n ; i++)
{
scanf("%d" , &a[i]);
}
int l = ;
dp[l] = a[];
for(int i = ; i <= n ; i++)
{
if(a[i] > dp[l])
{
dp[++l] = a[i];
}
else{
int index = halfsearch(dp , l , a[i]);
dp[index] = a[i];
}
for(int i = ; i <= l ; i++)
{
cout << dp[i] << " ";
}
cout << endl ;
}
cout << l << endl ; return ;
}
题意:给你n个数,求一个序列的和最大,条件是该序列是递增的。
#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
int v[] , w[] , dp[] , a[] ;
int len1 , len2 ; int main()
{
int n ;
while(~scanf("%d" , &n) && n )
{
for(int i = ; i <= n ; i++)
{
scanf("%d" , &a[i]);
dp[i] = a[i] ;
}
for(int i = ; i <= n ; i++)
{
for(int j = ; j <= i ; j++)
{
if(a[i] > a[j])
{
dp[i] = max(dp[i] , dp[j] + a[i]);//d[i]以i结尾的最大子序列和
}
}
}
int max1 = ;
for(int i = ; i <= n ; i++)
{
if(max1 < dp[i])
max1 = dp[i] ;
}
cout << max1 << endl ; } return ;
}
dp(最长升序列)的更多相关文章
- P1091 合唱队形 DP 最长升序列维护
题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...
- dp(最长升序列:二分查找时间优化nlogn)
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...
- 最长升序列 DP
class Solution: def lengthOfLIS(self,nums): if not nums:return 0 #边界处理 dp = [1 for _ in range(len(nu ...
- P1020 导弹拦截 dp 树状数组维护最长升序列
题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...
- (LIS)最长上升序列(DP+二分优化)
求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...
- XHXJ's LIS HDU - 4352 最长递增序列&数位dp
代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 // ...
- uva103(最长递增序列,dag上的最长路)
题目的意思是给定k个盒子,每个盒子的维度有n dimension 问最多有多少个盒子能够依次嵌套 但是这个嵌套的规则有点特殊,两个盒子,D = (d1,d2,...dn) ,E = (e1,e2... ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 洛谷 P1020 导弹拦截(dp+最长上升子序列变形)
传送门:Problem 1020 https://www.cnblogs.com/violet-acmer/p/9852294.html 讲解此题前,先谈谈何为最长上升子序列,以及求法: 一.相关概念 ...
随机推荐
- VUE CLI3.X 创建项目
Node.js环境搭建 Node.js基于V8引擎,可以让js代码脱离浏览器运行 Vue CLI3.0 需要Node.js 8.9或者更高版本. 用nvm或者nvm-windows在同一台电脑中管理多 ...
- 《Android程序设计》课程学习
一.课件内容 2019-2010-1学期课件,点击查看 二.作业相关 上交作业的方法 访问ftp://192.168.42.254:22,登录后找到自己的姓名文件夹,放入作业即可.登录账号为stu2, ...
- zabbix 4.2 安装教程
1.我这里使用的是ali的yum源 #wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7 ...
- luogu P3411 序列变换
链接 P3411 序列变换 如果要最小化答案,那么就最大化不移动的数. 那么不移动的子序列一定是最后答案的一段连续区间,而移动的数我们是一定有办法把他们还原的. 设\(f_{i}\)表示\(i\)点的 ...
- JAVA泛型通配符T,E,K,V区别,T以及Class<T>,Class<?>的区别
1. 先解释下泛型概念 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛 ...
- navicat连接客户端报错
怎么感觉oracle和sql server是一个货色.装个服务得装半天,还是mysql好,一下子就好了!下面有一个在centos7上面安装oracle11g的详细步骤,感觉找不到比这个更详细的了吧! ...
- [洛谷P4841][集训队作业2013]城市规划
传送门 题目大意 求出\(n\)个点的简单(无重边无自环)有标号无向连通图数目.\(n\leq 130000\). 题解 题意非常简单,但做起来很难.这是道生成函数经典题,博主当做例题学习用的.博主看 ...
- redis学习 --Sorted Set
一.概述: Sorted Set(有序集合)和Set类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted Set中的每一个成员都会有一个分数(sc ...
- java中super的用法总结
package com.ssm.java; /** * Super * usage1:super. 直接去调用父类的方法和属性. * usage2:放在构造器中的第一位,代表引用父类的构造器. */ ...
- rk3328编译Linux固件
一.编译 Linux 固件 这一章将介绍编译 ROC-RK3328-CC Linux 固件的整个流程. 1.1 准备工作 Linux 固件在如下的环境中编译: Ubuntu 16.04 amd64 安 ...