DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp

遇到了这道题 https://codeforces.com/problemset/problem/702/A

以为是最长上升子序列(Longest Increasomg Subsequence)的模板题,发现自己不会做

记录一下大概的思路:

\(O(n^2)\) 的算法:

  • \(L[i]\) 选择 \(A[i]\) 为结尾的LIS的长度
  • \(P[i]\) 选择 \(A[i]\) 为结尾的LIS的倒数第二个元素的Position(可以用来复现整个LIS)
  • 临时变量 \(k\) ,表示 \(A[i]\) 计算时找到的前面最长的LIS的位置
  • 对于每个 \(A[i]\) ,遍历它前面的每个 \(A[j]\) ,记录所有满足 \(A[j]<A[i]\) (可以把 \(A[i]\) 接在后面)且 \(L[j]>L[k]\) (更长的子序列)
  • \(L[i]=L[k]+1\)
  • \(P[i]=k\)

\(O(nlogn)\) 的算法:

  • \(L[i]\) 长度为\(i+1\) 的IS的末位元素的最小值
  • \(length\) 前 \(i\) 个元素构成的LIS的长度
  • 升序遍历 \(i\),故 \(length\) 非降,而且构造的过程中 \(L\) 数组是递增的
  • 如果 \(L[length-1]<A[i]\) ( \(A[i]\) 可以接在当前最长的(长度为 \(length\) 的)LIS后面),则 \(L[length++]=A[i]\)
  • 否则在已确定的 \(L[0]~L[length]\) 中(长度为 \(1\) 到 \(length\) 闭区间的LIS的最末元素)二分找到能更新的位置并更新 $*lower\_bound(L,L+length,A[i])=A[i]$
#include<bits/stdc++.h>
using namespace std;
#define ll long long int A[];
int L[]; int lis(){
L[]=A[];
int length=;
for(int i=;i<n;i++){
if(L[length-]<A[i]){
L[length++]=A[i];
}
else{
*lower_bound(L,L+length,A[i])=A[i];
//1 7 2 5,已有序列1 7,lower_bound(2)得到7,可以把长度为2的LIS的末尾换成2
}
}
return length;
} int main(){
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&A[i]);
}
printf("%d\n",lis()); }

复原的时候也很简单,把$L[0]~L[length-1]$全部输出就可以了

但是最后我发现这个702A并不是LIS,因为他不是子序列而是子串!

2019-01-16

Codeforces - 702A - Maximum Increase - 简单dp的更多相关文章

  1. CodeForces 702A Maximum Increase

    简单$dp$. 如果$a[i]>a[i-1]$,那么$dp[i]=dp[i-1]+1$.否则,$dp[i]=1$.答案为$dp[i]$中的最大值. #pragma comment(linker, ...

  2. Codeforces 702A Maximum Increase(dp)

    题目链接:http://codeforces.com/problemset/problem/702/A 题意: 给你N个数,a[0], a[1], a[2], ....., a[n-1],让你找出最长 ...

  3. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  4. Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学

    https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...

  5. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  6. Codeforces - 909C - Python Indentation - 简单dp

    http://codeforces.com/problemset/problem/909/C 好像以前做过,但是当时没做出来,看了题解也不太懂. 一开始以为只有上面的for有了循环体,这里的state ...

  7. Codeforces 375B Maximum Submatrix 2 (DP)

    <题目链接> 题目大意:给出一个01矩阵,行与行之间可以互换位置,问能够得到最大的全1矩阵的面积. #include <bits/stdc++.h> using namespa ...

  8. Codeforces 886E Maximum Element 组合数学 + dp

    我们定义dp[ i ]表示长度为 i 的序列, 最后没有一个==k的时候返回的方案数, 也就是最后强制返回 i 的方案数. 我们能得到dp方程   dp[ i ] = sum(dp[ i - j - ...

  9. Codeforces 332B Maximum Absurdity(DP+前缀和处理)

    题目链接:http://codeforces.com/problemset/problem/332/B 题目大意:给你n个数和一个整数k,要求找到不相交的两个长度为k的区间,使得区间和最大,输出这两个 ...

随机推荐

  1. ubuntu compile php from source code

    10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...

  2. 关于 redux-saga 中 take 使用方法详解

    本文介绍了关于redux-saga中take使用方法详解,分享给大家,具体如下: 带来一个自己研究好久的API使用方法. redux-saga中effect中take这个API使用方式,用的多的是ca ...

  3. INAPP

    1. Login API : 接口為您提供了我們的登入服務,可以讓使用者透過facebook帳號登入我們的服務,在您呼叫此API後,可以得到 使用者的UUID(Unique UID為使用者在平台的唯一 ...

  4. 【手记】走近科学之为什么JObject不能调用LINQ扩展方法

    Json.NET的JObject明明实现了IEnumerable<T>,具体来说是IEnumerable<KeyValuePair<string, JToken>> ...

  5. MVC程序部署后页面指向login.aspx

    MVC程序在本地没有问题,但是部署到服务器后老是跳转到Login.aspx页面,但是我的MVC程序中根本没有Login页面,看了一下链接是这样的 htttp://localhost:26290/log ...

  6. 安装NLTK

    在网上找了一圈,没找到几个靠谱的安装流程,在http://nltk.org/install.html上找到各平台下安装流程: Windows平台: 以下操作假定你的机器上还没有安装Python,如果你 ...

  7. java中方法中声明三个点“...”作用

    public class Test {  public static void main(String[] args) {   String str[] = {"s"," ...

  8. scrollView 代理方法的实现顺序的些许区别

  9. 大整数分解质因数(Pollard rho算法)

    #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...

  10. I2C测试【转】

    本文转载自: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...