洛谷 P4093 [HEOI2016/TJOI2016]序列(Cdq+dp)
题面
题解
\(Cdq分治+dp\)
\(mx[i],mn[i]\)分别表示第\(i\)位最大,最小能取到多少
那么有
\(j < i\)
\(mx[j] \le a[i]\)
\(a[j] \le mn[i]\)
然后就有了50分 \(O(n^2)\)的\(dp\)
上面那个东西是个三维偏序,
\(Cdq\)优化一下即可。
Code
50pts
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
template<class T> inline void read(T &x) {
x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
x = f ? -x : x;
return ;
}
template<class T> inline void write(T x) {
if (!x) {putchar(48);return ;}
if (x < 0) x = -x, putchar('-');
int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 100010;
int a[N];
int mx[N], mn[N], f[N];
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int n, m; read(n); read(m);
for (int i = 1; i <= n; i++) read(a[i]), mx[i] = mn[i] = a[i], f[i] = 1;
for (int i = 1; i <= m; i++) {
int x, y; read(x), read(y);
mx[x] = max(mx[x], y);
mn[x] = min(mn[x], y);
}
int ans = 0;
for (int i = 2; i <= n; i++)
for (int j = 1; j < i; j++)
if (a[i] >= mx[j] && mn[i] >= a[j])
f[i] = max(f[i], f[j]+1), ans = max(ans, f[i]);
printf("%d\n", ans);
return 0;
}
100pts
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
template<class T> inline void read(T &x) {
x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
x = f ? -x : x;
return ;
}
template<class T> inline void write(T x) {
if (!x) {putchar(48);return ;}
if (x < 0) x = -x, putchar('-');
int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 100010;
int a[N], b[N*4], tot, mx[N], mn[N];
int f[N], n, m;
int t[N], p[N];
#define lowbit(x) (x&(-x))
void add(int x, int k) {
while (x <= tot) {
t[x] = max(t[x], k);
x += lowbit(x);
}
return ;
}
void clr(int x) {
while (x <= tot) {
t[x] = 0;
x += lowbit(x);
}
return ;
}
int query(int x) {
int s = 0;
while (x) {
s = max(s, t[x]);
x -= lowbit(x);
}
return s;
}
inline bool cmp1(int i, int j) { return mx[i] < mx[j]; }
inline bool cmp2(int i, int j) { return a[i] < a[j]; }
void solve(int l, int r) {
if (l == r) {
f[l] = max(1, f[l]);
return ;
}
int mid = (l + r) >> 1;
solve(l, mid);
for (int i = l; i <= r; i++)
p[i] = i;
sort(p+l, p+mid+1, cmp1); sort(p+mid+1, p+r+1, cmp2);
for (int i = mid+1, j = l; i <= r; i++) {
while (j <= mid && mx[p[j]] <= a[p[i]]) {
add(a[p[j]], f[p[j]]);
j++;
}
f[p[i]] = max(f[p[i]], query(mn[p[i]])+1);
}
for (int i = l; i <= mid; i++)
clr(a[i]);
solve(mid+1, r);
return ;
}
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
read(n); read(m);
for (int i = 1; i <= n; i++) {
read(a[i]), mx[i] = mn[i] = a[i];
b[++tot] = a[i];
}
for (int i = 1; i <= m; i++) {
int x, y; read(x), read(y);
mx[x] = max(mx[x], y);
mn[x] = min(mn[x], y);
}
for (int i = 1; i <= n; i++) b[++tot] = mx[i], b[++tot] = mn[i];
sort(b+1, b+1+tot);
tot = unique(b+1, b+1+tot)-b-1;
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(b+1, b+1+tot, a[i])-b;
mx[i] = lower_bound(b+1, b+1+tot, mx[i])-b;
mn[i] = lower_bound(b+1, b+1+tot, mn[i])-b;
}
solve(1, n);
int ans = 0;
for (int i = 1; i <= n; i++)
ans = max(ans, f[i]);
printf("%d\n", ans);
return 0;
}
洛谷 P4093 [HEOI2016/TJOI2016]序列(Cdq+dp)的更多相关文章
- 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP
洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...
- 洛谷 P4093 [HEOI2016/TJOI2016]序列 解题报告
P4093 [HEOI2016/TJOI2016]序列 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一 ...
- BZOJ4553/洛谷P4093 [HEOI2016/TJOI2016]序列 动态规划 分治
原文链接http://www.cnblogs.com/zhouzhendong/p/8672434.html 题目传送门 - BZOJ4553 题目传送门 - 洛谷P4093 题解 设$Li$表示第$ ...
- 洛谷P4093 [HEOI2016/TJOI2016]序列
题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一个值发生变化.现在佳媛姐姐已经研究出了所有变化的可能性, ...
- 洛谷 P2824 [HEOI2016/TJOI2016]排序 解题报告
P2824 [HEOI2016/TJOI2016]排序 题意: 有一个长度为\(n\)的1-n的排列\(m\)次操作 \((0,l,r)\)表示序列从\(l\)到\(r\)降序 \((1,l,r)\) ...
- 洛谷 P4091 [HEOI2016/TJOI2016]求和 解题报告
P4091 [HEOI2016/TJOI2016]求和 题目描述 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: \[ f(n)=\sum_{i=0}^n\ ...
- 【洛谷P4093】 [HEOI2016/TJOI2016]序列 CDQ分治+动态规划
你发现只会改变一个位置,所以可以直接进行dp 具体转移的话用 CDQ 分治转移就好了~ #include <bits/stdc++.h> #define N 100006 #define ...
- Luogu P4093 [HEOI2016/TJOI2016]序列 dp套CDQ
题面 好久没写博客了..最近新学了CDQ...于是就来发一发一道CDQ的练习题 看上去就是可以dp的样子. 设\(dp_{i}\)为以i结尾的最长不下降序列. 易得:\(dp_{i}\)=\(max( ...
- [HEOI2016/TJOI2016]序列 CDQ分治
---题面--- 题解: 首先我们观察一下,如果一个点对(j, i), 要符合题中要求要满足哪些条件? 首先我们设 j < i 那么有: j < i max[j] < v[i] v[ ...
随机推荐
- Codeforces 1109C 线段树
题意及思路:https://www.cnblogs.com/TinyWong/p/10400682.html 代码: #include <bits/stdc++.h> #define ls ...
- Win10 pip安装pycocotools报错解决方法(cl: 命令行 error D8021 :无效的数值参数“/Wno-cpp”)
参考: https://blog.csdn.net/chixia1785/article/details/80040172 https://blog.csdn.net/gxiaoyaya/articl ...
- mysql for visual
http://dev.mysql.com/downloads/file.php?id=458484
- URL 与 URI
http://localhost:8080/TEST_Servlet/ClientRequest/test?name=wr getRequestURL:http://localhost:8080/TE ...
- python核心编程第5章课后题答案
5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the ...
- 编写高质量代码改善C#程序的157个建议——建议20:使用泛型集合代替非泛型集合
建议20:使用泛型集合代替非泛型集合 在建议1中我们知道,如果要让代码高效运行,应该尽量避免装箱和拆箱,以及尽量减少转型.很遗憾,在微软提供给我们的第一代集合类型中没有做到这一点,下面我们看Array ...
- LibreOJ 6278 数列分块入门 2(分块)
题解:非常高妙的分块,每个块对应一个桶,桶内元素全部sort过,加值时,对于零散块O(sqrt(n))暴力修改,然后暴力重构桶.对于大块直接整块加.查询时对于非完整块O(sqrt(n))暴力遍历.对 ...
- SQLServer 统计查询语句消耗时间
--方法1[set statistic ]: set statistics time on go --执行语句 xxxx go set statistics time off --方法2[getDat ...
- 说一下我认识的*nix下的服务器热重启
步骤: 第一: 收到SIGTERM以后现在的服务器监听socket停止accept 但是并没有停止listen,这个很关键.(所以客户端发起的tcp连接的syn得不到synack,只是继续等待,而不会 ...
- MongoDB插入时间不正确的问题
关于mongodb插入时间不正确的问题 今天在给mongodb插入日期格式的数据时发现,日期时间相差8个小时,原来存储在mongodb中的时间是标准时间UTC +0:00,而中国的时区是+8.00 . ...