P4093 [HEOI2016/TJOI2016] 序列 题解
题目链接:序列
对于 LIS 问题,很显而易见的有 dp方程为:
\]
本题考虑到对于转移的两位置,如果能从 \(j \rightarrow i\),那么在以上条件成立的基础情况下,我们由于可以更改二者中的任意一个值(因为同一时刻只能更改一个值),所以如果更改前者那么要保证更改后满足 \(max_j \le a_i\),依旧满足上述的对应关系。如果更改的是 \(i\) 位置的数,保证更改后也满足 \(a_j \le min_i\) 。(因为题目说的是任意一种变化,我们只需要考虑极端的变化即可)。那么转移方程 (\(dp_j \rightarrow dp_i\)) 的条件就为:
\(j < i\)
\(max_j \le a_i\)
\(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();
}
\]
P4093 [HEOI2016/TJOI2016] 序列 题解的更多相关文章
- 洛谷 P4093 [HEOI2016/TJOI2016]序列 解题报告
P4093 [HEOI2016/TJOI2016]序列 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一 ...
- 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP
洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...
- BZOJ4553/洛谷P4093 [HEOI2016/TJOI2016]序列 动态规划 分治
原文链接http://www.cnblogs.com/zhouzhendong/p/8672434.html 题目传送门 - BZOJ4553 题目传送门 - 洛谷P4093 题解 设$Li$表示第$ ...
- Luogu P4093 [HEOI2016/TJOI2016]序列 dp套CDQ
题面 好久没写博客了..最近新学了CDQ...于是就来发一发一道CDQ的练习题 看上去就是可以dp的样子. 设\(dp_{i}\)为以i结尾的最长不下降序列. 易得:\(dp_{i}\)=\(max( ...
- BZOJ4553:[HEOI2016/TJOI2016]序列——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4553 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某 ...
- 洛谷 P4093 [HEOI2016/TJOI2016]序列(Cdq+dp)
题面 luogu 题解 \(Cdq分治+dp\) \(mx[i],mn[i]\)分别表示第\(i\)位最大,最小能取到多少 那么有 \(j < i\) \(mx[j] \le a[i]\) \( ...
- 洛谷P4093 [HEOI2016/TJOI2016]序列
题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一个值发生变化.现在佳媛姐姐已经研究出了所有变化的可能性, ...
- P4093 [HEOI2016/TJOI2016]序列
题目链接 题意分析 我们假设每一个数都有一个变动范围\([L_i,R_i]\) 那么我们令\(dp[i]\)表示以\(i\)结尾的最长不下降子序列的长度 那么就是\(dp[i]=max\{dp[j]+ ...
- 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; ...
- 题解 P4093 【[HEOI2016/TJOI2016]序列】
这道题原来很水的? noteskey 一开始以为是顺序的 m 个修改,然后选出一段最长子序列使得每次修改后都满足不降 这 TM 根本不可做啊! 于是就去看题解了,然后看到转移要满足的条件的我发出了黑人 ...
随机推荐
- Dubbo 泛化调用在vivo统一配置系统的应用
作者:vivo 互联网服务器团队- Wang Fei.LinYupan Dubbo泛化调用特性可以在不依赖服务接口API包的场景中发起远程调用, 这种特性特别适合框架集成和网关类应用开发. 本文结合在 ...
- 打破 Serverless 落地边界,阿里云 SAE 发布 5 大新特性
微服务场景,开源自建真的最快最省最稳的?复杂性真的会成为 Kubernetes 的"致命伤"吗?企业应用容器化,一定得过 K8s 这座"独木桥"吗?Server ...
- JavaScrip基本语法
2. 上篇内容回顾 1. CSS属性 1. 高和宽 2. 字体相关 3. 文本相关 4. 背景相关 1. background-color: red 2. background-image: url( ...
- Java循环标签
大家是否见过这种for循环,在for循环前加了个标记的: outerLoop: for (; ; ) { for (; ; ) { break outerLoop; } } 我之前有一次在公司业务代码 ...
- 如何实现 Excel 表格转置(行列互换)
直接上经验贴: https://baijiahao.baidu.com/s?id=1690475581736550777&wfr=spider&for=pc 大概就是 先复制粘贴,在粘 ...
- nginx 工作原理及特点
本文为博主原创,未经允许不得转载: nginx 简介:是一个高性能 HTTP 和 反向代理 服务器. Nginx 特点是占有内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中 ...
- 搭建 spring boot + mybatis plus 项目框架并进行调试
本文为博主原创,未经允许不得转载: 1.创建一个spring boot的工程应用: File ---- > New ----->Project ----> 然后选中Spring In ...
- 使用vs插件进行远程调试linux服务器
魔改Raspberry Debugger插件实现linux远程开发 本插件是在树莓派的远程调试下修改实现并未全部本人实现 插件基本使用: 插件目前只能在.net core 3.1到.net 6的框架下 ...
- VS中多个源文件中只运行其中特定文件
1.问题 有时候一个项目中创建了多个源文件,但是我只想运行其中的一个,一起运行就会出现多个main入口的问题 2.解决方式 2.1 右键要排除的文件,点击属性 2.2 从生成中排除一项中选择是即可 2 ...
- [转帖]TPC-C 、TPC-H和TPC-DS区别
https://zhuanlan.zhihu.com/p/339886289 针对数据库不同的使用场景TPC组织发布了多项测试标准. TPC-C: TPC Benchmark C于1992年7月获得批 ...