[SCOI2014] 方伯伯的玉米田(dp+树状数组维护行列)

https://www.luogu.com.cn/problem/P3287

显然每次操作的区间一定是一个后缀

我们直接令 d p ( x , i ) dp(x,i) dp(x,i) 表示最后一个数是 x x x(加之后),加了 i i i 次的最长长度,转移显然 max ⁡ d p ( y ≤ x , j ≤ i ) \max dp(y\le x, j\le i) maxdp(y≤x,j≤i)

映射到二维平面上,考虑每次相当于是更新一条斜线,每个点等于其左下角矩阵的最大值+1。那很好做,我们直接对行列都维护拿树状数组维护即可。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
 #define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
 #define debug(...) void(0)
#endif
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
#define M 510
//#define mo
#define N 5510
int n, m, i, j, k, T; 
int ans, x, a[10010]; 

struct Bin1 {
	int cnt[N]; 
	int qry(int x) { ++x;  
		int ans = 0; 
		while(x) ans = max(ans, cnt[x]), x -= x & -x; 
		return ans; 
	}
	void add(int x, int k) { ++x;  
		while(x < N) cnt[x] = max(cnt[x], k), x += x & -x; 
	}
}B1[M]; 

struct Bin2 {
	int cnt[M]; 
	int qry(int x) {
		int ans = 0; ++x;  
		while(x) ans = max(ans, cnt[x]), x -= x & -x; 
		return ans; 
	}
	void add(int x, int k) { ++x;  
		while(x < M) cnt[x] = max(cnt[x], k), x += x & -x; 
	}
}B2[N]; 

signed main()
{
	#ifdef LOCAL
	  freopen("in.txt", "r", stdin);
	  freopen("out.txt", "w", stdout);
	#endif
//	srand(time(NULL));
//	T=read();
//	while(T--) {
//
//	}
	n = read(); m = read(); 
	for(i = 1; i <= n; ++i) a[i] = read(); 
	for(i = 1; i <= n; ++i) {
		x = 0; 
		for(k = 0; k <= m; ++k) {
			x = max({x, B1[k].qry(a[i] + k), B2[a[i] + k].qry(k)}); 
			B1[k].add(a[i] + k, x + 1); B2[a[i] + k].add(k, x + 1); 
			ans = max(ans, x + 1); 
		}
	}
	printf("%d", ans);  
	return 0;
}
相关推荐
延凡科技11 小时前
多场景落地复盘:端边云架构无人机智能巡检系统设计与实践
大数据·数据结构·人工智能·科技·架构·无人机·能源
皓月斯语17 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
春日见17 小时前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
萌动的小火苗18 小时前
嵌入式开发中的栈与队列:任务调度为什么依赖数据结构
数据结构·c++·单片机·嵌入式硬件
叩码以求索18 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana218 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
星恒随风18 小时前
C++ STL 栈详解:stack 的使用、经典题目与简单模拟实现
开发语言·数据结构·c++·笔记·学习
txzrxz18 小时前
单调队列讲解
数据结构·c++·算法·单调队列
Keven_1119 小时前
算法札记:树状数组的用途
数据结构·算法
cpp_250120 小时前
P1540 [NOIP 2010 提高组] 机器翻译
数据结构·c++·算法·队列·noip·洛谷题解