我是靠谱客的博主 飘逸大叔,这篇文章主要介绍Codeforces 487B. Strip DP+线段树+二分,现在分享给大家,希望可以做个参考。


dp[ i ]表示到第i个位置最少要分多少下, dp[ i ] = min ( dp [ i ] , dp [ j ] + 1 ) j 在合适的范围内 (  满足长度和最值差 )


对整个数组建立线段树维护最大值和最小值这样就可在nlogn的时间里求出某一段的最值差,这个范围是满足单调性的,所以对于每个i可以二分出j的最小值 . 

对每个dp[i]建立线段树,可以在nlogn时间内求出最小的j.

所以总时间复杂度n^2logn 

B. Strip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.

Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:

  • Each piece should contain at least l numbers.
  • The difference between the maximal and the minimal number on the piece should be at most s.

Please help Alexandra to find the minimal number of pieces meeting the condition above.

Input

The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).

The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).

Output

Output the minimal number of strip pieces.

If there are no ways to split the strip, output -1.

Sample test(s)
input
复制代码
1
2
7 2 2 1 3 1 2 4 1 2
output
复制代码
1
3
input
复制代码
1
2
7 2 2 1 100 1 100 1 100 1
output
复制代码
1
-1
Note

For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].

For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.



复制代码
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* *********************************************** Author :CKboss Created Time :2015年03月11日 星期三 23时20分17秒 File Name :CF487B.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; const int maxn = 100100; const int INF = 1e6; int n,l,s; int a[maxn]; int maxnum[maxn<<2],minnum[maxn<<2]; /// seg tree #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef pair<int,int> pII; void push_up(int rt) { maxnum[rt]=max(maxnum[rt<<1],maxnum[rt<<1|1]); minnum[rt]=min(minnum[rt<<1],minnum[rt<<1|1]); } void build(int l,int r,int rt) { if(l==r) { minnum[rt]=maxnum[rt]=a[l]; return ; } int m=(l+r)/2; build(lson); build(rson); push_up(rt); } pII query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { return make_pair(maxnum[rt],minnum[rt]); } int m=(l+r)/2; if(R<=m) return query(L,R,lson); else if(L>m) return query(L,R,rson); else { pII leftp=query(L,R,lson); pII rightp=query(L,R,rson); return make_pair( max( leftp.first,rightp.first) , min( leftp.second, rightp.second ) ); } } int dp[maxn]; /// seg tree 2 int tree2[maxn<<2]; void push_up2(int rt) { tree2[rt]=min(tree2[rt<<1],tree2[rt<<1|1]); } void build2() { memset(tree2,63,sizeof(tree2)); } void insert2(int val,int pos,int l,int r,int rt) { if(pos==l&&pos==r) { tree2[rt]=val; return ; } int m=(l+r)/2; if(pos<=m) insert2(val,pos,lson); else insert2(val,pos,rson); push_up2(rt); } int query2(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { return tree2[rt]; } int m=(l+r)/2; if(R<=m) return query2(L,R,lson); else if(L>m) return query2(L,R,rson); else { int one = query2(L,R,lson); int two = query2(L,R,rson); return min(one,two); } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d%d%d",&n,&s,&l); for(int i=1;i<=n;i++) { dp[i]=INF; scanf("%d",a+i); } build(1,n,1); dp[0]=0; dp[1]=1; if(l>1) dp[1]=INF; build2(); insert2(0,0,0,n,1); insert2(1,1,0,n,1); for(int i=2;i<=n;i++) { /// binary search to find left bound int low=1,high=i-l+1,leftb=-1; while(low<=high) { int mid=(low+high)/2; pII temp = query(mid,i,1,n,1); if(temp.first-temp.second<=s) { leftb=mid; high=mid-1; } else low=mid+1; } if(leftb==-1) dp[i]=INF; else { int mmm = query2(leftb-1,i-l,0,n,1); dp[i]=mmm+1; } insert2(dp[i],i,0,n,1); } if(dp[n]>=INF) dp[n]=-1; printf("%dn",dp[n]); return 0; }




最后

以上就是飘逸大叔最近收集整理的关于Codeforces 487B. Strip DP+线段树+二分的全部内容,更多相关Codeforces内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部