最朴素的dp是n^3的,肯定会超时,那么借鉴nlogn的LIS算法,先对每个球的值离散化,对于每个消耗值建一棵树状数组,,第t棵树状数组的叶子v表示消耗为t且以v结尾的最大长度,那么每次更新的时候,如果不交换,对每个消耗值t,求消耗值为t以1~a[i]-1结尾的最大长度,如果交换,对每个消耗值t,求消耗值为t-1以1~b[i]-1的最大长度,更新树状数组即可。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <string>
#define ll long long int
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define delf int m=(l+r)>>1
using namespace std;
int tree[1001][2002]; //消耗为i,以t结尾的最大长度
int a[1100];
int b[1100];
int v[2200];
int n,m,cnt,ans;
int min(int a,int b)
{
return a<b?a:b;
}
int max(int a,int b)
{
return a>b?a:b;
}
int id(int x)
{
return lower_bound(v,v+cnt,x)-v+1;
}
inline int lowbit(int x)
{