我是靠谱客的博主 愤怒牛排,这篇文章主要介绍CodeForces 487 B.Strip(dp+尺取+set),现在分享给大家,希望可以做个参考。

Description

给出一个长度为 n 的序列,要求将其分成尽可能少的段,使得每段长度不小于l,且每段极差至多为 s

Input

第一行三个整数n,s,l,第二行 n 个整数a1,...,n表示该序列 (1n105,0s109,1l105,109ai109)

Output

输出该序列最少可以被分成几个合法段,如果该序列没有满足条件的分割则输出-1

Sample Input

7 2 2
1 3 1 2 4 1 2

Sample Output

3

Solution

dp[i] 表示前 i 个数最少被分成的合法段数,那么有转移方程

dp[i]=min(dp[j])+1,ijl,max{aj+1,...,i}min{aj+1,...,i}s

尺取,拿一个 set 维护当前尺取的区间的 ai 值用来快速得到区间极差,再拿一个 set 维护对当前 dp 值有贡献的 dp 值,每次区间端点右移时维护这两个 set 即可

Code

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define maxn 111111 int n,s,l,a[maxn],dp[maxn]; multiset<int>s1,s2; //dp[i]:1~i最少分成几段 //s1:维护尺取过程中区间最值 //s2:维护尺取过程中合法dp值 int main() { while(~scanf("%d%d%d",&n,&s,&l)) { s1.clear(),s2.clear(); for(int i=1;i<=n;i++)scanf("%d",&a[i]); int i=1,j=1; while(i<=n) { s1.insert(a[i]); while(*s1.rbegin()-*s1.begin()>s) { s1.erase(s1.find(a[j])); if(i-j>=l)s2.erase(s2.find(dp[j-1])); j++; } if(i-j+1>=l)s2.insert(dp[i-l]); if(s2.size()==0)dp[i]=INF; else dp[i]=*s2.begin()+1; i++; } if(dp[n]>=INF)printf("-1n"); else printf("%dn",dp[n]); } return 0; }

最后

以上就是愤怒牛排最近收集整理的关于CodeForces 487 B.Strip(dp+尺取+set)的全部内容,更多相关CodeForces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部