题目链接:序列

对于 LIS 问题,很显而易见的有 dp方程为:

\[dp_i=\max{dp_j}+1 \ (j<i,a_j \le a_i) \text{ dp表示以某个位置结尾的最长 LIS}
\]

本题考虑到对于转移的两位置,如果能从 \(j \rightarrow i\),那么在以上条件成立的基础情况下,我们由于可以更改二者中的任意一个值(因为同一时刻只能更改一个值),所以如果更改前者那么要保证更改后满足 \(max_j \le a_i\),依旧满足上述的对应关系。如果更改的是 \(i\) 位置的数,保证更改后也满足 \(a_j \le min_i\) 。(因为题目说的是任意一种变化,我们只需要考虑极端的变化即可)。那么转移方程 (\(dp_j \rightarrow dp_i\)) 的条件就为:

  1. \(j < i\)

  2. \(max_j \le a_i\)

  3. \(a_j \le min_i\)

很显而易见,偏序问题,用 cdq 分治来做。注意到 dp 是从左往右,自带有序,所以我们 cdq 分治也应该从原有的两边合并转化为优先处理完左侧的数据再更新右侧数据,类似于二叉树先遍历完左子树。

细节

稍微注意一下,在排序时,我们不应该直接对两侧状态进行排序,因为我们是先遍历左半边的,再遍历右半边的,但遍历左半边对右半边的影响时,排序时会排序右半边导致右半边的 dp 状态量不再是从左往右了,导致遍历右半边时更新错误,所以应该开一个存取相对位置的下标数组进行排序而不影响原数组。

另外注意到我们的偏序条件,左边是 \(max_j\) 右边是 \(val_i\),所以左右两边排序条件的关键字是不一样的。查询我们可以考虑使用树状数组查询 \(val_j \le min_i\) 的 \(\max{dp_j}\) 用于更新当前的 \(dp_i\)。(值域 \(min_i,val_i\le n\))

参照代码
#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast,unroll-loops")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope> #endif using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
} template <typename T>
T lowBit(T x)
{
return x & -x;
} template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
} template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
} template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
} template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
} template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
} template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
} template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
} template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three; bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
} T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
}; template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
} template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
} constexpr int N = 1e5 + 10; struct DP
{
int val, max, min, dp; bool operator<(const DP& other) const
{
return dp < other.dp;
}
} node[N]; int n, m;
int bit[N]; inline void add(int x, const int val)
{
while (x <= n)uMax(bit[x], val), x += lowBit(x);
} inline void clear(int x)
{
while (x <= n)bit[x] = 0, x += lowBit(x);
} inline int query(int x)
{
int ans = 0;
while (x)uMax(ans, bit[x]), x -= lowBit(x);
return ans;
} int pos[N]; //拷贝一份地址数组 inline void cdq(const int L, const int R)
{
const int mid = L + R >> 1;
if (L == R)return;
cdq(L, mid);
forn(i, L, R)pos[i] = i;
stable_sort(pos + L, pos + mid + 1, [&](const int i, const int j)
{
return node[i].max < node[j].max;
});
stable_sort(pos + mid + 1, pos + R + 1, [&](const int i, const int j)
{
return node[i].val < node[j].val;
});
int l = L;
forn(r, mid+1, R)
{
while (l <= mid and node[pos[l]].max <= node[pos[r]].val)add(node[pos[l]].val, node[pos[l]].dp), l++;
uMax(node[pos[r]].dp, query(node[pos[r]].min) + 1);
}
forn(i, L, mid)clear(node[i].val);
cdq(mid + 1, R);
} inline void solve()
{
cin >> n >> m;
forn(i, 1, n)
{
cin >> node[i].val;
node[i].max = node[i].min = node[i].val;
node[i].dp = 1;
}
forn(i, 1, m)
{
int pos, val;
cin >> pos >> val;
uMax(node[pos].max, val);
uMin(node[pos].min, val);
}
cdq(1, n);
cout << max_element(node + 1, node + n + 1)->dp;
} signed int main()
{
Spider
//------------------------------------------------------
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
}
\[时间复杂度为 O(n\log^2{n})
\]

P4093 [HEOI2016/TJOI2016] 序列 题解的更多相关文章

  1. 洛谷 P4093 [HEOI2016/TJOI2016]序列 解题报告

    P4093 [HEOI2016/TJOI2016]序列 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一 ...

  2. 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP

    洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...

  3. BZOJ4553/洛谷P4093 [HEOI2016/TJOI2016]序列 动态规划 分治

    原文链接http://www.cnblogs.com/zhouzhendong/p/8672434.html 题目传送门 - BZOJ4553 题目传送门 - 洛谷P4093 题解 设$Li$表示第$ ...

  4. Luogu P4093 [HEOI2016/TJOI2016]序列 dp套CDQ

    题面 好久没写博客了..最近新学了CDQ...于是就来发一发一道CDQ的练习题 看上去就是可以dp的样子. 设\(dp_{i}\)为以i结尾的最长不下降序列. 易得:\(dp_{i}\)=\(max( ...

  5. BZOJ4553:[HEOI2016/TJOI2016]序列——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4553 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某 ...

  6. 洛谷 P4093 [HEOI2016/TJOI2016]序列(Cdq+dp)

    题面 luogu 题解 \(Cdq分治+dp\) \(mx[i],mn[i]\)分别表示第\(i\)位最大,最小能取到多少 那么有 \(j < i\) \(mx[j] \le a[i]\) \( ...

  7. 洛谷P4093 [HEOI2016/TJOI2016]序列

    题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一个值发生变化.现在佳媛姐姐已经研究出了所有变化的可能性, ...

  8. P4093 [HEOI2016/TJOI2016]序列

    题目链接 题意分析 我们假设每一个数都有一个变动范围\([L_i,R_i]\) 那么我们令\(dp[i]\)表示以\(i\)结尾的最长不下降子序列的长度 那么就是\(dp[i]=max\{dp[j]+ ...

  9. cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )

    hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...

  10. 题解 P4093 【[HEOI2016/TJOI2016]序列】

    这道题原来很水的? noteskey 一开始以为是顺序的 m 个修改,然后选出一段最长子序列使得每次修改后都满足不降 这 TM 根本不可做啊! 于是就去看题解了,然后看到转移要满足的条件的我发出了黑人 ...

随机推荐

  1. Vue第三篇 Vue组件

    01-组件的全局注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  2. jQuery位置 内容 大小 属性 文档的操作

    1. 位置 1. offset() 2. position() 2. 大小 1. 内容(content)>内填充(padding)>边框(border)>外边距(margin) 2. ...

  3. Socket 如何处理粘包

    Socket 如何处理粘包 什么是粘包什么是半包? 粘包: 比如发送了AA BB 两条消息,但是另一方接收到的消息却是AAB,像这种一次性读取了俩条数据的情况就是粘包 半包: 比如发送的消息是ABC时 ...

  4. 【动画进阶】神奇的 3D 卡片反光闪烁动效

    最近,有群里在群里发了这么一个非常有意思的卡片 Hover 动效,来源于此网站 -- key-drop,效果如下: 非常有意思酷炫的效果.而本文,我们不会完全还原此效果,而是基于此效果,尝试去制作这么 ...

  5. 京东App秒杀抢购流程接口分析(基于pypp技术)

    App数据抓包必需工具 必需工具:小米手机,Charles,HttpCanary 从2022年2月后,京东只限于从app发起抢购,所以,网上的很多工具已经无效了.只能分析app端的底层协议和流程. g ...

  6. spring-transaction源码分析(5)TransactionInterceptor事务拦截逻辑

    spring-tx的事务拦截逻辑在TransactionInterceptor类,本文将详细分析其实现方式. 事务拦截器TransactionInterceptor spring-tx的事务拦截逻辑在 ...

  7. 前端开发环境搭建踩坑笔记——npm install node-sass安装失败的解决方案

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  8. 使用华为路由连接WiFi,被限制网速?

    1.问题 使用华为路由连接WiFi,但是网速非常慢,经常半天才能加载一个界面? 2.解决方法 登陆http://192.168.3.1/网址,发现限速一栏被框选,取消即可

  9. [转帖]深入理解mysql-第六章 mysql存储引擎InnoDB的索引-B+树索引

    一.引入索引 在没有索引的情况下,不论是根据主键列或者其他列的值进行查找,由于我们并不能快速的定位到记录所在的页,所以只能从第一个页沿着双向链表一直往下找,因为要遍历所有的数据页,时间复杂度就是O(n ...

  10. [转帖]Kafka之ISR机制的理解

    Kafka对于producer发来的消息怎么保证可靠性? 每个partition都给配上副本,做数据同步,保证数据不丢失. 副本数据同步策略 和zookeeper不同的是,Kafka选择的是全部完成同 ...