A1928 最大上升子序列和(LIS 思想)
简单考虑,直接 VAI 在这一题中并不优秀,单调性欲说还休,决定放弃。
比较优秀的选择是用树状数组,单修前缀 max 基本同朴素 LIS,本质是用树状数组维护转移区间信息。
#include<bits/stdc++.h>
using namespace std;
const int N=1009,lim=1000;
int n,dp[N];
//通过 ++x 语句整体右移成为 1-index
int query(int x){
++x;
int ans=0;
while(x>0)
ans=max(ans,dp[x]),x-=(x&-x);
return ans;
}
void modify(int x,int k){
++x;
while(x<=lim)
dp[x]=max(dp[x],k),x+=(x&-x);
}
int main(){
int ans=0;
scanf("%d",&n);
for(int i=1,x,tmp;i<=n;i++){
scanf("%d",&x);
ans=max(ans,tmp=(query(x-1)+x));
modify(x,tmp);
}
printf("%d\n",ans);
return 0;
}

浙公网安备 33010602011771号