传送门:点击打开链接
题意:给你一些数,删除一个连续区间,使得连续区间上升区间最大。
思路:先求出SL,SR,分别表示以i结尾的最大连续区间长度和以i开头的最大连续区间长度
然后从左边向右边将SL插入到线段树中,并类似求逆序数的方法去查询小于A[i]的在线段树中最大的数,并加上R[i]
wa点:在递归线段树的时候,区间长度不应该是[1,n]而应该是[1,Max(A[i])],或者直接写成 1e4
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck printf("fuck")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MX = 1e4 + 5;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,10000,1
int MAX[MX << 2], SL[MX], SR[MX], A[MX];
void push_up(int rt) {
MAX[rt] = max(MAX[rt << 1], MAX[rt << 1 | 1]);
}
void update(int x, int d, int l, int r, int rt) {
if(l == r) {
MAX[rt] = max(MAX[rt], d);
return;
}
int m = (l + r) >> 1;
if(x <= m) update(x, d, lson);
else update(x, d, rson);
push_up(rt);
}
int query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return MAX[rt];
}
int m = (l + r) >> 1, ret = 0;
if(L <= m) ret = max(ret, query(L, R, lson));
if(R > m) ret = max(ret, query(L, R, rson));
return ret;
}
int main() {
int n; //FIN;
while(~scanf("%d", &n)) {
memset(MAX, 0, sizeof(MAX));
memset(SL, 0, sizeof(SL));
memset(SR, 0, sizeof(SR));
for(int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
}
SL[1] = 1;
for(int i = 2; i <= n; i++) {
SL[i] = (A[i - 1] < A[i] ? SL[i - 1] : 0) + 1;
}
SR[n] = 1;
for(int i = n - 1; i >= 1; i--) {
SR[i] = (A[i] < A[i + 1] ? SR[i + 1] : 0) + 1;
}
int ans = 1;
for(int i = 1; i <= n; i++) {
int temp = 0;
if(1 <= A[i] - 1) temp = query(1, A[i] - 1, root);
ans = max(ans, temp + SR[i]);
update(A[i], SL[i], root);
}
printf("%d\n", ans);
}
return 0;
}