题意:给两个序列L, R, 序列元素个数都是n, 现在要找一个序列v1, v2...vm, 使得满足条件:
1.vi < vi+1
2.L[vi] >= L[vi+1] && R[vi] <= R[vi+1]
思路: 这个dp的状态转移方程很好找,如果将R元素乘以一个负数,则就是求L[vi] >= L[vi+1] && R[vi] >= R[vi+1]了,
这个二维点的偏序关系论文专门讲过树状数组套树状数组的, 还有分治做法
树状数组套树状数组的做法:
先离散第一维, 离散之后往里面添加数, 再将加上的数离散化来做,之后类似二维数组里面的点用树状数组来做
树状数组 + 分治:
先求出对于右半部分[mid + 1, r]的所有dp值, 再将左边的dp求出, 不过左边的dp只能依靠右边的转移而来,然后
1.vi < vi+1
2.L[vi] >= L[vi+1] && R[vi] <= R[vi+1]
思路: 这个dp的状态转移方程很好找,如果将R元素乘以一个负数,则就是求L[vi] >= L[vi+1] && R[vi] >= R[vi+1]了,
这个二维点的偏序关系论文专门讲过树状数组套树状数组的, 还有分治做法
树状数组套树状数组的做法:
先离散第一维, 离散之后往里面添加数, 再将加上的数离散化来做,之后类似二维数组里面的点用树状数组来做
树状数组 + 分治:
先求出对于右半部分[mid + 1, r]的所有dp值, 再将左边的dp求出, 不过左边的dp只能依靠右边的转移而来,然后
递归解决左半部分的所有dp值
///树状数组套树状数组
#include<bits/stdc++.h>
const int maxn = 5e4 + 10;
using namespace std;
int dp[maxn], L[maxn], R[maxn], st;
int n, tot, ans, t[maxn], pre[maxn];
struct BIT {
vector<int> num, bit;
void init() {
sort(num.begin(), num.end());
num.erase(unique(num.begin(), num.end()), num.end());
bit.resize(num.size() + 1);
for(int i = 0; i <= num.size(); i++) bit[i] = 0;
}
void update(int x, int idx) {
x = lower_bound(num.begin(), num.end(), x) - num.begin() + 1;
while(x <= num.size()) {
if(dp[idx] > dp[bit[x]]) bit[x] = idx;
else if(dp[idx] == dp[bit[x]] && idx < bit[x]) bit[x] = idx;
x += x & -x;
}
}
int query(int x, int idx) {
x = upper_bound(num.begin(), num.end(), x) - num.begin();
while(x) {
int ind = bit[x];
if(dp[ind] > dp[idx]) idx = ind;
else if(dp[ind] == dp[idx] && ind < idx) idx = ind;
x -= x & -x;
}
return idx;
}
} res[maxn];
int main() {
while(scanf("%d", &n) != EOF) {
ans = tot = 0;
memset(dp, 0, sizeof dp);
memset(pre, 0, sizeof pre);
for(int i = 1; i <= n; i++) {
scanf("%d", &L[i]);
t[tot++] = L[i];
res[i].num.clear();
res[i].bit.clear();
}
res[0].num.clear(); res[0].bit.clear();
for(int i = 1; i <= n; i++) {
scanf("%d", &R[i]);
R[i] *= -1;
}
sort(t, t + tot);
tot = unique(t, t + tot) - t;
for(int i = 1; i <= n; i++) {
int x = L[i] = lower_bound(t, t + tot, L[i]) - t + 1;
while(x <= tot) {
res[x].num.push_back(R[i]);
x += x & -x;
}
}
for(int i = 0; i <= tot; i++) res[i].init();
for(int i = n; i; i--) {
int x = L[i], pa = 0, y = x;
while(x) {
pa = res[x].query(R[i], pa);
x -= x & -x;
}
dp[i] = dp[pa] + 1; pre[i] = pa;
x = L[i]; ans = max(ans, dp[i]);
while(x <= tot) {
res[x].update(R[i], i);
x += x & -x;
}
}
vector<int> vec;
printf("%d\n", ans);
for(int i = n; i; i--) if(dp[i] == ans) st = i;
while(st) { vec.push_back(st); st = pre[st]; }
for(int i = 0; i < vec.size(); i++) printf("%d%c", vec[i], i < vec.size() - 1 ? ' ' : '\n');
}
return 0;
}
///分治 + 树状数组
#include<bits/stdc++.h>
const int maxn = 5e4 + 10;
using namespace std;
struct P {
int x, y, op, id;
P() {}
P(int x, int y, int op, int id) :
x(x), y(y), op(op), id(id) {}
bool operator < (P p) const {
if(y != p.y) return y < p.y;
return op > p.op;
}
} res[maxn];
int L[maxn], R[maxn];
int dp[maxn], tal[maxn];
int n, m, ans, tot, st;
int nxt[maxn], C[maxn];
void update(int x, int val) {
for( ; x <= tot; x += x & -x) {
if(dp[C[x]] < dp[val]) C[x] = val;
else if(dp[C[x]] == dp[val] && C[x] > val) C[x] = val;
}
}
int query_max(int x, int idx) {
for( ; x; x -= x & -x) {
if(dp[C[x]] > dp[idx]) idx = C[x];
else if(dp[C[x]] == dp[idx] && C[x] < idx) idx = C[x];
}
return idx;
}
void cdq_merge(int l, int r) {
if(l == r) return ;
int mid = (l + r) >> 1, cnt = 0;
cdq_merge(mid + 1, r);
for(int i = l; i <= mid; i++) res[cnt++] = P(L[i], R[i], 0, i);
for(int i = mid + 1; i <= r; i++) res[cnt++] = P(L[i], R[i], 1, i);
sort(res, res + cnt);
for(int i = 0; i < cnt; i++) {
if(res[i].op) update(res[i].x, res[i].id); ///更新[mid + 1, r]
else {
int idx = query_max(res[i].x, 0);
int ts = dp[idx] + 1;
if(dp[res[i].id] < ts || (dp[res[i].id] == ts && nxt[res[i].id] > idx)) {
dp[res[i].id] = ts;
nxt[res[i].id] = idx;
}
}
}
for(int i = 0; i < cnt; i++) if(res[i].op) { ///恢复状态
for(int j = res[i].x; j <= tot; j += j & -j) {
C[j] = 0;
}
}
cdq_merge(l, mid);
}
int main() {
while(scanf("%d", &n) != EOF) {
dp[0] = ans = tot = 0;
memset(C, 0, sizeof C);
for(int i = 1; i <= n; i++) {
scanf("%d", &L[i]);
tal[tot++] = L[i];
dp[i] = 1;
}
for(int i = 1; i <= n; i++) {
scanf("%d", &R[i]);
R[i] *= -1;
}
sort(tal, tal + tot);
tot = unique(tal, tal + tot) - tal;
memset(nxt, 0, sizeof nxt);
for(int i = 1; i <= n; i++)
L[i] = lower_bound(tal, tal + tot, L[i]) - tal + 1;
cdq_merge(1, n);
for(int i = 1; i <= n; i++) if(dp[i] > ans) { ans = dp[i]; st = i; }
printf("%d\n", ans);
vector<int> vec;
while(st) { vec.push_back(st); st = nxt[st]; }
for(int i = 0; i < vec.size(); i++) {
printf("%d%c", vec[i], i < vec.size() - 1 ? ' ' : '\n');
}
}
return 0;
}