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 讲解此题前,先谈谈何为最长上升子序列,以及求法: 一.相关概念 ...
随机推荐
- docker可视化集中管理工具shipyard安装部署
docker可视化集中管理工具shipyard安装部署 Shipyard是在Docker Swarm实现对容器.镜像.docker集群.仓库.节点进行管理的web系统. 1.Shipyard功能 Sh ...
- Docker环境下的前后端分离项目部署与运维
本教程将从零开始部署一个前后端分离的开源项目,利用docker虚拟机的容器技术,采用分布式集群部署,将项目转换成为高性能.高负载.高可用的部署方案.包括了MySQL集群.Redis集群.负载均衡.双机 ...
- 学Python的第六天
今天发愤图强,死学Python!!!! 为此小编还买了自己喜欢的McPython! 从小老师就说兴趣是最好的老师,所以呢买这本书是想通过我的世界这款游戏来提高自己的学习兴趣!!! 当然也有私心啦... ...
- python if-else替代三元表达式
python中判断一个数是否是偶数的常规代码: def _compare(data): if data % 2 == 0: return True else: return False # 调用偶数判 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - A
题目链接:http://codeforces.com/contest/831/problem/A 题意:给定一个序列,问你这个序列是否是单峰的. 定义单峰的序列为: (序列值的变化趋势)开始是递增的, ...
- group_by
1.按照一个列或者多个列对数据分组 2.对每个组进行聚合操作 3. 对聚合后的结果进行判断 1. select avg(score) as score from teacher 2. select ...
- 【leetcode】870. Advantage Shuffle
题目如下: 解题思路:几千年前,一个古人曾经解过这个题目,他的名字叫做田忌,后人称他的解题思想叫做“田忌赛马”.言归正传,本题就是一个田忌赛马的问题,先将A与B进行排序,然后判断A[0]与B[0]的大 ...
- 对Asycn/Await的研究
1.async 函数就是 Generator 函数的语法糖. 例如: var fs = require('fs'); var readFile = function (fileName){ retur ...
- basic deepwalk
Get to know How deepwalk works by this project. Two steps: 1. gen the graph, and gen the corpus on t ...
- python 全栈开发,Day9(函数的初始,返回值,传参,三元运算)
一.函数的初始 比如python没有len()方法,如果求字符串的长度 使用for循环 s = 'asdfadsf' count = 0 for i in s: count += 1 print(co ...