传送门:cf 487B
给定一个长度为n的数组,要求把数组分为若干部分满足下面两个条件
(1):每个部分至少含有l个元素
(2):每个部分中两两数的差值的最大值不超过s
问在满足上述两个条件的情况下,最少能分成多少个部分。
可以预处理出每个点最靠左的可行起点位置left,然后dp处理出结果,状态转移方程如下:
ans[i] = min(f[k]) + 1 left[i]<=k<=i-l 当k无可行解时用INF表示无解
预处理的过程与求ans的过程都需要在整个过程中记录一个区间的值,并且需要快速找出这个区间的最大以及最小值,可以用multiset来解决
multiset 与set 类似可以有序存储数据,且multiset支持重复的数据
复制代码
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
46
47
48
49
50
51
52
53
54
55/****************************************************** * File Name: b.cpp * Author: kojimai * Create Time: 2014年11月22日 星期六 21时39分25秒 ******************************************************/ #include<cstdio> #include<set> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; #define FFF 100005 int a[FFF],ans[FFF],Left[FFF]; multiset<int> p; int main() { int n,l,s; cin>>n>>s>>l; for(int i = 0;i < n;i++) cin>>a[i]; int now = 0; for(int i = 0;i < n;i++) { while(!p.empty()) { int Min = *p.begin(),Max = *(--p.end()); if(Max - a[i] <= s && a[i] - Min <= s) break; p.erase(p.lower_bound(a[now]));//当前情况不满足,因此去除最左边的端点再判断一次,知道满足条件或者multiset为空 now++; } Left[i] = now;//Left存每个点最左边的连续串起点 p.insert(a[i]); } now = 0; p.clear(); ans[0] = 0; for(int i = 1;i <= n;i++) { if(i >= l) p.insert(ans[i-l]); while(now <= i-l && now < Left[i-1]) { p.erase(p.lower_bound(ans[now])); now++; } if(p.empty()) ans[i] = FFF; else ans[i] = *p.begin() + 1; //最小值排在最前面 } if(ans[n] >= FFF) cout<<-1<<endl; else cout<<ans[n]<<endl; return 0; }
最后
以上就是直率马里奥最近收集整理的关于codeforces 487B Strip dp的全部内容,更多相关codeforces内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复