Luogu[YNOI2019]排序(DP,线段树)

要最优?就要一步到位,不能做“马后炮”,走“回头路”,因此将序列映射到一个假定最优序列,发现移动原序列等价于删除原序列元素,以便生成最大不下降子序列。可线段树维护。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define QWQ
#ifdef QWQ
#define D_e_Line printf("
---------------
")
#define D_e(x) cout << (#x) << " : " << x << "
"
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define TIME() fprintf(stderr, "
TIME : %.3lfms
", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
#endif
struct ios {
	template<typename ATP> inline ios& operator >> (ATP &x) {
		x = 0; int f = 1; char c;
		for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
		while(c >= '0' && c <='9') x = x * 10 + (c ^ '0'), c = getchar();
		x *= f;
		return *this;
	}
}io;
using namespace std;
template<typename ATP> inline ATP Max(ATP a, ATP b) {
	return a > b ? a : b;
}
template<typename ATP> inline ATP Min(ATP a, ATP b) {
	return a < b ? a : b;
}
template<typename ATP> inline ATP Abs(ATP a) {
	return a < 0 ? -a : a;
}

const int N = 1e2 + 7;

int t[N << 2];
#define ls rt << 1
#define rs rt << 1 | 1
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
inline void Pushup(int &rt) {
	t[rt] = Max(t[ls], t[rs]);
}
inline void Modify(int rt, int l, int r, int x, int w) {
	if(l == r){
		t[rt] = w;
		return;
	}
	int mid = (l + r) >> 1;
	if(x <= mid)
		Modify(lson, x, w);
	else
		Modify(rson, x, w);
	Pushup(rt);
}
inline int Query(int rt, int l, int r, int L, int R) {
	if(L <= l && r <= R) return t[rt];
	int mid = (l + r) >> 1, maxx = -1e9;
	if(L <= mid) maxx = Max(maxx, Query(lson, L, R));
	if(R > mid) maxx = Max(maxx, Query(rson, L, R));
	return maxx;
}
int f[N], a[N], b[N];
int main() {
	int Tasks;
	io >> Tasks;
	while(Tasks--){
		int n, sum = 0;
		io >> n;
		R(i,1,n){
			io >> a[i];
			b[i] = a[i];
			f[i] = 0;
			sum += a[i];
		}
		
		sort(b + 1, b + n + 1);
		int m = unique(b + 1, b+ n + 1) - b- 1;
		R(i,1,n){
			int x = lower_bound(b + 1, b + n + 1, a[i]) - b;
			
			Modify(1, 1, m, x, Query(1, 1, m, 1, x) + a[i]);
			
//			R(j,1,i - 1){
//				if(a[i] >= a[j]){
//					f[i] = Max(f[i], f[j]);
//				}
//			}
//			f[i] += a[i];
		}
		
//		int delta = 0;
//		R(i,1,n){
//			delta = Max(delta, f[i]);
//		}
//		
		printf("%d
", sum - Query(1, 1, m, 1, m));
		
	}
	return 0;
}

原文地址:https://www.cnblogs.com/bingoyes/p/11747772.html