题目描述

Note that the memory limit in this problem is less than usual.

Let's consider an array consisting of positive integers, some positions of which contain gaps.

We have a collection of numbers that can be used to fill the gaps. Each number from the given collection can be used at most once.

Your task is to determine such way of filling gaps that the longest increasing subsequence in the formed array has a maximum size.

输入格式

The first line contains a single integer $ n $ — the length of the array ( $ 1<=n<=10^{5} $ ).

The second line contains $ n $ space-separated integers — the elements of the sequence. A gap is marked as "-1". The elements that are not gaps are positive integers not exceeding $ 10^{9} $ . It is guaranteed that the sequence contains $ 0<=k<=1000 $ gaps.

The third line contains a single positive integer $ m $ — the number of elements to fill the gaps ( $ k<=m<=10^{5} $ ).

The fourth line contains $ m $ positive integers — the numbers to fill gaps. Each number is a positive integer not exceeding $ 10^{9} $ . Some numbers may be equal.

输出格式

Print $ n $ space-separated numbers in a single line — the resulting sequence. If there are multiple possible answers, print any of them.

1.5s,128MB

LIS 问题有两种优化方式,所以这题也有两种做法。

1. 二分

考虑用二分维护 \(dp_{i}\) 表示目前长度为 \(i\) 的 序列里面树最小的是 \(i\),然后对于固定的数二分去修改,不固定的可以把 \(m\) 个数排序后维护一个指针扫过去找到第一个大于 \(a_i\) 的,这里复杂度 \(nk\)

但是要输出方案,而且不可能开的下 \(nk\) 的区间去记录每个点的前驱。对于每个正常的数仍然记录下他的前驱是哪个位置。设现在第 \(x\) 个位置是 LIS 中的第 \(p\) 个数,填了 \(y\)。那么如果 \(a_x\ne -1\),LIS 中 \(p-1\) 个数一定是它的前驱,否则我们就先找一下是否存在一个 \(dp\) 值为 \(p-1\) 并且 \(a_j\ne -1\) 的 \(j\),还要满足 \(a_j<y\),如果存在 \(p-1\) 位可以填他,否则 \(p-1\) 位就是上一个 -1 出现的地方。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int lst[N],mx,id[N],n,k,m,a[N],b[N],dp[N],ls[N],fr[N];//ls是值,fr 是位ç½
multiset<int>s;
vector<int>g[N];
int read()
{
int s=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
s=s*10+ch-48,ch=getchar();
return s*f;
}
void sou(int x,int y,int p)//在 x 处,填了y,子序列第p项
{
//printf("%d %d %d\n",x,y,p);
if(p==1)
{
if(a[x]==-1)
{
a[x]=y;
s.erase(s.lower_bound(y));
}
return;
}
if(a[x]==-1)
{
a[x]=y;
s.erase(s.lower_bound(y));
vector<int>::iterator k=lower_bound(g[p-1].begin(),g[p-1].end(),x);
if(k==g[p-1].begin()||a[*(k-1)]>=y)
sou(lst[x],*(lower_bound(b+1,b+m+1,y)-1),p-1);
else
sou(*(k-1),a[*(k-1)],p-1);
}
else
sou(ls[x],fr[x],p-1);
}
int main()
{
memset(dp,0x7f,sizeof(dp));
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
m=read();
for(int i=1;i<=m;i++)
s.insert(b[i]=read());
sort(b+1,b+m+1);
for(int i=1;i<=n;i++)
{
if(!~a[i-1])
lst[i]=i-1;
else
lst[i]=lst[i-1];
if(~a[i])
{
int t=lower_bound(dp+1,dp+n+1,a[i])-dp;
dp[t]=a[i],id[t]=i;
ls[i]=id[t-1],fr[i]=dp[t-1];
g[t].push_back(i);
//printf("%d %d\n",i,t);
}
else
{
int r=n;
for(int j=m;j;j--)
{
while(r^1&&dp[r-1]>=b[j])
--r;
dp[r]=b[j],id[r]=i;
}
}
}
for(int i=1;i<=n;i++)
if(dp[i+1]==0x7f7f7f7f)
mx=i,i=n;
//printf("%d\n",mx);
sou(id[mx],dp[mx],mx);
for(int i=1;i<=n;i++)
{
if(!~a[i])
{
a[i]=*s.begin();
s.erase(s.begin());
}
}
for(int i=1;i<=n;i++)
printf("%d ",a[i]);
}

2.数据结构

定义 \(dp_i\) 为以 \(i\) 为结尾的 LIS 最长是多少,这里我们忽略掉 -1 的存在。

设 \(c_i\) 为可以填的数中有多少个不超过 \(i\) 的数,\(a_i\) 为原数组中前 \(i\) 个数有多少个 -1

然后 \(dp_i=\max\limits_{j<i}dp_j+\min(c_{a_i}-c_{a_j},s_i-s_j)\)。

把 min 拆掉,就变成了一个三维偏序,CDQ 分治即可。

方案可以通过记录 CDQ 时线段树上的最大值来源就行了。

代码懒得写了

[CF568E] Longest Increasing Subsequence的更多相关文章

  1. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  2. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  3. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  4. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  6. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  7. 300. Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...

  8. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  9. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

  10. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. 论文解读(PERL)《PERL: Pivot-based Domain Adaptation for Pre-trained Deep Contextualized Embedding Models》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:PERL: Pivot-based Domain Adaptation for Pre-trained D ...

  2. MindSponge分子动力学模拟——使用迭代器进行系统演化(2023.09)

    技术背景 在前面几篇博客中,我们已经介绍过使用MindSponge去定义一个系统以及使用MindSponge计算一个分子系统的单点能.这篇文章我们将介绍一下在MindSponge中定义迭代器Updat ...

  3. 小白弄明白了 unix 时间戳的转换问题

    小白对于将 unix 时间戳转换为日期时间和使用日期时间转换为 unix 时间戳,在项目中见到过很多,每次使用时不是用现有的方法转换就是网上搜索方法. 小白见过各种转换方式觉得moment库很是方便, ...

  4. SQL查询中的小技巧:SELECT 1 和 LIMIT 1 替代 count(*)

    前言 在写SQL查询时,常规做法是使用SELECT count(*)来统计符合条件的记录数. 然而,在某些情况下,我们只关心是否存在符合条件的记录,而不需要知道具体的记录数. 为了优化性能,可以改用使 ...

  5. 在 Android Studio Java 项目里混合 Kotlin 编程

    首先,先搞明白一个概念,这里的 Java 混合 Kotlin 是指文件层级的混合,即 Java 代码还是写在 .java 文件中,Kotlin 代码还是写在 .kt 文件中,只不过是可以在 Java ...

  6. 解密Prompt系列15. LLM Agent之数据库应用设计:DIN & C3 & SQL-Palm & BIRD

    上一章我们主要讲搜索引擎和LLM的应用设计,这一章我们来唠唠大模型和DB数据库之间的交互方案.有很多数据平台已经接入,可以先去玩玩再来看下面的实现方案,推荐 sql translate:简单,文本到S ...

  7. Learning Hard C# 学习笔记: 6.C#中的接口

    目的: 由于C#中的类只能单个继承, 为了满足多重继承(一个子类可以继承多个父类)的需求, 所以产生了接口. 多重继承是指一个类可以从多个父类继承属性和方法.在C#中,只允许单继承,即一个类只能有一个 ...

  8. Go 语言的前生今世与介绍

    Go 语言的前生今世与介绍 目录 Go 语言的前生今世与介绍 一. Go 语言的发展 1.1 Go 语言是如何诞生的? 1.2 Go语言的早期团队和演进历程 1.3 Go语言正式发布并开源 1.4 G ...

  9. Dubbo3应用开发—Dubbo3注册中心(zookeeper、nacos、consul)的使用

    Dubbo3注册中心的使用 zookeeper注册中心的使用 依赖引入 <dependency> <groupId>org.apache.dubbo</groupId&g ...

  10. linux日常运维(三) GRUB 2的维护

    GRUB 2简介 GRUB GRUB是linux系统默认的引导加载程序.linux加载一个系统前,它必须有一个引导加载程序中特定指令(比如MBR记录)去引导系统.这个程序一般是位于系统的主硬盘驱动器或 ...