[CF568E] Longest Increasing Subsequence
题目描述
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的更多相关文章
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- vue3+vite2动态绑定图片优雅解决方案
优雅解决方案在最下面,小伙伴们儿可以直接前往 背景 在vue3+vite2项目中,我们有时候想要动态绑定资源,比如像下面的代码这样: <template> <div> < ...
- 以程序员的视角,介绍如何通过API接口获取淘宝商品数据的方法和步骤,并提供实际代码示例
当我们想要获取淘宝商品数据时,可以通过调用淘宝开放平台的API接口来实现.下面是一些步骤和示例代码来帮助你开始. 步骤1:申请开发者账号和应用 在开始之前,你需要在淘宝开放平台上注册一个开发者账号 ...
- Python 网页爬虫原理及代理 IP 使用
一.Python 网页爬虫原理 Python 是一种高效的编程语言,在 Web 开发和数据分析领域广受欢迎.Python 的优秀模块使其更加适合大规模数据处理和 Web 服务的编程.网络爬虫是 Pyt ...
- python爬虫——爬虫伪装和反“反爬”
前言爬虫伪装和反"反爬"是在爬虫领域中非常重要的话题.伪装可以让你的爬虫看起来更像普通的浏览器或者应用程序,从而减少被服务器封禁的风险:反"反爬"则是应对服务器 ...
- NineData SQL 窗口支持深色模式,让程序员不再怕长期用眼!
您有没有尝试过被明亮的显示器闪瞎眼的经历? 在夜间或低光环境下,明亮的界面会导致许多用眼健康问题,例如长时间使用导致的眼睛疲劳.干涩和不适感,同时夜间还可能会抑制褪黑素分泌,给您的睡眠质量带来影响. ...
- QFluentWidgets: 基于 C++ Qt 的 Fluent Design 组件库
简介 QFluentWidgets 是一个基于 Qt 的 Fluent Designer 组件库,内置超过 150 个开箱即用的 Fluent Designer 组件,支持亮暗主题无缝切换和自定义主题 ...
- 研发提速:nacos+openfeign环境下的本地链接服务
项目研发过程中,经常会遇到与测试人员工作重叠的情况,十分影响效率. 做了一个修改,可以在本地环境启动项目后和测试环境交互,并且不影响测试环境,理论上也可以用于线上环境的异常的快速处理. 准备事项如下: ...
- Python学习 —— 初步认知
写在前面 Python是一种流行的高级编程语言,具有简单易学.代码可读性高.应用广泛等优势.它是一种解释型语言,可以直接在终端或集成开发环境(IDE)中运行,而无需事先编译. Python的安装 Py ...
- 传纸条 luoguP1006
题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排坐成一个 mm 行 nn 列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈 ...
- CF1352D
题目简化和分析: 这题可以直接按照题意进行模拟,当然有些细节需要注意. 翻译的不足:这里的回合指任意一个人吃掉都算,而不是双方一个回合,最后一个人即使不满足也算一个回合. 我们可以采用两个指针模拟两个 ...